tor-browser

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

prod-private-method.js (3937B)


      1 // This file was procedurally generated from the following sources:
      2 // - src/class-elements/prod-private-method.case
      3 // - src/class-elements/private-methods/cls-decl.template
      4 /*---
      5 description: Private Method (private method definitions in a class declaration)
      6 esid: prod-MethodDefinition
      7 features: [class, class-methods-private]
      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 
     71 
     72 /*** template notes
     73 * method should always be #m
     74 * the template provides c.ref() for external reference
     75 */
     76 
     77 function hasProp(obj, name, expected, msg) {
     78  var hasOwnProperty = Object.prototype.hasOwnProperty.call(obj, name);
     79  assert.sameValue(hasOwnProperty, expected, msg);
     80 
     81  var hasProperty = Reflect.has(obj, name);
     82  assert.sameValue(hasProperty, expected, msg);
     83 }
     84 
     85 class C {
     86  #m() { return 42; }
     87 
     88 
     89  get ref() { return this.#m; }
     90 
     91  constructor() {
     92    hasProp(this, '#m', false, 'private methods are defined in an special internal slot and cannot be found as own properties');
     93    assert.sameValue(typeof this.#m, 'function');
     94    assert.sameValue(this.ref, this.#m, 'returns the same value');
     95    assert.sameValue(this.#m, (() => this)().#m, 'memberexpression and call expression forms');
     96 
     97    assert.sameValue(this.#m(), 42, 'already defined in the ctor');
     98    assert.sameValue(this.#m.name, '#m', 'function name inside constructor');
     99 
    100  }
    101 }
    102 
    103 var c = new C();
    104 var other = new C();
    105 
    106 hasProp(C.prototype, '#m', false, 'method is not defined in the prototype');
    107 hasProp(C, '#m', false, 'method is not defined in the contructor');
    108 hasProp(c, '#m', false, 'method cannot be seen outside of the class');
    109 
    110 /***
    111 * MethodDefinition : ClassElementName ( UniqueFormalParameters ) { FunctionBody }
    112 * 
    113 * 1. Let methodDef be DefineMethod of MethodDefinition with argument homeObject.
    114 * ...
    115 */
    116 assert.sameValue(c.ref, other.ref, 'The method is defined once, and reused on every new instance');
    117 
    118 // gets the returned value from #m
    119 assert.sameValue(c.ref(), 42, 'function return');
    120 assert.sameValue(c.ref.name, '#m', 'function name is preserved external reference');
    121 
    122 reportCompare(0, 0);