From 85ee704ad7579468ee1f62e9f1070c4678053b04 Mon Sep 17 00:00:00 2001 From: Leonardo Balter <leonardo.balter@gmail.com> Date: Tue, 12 Jan 2016 14:57:25 -0500 Subject: [PATCH] Add tests for Subclassing the built-in GeneratorFunction Objects --- .../GeneratorFunction/instance-length.js | 31 +++++++++++++ .../GeneratorFunction/instance-name.js | 46 +++++++++++++++++++ .../GeneratorFunction/instance-prototype.js | 38 +++++++++++++++ .../GeneratorFunction/regular-subclassing.js | 29 ++++++++++++ .../GeneratorFunction/super-must-be-called.js | 38 +++++++++++++++ 5 files changed, 182 insertions(+) create mode 100644 test/language/subclassing/GeneratorFunction/instance-length.js create mode 100644 test/language/subclassing/GeneratorFunction/instance-name.js create mode 100644 test/language/subclassing/GeneratorFunction/instance-prototype.js create mode 100644 test/language/subclassing/GeneratorFunction/regular-subclassing.js create mode 100644 test/language/subclassing/GeneratorFunction/super-must-be-called.js diff --git a/test/language/subclassing/GeneratorFunction/instance-length.js b/test/language/subclassing/GeneratorFunction/instance-length.js new file mode 100644 index 0000000000..8fd184b872 --- /dev/null +++ b/test/language/subclassing/GeneratorFunction/instance-length.js @@ -0,0 +1,31 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +es6id: 25.2.4.1 +description: > + Subclassed GeneratorFunction instances `length` property +info: > + 25.2.4.1 length + + The value of the length property is an integer that indicates the typical + number of arguments expected by the GeneratorFunction. However, the language + permits the function to be invoked with some other number of arguments. The + behaviour of a GeneratorFunction when invoked on a number of arguments other + than the number specified by its length property depends on the function. + + This property has the attributes { [[Writable]]: false, [[Enumerable]]: false, + [[Configurable]]: true }. +includes: [propertyHelper.js] +---*/ + +var GeneratorFunction = Object.getPrototypeOf(function* () {}).constructor; + +class GFn extends GeneratorFunction {} + +var gfn = new GFn('a', 'b', 'return a + b'); + +assert.sameValue(gfn.length, 2); + +verifyNotEnumerable(gfn, 'length'); +verifyNotWritable(gfn, 'length'); +verifyConfigurable(gfn, 'length'); diff --git a/test/language/subclassing/GeneratorFunction/instance-name.js b/test/language/subclassing/GeneratorFunction/instance-name.js new file mode 100644 index 0000000000..2da93f5057 --- /dev/null +++ b/test/language/subclassing/GeneratorFunction/instance-name.js @@ -0,0 +1,46 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +es6id: 25.2.4.2 +description: Subclassed GeneratorFunction instances `name` property +info: > + 25.2.4.2 name + + The specification for the name property of Function instances given in + 19.2.4.2 also applies to GeneratorFunction instances. + + 19.2.4.2 name + + The value of the name property is an String that is descriptive of the + function. The name has no semantic significance but is typically a variable or + property name that is used to refer to the function at its point of definition + in ECMAScript code. This property has the attributes { [[Writable]]: false, + [[Enumerable]]: false, [[Configurable]]: true }. + + Anonymous functions objects that do not have a contextual name associated with + them by this specification do not have a name own property but inherit the + name property of %FunctionPrototype%. + + 19.2.1.1.1 RuntimeSemantics: CreateDynamicFunction(constructor, newTarget, + kind, args) + + ... + 29. Perform SetFunctionName(F, "anonymous"). + ... +includes: [propertyHelper.js] +---*/ + +var GeneratorFunction = Object.getPrototypeOf(function* () {}).constructor; + +class GFn extends GeneratorFunction {} + +var gfn = new GFn('a', 'b', 'return a + b'); + +assert.sameValue( + gfn.name, 'anonymous', + 'Dynamic Functions are called anonymous' +); + +verifyNotEnumerable(gfn, 'name'); +verifyNotWritable(gfn, 'name'); +verifyConfigurable(gfn, 'name'); diff --git a/test/language/subclassing/GeneratorFunction/instance-prototype.js b/test/language/subclassing/GeneratorFunction/instance-prototype.js new file mode 100644 index 0000000000..73889e76c5 --- /dev/null +++ b/test/language/subclassing/GeneratorFunction/instance-prototype.js @@ -0,0 +1,38 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +es6id: 25.2.4.3 +description: > + Subclassed GeneratorFunction instances `prototype` property +info: > + 25.2.4.3 prototype + + Whenever a GeneratorFunction instance is created another ordinary object is + also created and is the initial value of the generator function’s prototype + property. The value of the prototype property is used to initialize the + [[Prototype]] internal slot of a newly created Generator object when the + generator function object is invoked using either [[Call]] or [[Construct]]. + + This property has the attributes { [[Writable]]: true, [[Enumerable]]: false, + [[Configurable]]: false }. +includes: [propertyHelper.js] +---*/ + +var GeneratorFunction = Object.getPrototypeOf(function* () {}).constructor; + +class GFn extends GeneratorFunction {} + +var gfn = new GFn(';'); + +assert.sameValue( + Object.keys(gfn.prototype).length, 0, + 'prototype is a new ordinary object' +); +assert.sameValue( + gfn.prototype.hasOwnProperty('constructor'), false, + 'prototype has no constructor reference' +); + +verifyNotEnumerable(gfn, 'prototype'); +verifyWritable(gfn, 'prototype'); +verifyNotConfigurable(gfn, 'prototype'); diff --git a/test/language/subclassing/GeneratorFunction/regular-subclassing.js b/test/language/subclassing/GeneratorFunction/regular-subclassing.js new file mode 100644 index 0000000000..cfbb473f9d --- /dev/null +++ b/test/language/subclassing/GeneratorFunction/regular-subclassing.js @@ -0,0 +1,29 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +es6id: 25.2.1 +description: Subclassing GeneratorFunction +info: > + 25.2.1 The GeneratorFunction Constructor + + ... + + GeneratorFunction is designed to be subclassable. It may be used as the value + of an extends clause of a class definition. Subclass constructors that intend + to inherit the specified GeneratorFunction behaviour must include a super call + to the GeneratorFunction constructor to create and initialize subclass + instances with the internal slots necessary for built-in GeneratorFunction + behaviour. + ... +---*/ + +var GeneratorFunction = Object.getPrototypeOf(function* () {}).constructor; + +class Gfn extends GeneratorFunction {} + +var gfn = new Gfn('a', 'yield a; yield a * 2;'); + +var iter = gfn(42); + +assert.sameValue(iter.next().value, 42); +assert.sameValue(iter.next().value, 84); diff --git a/test/language/subclassing/GeneratorFunction/super-must-be-called.js b/test/language/subclassing/GeneratorFunction/super-must-be-called.js new file mode 100644 index 0000000000..0288f95e1e --- /dev/null +++ b/test/language/subclassing/GeneratorFunction/super-must-be-called.js @@ -0,0 +1,38 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +es6id: 25.2.1 +description: > + super must be called to initialize GeneratorFunction internal slots +info: > + 25.2.1 The GeneratorFunction Constructor + + ... + + GeneratorFunction is designed to be subclassable. It may be used as the value + of an extends clause of a class definition. Subclass constructors that intend + to inherit the specified GeneratorFunction behaviour must include a super call + to the GeneratorFunction constructor to create and initialize subclass + instances with the internal slots necessary for built-in GeneratorFunction + behaviour. + ... +---*/ + +var GeneratorFunction = Object.getPrototypeOf(function* () {}).constructor; + +class GFn1 extends GeneratorFunction { + constructor() {} +} + +assert.throws(ReferenceError, function() { + new GFn1(); +}); + +class GFn2 extends GeneratorFunction { + constructor() { + super(); + } +} + +var fn = new GFn2(); +assert(fn instanceof GeneratorFunction); -- GitLab