Commit 834a8e9f authored by arv's avatar arv Committed by Commit bot

[es6] Iterators and generators should "extend" %IteratorPrototype%

All the builtin iterators as well as the generator objects have an
object called %IteratorPrototype% in the spec between them and
%ObjectPrototype%.

BUG=v8:3568
LOG=N

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

Cr-Commit-Position: refs/heads/master@{#28426}
parent d989a257
...@@ -215,6 +215,7 @@ action("js2c") { ...@@ -215,6 +215,7 @@ action("js2c") {
"src/regexp.js", "src/regexp.js",
"src/arraybuffer.js", "src/arraybuffer.js",
"src/typedarray.js", "src/typedarray.js",
"src/iterator-prototype.js",
"src/generator.js", "src/generator.js",
"src/object-observe.js", "src/object-observe.js",
"src/collection.js", "src/collection.js",
......
...@@ -12,7 +12,6 @@ var $arrayValues; ...@@ -12,7 +12,6 @@ var $arrayValues;
%CheckIsBootstrapping(); %CheckIsBootstrapping();
var GlobalArray = global.Array; var GlobalArray = global.Array;
var GlobalObject = global.Object;
macro TYPED_ARRAYS(FUNCTION) macro TYPED_ARRAYS(FUNCTION)
FUNCTION(Uint8Array) FUNCTION(Uint8Array)
...@@ -122,7 +121,7 @@ function ArrayKeys() { ...@@ -122,7 +121,7 @@ function ArrayKeys() {
} }
%FunctionSetPrototype(ArrayIterator, new GlobalObject()); %FunctionSetPrototype(ArrayIterator, {__proto__: $iteratorPrototype});
%FunctionSetInstanceClassName(ArrayIterator, 'Array Iterator'); %FunctionSetInstanceClassName(ArrayIterator, 'Array Iterator');
$installFunctions(ArrayIterator.prototype, DONT_ENUM, [ $installFunctions(ArrayIterator.prototype, DONT_ENUM, [
......
...@@ -2129,10 +2129,17 @@ bool Genesis::InstallNatives() { ...@@ -2129,10 +2129,17 @@ bool Genesis::InstallNatives() {
{ {
// Create generator meta-objects and install them on the builtins object. // Create generator meta-objects and install them on the builtins object.
Handle<JSObject> builtins(native_context()->builtins()); Handle<JSObject> builtins(native_context()->builtins());
Handle<JSObject> iterator_prototype =
factory()->NewJSObject(isolate()->object_function(), TENURED);
Handle<JSObject> generator_object_prototype = Handle<JSObject> generator_object_prototype =
factory()->NewJSObject(isolate()->object_function(), TENURED); factory()->NewJSObject(isolate()->object_function(), TENURED);
Handle<JSObject> generator_function_prototype = Handle<JSObject> generator_function_prototype =
factory()->NewJSObject(isolate()->object_function(), TENURED); factory()->NewJSObject(isolate()->object_function(), TENURED);
SetObjectPrototype(generator_object_prototype, iterator_prototype);
JSObject::AddProperty(
builtins, factory()->InternalizeUtf8String("$iteratorPrototype"),
iterator_prototype,
static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY));
JSObject::AddProperty( JSObject::AddProperty(
builtins, builtins,
factory()->InternalizeUtf8String("GeneratorFunctionPrototype"), factory()->InternalizeUtf8String("GeneratorFunctionPrototype"),
......
...@@ -14,7 +14,6 @@ var $setValues; ...@@ -14,7 +14,6 @@ var $setValues;
%CheckIsBootstrapping(); %CheckIsBootstrapping();
var GlobalMap = global.Map; var GlobalMap = global.Map;
var GlobalObject = global.Object;
var GlobalSet = global.Set; var GlobalSet = global.Set;
// ------------------------------------------------------------------- // -------------------------------------------------------------------
...@@ -49,11 +48,6 @@ function SetIteratorNextJS() { ...@@ -49,11 +48,6 @@ function SetIteratorNextJS() {
} }
function SetIteratorSymbolIterator() {
return this;
}
function SetEntries() { function SetEntries() {
if (!IS_SET(this)) { if (!IS_SET(this)) {
throw MakeTypeError(kIncompatibleMethodReceiver, throw MakeTypeError(kIncompatibleMethodReceiver,
...@@ -74,15 +68,12 @@ function SetValues() { ...@@ -74,15 +68,12 @@ function SetValues() {
// ------------------------------------------------------------------- // -------------------------------------------------------------------
%SetCode(SetIterator, SetIteratorConstructor); %SetCode(SetIterator, SetIteratorConstructor);
%FunctionSetPrototype(SetIterator, new GlobalObject()); %FunctionSetPrototype(SetIterator, {__proto__: $iteratorPrototype});
%FunctionSetInstanceClassName(SetIterator, 'Set Iterator'); %FunctionSetInstanceClassName(SetIterator, 'Set Iterator');
$installFunctions(SetIterator.prototype, DONT_ENUM, [ $installFunctions(SetIterator.prototype, DONT_ENUM, [
'next', SetIteratorNextJS 'next', SetIteratorNextJS
]); ]);
$setFunctionName(SetIteratorSymbolIterator, symbolIterator);
%AddNamedProperty(SetIterator.prototype, symbolIterator,
SetIteratorSymbolIterator, DONT_ENUM);
%AddNamedProperty(SetIterator.prototype, symbolToStringTag, %AddNamedProperty(SetIterator.prototype, symbolToStringTag,
"Set Iterator", READ_ONLY | DONT_ENUM); "Set Iterator", READ_ONLY | DONT_ENUM);
...@@ -104,11 +95,6 @@ function MapIteratorConstructor(map, kind) { ...@@ -104,11 +95,6 @@ function MapIteratorConstructor(map, kind) {
} }
function MapIteratorSymbolIterator() {
return this;
}
function MapIteratorNextJS() { function MapIteratorNextJS() {
if (!IS_MAP_ITERATOR(this)) { if (!IS_MAP_ITERATOR(this)) {
throw MakeTypeError(kIncompatibleMethodReceiver, throw MakeTypeError(kIncompatibleMethodReceiver,
...@@ -164,15 +150,12 @@ function MapValues() { ...@@ -164,15 +150,12 @@ function MapValues() {
// ------------------------------------------------------------------- // -------------------------------------------------------------------
%SetCode(MapIterator, MapIteratorConstructor); %SetCode(MapIterator, MapIteratorConstructor);
%FunctionSetPrototype(MapIterator, new GlobalObject()); %FunctionSetPrototype(MapIterator, {__proto__: $iteratorPrototype});
%FunctionSetInstanceClassName(MapIterator, 'Map Iterator'); %FunctionSetInstanceClassName(MapIterator, 'Map Iterator');
$installFunctions(MapIterator.prototype, DONT_ENUM, [ $installFunctions(MapIterator.prototype, DONT_ENUM, [
'next', MapIteratorNextJS 'next', MapIteratorNextJS
]); ]);
$setFunctionName(MapIteratorSymbolIterator, symbolIterator);
%AddNamedProperty(MapIterator.prototype, symbolIterator,
MapIteratorSymbolIterator, DONT_ENUM);
%AddNamedProperty(MapIterator.prototype, symbolToStringTag, %AddNamedProperty(MapIterator.prototype, symbolToStringTag,
"Map Iterator", READ_ONLY | DONT_ENUM); "Map Iterator", READ_ONLY | DONT_ENUM);
......
...@@ -66,11 +66,6 @@ function GeneratorObjectThrow(exn) { ...@@ -66,11 +66,6 @@ function GeneratorObjectThrow(exn) {
} }
function GeneratorObjectIterator() {
return this;
}
function GeneratorFunctionConstructor(arg1) { // length == 1 function GeneratorFunctionConstructor(arg1) { // length == 1
var source = $newFunctionString(arguments, 'function*'); var source = $newFunctionString(arguments, 'function*');
var global_proxy = %GlobalProxy(GeneratorFunctionConstructor); var global_proxy = %GlobalProxy(GeneratorFunctionConstructor);
...@@ -95,9 +90,6 @@ $installFunctions(GeneratorObjectPrototype, ...@@ -95,9 +90,6 @@ $installFunctions(GeneratorObjectPrototype,
["next", GeneratorObjectNext, ["next", GeneratorObjectNext,
"throw", GeneratorObjectThrow]); "throw", GeneratorObjectThrow]);
$setFunctionName(GeneratorObjectIterator, symbolIterator);
%AddNamedProperty(GeneratorObjectPrototype, symbolIterator,
GeneratorObjectIterator, DONT_ENUM | DONT_DELETE | READ_ONLY);
%AddNamedProperty(GeneratorObjectPrototype, "constructor", %AddNamedProperty(GeneratorObjectPrototype, "constructor",
GeneratorFunctionPrototype, DONT_ENUM | READ_ONLY); GeneratorFunctionPrototype, DONT_ENUM | READ_ONLY);
%AddNamedProperty(GeneratorObjectPrototype, %AddNamedProperty(GeneratorObjectPrototype,
......
// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
var $iteratorPrototype;
(function(global, shared, exports) {
"use strict";
%CheckIsBootstrapping();
var GlobalObject = global.Object;
// 25.1.2.1 %IteratorPrototype% [ @@iterator ] ( )
function IteratorPrototypeIterator() {
return this;
}
$setFunctionName(IteratorPrototypeIterator, symbolIterator);
%AddNamedProperty($iteratorPrototype, symbolIterator,
IteratorPrototypeIterator, DONT_ENUM);
})
...@@ -8,7 +8,6 @@ ...@@ -8,7 +8,6 @@
%CheckIsBootstrapping(); %CheckIsBootstrapping();
var GlobalObject = global.Object;
var GlobalString = global.String; var GlobalString = global.String;
//------------------------------------------------------------------- //-------------------------------------------------------------------
...@@ -31,12 +30,6 @@ function CreateStringIterator(string) { ...@@ -31,12 +30,6 @@ function CreateStringIterator(string) {
} }
// 21.1.5.2.2 %StringIteratorPrototype%[@@iterator]
function StringIteratorIterator() {
return this;
}
// 21.1.5.2.1 %StringIteratorPrototype%.next( ) // 21.1.5.2.1 %StringIteratorPrototype%.next( )
function StringIteratorNext() { function StringIteratorNext() {
var iterator = $toObject(this); var iterator = $toObject(this);
...@@ -85,15 +78,12 @@ function StringPrototypeIterator() { ...@@ -85,15 +78,12 @@ function StringPrototypeIterator() {
//------------------------------------------------------------------- //-------------------------------------------------------------------
%FunctionSetPrototype(StringIterator, new GlobalObject()); %FunctionSetPrototype(StringIterator, {__proto__: $iteratorPrototype});
%FunctionSetInstanceClassName(StringIterator, 'String Iterator'); %FunctionSetInstanceClassName(StringIterator, 'String Iterator');
$installFunctions(StringIterator.prototype, DONT_ENUM, [ $installFunctions(StringIterator.prototype, DONT_ENUM, [
'next', StringIteratorNext 'next', StringIteratorNext
]); ]);
$setFunctionName(StringIteratorIterator, symbolIterator);
%AddNamedProperty(StringIterator.prototype, symbolIterator,
StringIteratorIterator, DONT_ENUM);
%AddNamedProperty(StringIterator.prototype, symbolToStringTag, %AddNamedProperty(StringIterator.prototype, symbolToStringTag,
"String Iterator", READ_ONLY | DONT_ENUM); "String Iterator", READ_ONLY | DONT_ENUM);
......
...@@ -35,6 +35,7 @@ function* g() { yield 1; } ...@@ -35,6 +35,7 @@ function* g() { yield 1; }
var GeneratorFunctionPrototype = Object.getPrototypeOf(g); var GeneratorFunctionPrototype = Object.getPrototypeOf(g);
var GeneratorFunction = GeneratorFunctionPrototype.constructor; var GeneratorFunction = GeneratorFunctionPrototype.constructor;
var GeneratorObjectPrototype = GeneratorFunctionPrototype.prototype; var GeneratorObjectPrototype = GeneratorFunctionPrototype.prototype;
var IteratorPrototype = Object.getPrototypeOf(GeneratorObjectPrototype);
// A generator function should have the same set of properties as any // A generator function should have the same set of properties as any
// other function. // other function.
...@@ -100,7 +101,7 @@ TestGeneratorFunctionPrototype(); ...@@ -100,7 +101,7 @@ TestGeneratorFunctionPrototype();
// Functions that we associate with generator objects are actually defined by // Functions that we associate with generator objects are actually defined by
// a common prototype. // a common prototype.
function TestGeneratorObjectPrototype() { function TestGeneratorObjectPrototype() {
assertSame(Object.prototype, assertSame(IteratorPrototype,
Object.getPrototypeOf(GeneratorObjectPrototype)); Object.getPrototypeOf(GeneratorObjectPrototype));
assertSame(GeneratorObjectPrototype, assertSame(GeneratorObjectPrototype,
Object.getPrototypeOf((function*(){yield 1}).prototype)); Object.getPrototypeOf((function*(){yield 1}).prototype));
...@@ -134,16 +135,6 @@ function TestGeneratorObjectPrototype() { ...@@ -134,16 +135,6 @@ function TestGeneratorObjectPrototype() {
assertTrue(throw_desc.writable); assertTrue(throw_desc.writable);
assertFalse(throw_desc.enumerable); assertFalse(throw_desc.enumerable);
assertTrue(throw_desc.configurable); assertTrue(throw_desc.configurable);
var iterator_desc = Object.getOwnPropertyDescriptor(GeneratorObjectPrototype,
Symbol.iterator);
assertTrue(iterator_desc !== undefined);
assertFalse(iterator_desc.writable);
assertFalse(iterator_desc.enumerable);
assertFalse(iterator_desc.configurable);
// The generator object's "iterator" function is just the identity.
assertSame(iterator_desc.value.call(42), 42);
} }
TestGeneratorObjectPrototype(); TestGeneratorObjectPrototype();
......
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
var arrayIteratorPrototype = [].entries().__proto__;
var iteratorPrototype = arrayIteratorPrototype.__proto__;
assertSame(Object.prototype, Object.getPrototypeOf(iteratorPrototype));
assertTrue(Object.isExtensible(iteratorPrototype));
assertSame(0, Object.getOwnPropertyNames(iteratorPrototype).length);
assertSame(1, Object.getOwnPropertySymbols(iteratorPrototype).length);
assertSame(Symbol.iterator,
Object.getOwnPropertySymbols(iteratorPrototype)[0]);
var descr = Object.getOwnPropertyDescriptor(iteratorPrototype, Symbol.iterator);
assertTrue(descr.configurable);
assertFalse(descr.enumerable);
assertTrue(descr.writable);
var iteratorFunction = descr.value;
assertSame('function', typeof iteratorFunction);
assertSame(0, iteratorFunction.length);
assertSame('[Symbol.iterator]', iteratorFunction.name);
var obj = {};
assertSame(obj, iteratorFunction.call(obj));
assertSame(iteratorPrototype, iteratorPrototype[Symbol.iterator]());
var mapIteratorPrototype = new Map().entries().__proto__;
var setIteratorPrototype = new Set().values().__proto__;
var stringIteratorPrototype = 'abc'[Symbol.iterator]().__proto__;
assertSame(iteratorPrototype, mapIteratorPrototype.__proto__);
assertSame(iteratorPrototype, setIteratorPrototype.__proto__);
assertSame(iteratorPrototype, stringIteratorPrototype.__proto__);
var typedArrays = [
Float32Array,
Float64Array,
Int16Array,
Int32Array,
Int8Array,
Uint16Array,
Uint32Array,
Uint8Array,
Uint8ClampedArray,
];
for (var constructor of typedArrays) {
var array = new constructor();
var iterator = array[Symbol.iterator]();
assertSame(iteratorPrototype, iterator.__proto__.__proto__);
}
function* gen() {}
assertSame(iteratorPrototype, gen.prototype.__proto__.__proto__);
var g = gen();
assertSame(gen.prototype, g.__proto__);
assertSame(iteratorPrototype, g.__proto__.__proto__.__proto__);
...@@ -1720,6 +1720,7 @@ ...@@ -1720,6 +1720,7 @@
'../../src/regexp.js', '../../src/regexp.js',
'../../src/arraybuffer.js', '../../src/arraybuffer.js',
'../../src/typedarray.js', '../../src/typedarray.js',
'../../src/iterator-prototype.js',
'../../src/generator.js', '../../src/generator.js',
'../../src/object-observe.js', '../../src/object-observe.js',
'../../src/collection.js', '../../src/collection.js',
......
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