Index: src/typedarray.js |
diff --git a/src/typedarray.js b/src/typedarray.js |
index 2a71543104f85396d2b3f91bc3cc810e9e4b050f..8c634ddfbe68bf91e4525699895bc7eafa79e88e 100644 |
--- a/src/typedarray.js |
+++ b/src/typedarray.js |
@@ -82,14 +82,120 @@ function ArrayBufferSlice(start, end) { |
return result; |
} |
+// --------------- Typed Arrays --------------------- |
+ |
+function CreateTypedArrayConstructor(name, elementSize, arrayId, proto) { |
rossberg
2013/04/15 13:11:03
The 'proto' argument is misnamed: it's not the pro
Dmitry Lomov (no reviews)
2013/04/15 15:40:58
Done.
|
+ return function (buffer, byteOffset, length) { |
+ if (%_IsConstructCall()) { |
+ if (!IS_ARRAYBUFFER(buffer)) { |
+ throw MakeTypeError("Type error!"); |
+ } |
+ var offset; |
+ if (IS_UNDEFINED(byteOffset)) { |
rossberg
2013/04/15 13:11:03
You can use ?: here
Dmitry Lomov (no reviews)
2013/04/15 15:40:58
Done.
|
+ offset = 0; |
+ } else { |
+ offset = TO_POSITIVE_INTEGER(byteOffset); |
+ } |
+ 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) { |
+ throw MakeRangeError("invalid_typed_array_alignment", |
+ "byte length", name, elementSize); |
+ } |
+ newByteLength = bufferByteLength - offset; |
+ newLength = newByteLength / elementSize; |
+ } else { |
+ var newLength = TO_POSITIVE_INTEGER(length); |
+ newByteLength = newLength * elementSize; |
+ } |
+ if (newByteLength > bufferByteLength) { |
+ throw MakeRangeError("invalid_typed_array_length"); |
+ } |
+ %TypedArrayInitialize(this, arrayId, buffer, offset, newLength, newByteLength); |
rossberg
2013/04/15 13:11:03
Line too long
Dmitry Lomov (no reviews)
2013/04/15 15:40:58
Done.
|
+ } else { |
+ return new proto(buffer, byteOffset, length); |
+ } |
+ } |
+} |
+ |
+function TypedArrayGetBuffer() { |
+ return %TypedArrayGetBuffer(this); |
+} |
+ |
+function TypedArrayGetByteLength() { |
+ return %TypedArrayGetByteLength(this); |
+} |
+ |
+function TypedArrayGetByteOffset() { |
+ return %TypedArrayGetByteOffset(this); |
+} |
+ |
+function TypedArrayGetLength() { |
+ return %TypedArrayGetLength(this); |
+} |
+ |
+ |
+var typedArrayDescs = |
+ // arrayIds below should be synchronized with Runtime_TypedArrayInitialize. |
+ [{name:"Uint8Array", |
rossberg
2013/04/15 13:11:03
Style: put in a space after ':'
|
+ protoObject:global.__Uint8Array, |
rossberg
2013/04/15 13:11:03
s/protoObject/constructor/g
|
+ arrayId:1, |
+ elementSize:1}, |
+ |
+ {name:"Int8Array", |
+ protoObject:global.__Int8Array, |
+ arrayId:2, |
+ elementSize:1}, |
+ |
+ {name:"Uint16Array", |
+ protoObject:global.__Uint16Array, |
+ arrayId:3, |
+ elementSize:2}, |
+ |
+ {name:"Int16Array", |
+ protoObject:global.__Int16Array, |
+ arrayId:4, |
+ elementSize:2}, |
+ |
+ {name:"Uint32Array", |
+ protoObject:global.__Uint32Array, |
+ arrayId:5, |
+ elementSize:4}, |
+ |
+ {name:"Int32Array", |
+ protoObject:global.__Int32Array, |
+ arrayId:6, |
+ elementSize:4}, |
+ |
+ {name:"Float32Array", |
+ protoObject:global.__Float32Array, |
+ arrayId:7, |
+ elementSize:4}, |
+ |
+ {name:"Float64Array", |
+ protoObject:global.__Float64Array, |
+ arrayId:8, |
+ elementSize:8} |
+ ]; |
// ------------------------------------------------------------------- |
function SetUpArrayBuffer() { |
%CheckIsBootstrapping(); |
- // Set up the Uint16Array constructor function. |
+ // Set up the ArrayBuffer constructor function. |
%SetCode($ArrayBuffer, ArrayBufferConstructor); |
+ %FunctionSetPrototype($ArrayBuffer, new $Object()); |
// Set up the constructor property on the ArrayBuffer prototype object. |
%SetProperty($ArrayBuffer.prototype, "constructor", $ArrayBuffer, DONT_ENUM); |
@@ -99,6 +205,23 @@ function SetUpArrayBuffer() { |
InstallFunctions($ArrayBuffer.prototype, DONT_ENUM, $Array( |
"slice", ArrayBufferSlice |
)); |
+ for (var i = 0; i < typedArrayDescs.length; i++) { |
rossberg
2013/04/15 13:11:03
Hm, I'd prefer separating initialization of thyped
Dmitry Lomov (no reviews)
2013/04/15 15:40:58
Done.
|
+ var desc = typedArrayDescs[i]; |
+ var f = CreateTypedArrayConstructor(desc.name, desc.elementSize, |
+ desc.arrayId, desc.protoObject); |
+ %SetCode(desc.protoObject, f); |
+ %FunctionSetPrototype(desc.protoObject, new $Object()); |
+ |
+ %SetProperty(desc.protoObject.prototype, |
+ "constructor", desc.protoObject, DONT_ENUM); |
+ %SetProperty(desc.protoObject.prototype, |
+ "BYTES_PER_ELEMENT", desc.elementSize, |
+ READ_ONLY | DONT_ENUM | DONT_DELETE); |
+ InstallGetter(desc.protoObject.prototype, "buffer", TypedArrayGetBuffer); |
+ InstallGetter(desc.protoObject.prototype, "byteOffset", TypedArrayGetByteOffset); |
rossberg
2013/04/15 13:11:03
Line too long (and below)
Dmitry Lomov (no reviews)
2013/04/15 15:40:58
Done.
|
+ InstallGetter(desc.protoObject.prototype, "byteLength", TypedArrayGetByteLength); |
+ InstallGetter(desc.protoObject.prototype, "length", TypedArrayGetLength); |
+ } |
} |
SetUpArrayBuffer(); |