prod-private-method-initialize-order.js (4654B)
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-decl.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 declaration) 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 /*** template notes 88 * method should always be #m 89 * the template provides c.ref() for external reference 90 */ 91 92 function hasProp(obj, name, expected, msg) { 93 var hasOwnProperty = Object.prototype.hasOwnProperty.call(obj, name); 94 assert.sameValue(hasOwnProperty, expected, msg); 95 96 var hasProperty = Reflect.has(obj, name); 97 assert.sameValue(hasProperty, expected, msg); 98 } 99 100 class C { 101 a = this.#m(); 102 103 #m() { return 42; } 104 get bGetter() { return this.#b; } 105 106 #b = this.#m(); 107 108 109 get ref() { return this.#m; } 110 111 constructor() { 112 hasProp(this, '#m', false, 'private methods are defined in an special internal slot and cannot be found as own properties'); 113 assert.sameValue(typeof this.#m, 'function'); 114 assert.sameValue(this.ref, this.#m, 'returns the same value'); 115 assert.sameValue(this.#m, (() => this)().#m, 'memberexpression and call expression forms'); 116 117 assert.sameValue(this.a, 42); 118 assert.sameValue(this.#b, 42); 119 120 } 121 } 122 123 var c = new C(); 124 var other = new C(); 125 126 hasProp(C.prototype, '#m', false, 'method is not defined in the prototype'); 127 hasProp(C, '#m', false, 'method is not defined in the contructor'); 128 hasProp(c, '#m', false, 'method cannot be seen outside of the class'); 129 130 /*** 131 * MethodDefinition : ClassElementName ( UniqueFormalParameters ) { FunctionBody } 132 * 133 * 1. Let methodDef be DefineMethod of MethodDefinition with argument homeObject. 134 * ... 135 */ 136 assert.sameValue(c.ref, other.ref, 'The method is defined once, and reused on every new instance'); 137 138 assert.sameValue(c.a, 42); 139 assert.sameValue(c.bGetter, 42); 140 141 reportCompare(0, 0);