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