Add tests for Subclassing the built-in GeneratorFunction Objects

This commit is contained in:
Leonardo Balter 2016-01-12 14:57:25 -05:00
parent 4e079a8cab
commit 85ee704ad7
5 changed files with 182 additions and 0 deletions

View File

@ -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');

View File

@ -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');

View File

@ -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 functions 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');

View File

@ -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);

View File

@ -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);