Throw if first argument to TypedArray.set is a number.
authordslomov@chromium.org <dslomov@chromium.org@ce2b1a6d-e550-0410-aec6-3dcde31c8c00>
Tue, 16 Jul 2013 08:11:30 +0000 (08:11 +0000)
committerdslomov@chromium.org <dslomov@chromium.org@ce2b1a6d-e550-0410-aec6-3dcde31c8c00>
Tue, 16 Jul 2013 08:11:30 +0000 (08:11 +0000)
Further refinement to semantics that I have missed in previous change.
Both Blink and Firefox are permissive with arguments to .set method.
However, when first argument to "set" is a number, all implementations
throw, so that users know that
   a.set(0,27)
does not assign 27 to 0th element of a, not 0 to 27th element of a.

R=bmeurer@chromium.org

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

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

src/typedarray.js
test/mjsunit/external-array-no-sse2.js
test/mjsunit/external-array.js
test/mjsunit/harmony/typedarrays.js

index ee1fa9d..a7421ae 100644 (file)
@@ -154,6 +154,13 @@ function TypedArraySet(obj, offset) {
 
   var l = obj.length;
   if (IS_UNDEFINED(l)) {
+    if (IS_NUMBER(obj)) {
+        // For number as a first argument, throw TypeError
+        // instead of silently ignoring the call, so that
+        // the user knows (s)he did something wrong.
+        // (Consistent with Firefox and Blink/WebKit)
+        throw MakeTypeError("invalid_argument");
+    }
     return;
   }
   if (intOffset + l > this.length) {
index 9a008fb..11e61ba 100644 (file)
@@ -606,7 +606,7 @@ a61.set(a62)
 assertArrayPrefix([1, 12], a61)
 
 // Invalid source
-a.set(0); // does not throw
+assertThrows(function() { a.set(0); }, TypeError);
 assertArrayPrefix([1,2,3,4,5,6], a);
 a.set({}); // does not throw
 assertArrayPrefix([1,2,3,4,5,6], a);
index 81788d4..3fcd544 100644 (file)
@@ -605,7 +605,7 @@ a61.set(a62)
 assertArrayPrefix([1, 12], a61)
 
 // Invalid source
-a.set(0); // does not throw
+assertThrows(function() { a.set(0); }, TypeError);
 assertArrayPrefix([1,2,3,4,5,6], a);
 a.set({}); // does not throw
 assertArrayPrefix([1,2,3,4,5,6], a);
index 8e8ec57..4465aaf 100644 (file)
@@ -458,12 +458,13 @@ function TestTypedArraySet() {
     a[i] = i;
     expected.push(i);
   }
-  a.set(0);
-  assertArrayPrefix(expected, a);
   a.set({});
   assertArrayPrefix(expected, a);
   assertThrows(function() { a.set.call({}) }, TypeError);
   assertThrows(function() { a.set.call([]) }, TypeError);
+
+  assertThrows(function() { a.set(0); }, TypeError);
+  assertThrows(function() { a.set(0, 1); }, TypeError);
 }
 
 TestTypedArraySet();