Commit 9e2c046f authored by dslomov@chromium.org's avatar dslomov@chromium.org

TypedArray(length) constructor

R=rossberg@chromium.org

Review URL: https://codereview.chromium.org/14460008

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@14486 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent e6570d43
...@@ -85,11 +85,7 @@ function ArrayBufferSlice(start, end) { ...@@ -85,11 +85,7 @@ function ArrayBufferSlice(start, end) {
// --------------- Typed Arrays --------------------- // --------------- Typed Arrays ---------------------
function CreateTypedArrayConstructor(name, elementSize, arrayId, constructor) { function CreateTypedArrayConstructor(name, elementSize, arrayId, constructor) {
return function (buffer, byteOffset, length) { function ConstructByArrayBuffer(obj, buffer, byteOffset, length) {
if (%_IsConstructCall()) {
if (!IS_ARRAYBUFFER(buffer)) {
throw MakeTypeError("Type error!");
}
var offset = IS_UNDEFINED(byteOffset) var offset = IS_UNDEFINED(byteOffset)
? 0 : offset = TO_POSITIVE_INTEGER(byteOffset); ? 0 : offset = TO_POSITIVE_INTEGER(byteOffset);
...@@ -118,9 +114,25 @@ function CreateTypedArrayConstructor(name, elementSize, arrayId, constructor) { ...@@ -118,9 +114,25 @@ function CreateTypedArrayConstructor(name, elementSize, arrayId, constructor) {
if (newByteLength > bufferByteLength) { if (newByteLength > bufferByteLength) {
throw MakeRangeError("invalid_typed_array_length"); throw MakeRangeError("invalid_typed_array_length");
} }
%TypedArrayInitialize(this, arrayId, buffer, offset, newByteLength); %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);
}
return function (arg1, arg2, arg3) {
if (%_IsConstructCall()) {
if (IS_ARRAYBUFFER(arg1)) {
ConstructByArrayBuffer(this, arg1, arg2, arg3);
} else { } else {
return new constructor(buffer, byteOffset, length); ConstructByLength(this, arg1);
}
} else {
return new constructor(arg1, arg2, arg3);
} }
} }
} }
...@@ -164,9 +176,10 @@ function SetUpArrayBuffer() { ...@@ -164,9 +176,10 @@ function SetUpArrayBuffer() {
SetUpArrayBuffer(); SetUpArrayBuffer();
function SetupTypedArray(arrayId, name, constructor, elementSize) { function SetupTypedArray(arrayId, name, constructor, elementSize) {
var f = CreateTypedArrayConstructor(name, elementSize, %CheckIsBootstrapping();
var fun = CreateTypedArrayConstructor(name, elementSize,
arrayId, constructor); arrayId, constructor);
%SetCode(constructor, f); %SetCode(constructor, fun);
%FunctionSetPrototype(constructor, new $Object()); %FunctionSetPrototype(constructor, new $Object());
%SetProperty(constructor.prototype, %SetProperty(constructor.prototype,
......
...@@ -113,6 +113,27 @@ TestArrayBufferSlice(); ...@@ -113,6 +113,27 @@ TestArrayBufferSlice();
function TestTypedArray(proto, elementSize, typicalElement) { function TestTypedArray(proto, elementSize, typicalElement) {
var ab = new ArrayBuffer(256*elementSize); var ab = new ArrayBuffer(256*elementSize);
var a0 = new proto(30);
assertSame(elementSize, a0.BYTES_PER_ELEMENT);
assertSame(30, a0.length);
assertSame(30*elementSize, a0.byteLength);
assertSame(0, a0.byteOffset);
assertSame(30*elementSize, a0.buffer.byteLength);
var aLen0 = new proto(0);
assertSame(elementSize, aLen0.BYTES_PER_ELEMENT);
assertSame(0, aLen0.length);
assertSame(0, aLen0.byteLength);
assertSame(0, aLen0.byteOffset);
assertSame(0, aLen0.buffer.byteLength);
var aOverBufferLen0 = new proto(ab, 128*elementSize, 0);
assertSame(ab, aOverBufferLen0.buffer);
assertSame(elementSize, aOverBufferLen0.BYTES_PER_ELEMENT);
assertSame(0, aOverBufferLen0.length);
assertSame(0, aOverBufferLen0.byteLength);
assertSame(128*elementSize, aOverBufferLen0.byteOffset);
var a1 = new proto(ab, 128*elementSize, 128); var a1 = new proto(ab, 128*elementSize, 128);
assertSame(ab, a1.buffer); assertSame(ab, a1.buffer);
assertSame(elementSize, a1.BYTES_PER_ELEMENT); assertSame(elementSize, a1.BYTES_PER_ELEMENT);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment