From 11a38ed875261707b760233f40f8d213451312c0 Mon Sep 17 00:00:00 2001 From: "dslomov@chromium.org" Date: Tue, 16 Jul 2013 08:11:30 +0000 Subject: [PATCH] Throw if first argument to TypedArray.set is a number. 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 | 7 +++++++ test/mjsunit/external-array-no-sse2.js | 2 +- test/mjsunit/external-array.js | 2 +- test/mjsunit/harmony/typedarrays.js | 5 +++-- 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/typedarray.js b/src/typedarray.js index ee1fa9d..a7421ae 100644 --- a/src/typedarray.js +++ b/src/typedarray.js @@ -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) { diff --git a/test/mjsunit/external-array-no-sse2.js b/test/mjsunit/external-array-no-sse2.js index 9a008fb..11e61ba 100644 --- a/test/mjsunit/external-array-no-sse2.js +++ b/test/mjsunit/external-array-no-sse2.js @@ -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); diff --git a/test/mjsunit/external-array.js b/test/mjsunit/external-array.js index 81788d4..3fcd544 100644 --- a/test/mjsunit/external-array.js +++ b/test/mjsunit/external-array.js @@ -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); diff --git a/test/mjsunit/harmony/typedarrays.js b/test/mjsunit/harmony/typedarrays.js index 8e8ec57..4465aaf 100644 --- a/test/mjsunit/harmony/typedarrays.js +++ b/test/mjsunit/harmony/typedarrays.js @@ -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(); -- 2.7.4