tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

prod-private-async-method.js (4179B)


      1 // |reftest| async
      2 // This file was procedurally generated from the following sources:
      3 // - src/class-elements/prod-private-async-method.case
      4 // - src/class-elements/private-methods/cls-expr.template
      5 /*---
      6 description: Private Async Method (private method definitions in a class expression)
      7 esid: prod-MethodDefinition
      8 features: [async-functions, class, class-methods-private]
      9 flags: [generated, async]
     10 info: |
     11    ClassElement :
     12      MethodDefinition
     13      ...
     14      ;
     15 
     16    ClassElementName :
     17      PropertyName
     18      PrivateName
     19 
     20    PrivateName ::
     21      # IdentifierName
     22 
     23    MethodDefinition :
     24      ClassElementName ( UniqueFormalParameters ) { FunctionBody }
     25      GeneratorMethod
     26      AsyncMethod
     27      AsyncGeneratorMethod 
     28      get ClassElementName () { FunctionBody }
     29      set ClassElementName ( PropertySetParameterList ) { FunctionBody }
     30 
     31    GeneratorMethod :
     32      * ClassElementName ( UniqueFormalParameters ){GeneratorBody}
     33 
     34    AsyncMethod :
     35      async [no LineTerminator here] ClassElementName ( UniqueFormalParameters ) { AsyncFunctionBody }
     36 
     37    AsyncGeneratorMethod :
     38      async [no LineTerminator here]* ClassElementName ( UniqueFormalParameters ) { AsyncGeneratorBody }
     39 
     40    ---
     41 
     42    InitializeClassElements ( F, proto )
     43 
     44    ...
     45    5. For each item element in order from elements,
     46      a. Assert: If element.[[Placement]] is "prototype" or "static", then element.[[Key]] is not a Private Name.
     47      b. If element.[[Kind]] is "method" and element.[[Placement]] is "static" or "prototype",
     48        i. Let receiver be F if element.[[Placement]] is "static", else let receiver be proto.
     49        ii. Perform ? DefineClassElement(receiver, element).
     50 
     51    InitializeInstanceElements ( O, constructor )
     52 
     53    ...
     54    3. Let elements be the value of F's [[Elements]] internal slot.
     55    4. For each item element in order from elements,
     56      a. If element.[[Placement]] is "own" and element.[[Kind]] is "method",
     57        i. Perform ? DefineClassElement(O, element).
     58 
     59    DefineClassElement (receiver, element)
     60 
     61    ...
     62    6. If key is a Private Name,
     63      a. Perform ? PrivateFieldDefine(receiver, key, descriptor).
     64 
     65    PrivateFieldDefine (P, O, desc)
     66 
     67    ...
     68    6. Append { [[PrivateName]]: P, [[PrivateFieldDescriptor]]: desc } to O.[[PrivateFieldDescriptors]].
     69 
     70 ---*/
     71 var ctorPromise;
     72 
     73 
     74 
     75 /***
     76 * template notes:
     77 * 1. method should always be #m
     78 * 2. the template provides c.ref/other.ref for external reference
     79 */
     80 
     81 function hasProp(obj, name, expected, msg) {
     82  var hasOwnProperty = Object.prototype.hasOwnProperty.call(obj, name);
     83  assert.sameValue(hasOwnProperty, expected, msg);
     84 
     85  var hasProperty = Reflect.has(obj, name);
     86  assert.sameValue(hasProperty, expected, msg);
     87 }
     88 
     89 var C = class {
     90  async #m() { return 42; }
     91 
     92 
     93  get ref() { return this.#m; }
     94 
     95  constructor() {
     96    hasProp(this, '#m', false, 'private methods are defined in an special internal slot and cannot be found as own properties');
     97    assert.sameValue(typeof this.#m, 'function');
     98    assert.sameValue(this.ref, this.#m, 'returns the same value');
     99    assert.sameValue(this.#m, (() => this)().#m, 'memberexpression and call expression forms');
    100 
    101    assert.sameValue(this.#m.name, '#m', 'function name inside constructor');
    102    ctorPromise = this.#m().then(value => {
    103        assert.sameValue(value, 42, 'already defined in the ctor');
    104    }, $DONE);
    105 
    106  }
    107 }
    108 
    109 var c = new C();
    110 var other = new C();
    111 
    112 hasProp(C.prototype, '#m', false, 'method is not defined in the prototype');
    113 hasProp(C, '#m', false, 'method is not defined in the contructor');
    114 hasProp(c, '#m', false, 'method cannot be seen outside of the class');
    115 
    116 /***
    117 * MethodDefinition : ClassElementName ( UniqueFormalParameters ) { FunctionBody }
    118 * 
    119 * 1. Let methodDef be DefineMethod of MethodDefinition with argument homeObject.
    120 * ...
    121 */
    122 assert.sameValue(c.ref, other.ref, 'The method is defined once, and reused on every new instance');
    123 
    124 assert.sameValue(c.ref.name, '#m', 'function name is preserved external reference');
    125 ctorPromise.then(() => {
    126    // gets the returned promise from #m
    127    return c.ref().then(value => {
    128        assert.sameValue(value, 42, 'function return');
    129    });
    130 }).then($DONE, $DONE);