Commit 2751eeb3 authored by dslomov@chromium.org's avatar dslomov@chromium.org

More typed array constructors.

R=rossberg@chromium.org

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

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@14518 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent 6e861419
......@@ -101,6 +101,8 @@ var kMessages = {
observe_type_non_string: ["Invalid changeRecord with non-string 'type' property"],
observe_notify_non_notifier: ["notify called on non-notifier object"],
proto_poison_pill: ["Generic use of __proto__ accessor not allowed"],
parameterless_typed_array_constr:
["%0"," constructor should have at least one argument."],
// RangeError
invalid_array_length: ["Invalid array length"],
invalid_array_buffer_length: ["Invalid array buffer length"],
......
......@@ -123,12 +123,27 @@ function CreateTypedArrayConstructor(name, elementSize, arrayId, constructor) {
%TypedArrayInitialize(obj, arrayId, buffer, 0, byteLength);
}
function ConstructByArrayLike(obj, arrayLike) {
var length = arrayLike.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);
for (var i = 0; i < l; i++) {
obj[i] = arrayLike[i];
}
}
return function (arg1, arg2, arg3) {
if (%_IsConstructCall()) {
if (IS_ARRAYBUFFER(arg1)) {
ConstructByArrayBuffer(this, arg1, arg2, arg3);
} else {
} else if (IS_NUMBER(arg1) || IS_STRING(arg1) || IS_BOOLEAN(arg1)) {
ConstructByLength(this, arg1);
} else if (!IS_UNDEFINED(arg1)){
ConstructByArrayLike(this, arg1);
} else {
throw MakeTypeError("parameterless_typed_array_constr", name);
}
} else {
return new constructor(arg1, arg2, arg3);
......
......@@ -203,8 +203,29 @@ function TestTypedArray(proto, elementSize, typicalElement) {
assertThrows(function() { new proto(unalignedArrayBuffer)}, RangeError);
assertThrows(function() { new proto(unalignedArrayBuffer, 5*elementSize)},
RangeError);
assertThrows(function() { new proto() }, TypeError);
}
var aFromString = new proto("30");
assertSame(elementSize, aFromString.BYTES_PER_ELEMENT);
assertSame(30, aFromString.length);
assertSame(30*elementSize, aFromString.byteLength);
assertSame(0, aFromString.byteOffset);
assertSame(30*elementSize, aFromString.buffer.byteLength);
var jsArray = [];
for (i = 0; i < 30; i++) {
jsArray.push(typicalElement);
}
var aFromArray = new proto(jsArray);
assertSame(elementSize, aFromArray.BYTES_PER_ELEMENT);
assertSame(30, aFromArray.length);
assertSame(30*elementSize, aFromArray.byteLength);
assertSame(0, aFromArray.byteOffset);
assertSame(30*elementSize, aFromArray.buffer.byteLength);
for (i = 0; i < 30; i++) {
assertSame(typicalElement, aFromArray[i]);
}
}
TestTypedArray(Uint8Array, 1, 0xFF);
......
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