Browse files

Remove TypedArrays set(index, val) and match WebKit exception strings.

It seems that like get(), set(index, val) was added as a misreading of the spec.
There are only two set() methods defined in the spec:
    void set(TypedArray array, optional unsigned long offset)
    void set(type[] array, optional unsigned long offset)
The set(index, val) is handled by the []= operator.
Additionally updated a few exception error strings to match WebKit.
  • Loading branch information...
1 parent 18ea925 commit 254907baa7cc3efa922261f8a5e24d075b3f3149 @deanm committed Dec 19, 2012
Showing with 57 additions and 61 deletions.
  1. +54 −58 src/v8_typed_array.cc
  2. +3 −3 test/simple/test-typed-arrays.js
View
112 src/v8_typed_array.cc
@@ -351,65 +351,61 @@ class TypedArray {
static v8::Handle<v8::Value> set(const v8::Arguments& args) {
if (args.Length() < 1)
- return ThrowError("Wrong number of arguments.");
+ return ThrowTypeError("Not enough arguments");
- //if (!args[0]->IsObject())
- // return ThrowTypeError("Type error.");
-
- if (args[0]->IsNumber()) { // index, <type> value
- args.This()->Set(args[0]->Uint32Value(), args[1]);
- } else if (args[0]->IsObject()) {
- v8::Handle<v8::Object> obj = v8::Handle<v8::Object>::Cast(args[0]);
-
- if (TypedArray<TBytes, TEAType>::HasInstance(obj)) { // ArrayBufferView.
- if (args[1]->Int32Value() < 0)
- return ThrowRangeError("Offset may not be negative.");
-
- unsigned int offset = args[1]->Uint32Value();
- unsigned int src_length =
- obj->Get(v8::String::New("length"))->Uint32Value();
- unsigned int dst_length =
- args.This()->Get(v8::String::New("length"))->Uint32Value();
- if (offset > dst_length)
- return ThrowRangeError("Offset out of range.");
-
- if (src_length > dst_length - offset)
- return ThrowRangeError("Offset/length out of range.");
-
- // We don't want to get the buffer pointer, because that means we'll have
- // to just do the calculations for byteOffset / byteLength again.
- // Instead just use the pointer on the external array data.
- void* src_ptr = obj->GetIndexedPropertiesExternalArrayData();
- void* dst_ptr = args.This()->GetIndexedPropertiesExternalArrayData();
-
- // From the spec:
- // If the input array is a TypedArray, the two arrays may use the same
- // underlying ArrayBuffer. In this situation, setting the values takes
- // place as if all the data is first copied into a temporary buffer that
- // does not overlap either of the arrays, and then the data from the
- // temporary buffer is copied into the current array.
- memmove(reinterpret_cast<char*>(dst_ptr) + offset * TBytes, src_ptr,
- src_length * TBytes);
- } else { // type[]
- if (args[1]->Int32Value() < 0)
- return ThrowRangeError("Offset may not be negative.");
-
- unsigned int src_length =
- obj->Get(v8::String::New("length"))->Uint32Value();
- unsigned int dst_length =
- args.This()->Get(v8::String::New("length"))->Uint32Value();
- unsigned int offset = args[1]->Uint32Value();
-
- if (offset > dst_length)
- return ThrowRangeError("Offset out of range.");
-
- if (src_length > dst_length - offset)
- return ThrowRangeError("Offset/length out of range.");
-
- for (uint32_t i = 0; i < src_length; ++i) {
- // Use the v8 setter to deal with typing. Maybe slow?
- args.This()->Set(i + offset, obj->Get(i));
- }
+ if (!args[0]->IsObject())
+ return ThrowTypeError("Invalid argument");
+
+ v8::Handle<v8::Object> obj = v8::Handle<v8::Object>::Cast(args[0]);
+
+ if (TypedArray<TBytes, TEAType>::HasInstance(obj)) { // ArrayBufferView.
+ if (args[1]->Int32Value() < 0)
+ return ThrowRangeError("Offset may not be negative.");
+
+ unsigned int offset = args[1]->Uint32Value();
+ unsigned int src_length =
+ obj->Get(v8::String::New("length"))->Uint32Value();
+ unsigned int dst_length =
+ args.This()->Get(v8::String::New("length"))->Uint32Value();
+ if (offset > dst_length)
+ return ThrowRangeError("Offset out of range.");
+
+ if (src_length > dst_length - offset)
+ return ThrowRangeError("Index is out of range.");
+
+ // We don't want to get the buffer pointer, because that means we'll have
+ // to just do the calculations for byteOffset / byteLength again.
+ // Instead just use the pointer on the external array data.
+ void* src_ptr = obj->GetIndexedPropertiesExternalArrayData();
+ void* dst_ptr = args.This()->GetIndexedPropertiesExternalArrayData();
+
+ // From the spec:
+ // If the input array is a TypedArray, the two arrays may use the same
+ // underlying ArrayBuffer. In this situation, setting the values takes
+ // place as if all the data is first copied into a temporary buffer that
+ // does not overlap either of the arrays, and then the data from the
+ // temporary buffer is copied into the current array.
+ memmove(reinterpret_cast<char*>(dst_ptr) + offset * TBytes, src_ptr,
+ src_length * TBytes);
+ } else { // type[]
+ if (args[1]->Int32Value() < 0)
+ return ThrowRangeError("Offset may not be negative.");
+
+ unsigned int src_length =
+ obj->Get(v8::String::New("length"))->Uint32Value();
+ unsigned int dst_length =
+ args.This()->Get(v8::String::New("length"))->Uint32Value();
+ unsigned int offset = args[1]->Uint32Value();
+
+ if (offset > dst_length)
+ return ThrowRangeError("Offset out of range.");
+
+ if (src_length > dst_length - offset)
+ return ThrowRangeError("Index is out of range.");
+
+ for (uint32_t i = 0; i < src_length; ++i) {
+ // Use the v8 setter to deal with typing. Maybe slow?
+ args.This()->Set(i + offset, obj->Get(i));
}
}
View
6 test/simple/test-typed-arrays.js
@@ -154,7 +154,7 @@ assert.equal(uint8[2], 0x12);
assert.equal(uint8[3], 0x34);
// test .set(index, value), .set(arr, offset)
-uint8.set(1, 0x09);
+uint8[1] = 0x09;
uint8.set([0x0a, 0x0b], 2);
assert.equal(uint8[1], 0x09);
@@ -169,8 +169,8 @@ uint8c[1] = 257;
assert.equal(uint8c[0], 0);
assert.equal(uint8c[1], 255);
-uint8c.set(0, -10);
-uint8c.set(1, 260);
+uint8c[0] = -10;
+uint8c[1] = 260;
assert.equal(uint8c[0], 0);
assert.equal(uint8c[1], 255);

0 comments on commit 254907b

Please sign in to comment.