Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(550)

Unified Diff: src/typedarray.js

Issue 14460008: TypedArray(length) constructor (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Added unit-tests for typed array length 0 Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | test/mjsunit/harmony/typedarrays.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/typedarray.js
diff --git a/src/typedarray.js b/src/typedarray.js
index 7549652e1fa2ef92c68bc8a02127cafef99c9a27..4e50f7f3dffdb46eadecca0b3faf3bd7587b9c1c 100644
--- a/src/typedarray.js
+++ b/src/typedarray.js
@@ -85,42 +85,54 @@ function ArrayBufferSlice(start, end) {
// --------------- 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);
+ function ConstructByArrayBuffer(obj, buffer, byteOffset, length) {
+ 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");
+ }
- if (offset % elementSize !== 0) {
+ var newByteLength;
+ var newLength;
+ if (IS_UNDEFINED(length)) {
+ if (bufferByteLength % 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");
+ "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(obj, arrayId, buffer, offset, newByteLength);
+ }
+
+ function ConstructByLength(obj, length) {
+ var l = IS_UNDEFINED(length) ? 0 : TO_POSITIVE_INTEGER(length);
+ var byteLength = l * elementSize;
+ var buffer = new $ArrayBuffer(byteLength);
+ %TypedArrayInitialize(obj, arrayId, buffer, 0, byteLength);
+ }
- 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;
+ return function (arg1, arg2, arg3) {
+ if (%_IsConstructCall()) {
+ if (IS_ARRAYBUFFER(arg1)) {
+ ConstructByArrayBuffer(this, arg1, arg2, arg3);
} else {
- var newLength = TO_POSITIVE_INTEGER(length);
- newByteLength = newLength * elementSize;
- }
- if (newByteLength > bufferByteLength) {
- throw MakeRangeError("invalid_typed_array_length");
+ ConstructByLength(this, arg1);
}
- %TypedArrayInitialize(this, arrayId, buffer, offset, newByteLength);
} else {
- return new constructor(buffer, byteOffset, length);
+ return new constructor(arg1, arg2, arg3);
}
}
}
@@ -164,9 +176,10 @@ function SetUpArrayBuffer() {
SetUpArrayBuffer();
function SetupTypedArray(arrayId, name, constructor, elementSize) {
- var f = CreateTypedArrayConstructor(name, elementSize,
- arrayId, constructor);
- %SetCode(constructor, f);
+ %CheckIsBootstrapping();
+ var fun = CreateTypedArrayConstructor(name, elementSize,
+ arrayId, constructor);
+ %SetCode(constructor, fun);
%FunctionSetPrototype(constructor, new $Object());
%SetProperty(constructor.prototype,
« no previous file with comments | « no previous file | test/mjsunit/harmony/typedarrays.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698