Add @@iterator, .entries(), .values(), .keys() support to typed arrays
authorwingo@igalia.com <wingo@igalia.com@ce2b1a6d-e550-0410-aec6-3dcde31c8c00>
Wed, 25 Jun 2014 08:46:53 +0000 (08:46 +0000)
committerwingo@igalia.com <wingo@igalia.com@ce2b1a6d-e550-0410-aec6-3dcde31c8c00>
Wed, 25 Jun 2014 08:46:53 +0000 (08:46 +0000)
R=arv@chromium.org, rossberg@chromium.org
BUG=

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

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

src/array-iterator.js
test/mjsunit/harmony/typed-array-iterator.js [new file with mode: 0644]
tools/generate-runtime-tests.py

index de52922..f1939ea 100644 (file)
@@ -128,3 +128,30 @@ function ExtendArrayPrototype() {
   %SetProperty($Array.prototype, symbolIterator, ArrayValues, DONT_ENUM);
 }
 ExtendArrayPrototype();
+
+
+function ExtendTypedArrayPrototypes() {
+  %CheckIsBootstrapping();
+
+macro TYPED_ARRAYS(FUNCTION)
+  FUNCTION(Uint8Array)
+  FUNCTION(Int8Array)
+  FUNCTION(Uint16Array)
+  FUNCTION(Int16Array)
+  FUNCTION(Uint32Array)
+  FUNCTION(Int32Array)
+  FUNCTION(Float32Array)
+  FUNCTION(Float64Array)
+  FUNCTION(Uint8ClampedArray)
+endmacro
+
+macro EXTEND_TYPED_ARRAY(NAME)
+  %SetProperty($NAME.prototype, 'entries', ArrayEntries, DONT_ENUM);
+  %SetProperty($NAME.prototype, 'values', ArrayValues, DONT_ENUM);
+  %SetProperty($NAME.prototype, 'keys', ArrayKeys, DONT_ENUM);
+  %SetProperty($NAME.prototype, symbolIterator, ArrayValues, DONT_ENUM);
+endmacro
+
+  TYPED_ARRAYS(EXTEND_TYPED_ARRAY)
+}
+ExtendTypedArrayPrototypes();
diff --git a/test/mjsunit/harmony/typed-array-iterator.js b/test/mjsunit/harmony/typed-array-iterator.js
new file mode 100644 (file)
index 0000000..8559dfe
--- /dev/null
@@ -0,0 +1,41 @@
+// 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.
+
+// Flags: --harmony-iteration
+
+
+var constructors = [Uint8Array, Int8Array,
+                    Uint16Array, Int16Array,
+                    Uint32Array, Int32Array,
+                    Float32Array, Float64Array,
+                    Uint8ClampedArray];
+
+function TestTypedArrayPrototype(constructor) {
+  assertTrue(constructor.prototype.hasOwnProperty('entries'));
+  assertTrue(constructor.prototype.hasOwnProperty('values'));
+  assertTrue(constructor.prototype.hasOwnProperty('keys'));
+  assertTrue(constructor.prototype.hasOwnProperty(Symbol.iterator));
+
+  assertFalse(constructor.prototype.propertyIsEnumerable('entries'));
+  assertFalse(constructor.prototype.propertyIsEnumerable('values'));
+  assertFalse(constructor.prototype.propertyIsEnumerable('keys'));
+  assertFalse(constructor.prototype.propertyIsEnumerable(Symbol.iterator));
+
+  assertEquals(Array.prototype.entries, constructor.prototype.entries);
+  assertEquals(Array.prototype.values, constructor.prototype.values);
+  assertEquals(Array.prototype.keys, constructor.prototype.keys);
+  assertEquals(Array.prototype.values, constructor.prototype[Symbol.iterator]);
+}
+constructors.forEach(TestTypedArrayPrototype);
+
+
+function TestTypedArrayValues(constructor) {
+  var array = [1, 2, 3];
+  var i = 0;
+  for (var value of new constructor(array)) {
+    assertEquals(array[i++], value);
+  }
+  assertEquals(i, array.length);
+}
+constructors.forEach(TestTypedArrayValues);
index d15d0d4..81e5781 100755 (executable)
@@ -51,7 +51,7 @@ EXPECTED_FUNCTION_COUNT = 358
 EXPECTED_FUZZABLE_COUNT = 326
 EXPECTED_CCTEST_COUNT = 6
 EXPECTED_UNKNOWN_COUNT = 4
-EXPECTED_BUILTINS_COUNT = 807
+EXPECTED_BUILTINS_COUNT = 808
 
 
 # Don't call these at all.