OLD | NEW |
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
216 if (!ExecuteString(source, String::New(*file), false, false)) { | 216 if (!ExecuteString(source, String::New(*file), false, false)) { |
217 return ThrowException(String::New("Error executing file")); | 217 return ThrowException(String::New("Error executing file")); |
218 } | 218 } |
219 } | 219 } |
220 return Undefined(); | 220 return Undefined(); |
221 } | 221 } |
222 | 222 |
223 | 223 |
224 Handle<Value> Shell::CreateExternalArray(const Arguments& args, | 224 Handle<Value> Shell::CreateExternalArray(const Arguments& args, |
225 ExternalArrayType type, | 225 ExternalArrayType type, |
226 int element_size) { | 226 size_t element_size) { |
| 227 ASSERT(element_size == 1 || element_size == 2 || element_size == 4 || |
| 228 element_size == 8); |
227 if (args.Length() != 1) { | 229 if (args.Length() != 1) { |
228 return ThrowException( | 230 return ThrowException( |
229 String::New("Array constructor needs one parameter.")); | 231 String::New("Array constructor needs one parameter.")); |
230 } | 232 } |
231 int length = args[0]->Int32Value(); | 233 size_t length = 0; |
232 void* data = malloc(length * element_size); | 234 if (args[0]->IsUint32()) { |
233 memset(data, 0, length * element_size); | 235 length = args[0]->Uint32Value(); |
| 236 } else if (args[0]->IsNumber()) { |
| 237 double raw_length = args[0]->NumberValue(); |
| 238 if (raw_length < 0) { |
| 239 return ThrowException(String::New("Array length must not be negative.")); |
| 240 } |
| 241 if (raw_length > v8::internal::ExternalArray::kMaxLength) { |
| 242 return ThrowException( |
| 243 String::New("Array length exceeds maximum length.")); |
| 244 } |
| 245 length = static_cast<size_t>(raw_length); |
| 246 } else { |
| 247 return ThrowException(String::New("Array length must be a number.")); |
| 248 } |
| 249 if (length > static_cast<size_t>(internal::ExternalArray::kMaxLength)) { |
| 250 return ThrowException(String::New("Array length exceeds maximum length.")); |
| 251 } |
| 252 void* data = calloc(length, element_size); |
| 253 if (data == NULL) { |
| 254 return ThrowException(String::New("Memory allocation failed.")); |
| 255 } |
234 Handle<Object> array = Object::New(); | 256 Handle<Object> array = Object::New(); |
235 Persistent<Object> persistent_array = Persistent<Object>::New(array); | 257 Persistent<Object> persistent_array = Persistent<Object>::New(array); |
236 persistent_array.MakeWeak(data, ExternalArrayWeakCallback); | 258 persistent_array.MakeWeak(data, ExternalArrayWeakCallback); |
237 persistent_array.MarkIndependent(); | 259 persistent_array.MarkIndependent(); |
238 array->SetIndexedPropertiesToExternalArrayData(data, type, length); | 260 array->SetIndexedPropertiesToExternalArrayData(data, type, length); |
239 array->Set(String::New("length"), Int32::New(length), ReadOnly); | 261 array->Set(String::New("length"), Int32::New(length), ReadOnly); |
240 array->Set(String::New("BYTES_PER_ELEMENT"), Int32::New(element_size)); | 262 array->Set(String::New("BYTES_PER_ELEMENT"), Int32::New(element_size)); |
241 return array; | 263 return array; |
242 } | 264 } |
243 | 265 |
(...skipping 748 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
992 } | 1014 } |
993 | 1015 |
994 } // namespace v8 | 1016 } // namespace v8 |
995 | 1017 |
996 | 1018 |
997 #ifndef GOOGLE3 | 1019 #ifndef GOOGLE3 |
998 int main(int argc, char* argv[]) { | 1020 int main(int argc, char* argv[]) { |
999 return v8::Shell::Main(argc, argv); | 1021 return v8::Shell::Main(argc, argv); |
1000 } | 1022 } |
1001 #endif | 1023 #endif |
OLD | NEW |