 Chromium Code Reviews
 Chromium Code Reviews Issue 13975012:
  First cut at impementing ES6 TypedArrays in V8.  (Closed) 
  Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
    
  
    Issue 13975012:
  First cut at impementing ES6 TypedArrays in V8.  (Closed) 
  Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge| Index: src/typedarray.js | 
| diff --git a/src/typedarray.js b/src/typedarray.js | 
| index 2a71543104f85396d2b3f91bc3cc810e9e4b050f..4ff6375816063fdf5e8c4bb6a2548ced24e8c44b 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, constr) { | 
| 
rossberg
2013/04/16 13:30:43
Nit: spell out constructor, it still fits the line
 
Dmitry Lomov (no reviews)
2013/04/16 13:47:13
Done.
 | 
| + 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 constr(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,35 @@ 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); | 
| +} | 
| + | 
| +function SetupAllTypedArrays() { | 
| 
rossberg
2013/04/16 13:30:43
Nit: I'd remove this wrapper.
 
Dmitry Lomov (no reviews)
2013/04/16 13:47:13
Done. No pereference either way.
On 2013/04/16 13:
 | 
| + // 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); | 
| +} | 
| + | 
| +SetupAllTypedArrays(); | 
| + |