Commit 3b624a17 authored by mike's avatar mike Committed by Commit bot

Re-implement %Generator% intrinsic as an object

From ES6 25.2.3 ("Properties of the GeneratorFunction Prototype
Object"):

> The GeneratorFunction prototype object is an ordinary object. It is
> not a function object and does not have an [[ECMAScriptCode]] internal
> slot or any other of the internal slots listed in Table 27 or Table
> 56.

Introduce one assertion for the value's type and additional tests for its
properties. Remove an invalid assertion that fails as a result of this
fix.

BUG=v8:3991
LOG=N

Review URL: https://codereview.chromium.org/1062633002

Cr-Commit-Position: refs/heads/master@{#27603}
parent 189b355a
...@@ -2059,10 +2059,20 @@ bool Genesis::InstallNatives() { ...@@ -2059,10 +2059,20 @@ bool Genesis::InstallNatives() {
Handle<JSObject> builtins(native_context()->builtins()); Handle<JSObject> builtins(native_context()->builtins());
Handle<JSObject> generator_object_prototype = Handle<JSObject> generator_object_prototype =
factory()->NewJSObject(isolate()->object_function(), TENURED); factory()->NewJSObject(isolate()->object_function(), TENURED);
Handle<JSFunction> generator_function_prototype = Handle<JSObject> generator_function_prototype =
InstallFunction(builtins, "GeneratorFunctionPrototype", factory()->NewJSObject(isolate()->object_function(), TENURED);
JS_FUNCTION_TYPE, JSFunction::kHeaderSize, JSObject::AddProperty(
generator_object_prototype, Builtins::kIllegal); builtins,
factory()->InternalizeUtf8String("GeneratorFunctionPrototype"),
generator_function_prototype,
static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY));
JSObject::AddProperty(
generator_function_prototype,
factory()->InternalizeUtf8String("prototype"),
generator_object_prototype,
static_cast<PropertyAttributes>(DONT_ENUM | READ_ONLY));
InstallFunction(builtins, "GeneratorFunction", JS_FUNCTION_TYPE, InstallFunction(builtins, "GeneratorFunction", JS_FUNCTION_TYPE,
JSFunction::kSize, generator_function_prototype, JSFunction::kSize, generator_function_prototype,
Builtins::kIllegal); Builtins::kIllegal);
......
...@@ -67,12 +67,6 @@ function GeneratorObjectIterator() { ...@@ -67,12 +67,6 @@ function GeneratorObjectIterator() {
return this; return this;
} }
function GeneratorFunctionPrototypeConstructor(x) {
if (%_IsConstructCall()) {
throw MakeTypeError('not_constructor', ['GeneratorFunctionPrototype']);
}
}
function GeneratorFunctionConstructor(arg1) { // length == 1 function GeneratorFunctionConstructor(arg1) { // length == 1
var source = NewFunctionString(arguments, 'function*'); var source = NewFunctionString(arguments, 'function*');
var global_proxy = %GlobalProxy(global); var global_proxy = %GlobalProxy(global);
...@@ -109,7 +103,6 @@ function SetUpGenerators() { ...@@ -109,7 +103,6 @@ function SetUpGenerators() {
%InternalSetPrototype(GeneratorFunctionPrototype, $Function.prototype); %InternalSetPrototype(GeneratorFunctionPrototype, $Function.prototype);
%AddNamedProperty(GeneratorFunctionPrototype, %AddNamedProperty(GeneratorFunctionPrototype,
symbolToStringTag, "GeneratorFunction", DONT_ENUM | READ_ONLY); symbolToStringTag, "GeneratorFunction", DONT_ENUM | READ_ONLY);
%SetCode(GeneratorFunctionPrototype, GeneratorFunctionPrototypeConstructor);
%AddNamedProperty(GeneratorFunctionPrototype, "constructor", %AddNamedProperty(GeneratorFunctionPrototype, "constructor",
GeneratorFunction, DONT_ENUM | READ_ONLY); GeneratorFunction, DONT_ENUM | READ_ONLY);
%InternalSetPrototype(GeneratorFunction, $Function); %InternalSetPrototype(GeneratorFunction, $Function);
......
...@@ -39,12 +39,26 @@ function isFunction(obj) { ...@@ -39,12 +39,26 @@ function isFunction(obj) {
} }
function isV8Native(name) { function isV8Native(name) {
return name == "GeneratorFunctionPrototype" || return name == "GeneratorFunction" ||
name == "GeneratorFunctionPrototype" ||
name == "SetIterator" || name == "SetIterator" ||
name == "MapIterator" || name == "MapIterator" ||
name == "ArrayIterator" || name == "ArrayIterator" ||
name == "StringIterator"; name == "StringIterator";
} }
var V8NativePrototypes = {
GeneratorFunction: Function.prototype,
// TODO(jugglinmike): Update the following values to the %IteratorPrototype%
// intrinsic once it is implemented.
// Issue 3568: Generator Prototype should have an object between itself
// and Object.prototype
// https://code.google.com/p/v8/issues/detail?id=3568
GeneratorFunctionPrototype: Object.prototype,
SetIterator: Object.prototype,
MapIterator: Object.prototype,
ArrayIterator: Object.prototype,
StringIterator: Object.prototype
};
function checkConstructor(func, name) { function checkConstructor(func, name) {
// A constructor is a function with a prototype and properties on the // A constructor is a function with a prototype and properties on the
...@@ -62,7 +76,7 @@ function checkConstructor(func, name) { ...@@ -62,7 +76,7 @@ function checkConstructor(func, name) {
assertFalse(proto_desc.writable, name); assertFalse(proto_desc.writable, name);
assertFalse(proto_desc.configurable, name); assertFalse(proto_desc.configurable, name);
var prototype = proto_desc.value; var prototype = proto_desc.value;
assertEquals(isV8Native(name) ? Object.prototype : null, assertEquals(V8NativePrototypes[name] || null,
Object.getPrototypeOf(prototype), Object.getPrototypeOf(prototype),
name); name);
for (var i = 0; i < propNames.length; i++) { for (var i = 0; i < propNames.length; i++) {
......
...@@ -78,6 +78,23 @@ function TestGeneratorFunctionPrototype() { ...@@ -78,6 +78,23 @@ function TestGeneratorFunctionPrototype() {
Object.getPrototypeOf(GeneratorFunctionPrototype)); Object.getPrototypeOf(GeneratorFunctionPrototype));
assertSame(GeneratorFunctionPrototype, assertSame(GeneratorFunctionPrototype,
Object.getPrototypeOf(function* () {})); Object.getPrototypeOf(function* () {}));
assertSame("object", typeof GeneratorFunctionPrototype);
var constructor_desc = Object.getOwnPropertyDescriptor(
GeneratorFunctionPrototype, "constructor");
assertTrue(constructor_desc !== undefined);
assertSame(GeneratorFunction, constructor_desc.value);
assertFalse(constructor_desc.writable);
assertFalse(constructor_desc.enumerable);
assertTrue(constructor_desc.configurable);
var prototype_desc = Object.getOwnPropertyDescriptor(
GeneratorFunctionPrototype, "prototype");
assertTrue(prototype_desc !== undefined);
assertSame(GeneratorObjectPrototype, prototype_desc.value);
assertFalse(prototype_desc.writable);
assertFalse(prototype_desc.enumerable);
assertTrue(prototype_desc.configurable);
} }
TestGeneratorFunctionPrototype(); TestGeneratorFunctionPrototype();
...@@ -136,7 +153,6 @@ TestGeneratorFunction(); ...@@ -136,7 +153,6 @@ TestGeneratorFunction();
function TestPerGeneratorPrototype() { function TestPerGeneratorPrototype() {
assertTrue((function*(){}).prototype !== (function*(){}).prototype); assertTrue((function*(){}).prototype !== (function*(){}).prototype);
assertTrue((function*(){}).prototype !== g.prototype); assertTrue((function*(){}).prototype !== g.prototype);
assertTrue(g.prototype instanceof GeneratorFunctionPrototype);
assertSame(GeneratorObjectPrototype, Object.getPrototypeOf(g.prototype)); assertSame(GeneratorObjectPrototype, Object.getPrototypeOf(g.prototype));
assertTrue(!(g.prototype instanceof Function)); assertTrue(!(g.prototype instanceof Function));
assertSame(typeof (g.prototype), "object"); assertSame(typeof (g.prototype), "object");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment