diff options
Diffstat (limited to 'js/src/tests/test262/language/statements/class/elements/private-methods')
7 files changed, 652 insertions, 0 deletions
diff --git a/js/src/tests/test262/language/statements/class/elements/private-methods/browser.js b/js/src/tests/test262/language/statements/class/elements/private-methods/browser.js new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/js/src/tests/test262/language/statements/class/elements/private-methods/browser.js diff --git a/js/src/tests/test262/language/statements/class/elements/private-methods/prod-private-async-generator.js b/js/src/tests/test262/language/statements/class/elements/private-methods/prod-private-async-generator.js new file mode 100644 index 0000000000..c703df72d4 --- /dev/null +++ b/js/src/tests/test262/language/statements/class/elements/private-methods/prod-private-async-generator.js @@ -0,0 +1,134 @@ +// |reftest| async +// This file was procedurally generated from the following sources: +// - src/class-elements/prod-private-async-generator.case +// - src/class-elements/private-methods/cls-decl.template +/*--- +description: Private Async Generator (private method definitions in a class declaration) +esid: prod-MethodDefinition +features: [async-iteration, class, class-methods-private] +flags: [generated, async] +info: | + ClassElement : + MethodDefinition + ... + ; + + ClassElementName : + PropertyName + PrivateName + + PrivateName :: + # IdentifierName + + MethodDefinition : + ClassElementName ( UniqueFormalParameters ) { FunctionBody } + GeneratorMethod + AsyncMethod + AsyncGeneratorMethod + get ClassElementName () { FunctionBody } + set ClassElementName ( PropertySetParameterList ) { FunctionBody } + + GeneratorMethod : + * ClassElementName ( UniqueFormalParameters ){GeneratorBody} + + AsyncMethod : + async [no LineTerminator here] ClassElementName ( UniqueFormalParameters ) { AsyncFunctionBody } + + AsyncGeneratorMethod : + async [no LineTerminator here]* ClassElementName ( UniqueFormalParameters ) { AsyncGeneratorBody } + + --- + + InitializeClassElements ( F, proto ) + + ... + 5. For each item element in order from elements, + a. Assert: If element.[[Placement]] is "prototype" or "static", then element.[[Key]] is not a Private Name. + b. If element.[[Kind]] is "method" and element.[[Placement]] is "static" or "prototype", + i. Let receiver be F if element.[[Placement]] is "static", else let receiver be proto. + ii. Perform ? DefineClassElement(receiver, element). + + InitializeInstanceElements ( O, constructor ) + + ... + 3. Let elements be the value of F's [[Elements]] internal slot. + 4. For each item element in order from elements, + a. If element.[[Placement]] is "own" and element.[[Kind]] is "method", + i. Perform ? DefineClassElement(O, element). + + DefineClassElement (receiver, element) + + ... + 6. If key is a Private Name, + a. Perform ? PrivateFieldDefine(receiver, key, descriptor). + + PrivateFieldDefine (P, O, desc) + + ... + 6. Append { [[PrivateName]]: P, [[PrivateFieldDescriptor]]: desc } to O.[[PrivateFieldDescriptors]]. + +---*/ +var ctorPromise; + + + +/*** template notes + * method should always be #m + * the template provides c.ref() for external reference + */ + +function hasProp(obj, name, expected, msg) { + var hasOwnProperty = Object.prototype.hasOwnProperty.call(obj, name); + assert.sameValue(hasOwnProperty, expected, msg); + + var hasProperty = Reflect.has(obj, name); + assert.sameValue(hasProperty, expected, msg); +} + +class C { + async * #m() { return 42; } + + + get ref() { return this.#m; } + + constructor() { + hasProp(this, '#m', false, 'private methods are defined in an special internal slot and cannot be found as own properties'); + assert.sameValue(typeof this.#m, 'function'); + assert.sameValue(this.ref, this.#m, 'returns the same value'); + assert.sameValue(this.#m, (() => this)().#m, 'memberexpression and call expression forms'); + + var ctorIter = this.#m(); + var p = ctorIter.next(); + ctorPromise = p.then(({ value, done }) => { + assert.sameValue(value, 42, 'return from generator method, inside ctor'); + assert.sameValue(done, true, 'iterator is done, inside ctor'); + }, $DONE); + assert.sameValue(this.#m.name, '#m', 'function name inside constructor'); + + } +} + +var c = new C(); +var other = new C(); + +hasProp(C.prototype, '#m', false, 'method is not defined in the prototype'); +hasProp(C, '#m', false, 'method is not defined in the contructor'); +hasProp(c, '#m', false, 'method cannot be seen outside of the class'); + +/*** + * MethodDefinition : ClassElementName ( UniqueFormalParameters ) { FunctionBody } + * + * 1. Let methodDef be DefineMethod of MethodDefinition with argument homeObject. + * ... + */ +assert.sameValue(c.ref, other.ref, 'The method is defined once, and reused on every new instance'); + +assert.sameValue(c.ref.name, '#m', 'function name is preserved external reference'); +ctorPromise.then(() => { + // gets the returned async iterator from #m + var iter = c.ref(); + return iter.next().then(({ value, done }) => { + assert.sameValue(value, 42, 'return from generator method'); + assert.sameValue(done, true, 'iterator is done'); + }); +}).then($DONE, $DONE); diff --git a/js/src/tests/test262/language/statements/class/elements/private-methods/prod-private-async-method.js b/js/src/tests/test262/language/statements/class/elements/private-methods/prod-private-async-method.js new file mode 100644 index 0000000000..fa42763d30 --- /dev/null +++ b/js/src/tests/test262/language/statements/class/elements/private-methods/prod-private-async-method.js @@ -0,0 +1,129 @@ +// |reftest| async +// This file was procedurally generated from the following sources: +// - src/class-elements/prod-private-async-method.case +// - src/class-elements/private-methods/cls-decl.template +/*--- +description: Private Async Method (private method definitions in a class declaration) +esid: prod-MethodDefinition +features: [async-functions, class, class-methods-private] +flags: [generated, async] +info: | + ClassElement : + MethodDefinition + ... + ; + + ClassElementName : + PropertyName + PrivateName + + PrivateName :: + # IdentifierName + + MethodDefinition : + ClassElementName ( UniqueFormalParameters ) { FunctionBody } + GeneratorMethod + AsyncMethod + AsyncGeneratorMethod + get ClassElementName () { FunctionBody } + set ClassElementName ( PropertySetParameterList ) { FunctionBody } + + GeneratorMethod : + * ClassElementName ( UniqueFormalParameters ){GeneratorBody} + + AsyncMethod : + async [no LineTerminator here] ClassElementName ( UniqueFormalParameters ) { AsyncFunctionBody } + + AsyncGeneratorMethod : + async [no LineTerminator here]* ClassElementName ( UniqueFormalParameters ) { AsyncGeneratorBody } + + --- + + InitializeClassElements ( F, proto ) + + ... + 5. For each item element in order from elements, + a. Assert: If element.[[Placement]] is "prototype" or "static", then element.[[Key]] is not a Private Name. + b. If element.[[Kind]] is "method" and element.[[Placement]] is "static" or "prototype", + i. Let receiver be F if element.[[Placement]] is "static", else let receiver be proto. + ii. Perform ? DefineClassElement(receiver, element). + + InitializeInstanceElements ( O, constructor ) + + ... + 3. Let elements be the value of F's [[Elements]] internal slot. + 4. For each item element in order from elements, + a. If element.[[Placement]] is "own" and element.[[Kind]] is "method", + i. Perform ? DefineClassElement(O, element). + + DefineClassElement (receiver, element) + + ... + 6. If key is a Private Name, + a. Perform ? PrivateFieldDefine(receiver, key, descriptor). + + PrivateFieldDefine (P, O, desc) + + ... + 6. Append { [[PrivateName]]: P, [[PrivateFieldDescriptor]]: desc } to O.[[PrivateFieldDescriptors]]. + +---*/ +var ctorPromise; + + + +/*** template notes + * method should always be #m + * the template provides c.ref() for external reference + */ + +function hasProp(obj, name, expected, msg) { + var hasOwnProperty = Object.prototype.hasOwnProperty.call(obj, name); + assert.sameValue(hasOwnProperty, expected, msg); + + var hasProperty = Reflect.has(obj, name); + assert.sameValue(hasProperty, expected, msg); +} + +class C { + async #m() { return 42; } + + + get ref() { return this.#m; } + + constructor() { + hasProp(this, '#m', false, 'private methods are defined in an special internal slot and cannot be found as own properties'); + assert.sameValue(typeof this.#m, 'function'); + assert.sameValue(this.ref, this.#m, 'returns the same value'); + assert.sameValue(this.#m, (() => this)().#m, 'memberexpression and call expression forms'); + + assert.sameValue(this.#m.name, '#m', 'function name inside constructor'); + ctorPromise = this.#m().then(value => { + assert.sameValue(value, 42, 'already defined in the ctor'); + }, $DONE); + + } +} + +var c = new C(); +var other = new C(); + +hasProp(C.prototype, '#m', false, 'method is not defined in the prototype'); +hasProp(C, '#m', false, 'method is not defined in the contructor'); +hasProp(c, '#m', false, 'method cannot be seen outside of the class'); + +/*** + * MethodDefinition : ClassElementName ( UniqueFormalParameters ) { FunctionBody } + * + * 1. Let methodDef be DefineMethod of MethodDefinition with argument homeObject. + * ... + */ +assert.sameValue(c.ref, other.ref, 'The method is defined once, and reused on every new instance'); + +assert.sameValue(c.ref.name, '#m', 'function name is preserved external reference'); +ctorPromise.then(() => { + // gets the returned promise from #m + return c.ref().then(value => { + assert.sameValue(value, 42, 'function return'); + }); +}).then($DONE, $DONE); diff --git a/js/src/tests/test262/language/statements/class/elements/private-methods/prod-private-generator.js b/js/src/tests/test262/language/statements/class/elements/private-methods/prod-private-generator.js new file mode 100644 index 0000000000..4cd79a852d --- /dev/null +++ b/js/src/tests/test262/language/statements/class/elements/private-methods/prod-private-generator.js @@ -0,0 +1,126 @@ +// This file was procedurally generated from the following sources: +// - src/class-elements/prod-private-generator.case +// - src/class-elements/private-methods/cls-decl.template +/*--- +description: Private Generator (private method definitions in a class declaration) +esid: prod-MethodDefinition +features: [generators, class, class-methods-private] +flags: [generated] +info: | + ClassElement : + MethodDefinition + ... + ; + + ClassElementName : + PropertyName + PrivateName + + PrivateName :: + # IdentifierName + + MethodDefinition : + ClassElementName ( UniqueFormalParameters ) { FunctionBody } + GeneratorMethod + AsyncMethod + AsyncGeneratorMethod + get ClassElementName () { FunctionBody } + set ClassElementName ( PropertySetParameterList ) { FunctionBody } + + GeneratorMethod : + * ClassElementName ( UniqueFormalParameters ){GeneratorBody} + + AsyncMethod : + async [no LineTerminator here] ClassElementName ( UniqueFormalParameters ) { AsyncFunctionBody } + + AsyncGeneratorMethod : + async [no LineTerminator here]* ClassElementName ( UniqueFormalParameters ) { AsyncGeneratorBody } + + --- + + InitializeClassElements ( F, proto ) + + ... + 5. For each item element in order from elements, + a. Assert: If element.[[Placement]] is "prototype" or "static", then element.[[Key]] is not a Private Name. + b. If element.[[Kind]] is "method" and element.[[Placement]] is "static" or "prototype", + i. Let receiver be F if element.[[Placement]] is "static", else let receiver be proto. + ii. Perform ? DefineClassElement(receiver, element). + + InitializeInstanceElements ( O, constructor ) + + ... + 3. Let elements be the value of F's [[Elements]] internal slot. + 4. For each item element in order from elements, + a. If element.[[Placement]] is "own" and element.[[Kind]] is "method", + i. Perform ? DefineClassElement(O, element). + + DefineClassElement (receiver, element) + + ... + 6. If key is a Private Name, + a. Perform ? PrivateFieldDefine(receiver, key, descriptor). + + PrivateFieldDefine (P, O, desc) + + ... + 6. Append { [[PrivateName]]: P, [[PrivateFieldDescriptor]]: desc } to O.[[PrivateFieldDescriptors]]. + +---*/ + + +/*** template notes + * method should always be #m + * the template provides c.ref() for external reference + */ + +function hasProp(obj, name, expected, msg) { + var hasOwnProperty = Object.prototype.hasOwnProperty.call(obj, name); + assert.sameValue(hasOwnProperty, expected, msg); + + var hasProperty = Reflect.has(obj, name); + assert.sameValue(hasProperty, expected, msg); +} + +class C { + * #m() { return 42; } + + + get ref() { return this.#m; } + + constructor() { + hasProp(this, '#m', false, 'private methods are defined in an special internal slot and cannot be found as own properties'); + assert.sameValue(typeof this.#m, 'function'); + assert.sameValue(this.ref, this.#m, 'returns the same value'); + assert.sameValue(this.#m, (() => this)().#m, 'memberexpression and call expression forms'); + + var res = this.#m().next(); + assert.sameValue(res.value, 42, 'return from generator method, inside ctor'); + assert.sameValue(res.done, true, 'iterator is done, inside ctor'); + assert.sameValue(this.#m.name, '#m', 'function name inside constructor'); + + } +} + +var c = new C(); +var other = new C(); + +hasProp(C.prototype, '#m', false, 'method is not defined in the prototype'); +hasProp(C, '#m', false, 'method is not defined in the contructor'); +hasProp(c, '#m', false, 'method cannot be seen outside of the class'); + +/*** + * MethodDefinition : ClassElementName ( UniqueFormalParameters ) { FunctionBody } + * + * 1. Let methodDef be DefineMethod of MethodDefinition with argument homeObject. + * ... + */ +assert.sameValue(c.ref, other.ref, 'The method is defined once, and reused on every new instance'); + +// gets the returned iterator from #m +var res = c.ref().next(); +assert.sameValue(res.value, 42, 'return from generator method'); +assert.sameValue(res.done, true, 'iterator is done'); +assert.sameValue(c.ref.name, '#m', 'function name is preserved external reference'); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/language/statements/class/elements/private-methods/prod-private-method-initialize-order.js b/js/src/tests/test262/language/statements/class/elements/private-methods/prod-private-method-initialize-order.js new file mode 100644 index 0000000000..a44bf73cd1 --- /dev/null +++ b/js/src/tests/test262/language/statements/class/elements/private-methods/prod-private-method-initialize-order.js @@ -0,0 +1,141 @@ +// This file was procedurally generated from the following sources: +// - src/class-elements/prod-private-method-initialize-order.case +// - src/class-elements/private-methods/cls-decl.template +/*--- +description: Private methods are added before any field initializer is run, even if they appear textually later (private method definitions in a class declaration) +esid: prod-MethodDefinition +features: [class-methods-private, class-fields-private, class-fields-public, class] +flags: [generated] +info: | + ClassElement : + MethodDefinition + ... + ; + + ClassElementName : + PropertyName + PrivateName + + PrivateName :: + # IdentifierName + + MethodDefinition : + ClassElementName ( UniqueFormalParameters ) { FunctionBody } + GeneratorMethod + AsyncMethod + AsyncGeneratorMethod + get ClassElementName () { FunctionBody } + set ClassElementName ( PropertySetParameterList ) { FunctionBody } + + GeneratorMethod : + * ClassElementName ( UniqueFormalParameters ){GeneratorBody} + + AsyncMethod : + async [no LineTerminator here] ClassElementName ( UniqueFormalParameters ) { AsyncFunctionBody } + + AsyncGeneratorMethod : + async [no LineTerminator here]* ClassElementName ( UniqueFormalParameters ) { AsyncGeneratorBody } + + --- + + InitializeClassElements ( F, proto ) + + ... + 5. For each item element in order from elements, + a. Assert: If element.[[Placement]] is "prototype" or "static", then element.[[Key]] is not a Private Name. + b. If element.[[Kind]] is "method" and element.[[Placement]] is "static" or "prototype", + i. Let receiver be F if element.[[Placement]] is "static", else let receiver be proto. + ii. Perform ? DefineClassElement(receiver, element). + + InitializeInstanceElements ( O, constructor ) + + ... + 3. Let elements be the value of F's [[Elements]] internal slot. + 4. For each item element in order from elements, + a. If element.[[Placement]] is "own" and element.[[Kind]] is "method", + i. Perform ? DefineClassElement(O, element). + + DefineClassElement (receiver, element) + + ... + 6. If key is a Private Name, + a. Perform ? PrivateFieldDefine(receiver, key, descriptor). + + PrivateFieldDefine (P, O, desc) + + ... + 6. Append { [[PrivateName]]: P, [[PrivateFieldDescriptor]]: desc } to O.[[PrivateFieldDescriptors]]. + + + InitializeInstanceElements ( O, constructor ) + ... + 4. For each item element in order from elements, + a. If element.[[Placement]] is "own" and element.[[Kind]] is "method", + i. Perform ? DefineClassElement(O, element). + 5. For each item element in order from elements, + a. If element.[[Placement]] is "own" and element.[[Kind]] is "field", + i. Assert: element.[[Descriptor]] does not have a [[Value]], [[Get]] or [[Set]] slot. + ii. Perform ? DefineClassElement(O, element). + 6. Return. + + EDITOR'S NOTE: + Value properties are added before initializers so that private methods are visible from all initializers. + +---*/ + + +/*** template notes + * method should always be #m + * the template provides c.ref() for external reference + */ + +function hasProp(obj, name, expected, msg) { + var hasOwnProperty = Object.prototype.hasOwnProperty.call(obj, name); + assert.sameValue(hasOwnProperty, expected, msg); + + var hasProperty = Reflect.has(obj, name); + assert.sameValue(hasProperty, expected, msg); +} + +class C { + a = this.#m(); + + #m() { return 42; } + get bGetter() { return this.#b; } + + #b = this.#m(); + + + get ref() { return this.#m; } + + constructor() { + hasProp(this, '#m', false, 'private methods are defined in an special internal slot and cannot be found as own properties'); + assert.sameValue(typeof this.#m, 'function'); + assert.sameValue(this.ref, this.#m, 'returns the same value'); + assert.sameValue(this.#m, (() => this)().#m, 'memberexpression and call expression forms'); + + assert.sameValue(this.a, 42); + assert.sameValue(this.#b, 42); + + } +} + +var c = new C(); +var other = new C(); + +hasProp(C.prototype, '#m', false, 'method is not defined in the prototype'); +hasProp(C, '#m', false, 'method is not defined in the contructor'); +hasProp(c, '#m', false, 'method cannot be seen outside of the class'); + +/*** + * MethodDefinition : ClassElementName ( UniqueFormalParameters ) { FunctionBody } + * + * 1. Let methodDef be DefineMethod of MethodDefinition with argument homeObject. + * ... + */ +assert.sameValue(c.ref, other.ref, 'The method is defined once, and reused on every new instance'); + +assert.sameValue(c.a, 42); +assert.sameValue(c.bGetter, 42); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/language/statements/class/elements/private-methods/prod-private-method.js b/js/src/tests/test262/language/statements/class/elements/private-methods/prod-private-method.js new file mode 100644 index 0000000000..77c16e2a58 --- /dev/null +++ b/js/src/tests/test262/language/statements/class/elements/private-methods/prod-private-method.js @@ -0,0 +1,122 @@ +// This file was procedurally generated from the following sources: +// - src/class-elements/prod-private-method.case +// - src/class-elements/private-methods/cls-decl.template +/*--- +description: Private Method (private method definitions in a class declaration) +esid: prod-MethodDefinition +features: [class, class-methods-private] +flags: [generated] +info: | + ClassElement : + MethodDefinition + ... + ; + + ClassElementName : + PropertyName + PrivateName + + PrivateName :: + # IdentifierName + + MethodDefinition : + ClassElementName ( UniqueFormalParameters ) { FunctionBody } + GeneratorMethod + AsyncMethod + AsyncGeneratorMethod + get ClassElementName () { FunctionBody } + set ClassElementName ( PropertySetParameterList ) { FunctionBody } + + GeneratorMethod : + * ClassElementName ( UniqueFormalParameters ){GeneratorBody} + + AsyncMethod : + async [no LineTerminator here] ClassElementName ( UniqueFormalParameters ) { AsyncFunctionBody } + + AsyncGeneratorMethod : + async [no LineTerminator here]* ClassElementName ( UniqueFormalParameters ) { AsyncGeneratorBody } + + --- + + InitializeClassElements ( F, proto ) + + ... + 5. For each item element in order from elements, + a. Assert: If element.[[Placement]] is "prototype" or "static", then element.[[Key]] is not a Private Name. + b. If element.[[Kind]] is "method" and element.[[Placement]] is "static" or "prototype", + i. Let receiver be F if element.[[Placement]] is "static", else let receiver be proto. + ii. Perform ? DefineClassElement(receiver, element). + + InitializeInstanceElements ( O, constructor ) + + ... + 3. Let elements be the value of F's [[Elements]] internal slot. + 4. For each item element in order from elements, + a. If element.[[Placement]] is "own" and element.[[Kind]] is "method", + i. Perform ? DefineClassElement(O, element). + + DefineClassElement (receiver, element) + + ... + 6. If key is a Private Name, + a. Perform ? PrivateFieldDefine(receiver, key, descriptor). + + PrivateFieldDefine (P, O, desc) + + ... + 6. Append { [[PrivateName]]: P, [[PrivateFieldDescriptor]]: desc } to O.[[PrivateFieldDescriptors]]. + +---*/ + + +/*** template notes + * method should always be #m + * the template provides c.ref() for external reference + */ + +function hasProp(obj, name, expected, msg) { + var hasOwnProperty = Object.prototype.hasOwnProperty.call(obj, name); + assert.sameValue(hasOwnProperty, expected, msg); + + var hasProperty = Reflect.has(obj, name); + assert.sameValue(hasProperty, expected, msg); +} + +class C { + #m() { return 42; } + + + get ref() { return this.#m; } + + constructor() { + hasProp(this, '#m', false, 'private methods are defined in an special internal slot and cannot be found as own properties'); + assert.sameValue(typeof this.#m, 'function'); + assert.sameValue(this.ref, this.#m, 'returns the same value'); + assert.sameValue(this.#m, (() => this)().#m, 'memberexpression and call expression forms'); + + assert.sameValue(this.#m(), 42, 'already defined in the ctor'); + assert.sameValue(this.#m.name, '#m', 'function name inside constructor'); + + } +} + +var c = new C(); +var other = new C(); + +hasProp(C.prototype, '#m', false, 'method is not defined in the prototype'); +hasProp(C, '#m', false, 'method is not defined in the contructor'); +hasProp(c, '#m', false, 'method cannot be seen outside of the class'); + +/*** + * MethodDefinition : ClassElementName ( UniqueFormalParameters ) { FunctionBody } + * + * 1. Let methodDef be DefineMethod of MethodDefinition with argument homeObject. + * ... + */ +assert.sameValue(c.ref, other.ref, 'The method is defined once, and reused on every new instance'); + +// gets the returned value from #m +assert.sameValue(c.ref(), 42, 'function return'); +assert.sameValue(c.ref.name, '#m', 'function name is preserved external reference'); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/language/statements/class/elements/private-methods/shell.js b/js/src/tests/test262/language/statements/class/elements/private-methods/shell.js new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/js/src/tests/test262/language/statements/class/elements/private-methods/shell.js |