tor-browser

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

prod-private-method.js (3960B)


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