Chromium Code Reviews| Index: src/typedarray.js |
| diff --git a/src/typedarray.js b/src/typedarray.js |
| index 1e67bc30c6cd7d21023620acec95a9aa69bb0e42..121c1865dd51ae39c9c1e297cb536a444c7a79d8 100644 |
| --- a/src/typedarray.js |
| +++ b/src/typedarray.js |
| @@ -35,58 +35,58 @@ var $ArrayBuffer = global.ArrayBuffer; |
| // --------------- Typed Arrays --------------------- |
| -function CreateTypedArrayConstructor(name, elementSize, arrayId, constructor) { |
| - function ConstructByArrayBuffer(obj, buffer, byteOffset, length) { |
| - var offset = ToPositiveInteger(byteOffset, "invalid_typed_array_length") |
| +macro TYPED_ARRAY_CONSTRUCTOR(ARRAY_ID, NAME, ELEMENT_SIZE) |
| + function NAMEConstructor(arg1, arg2, arg3) { |
| + function ConstructByArrayBuffer(obj, buffer, byteOffset, length) { |
| + var offset = ToPositiveInteger(byteOffset, "invalid_typed_array_length") |
| - if (offset % elementSize !== 0) { |
| - throw MakeRangeError("invalid_typed_array_alignment", |
| - "start offset", name, elementSize); |
| - } |
| - var bufferByteLength = %ArrayBufferGetByteLength(buffer); |
| - if (offset > bufferByteLength) { |
| - throw MakeRangeError("invalid_typed_array_offset"); |
| - } |
| - |
| - var newByteLength; |
| - var newLength; |
| - if (IS_UNDEFINED(length)) { |
| - if (bufferByteLength % elementSize !== 0) { |
| + if (offset % ELEMENT_SIZE !== 0) { |
| throw MakeRangeError("invalid_typed_array_alignment", |
| - "byte length", name, elementSize); |
| + "start offset", "NAME", ELEMENT_SIZE); |
| } |
| - newByteLength = bufferByteLength - offset; |
| - newLength = newByteLength / elementSize; |
| - } else { |
| - var newLength = ToPositiveInteger(length, "invalid_typed_array_length"); |
| - newByteLength = newLength * elementSize; |
| - } |
| - if (offset + newByteLength > bufferByteLength) { |
| - throw MakeRangeError("invalid_typed_array_length"); |
| + var bufferByteLength = %ArrayBufferGetByteLength(buffer); |
| + if (offset > bufferByteLength) { |
| + throw MakeRangeError("invalid_typed_array_offset"); |
| + } |
| + |
| + var newByteLength; |
| + var newLength; |
| + if (IS_UNDEFINED(length)) { |
| + if (bufferByteLength % ELEMENT_SIZE !== 0) { |
| + throw MakeRangeError("invalid_typed_array_alignment", |
| + "byte length", "NAME", ELEMENT_SIZE); |
| + } |
| + newByteLength = bufferByteLength - offset; |
| + newLength = newByteLength / ELEMENT_SIZE; |
| + } else { |
| + var newLength = ToPositiveInteger(length, "invalid_typed_array_length"); |
| + newByteLength = newLength * ELEMENT_SIZE; |
| + } |
| + if (offset + newByteLength > bufferByteLength) { |
| + throw MakeRangeError("invalid_typed_array_length"); |
| + } |
| + %TypedArrayInitialize(obj, ARRAY_ID, buffer, offset, newByteLength); |
| } |
| - %TypedArrayInitialize(obj, arrayId, buffer, offset, newByteLength); |
| - } |
| - function ConstructByLength(obj, length) { |
| - var l = ToPositiveInteger(length, "invalid_typed_array_length"); |
| - var byteLength = l * elementSize; |
| - var buffer = new $ArrayBuffer(byteLength); |
| - %TypedArrayInitialize(obj, arrayId, buffer, 0, byteLength); |
| - } |
| + function ConstructByLength(obj, length) { |
| + var l = ToPositiveInteger(length, "invalid_typed_array_length"); |
| + var byteLength = l * ELEMENT_SIZE; |
| + var buffer = new $ArrayBuffer(byteLength); |
| + %TypedArrayInitialize(obj, ARRAY_ID, buffer, 0, byteLength); |
| + } |
| - function ConstructByArrayLike(obj, arrayLike) { |
| - var length = arrayLike.length; |
| - var l = ToPositiveInteger(length, "invalid_typed_array_length"); |
| - if(!%TypedArrayInitializeFromArrayLike(obj, arrayId, arrayLike, l)) { |
| - for (var i = 0; i < l; i++) { |
| - // It is crucial that we let any execptions from arrayLike[i] |
| - // propagate outside the function. |
| - obj[i] = arrayLike[i]; |
| + function ConstructByArrayLike(obj, arrayLike) { |
| + var length = arrayLike.length; |
| + var l = ToPositiveInteger(length, "invalid_typed_array_length"); |
| + if(!%TypedArrayInitializeFromArrayLike(obj, ARRAY_ID, arrayLike, l)) { |
| + for (var i = 0; i < l; i++) { |
| + // It is crucial that we let any execptions from arrayLike[i] |
| + // propagate outside the function. |
| + obj[i] = arrayLike[i]; |
| + } |
| } |
| } |
| - } |
| - return function (arg1, arg2, arg3) { |
| if (%_IsConstructCall()) { |
| if (IS_ARRAYBUFFER(arg1)) { |
| ConstructByArrayBuffer(this, arg1, arg2, arg3); |
| @@ -97,10 +97,21 @@ function CreateTypedArrayConstructor(name, elementSize, arrayId, constructor) { |
| ConstructByArrayLike(this, arg1); |
| } |
| } else { |
| - throw MakeTypeError("constructor_not_function", [name]) |
| + throw MakeTypeError("constructor_not_function", ["NAME"]) |
| } |
| } |
| -} |
| +endmacro |
| + |
| +// arrayIds below should be synchronized with Runtime_TypedArrayInitialize. |
| +TYPED_ARRAY_CONSTRUCTOR(1, Uint8Array, 1) |
| +TYPED_ARRAY_CONSTRUCTOR(2, Int8Array, 1) |
| +TYPED_ARRAY_CONSTRUCTOR(3, Uint16Array, 2) |
| +TYPED_ARRAY_CONSTRUCTOR(4, Int16Array, 2) |
| +TYPED_ARRAY_CONSTRUCTOR(5, Uint32Array, 4) |
| +TYPED_ARRAY_CONSTRUCTOR(6, Int32Array, 4) |
| +TYPED_ARRAY_CONSTRUCTOR(7, Float32Array, 4) |
| +TYPED_ARRAY_CONSTRUCTOR(8, Float64Array, 8) |
| +TYPED_ARRAY_CONSTRUCTOR(9, Uint8ClampedArray, 1) |
| function TypedArrayGetBuffer() { |
| return %TypedArrayGetBuffer(this); |
| @@ -247,10 +258,8 @@ function TypedArraySet(obj, offset) { |
| // ------------------------------------------------------------------- |
| -function SetupTypedArray(arrayId, name, constructor, elementSize) { |
| +function SetupTypedArray(constructor, fun, elementSize) { |
| %CheckIsBootstrapping(); |
| - var fun = CreateTypedArrayConstructor(name, elementSize, |
| - arrayId, constructor); |
| %SetCode(constructor, fun); |
| %FunctionSetPrototype(constructor, new $Object()); |
| @@ -272,16 +281,20 @@ function SetupTypedArray(arrayId, name, constructor, elementSize) { |
| )); |
| } |
| -// arrayIds below should be synchronized with Runtime_TypedArrayInitialize. |
| -SetupTypedArray(1, "Uint8Array", global.Uint8Array, 1); |
| -SetupTypedArray(2, "Int8Array", global.Int8Array, 1); |
| -SetupTypedArray(3, "Uint16Array", global.Uint16Array, 2); |
| -SetupTypedArray(4, "Int16Array", global.Int16Array, 2); |
| -SetupTypedArray(5, "Uint32Array", global.Uint32Array, 4); |
| -SetupTypedArray(6, "Int32Array", global.Int32Array, 4); |
| -SetupTypedArray(7, "Float32Array", global.Float32Array, 4); |
| -SetupTypedArray(8, "Float64Array", global.Float64Array, 8); |
| -SetupTypedArray(9, "Uint8ClampedArray", global.Uint8ClampedArray, 1); |
| + |
| +macro SETUP_TYPED_ARRAY(NAME, ELEMENT_SIZE) |
|
rossberg
2013/10/28 10:40:55
I'd argue that using a macro here is overkill, but
|
| + SetupTypedArray(global.NAME, NAMEConstructor, ELEMENT_SIZE); |
| +endmacro |
| + |
| +SETUP_TYPED_ARRAY(Uint8Array, 1) |
| +SETUP_TYPED_ARRAY(Int8Array, 1) |
| +SETUP_TYPED_ARRAY(Uint16Array, 2) |
| +SETUP_TYPED_ARRAY(Int16Array, 2) |
| +SETUP_TYPED_ARRAY(Uint32Array, 4) |
| +SETUP_TYPED_ARRAY(Int32Array, 4) |
| +SETUP_TYPED_ARRAY(Float32Array, 4) |
| +SETUP_TYPED_ARRAY(Float64Array, 8) |
| +SETUP_TYPED_ARRAY(Uint8ClampedArray, 1) |
| // --------------------------- DataView ----------------------------- |