Index: src/typedarray.js |
diff --git a/src/typedarray.js b/src/typedarray.js |
index 2a71543104f85396d2b3f91bc3cc810e9e4b050f..24fcf1e45536e01067e6c617e4d7087fc048823c 100644 |
--- a/src/typedarray.js |
+++ b/src/typedarray.js |
@@ -82,14 +82,74 @@ function ArrayBufferSlice(start, end) { |
return result; |
} |
+// --------------- Typed Arrays --------------------- |
+ |
+function CreateTypedArrayConstructor(name, elementSize, arrayId, constructor) { |
+ return function (buffer, byteOffset, length) { |
+ if (%_IsConstructCall()) { |
+ if (!IS_ARRAYBUFFER(buffer)) { |
+ throw MakeTypeError("Type error!"); |
+ } |
+ var offset = IS_UNDEFINED(byteOffset) |
+ ? 0 : 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, newByteLength); |
+ } else { |
+ return new constructor(buffer, byteOffset, length); |
+ } |
+ } |
+} |
+ |
+function TypedArrayGetBuffer() { |
+ return %TypedArrayGetBuffer(this); |
+} |
+ |
+function TypedArrayGetByteLength() { |
+ return %TypedArrayGetByteLength(this); |
+} |
+ |
+function TypedArrayGetByteOffset() { |
+ return %TypedArrayGetByteOffset(this); |
+} |
+ |
+function TypedArrayGetLength() { |
+ return %TypedArrayGetLength(this); |
+} |
+ |
// ------------------------------------------------------------------- |
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); |
@@ -102,3 +162,31 @@ function SetUpArrayBuffer() { |
} |
SetUpArrayBuffer(); |
+ |
+function SetupTypedArray(arrayId, name, constructor, elementSize) { |
+ var f = CreateTypedArrayConstructor(name, elementSize, |
+ arrayId, constructor); |
+ %SetCode(constructor, f); |
+ %FunctionSetPrototype(constructor, new $Object()); |
+ |
+ %SetProperty(constructor.prototype, |
+ "constructor", constructor, DONT_ENUM); |
+ %SetProperty(constructor.prototype, |
+ "BYTES_PER_ELEMENT", elementSize, |
+ READ_ONLY | DONT_ENUM | DONT_DELETE); |
+ InstallGetter(constructor.prototype, "buffer", TypedArrayGetBuffer); |
+ InstallGetter(constructor.prototype, "byteOffset", TypedArrayGetByteOffset); |
+ InstallGetter(constructor.prototype, "byteLength", TypedArrayGetByteLength); |
+ InstallGetter(constructor.prototype, "length", TypedArrayGetLength); |
+} |
+ |
+// 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); |
+ |