Index: src/d8.cc |
=================================================================== |
--- src/d8.cc (revision 10785) |
+++ src/d8.cc (working copy) |
@@ -330,16 +330,14 @@ |
} |
ASSERT(element_size == 1 || element_size == 2 || element_size == 4 || |
element_size == 8); |
- if (args.Length() == 0) { |
- return ThrowException( |
- String::New("Array constructor must have at least one " |
- "parameter.")); |
- } |
+ |
bool first_arg_is_array_buffer = |
+ args.Length() > 0 && |
args[0]->IsObject() && |
args[0]->ToObject()->Get( |
String::New(kArrayBufferMarkerPropName))->IsTrue(); |
// Currently, only the following constructors are supported: |
+ // TypedArray() |
// TypedArray(unsigned long length) |
// TypedArray(ArrayBuffer buffer, |
// optional unsigned long byteOffset, |
@@ -347,16 +345,21 @@ |
if (args.Length() > 3) { |
return ThrowException( |
String::New("Array constructor from ArrayBuffer must " |
- "have 1-3 parameters.")); |
+ "have 0-3 parameters.")); |
} |
- Local<Value> length_value = (args.Length() < 3) |
- ? (first_arg_is_array_buffer |
- ? args[0]->ToObject()->Get(String::New("length")) |
- : args[0]) |
- : args[2]; |
- size_t length = convertToUint(length_value, &try_catch); |
- if (try_catch.HasCaught()) return try_catch.Exception(); |
+ size_t length; |
+ if (args.Length() > 0) { |
+ Local<Value> length_value = (args.Length() < 3) |
+ ? (first_arg_is_array_buffer |
+ ? args[0]->ToObject()->Get(String::New("length")) |
+ : args[0]) |
+ : args[2]; |
+ length = convertToUint(length_value, &try_catch); |
+ if (try_catch.HasCaught()) return try_catch.Exception(); |
+ } else { |
+ length = 0; |
+ } |
void* data = NULL; |
size_t offset = 0; |
@@ -428,7 +431,7 @@ |
Persistent<Object> persistent_array = Persistent<Object>::New(array); |
persistent_array.MakeWeak(data, ExternalArrayWeakCallback); |
persistent_array.MarkIndependent(); |
- if (data == NULL && length != 0) { |
+ if (data == NULL) { |
data = calloc(length, element_size); |
if (data == NULL) { |
return ThrowException(String::New("Memory allocation failed.")); |