d8 external array c'tors: allow parameters that can be converted to numbers

BUG=v8:1681
TEST=d8 accepts: var a = new Int32Array("2");

Review URL: http://codereview.chromium.org/7867036

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@9243 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent 2ebe8039
......@@ -295,8 +295,12 @@ Handle<Value> Shell::CreateExternalArray(const Arguments& args,
size_t length = 0;
if (args[0]->IsUint32()) {
length = args[0]->Uint32Value();
} else if (args[0]->IsNumber()) {
double raw_length = args[0]->NumberValue();
} else {
Local<Number> number = args[0]->ToNumber();
if (number.IsEmpty() || !number->IsNumber()) {
return ThrowException(String::New("Array length must be a number."));
}
double raw_length = number->NumberValue();
if (raw_length < 0) {
return ThrowException(String::New("Array length must not be negative."));
}
......@@ -305,8 +309,6 @@ Handle<Value> Shell::CreateExternalArray(const Arguments& args,
String::New("Array length exceeds maximum length."));
}
length = static_cast<size_t>(raw_length);
} else {
return ThrowException(String::New("Array length must be a number."));
}
if (length > static_cast<size_t>(kMaxLength)) {
return ThrowException(String::New("Array length exceeds maximum length."));
......
......@@ -81,6 +81,19 @@ assertEquals(2.5, get(array, 0));
assertEquals(3.5, get(array, 1));
}
// Test non-number parameters.
var array_with_length_from_non_number = new Int32Array("2");
assertEquals(2, array_with_length_from_non_number.length);
array_with_length_from_non_number = new Int32Array(undefined);
assertEquals(0, array_with_length_from_non_number.length);
var foo = { valueOf: function() { return 3; } };
array_with_length_from_non_number = new Int32Array(foo);
assertEquals(3, array_with_length_from_non_number.length);
foo = { toString: function() { return "4"; } };
array_with_length_from_non_number = new Int32Array(foo);
assertEquals(4, array_with_length_from_non_number.length);
// Test loads and stores.
types = [Array, Int8Array, Uint8Array, Int16Array, Uint16Array, Int32Array,
Uint32Array, PixelArray, Float32Array, Float64Array];
......
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