tor-browser

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

prod-private-method-initialize-order.js (4677B)


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