From 8db908784e5592de1c844d4947bcf6e55c6fefe3 Mon Sep 17 00:00:00 2001 From: "verwaest@chromium.org" Date: Fri, 9 May 2014 16:37:04 +0000 Subject: [PATCH] Array Iterator prototype should not have a constructor. BUG=v8:3293 LOG=Y R=verwaest@chromium.org Review URL: https://codereview.chromium.org/258793005 Patch from Erik Arvidsson . git-svn-id: https://v8.googlecode.com/svn/branches/bleeding_edge@21234 ce2b1a6d-e550-0410-aec6-3dcde31c8c00 --- src/array-iterator.js | 15 ++++++++++--- test/mjsunit/harmony/array-iterator.js | 41 +++++++++++++++++++++++++--------- 2 files changed, 43 insertions(+), 13 deletions(-) diff --git a/src/array-iterator.js b/src/array-iterator.js index 10116b1..37f098a 100644 --- a/src/array-iterator.js +++ b/src/array-iterator.js @@ -27,16 +27,20 @@ 'use strict'; + // This file relies on the fact that the following declaration has been made // in runtime.js: // var $Array = global.Array; + var arrayIteratorObjectSymbol = GLOBAL_PRIVATE("ArrayIterator#object"); var arrayIteratorNextIndexSymbol = GLOBAL_PRIVATE("ArrayIterator#next"); var arrayIterationKindSymbol = GLOBAL_PRIVATE("ArrayIterator#kind"); + function ArrayIterator() {} + // 15.4.5.1 CreateArrayIterator Abstract Operation function CreateArrayIterator(array, kind) { var object = ToObject(array); @@ -47,11 +51,13 @@ function CreateArrayIterator(array, kind) { return iterator; } + // 15.19.4.3.4 CreateItrResultObject function CreateIteratorResultObject(value, done) { return {value: value, done: done}; } + // 15.4.5.2.2 ArrayIterator.prototype.next( ) function ArrayIteratorNext() { var iterator = ToObject(this); @@ -83,31 +89,35 @@ function ArrayIteratorNext() { return CreateIteratorResultObject(index, false); } + function ArrayEntries() { return CreateArrayIterator(this, ITERATOR_KIND_ENTRIES); } + function ArrayValues() { return CreateArrayIterator(this, ITERATOR_KIND_VALUES); } + function ArrayKeys() { return CreateArrayIterator(this, ITERATOR_KIND_KEYS); } + function SetUpArrayIterator() { %CheckIsBootstrapping(); + %FunctionSetPrototype(ArrayIterator, new $Object()); %FunctionSetInstanceClassName(ArrayIterator, 'Array Iterator'); - %FunctionSetReadOnlyPrototype(ArrayIterator); InstallFunctions(ArrayIterator.prototype, DONT_ENUM, $Array( 'next', ArrayIteratorNext )); } - SetUpArrayIterator(); + function ExtendArrayPrototype() { %CheckIsBootstrapping(); @@ -117,5 +127,4 @@ function ExtendArrayPrototype() { 'keys', ArrayKeys )); } - ExtendArrayPrototype(); diff --git a/test/mjsunit/harmony/array-iterator.js b/test/mjsunit/harmony/array-iterator.js index 6a402e7..cbe3b14 100644 --- a/test/mjsunit/harmony/array-iterator.js +++ b/test/mjsunit/harmony/array-iterator.js @@ -27,6 +27,7 @@ // Flags: --harmony-iteration --allow-natives-syntax + function TestArrayPrototype() { assertTrue(Array.prototype.hasOwnProperty('entries')); assertTrue(Array.prototype.hasOwnProperty('values')); @@ -38,10 +39,12 @@ function TestArrayPrototype() { } TestArrayPrototype(); + function assertIteratorResult(value, done, result) { assertEquals({value: value, done: done}, result); } + function TestValues() { var array = ['a', 'b', 'c']; var iterator = array.values(); @@ -55,6 +58,7 @@ function TestValues() { } TestValues(); + function TestValuesMutate() { var array = ['a', 'b', 'c']; var iterator = array.values(); @@ -67,6 +71,7 @@ function TestValuesMutate() { } TestValuesMutate(); + function TestKeys() { var array = ['a', 'b', 'c']; var iterator = array.keys(); @@ -80,6 +85,7 @@ function TestKeys() { } TestKeys(); + function TestKeysMutate() { var array = ['a', 'b', 'c']; var iterator = array.keys(); @@ -92,6 +98,7 @@ function TestKeysMutate() { } TestKeysMutate(); + function TestEntries() { var array = ['a', 'b', 'c']; var iterator = array.entries(); @@ -105,6 +112,7 @@ function TestEntries() { } TestEntries(); + function TestEntriesMutate() { var array = ['a', 'b', 'c']; var iterator = array.entries(); @@ -117,29 +125,40 @@ function TestEntriesMutate() { } TestEntriesMutate(); + +function TestArrayIteratorPrototype() { + var ArrayIteratorPrototype = [].values().__proto__; + assertFalse(ArrayIteratorPrototype.hasOwnProperty('constructor')); + assertEquals(ArrayIteratorPrototype.__proto__, Object.prototype); + assertArrayEquals(['next'], + Object.getOwnPropertyNames(ArrayIteratorPrototype)); +} +TestArrayIteratorPrototype(); + + function TestArrayIteratorPrototype() { var array = []; var iterator = array.values(); - var ArrayIterator = iterator.constructor; - assertEquals(ArrayIterator.prototype, array.values().__proto__); - assertEquals(ArrayIterator.prototype, array.keys().__proto__); - assertEquals(ArrayIterator.prototype, array.entries().__proto__); + var ArrayIteratorPrototype = iterator.__proto__; - assertEquals(Object.prototype, ArrayIterator.prototype.__proto__); + assertEquals(ArrayIteratorPrototype, array.values().__proto__); + assertEquals(ArrayIteratorPrototype, array.keys().__proto__); + assertEquals(ArrayIteratorPrototype, array.entries().__proto__); + + assertEquals(Object.prototype, ArrayIteratorPrototype.__proto__); assertEquals('Array Iterator', %_ClassOf(array.values())); assertEquals('Array Iterator', %_ClassOf(array.keys())); assertEquals('Array Iterator', %_ClassOf(array.entries())); - var prototypeDescriptor = - Object.getOwnPropertyDescriptor(ArrayIterator, 'prototype'); - assertFalse(prototypeDescriptor.configurable); - assertFalse(prototypeDescriptor.enumerable); - assertFalse(prototypeDescriptor.writable); + assertFalse(ArrayIteratorPrototype.hasOwnProperty('constructor')); + assertArrayEquals(['next'], + Object.getOwnPropertyNames(ArrayIteratorPrototype)); } TestArrayIteratorPrototype(); + function TestForArrayValues() { var buffer = []; var array = [0, 'a', true, false, null, /* hole */, undefined, NaN]; @@ -157,6 +176,7 @@ function TestForArrayValues() { } TestForArrayValues(); + function TestForArrayKeys() { var buffer = []; var array = [0, 'a', true, false, null, /* hole */, undefined, NaN]; @@ -173,6 +193,7 @@ function TestForArrayKeys() { } TestForArrayKeys(); + function TestForArrayEntries() { var buffer = []; var array = [0, 'a', true, false, null, /* hole */, undefined, NaN]; -- 2.7.4