Test that TypedArray methods don't read length
authordehrenberg <dehrenberg@chromium.org>
Fri, 15 May 2015 02:03:47 +0000 (19:03 -0700)
committerCommit bot <commit-bot@chromium.org>
Fri, 15 May 2015 02:03:44 +0000 (02:03 +0000)
ES6 specifies that methods on TypedArrays reference an internal length
slot, rather than their length property. This patch tests that for the
TypedArray methods that exist currently.

R=arv@chromium.org
BUG=v8:3578
LOG=Y

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

Cr-Commit-Position: refs/heads/master@{#28409}

test/mjsunit/harmony/typedarray-fill.js
test/mjsunit/harmony/typedarray-find.js
test/mjsunit/harmony/typedarray-findindex.js
test/mjsunit/harmony/typedarrays-every.js
test/mjsunit/harmony/typedarrays-foreach.js

index 40605bb..4452bf6 100644 (file)
@@ -36,4 +36,12 @@ for (var constructor of typedArrayConstructors) {
   assertThrows('constructor.prototype.fill.call(null)', TypeError);
   assertThrows('constructor.prototype.fill.call(undefined)', TypeError);
   assertThrows('constructor.prototype.fill.call([])', TypeError);
+
+  // Shadowing length doesn't affect fill, unlike Array.prototype.fill
+  var a = new constructor([2, 2]);
+  Object.defineProperty(a, 'length', {value: 1});
+  a.fill(3);
+  assertArrayEquals([a[0], a[1]], [3, 3]);
+  Array.prototype.fill.call(a, 4);
+  assertArrayEquals([a[0], a[1]], [4, 3]);
 }
index 5e35125..3b7ba1e 100644 (file)
@@ -176,4 +176,14 @@ assertThrows('new constructor([]).find({})', TypeError);
 assertThrows('new constructor([]).find([])', TypeError);
 assertThrows('new constructor([]).find(/\d+/)', TypeError);
 
+// Shadowing length doesn't affect find, unlike Array.prototype.find
+a = new constructor([1, 2]);
+Object.defineProperty(a, 'length', {value: 1});
+var x = 0;
+assertEquals(a.find(function(elt) { x += elt; return false; }), undefined);
+assertEquals(x, 3);
+assertEquals(Array.prototype.find.call(a,
+    function(elt) { x += elt; return false; }), undefined);
+assertEquals(x, 4);
+
 }
index 5a0afb4..1634448 100644 (file)
@@ -176,4 +176,14 @@ assertThrows('new constructor([]).findIndex({})', TypeError);
 assertThrows('new constructor([]).findIndex([])', TypeError);
 assertThrows('new constructor([]).findIndex(/\d+/)', TypeError);
 
+// Shadowing length doesn't affect findIndex, unlike Array.prototype.findIndex
+a = new constructor([1, 2]);
+Object.defineProperty(a, 'length', {value: 1});
+var x = 0;
+assertEquals(a.findIndex(function(elt) { x += elt; return false; }), -1);
+assertEquals(x, 3);
+assertEquals(Array.prototype.findIndex.call(a,
+    function(elt) { x += elt; return false; }), -1);
+assertEquals(x, 4);
+
 }
index c81fd96..e6acb56 100644 (file)
@@ -135,6 +135,16 @@ function TestTypedArrayForEach(constructor) {
     constructor.prototype.every.call(a, function (x) { count++; return true; });
     assertEquals(a.length, count);
   }
+
+  // Shadowing length doesn't affect every, unlike Array.prototype.every
+  a = new constructor([1, 2]);
+  Object.defineProperty(a, 'length', {value: 1});
+  var x = 0;
+  assertEquals(a.every(function(elt) { x += elt; return true; }), true);
+  assertEquals(x, 3);
+  assertEquals(Array.prototype.every.call(a,
+      function(elt) { x += elt; return true; }), true);
+  assertEquals(x, 4);
 }
 
 for (i = 0; i < typedArrayConstructors.length; i++) {
index 0d34c78..e1d375d 100644 (file)
@@ -138,6 +138,16 @@ function TestTypedArrayForEach(constructor) {
     constructor.prototype.forEach.call(a, function (x) { count++ });
     assertEquals(a.length, count);
   }
+
+  // Shadowing length doesn't affect forEach, unlike Array.prototype.forEach
+  a = new constructor([1, 2]);
+  Object.defineProperty(a, 'length', {value: 1});
+  var x = 0;
+  assertEquals(a.forEach(function(elt) { x += elt; }), undefined);
+  assertEquals(x, 3);
+  assertEquals(Array.prototype.forEach.call(a,
+      function(elt) { x += elt; }), undefined);
+  assertEquals(x, 4);
 }
 
 for (i = 0; i < typedArrayConstructors.length; i++) {