Add @@iterator to Array.prototype
authorwingo@igalia.com <wingo@igalia.com@ce2b1a6d-e550-0410-aec6-3dcde31c8c00>
Wed, 25 Jun 2014 07:32:57 +0000 (07:32 +0000)
committerwingo@igalia.com <wingo@igalia.com@ce2b1a6d-e550-0410-aec6-3dcde31c8c00>
Wed, 25 Jun 2014 07:32:57 +0000 (07:32 +0000)
R=rossberg@chromium.org
BUG=

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

git-svn-id: https://v8.googlecode.com/svn/branches/bleeding_edge@21993 ce2b1a6d-e550-0410-aec6-3dcde31c8c00

src/array-iterator.js
test/mjsunit/harmony/array-iterator.js

index 9511b6d..de52922 100644 (file)
@@ -111,7 +111,7 @@ function SetUpArrayIterator() {
   ));
   %FunctionSetName(ArrayIteratorIterator, '[Symbol.iterator]');
   %SetProperty(ArrayIterator.prototype, symbolIterator, ArrayIteratorIterator,
-      DONT_ENUM | DONT_DELETE | READ_ONLY);
+      DONT_ENUM);
 }
 SetUpArrayIterator();
 
@@ -124,5 +124,7 @@ function ExtendArrayPrototype() {
     'values', ArrayValues,
     'keys', ArrayKeys
   ));
+
+  %SetProperty($Array.prototype, symbolIterator, ArrayValues, DONT_ENUM);
 }
 ExtendArrayPrototype();
index 2642d7b..2328876 100644 (file)
 // Flags: --harmony-iteration --allow-natives-syntax
 
 
+var NONE = 0;
+var READ_ONLY = 1;
+var DONT_ENUM = 2;
+var DONT_DELETE = 4;
+
+
+function assertHasOwnProperty(object, name, attrs) {
+  assertTrue(object.hasOwnProperty(name));
+  var desc = Object.getOwnPropertyDescriptor(object, name);
+  assertEquals(desc.writable, !(attrs & READ_ONLY));
+  assertEquals(desc.enumerable, !(attrs & DONT_ENUM));
+  assertEquals(desc.configurable, !(attrs & DONT_DELETE));
+}
+
+
 function TestArrayPrototype() {
-  assertTrue(Array.prototype.hasOwnProperty('entries'));
-  assertTrue(Array.prototype.hasOwnProperty('values'));
-  assertTrue(Array.prototype.hasOwnProperty('keys'));
+  assertHasOwnProperty(Array.prototype, 'entries', DONT_ENUM);
+  assertHasOwnProperty(Array.prototype, 'values', DONT_ENUM);
+  assertHasOwnProperty(Array.prototype, 'keys', DONT_ENUM);
+  assertHasOwnProperty(Array.prototype, Symbol.iterator, DONT_ENUM);
 
-  assertFalse(Array.prototype.propertyIsEnumerable('entries'));
-  assertFalse(Array.prototype.propertyIsEnumerable('values'));
-  assertFalse(Array.prototype.propertyIsEnumerable('keys'));
+  assertEquals(Array.prototype.values, Array.prototype[Symbol.iterator]);
 }
 TestArrayPrototype();
 
@@ -127,16 +141,6 @@ 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();
 
@@ -155,6 +159,8 @@ function TestArrayIteratorPrototype() {
   assertFalse(ArrayIteratorPrototype.hasOwnProperty('constructor'));
   assertArrayEquals(['next'],
       Object.getOwnPropertyNames(ArrayIteratorPrototype));
+  assertHasOwnProperty(ArrayIteratorPrototype, 'next', DONT_ENUM);
+  assertHasOwnProperty(ArrayIteratorPrototype, Symbol.iterator, DONT_ENUM);
 }
 TestArrayIteratorPrototype();
 
@@ -170,7 +176,7 @@ function TestForArrayValues() {
   assertEquals(8, buffer.length);
 
   for (var i = 0; i < buffer.length - 1; i++) {
-    assertEquals(array[i], buffer[i]);
+    assertSame(array[i], buffer[i]);
   }
   assertTrue(isNaN(buffer[buffer.length - 1]));
 }
@@ -205,7 +211,7 @@ function TestForArrayEntries() {
   assertEquals(8, buffer.length);
 
   for (var i = 0; i < buffer.length - 1; i++) {
-    assertEquals(array[i], buffer[i][1]);
+    assertSame(array[i], buffer[i][1]);
   }
   assertTrue(isNaN(buffer[buffer.length - 1][1]));
 
@@ -216,6 +222,24 @@ function TestForArrayEntries() {
 TestForArrayEntries();
 
 
+function TestForArray() {
+  var buffer = [];
+  var array = [0, 'a', true, false, null, /* hole */, undefined, NaN];
+  var i = 0;
+  for (var value of array) {
+    buffer[i++] = value;
+  }
+
+  assertEquals(8, buffer.length);
+
+  for (var i = 0; i < buffer.length - 1; i++) {
+    assertSame(array[i], buffer[i]);
+  }
+  assertTrue(isNaN(buffer[buffer.length - 1]));
+}
+TestForArrayValues();
+
+
 function TestNonOwnSlots() {
   var array = [0];
   var iterator = array.values();