Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(39)

Unified Diff: test/mjsunit/harmony/typedarrays.js

Issue 1181903003: Allow TypedArrays to be initialized with iterables (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Fixes from arv's comments Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/typedarray.js ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: test/mjsunit/harmony/typedarrays.js
diff --git a/test/mjsunit/harmony/typedarrays.js b/test/mjsunit/harmony/typedarrays.js
index 70bd17e3382db1cdb115d5aae6e48cf9141bc82a..ef7955ce928bfd80deff04bea4ef71f2ad8395cc 100644
--- a/test/mjsunit/harmony/typedarrays.js
+++ b/test/mjsunit/harmony/typedarrays.js
@@ -276,6 +276,43 @@ function TestTypedArray(constr, elementSize, typicalElement) {
assertFalse(!!desc.writable);
assertFalse(!!desc.set);
assertEquals("function", typeof desc.get);
+
+ // Test that the constructor can be called with an iterable
+ function* gen() { for (var i = 0; i < 10; i++) yield i; }
+ var genArr = new constr(gen());
+ assertEquals(10, genArr.length);
+ assertEquals(0, genArr[0]);
+ assertEquals(9, genArr[9]);
+ // Arrays can be converted to TypedArrays
+ genArr = new constr([1, 2, 3]);
+ assertEquals(3, genArr.length);
+ assertEquals(1, genArr[0]);
+ assertEquals(3, genArr[2]);
+ // Redefining Array.prototype[Symbol.iterator] still works
+ var arrayIterator = Array.prototype[Symbol.iterator];
+ Array.prototype[Symbol.iterator] = gen;
+ genArr = new constr([1, 2, 3]);
+ assertEquals(10, genArr.length);
+ assertEquals(0, genArr[0]);
+ assertEquals(9, genArr[9]);
+ Array.prototype[Symbol.iterator] = arrayIterator;
+ // Other array-like things can be made into a TypedArray
+ var myObject = { 0: 5, 1: 6, length: 2 };
+ genArr = new constr(myObject);
+ assertEquals(2, genArr.length);
+ assertEquals(5, genArr[0]);
+ assertEquals(6, genArr[1]);
+ // Iterator takes precedence over array-like, and the property
+ // is read only once.
+ var iteratorReadCount = 0;
+ Object.defineProperty(myObject, Symbol.iterator, {
+ get: function() { iteratorReadCount++; return gen; }
+ });
+ genArr = new constr(myObject);
+ assertEquals(10, genArr.length);
+ assertEquals(0, genArr[0]);
+ assertEquals(9, genArr[9]);
+ assertEquals(1, iteratorReadCount);
}
TestTypedArray(Uint8Array, 1, 0xFF);
« no previous file with comments | « src/typedarray.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698