Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2012 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 |
| 11 // with the distribution. | 11 // with the distribution. |
| (...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 274 if (source.IsEmpty()) { | 274 if (source.IsEmpty()) { |
| 275 return ThrowException(String::New("Error loading file")); | 275 return ThrowException(String::New("Error loading file")); |
| 276 } | 276 } |
| 277 if (!ExecuteString(source, String::New(*file), false, true)) { | 277 if (!ExecuteString(source, String::New(*file), false, true)) { |
| 278 return ThrowException(String::New("Error executing file")); | 278 return ThrowException(String::New("Error executing file")); |
| 279 } | 279 } |
| 280 } | 280 } |
| 281 return Undefined(); | 281 return Undefined(); |
| 282 } | 282 } |
| 283 | 283 |
| 284 static size_t convertToUint(Local<Value> value_in, TryCatch* try_catch) { | |
| 285 if (value_in->IsUint32()) { | |
| 286 return value_in->Uint32Value(); | |
| 287 } | |
| 288 | |
| 289 Local<Value> number = value_in->ToNumber(); | |
| 290 if (try_catch->HasCaught()) return 0; | |
| 291 | |
| 292 ASSERT(number->IsNumber()); | |
| 293 Local<Int32> int32 = number->ToInt32(); | |
| 294 if (try_catch->HasCaught() || int32.IsEmpty()) return 0; | |
| 295 | |
| 296 int32_t raw_value = int32->Int32Value(); | |
| 297 if (try_catch->HasCaught()) return 0; | |
| 298 | |
| 299 if (raw_value < 0) { | |
| 300 ThrowException(String::New("Array length must not be negative.")); | |
| 301 return 0; | |
| 302 } | |
| 303 | |
| 304 static const int kMaxLength = 0x3fffffff; | |
| 305 #ifndef V8_SHARED | |
| 306 ASSERT(kMaxLength == i::ExternalArray::kMaxLength); | |
| 307 #endif // V8_SHARED | |
| 308 if (raw_value > static_cast<int32_t>(kMaxLength)) { | |
| 309 ThrowException( | |
| 310 String::New("Array length exceeds maximum length.")); | |
| 311 } | |
| 312 return static_cast<size_t>(raw_value); | |
| 313 } | |
| 314 | |
| 315 | |
| 316 const char kArrayBufferReferencePropName[] = "_is_array_buffer_"; | |
| 317 const char kArrayBufferMarkerPropName[] = "_array_buffer_ref_"; | |
| 318 | |
| 284 | 319 |
| 285 Handle<Value> Shell::CreateExternalArray(const Arguments& args, | 320 Handle<Value> Shell::CreateExternalArray(const Arguments& args, |
| 286 ExternalArrayType type, | 321 ExternalArrayType type, |
| 287 size_t element_size) { | 322 size_t element_size) { |
| 323 TryCatch try_catch; | |
| 324 bool is_array_buffer_construct = element_size == 0; | |
| 325 if (is_array_buffer_construct) { | |
| 326 type = v8::kExternalByteArray; | |
| 327 element_size = 1; | |
| 328 } | |
| 288 ASSERT(element_size == 1 || element_size == 2 || element_size == 4 || | 329 ASSERT(element_size == 1 || element_size == 2 || element_size == 4 || |
| 289 element_size == 8); | 330 element_size == 8); |
| 290 if (args.Length() != 1) { | 331 if (args.Length() == 0) { |
| 291 return ThrowException( | 332 return ThrowException( |
| 292 String::New("Array constructor needs one parameter.")); | 333 String::New("Array constructor must have at least one " |
| 334 "parameter.")); | |
| 293 } | 335 } |
| 294 static const int kMaxLength = 0x3fffffff; | 336 bool first_arg_is_array_buffer = |
| 295 #ifndef V8_SHARED | 337 args[0]->IsObject() && |
| 296 ASSERT(kMaxLength == i::ExternalArray::kMaxLength); | 338 args[0]->ToObject()->Get( |
| 297 #endif // V8_SHARED | 339 String::New(kArrayBufferMarkerPropName))->IsTrue(); |
| 298 size_t length = 0; | 340 // Currently, only the following constructors are supported: |
| 299 TryCatch try_catch; | 341 // TypedArray(unsigned long length) |
| 300 if (args[0]->IsUint32()) { | 342 // TypedArray(ArrayBuffer buffer, |
| 301 length = args[0]->Uint32Value(); | 343 // optional unsigned long byteOffset, |
| 302 } else { | 344 // optional unsigned long length) |
| 303 Local<Number> number = args[0]->ToNumber(); | 345 if (args.Length() > 3) { |
| 304 if (number.IsEmpty()) { | 346 return ThrowException( |
| 305 ASSERT(try_catch.HasCaught()); | 347 String::New("Array constructor from ArrayBuffer must " |
| 306 return try_catch.Exception(); | 348 "have 1-3 parameters.")); |
| 349 } | |
| 350 | |
| 351 Local<Value> length_value = (args.Length() < 3) | |
| 352 ? (first_arg_is_array_buffer | |
| 353 ? args[0]->ToObject()->Get(String::New("length")) | |
| 354 : args[0]) | |
| 355 : args[2]; | |
| 356 size_t length = convertToUint(length_value, &try_catch); | |
| 357 if (try_catch.HasCaught()) return try_catch.Exception(); | |
| 358 | |
| 359 void* data = NULL; | |
| 360 size_t offset = 0; | |
| 361 | |
| 362 Handle<Object> array = Object::New(); | |
| 363 if (first_arg_is_array_buffer) { | |
| 364 Handle<Object> derived_from = args[0]->ToObject(); | |
| 365 data = derived_from->GetIndexedPropertiesExternalArrayData(); | |
| 366 | |
| 367 size_t array_buffer_length = convertToUint( | |
| 368 derived_from->Get(String::New("length")), | |
| 369 &try_catch); | |
| 370 if (try_catch.HasCaught()) return try_catch.Exception(); | |
| 371 | |
| 372 if (data == NULL && array_buffer_length != 0) { | |
| 373 return ThrowException( | |
| 374 String::New("ArrayBuffer doesn't have data")); | |
| 307 } | 375 } |
| 308 ASSERT(number->IsNumber()); | 376 |
| 309 Local<Int32> int32 = number->ToInt32(); | 377 if (args.Length() > 1) { |
| 310 if (int32.IsEmpty()) { | 378 offset = convertToUint(args[1], &try_catch); |
| 311 if (try_catch.HasCaught()) { | 379 if (try_catch.HasCaught()) return try_catch.Exception(); |
| 312 return try_catch.Exception(); | 380 |
| 381 // The given byteOffset must be a multiple of the element size of the | |
| 382 // specific type, otherwise an exception is raised. | |
| 383 if (offset % element_size != 0) { | |
| 384 return ThrowException( | |
| 385 String::New("offset must be multiple of element_size")); | |
| 313 } | 386 } |
| 314 } | 387 } |
| 315 int32_t raw_length = int32->Int32Value(); | 388 |
| 316 if (try_catch.HasCaught()) { | 389 if (args.Length() == 2) { |
| 317 return try_catch.Exception(); | 390 // If length is not explicitly specified, the length of the ArrayBuffer |
| 391 // minus the byteOffset must be a multiple of the element size of the | |
| 392 // specific type, or an exception is raised. | |
| 393 length = array_buffer_length - offset; | |
| 318 } | 394 } |
| 319 if (raw_length < 0) { | 395 |
| 320 return ThrowException(String::New("Array length must not be negative.")); | 396 if (args.Length() == 3) { |
| 397 // If a given byteOffset and length references an area beyond the end of | |
| 398 // the ArrayBuffer an exception is raised. | |
| 399 if (offset + (length * element_size) > array_buffer_length) { | |
|
Jakob Kummerow
2012/01/11 13:54:27
You need an equivalent check (offset > array_buffe
danno
2012/01/11 15:10:20
Done.
| |
| 400 return ThrowException( | |
| 401 String::New("length references an area beyond the end of the " | |
| 402 "ArrayBuffer")); | |
| 403 } | |
| 404 } else { | |
| 405 if (length % element_size != 0) { | |
| 406 return ThrowException( | |
| 407 String::New("ArrayBuffer minus the byteOffset must be a " | |
|
Jakob Kummerow
2012/01/11 13:54:27
nit: s/ArrayBuffer/ArrayBuffer length/
danno
2012/01/11 15:10:20
Done.
| |
| 408 "multiple of the element size")); | |
| 409 } | |
| 410 length /= element_size; | |
| 321 } | 411 } |
| 322 if (raw_length > static_cast<int32_t>(kMaxLength)) { | 412 |
| 323 return ThrowException( | 413 // Hold a reference to the ArrayBuffer so its buffer doesn't get collected. |
| 324 String::New("Array length exceeds maximum length.")); | 414 array->Set(String::New(kArrayBufferReferencePropName), args[0], ReadOnly); |
| 325 } | |
| 326 length = static_cast<size_t>(raw_length); | |
| 327 } | 415 } |
| 328 if (length > static_cast<size_t>(kMaxLength)) { | 416 |
| 329 return ThrowException(String::New("Array length exceeds maximum length.")); | 417 if (is_array_buffer_construct) { |
| 418 array->Set(String::New(kArrayBufferMarkerPropName), True(), ReadOnly); | |
| 330 } | 419 } |
| 331 void* data = calloc(length, element_size); | 420 |
| 332 if (data == NULL) { | |
| 333 return ThrowException(String::New("Memory allocation failed.")); | |
| 334 } | |
| 335 Handle<Object> array = Object::New(); | |
| 336 Persistent<Object> persistent_array = Persistent<Object>::New(array); | 421 Persistent<Object> persistent_array = Persistent<Object>::New(array); |
| 337 persistent_array.MakeWeak(data, ExternalArrayWeakCallback); | 422 persistent_array.MakeWeak(data, ExternalArrayWeakCallback); |
| 338 persistent_array.MarkIndependent(); | 423 persistent_array.MarkIndependent(); |
| 339 array->SetIndexedPropertiesToExternalArrayData(data, type, | 424 if (data == NULL && length != 0) { |
| 340 static_cast<int>(length)); | 425 data = calloc(length, element_size); |
| 426 if (data == NULL) { | |
| 427 return ThrowException(String::New("Memory allocation failed.")); | |
| 428 } | |
| 429 } | |
| 430 | |
| 431 array->SetIndexedPropertiesToExternalArrayData( | |
| 432 reinterpret_cast<uint8_t*>(data) + offset, type, | |
| 433 static_cast<int>(length)); | |
| 341 array->Set(String::New("length"), | 434 array->Set(String::New("length"), |
| 342 Int32::New(static_cast<int32_t>(length)), ReadOnly); | 435 Int32::New(static_cast<int32_t>(length)), ReadOnly); |
| 343 array->Set(String::New("BYTES_PER_ELEMENT"), | 436 array->Set(String::New("BYTES_PER_ELEMENT"), |
| 344 Int32::New(static_cast<int32_t>(element_size))); | 437 Int32::New(static_cast<int32_t>(element_size))); |
| 345 return array; | 438 return array; |
| 346 } | 439 } |
| 347 | 440 |
| 348 | 441 |
| 349 void Shell::ExternalArrayWeakCallback(Persistent<Value> object, void* data) { | 442 void Shell::ExternalArrayWeakCallback(Persistent<Value> object, void* data) { |
| 350 free(data); | 443 HandleScope scope; |
| 444 Handle<String> prop_name = String::New(kArrayBufferReferencePropName); | |
| 445 Handle<Object> converted_object = object->ToObject(); | |
| 446 Local<Value> prop_value = converted_object->Get(prop_name); | |
| 447 if (data != NULL && !prop_value->IsObject()) { | |
| 448 free(data); | |
| 449 } | |
| 351 object.Dispose(); | 450 object.Dispose(); |
| 352 } | 451 } |
| 353 | 452 |
| 354 | 453 |
| 454 Handle<Value> Shell::ArrayBuffer(const Arguments& args) { | |
| 455 Handle<Value> result = CreateExternalArray(args, v8::kExternalByteArray, 0); | |
| 456 return result; | |
|
Jakob Kummerow
2012/01/11 13:54:27
nit: you don't need this line, could return direct
danno
2012/01/11 15:10:20
Done.
| |
| 457 } | |
| 458 | |
| 459 | |
| 355 Handle<Value> Shell::Int8Array(const Arguments& args) { | 460 Handle<Value> Shell::Int8Array(const Arguments& args) { |
| 356 return CreateExternalArray(args, v8::kExternalByteArray, sizeof(int8_t)); | 461 return CreateExternalArray(args, v8::kExternalByteArray, sizeof(int8_t)); |
| 357 } | 462 } |
| 358 | 463 |
| 359 | 464 |
| 360 Handle<Value> Shell::Uint8Array(const Arguments& args) { | 465 Handle<Value> Shell::Uint8Array(const Arguments& args) { |
| 361 return CreateExternalArray(args, kExternalUnsignedByteArray, sizeof(uint8_t)); | 466 return CreateExternalArray(args, kExternalUnsignedByteArray, sizeof(uint8_t)); |
| 362 } | 467 } |
| 363 | 468 |
| 364 | 469 |
| (...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 686 FunctionTemplate::New(ReadLine)); | 791 FunctionTemplate::New(ReadLine)); |
| 687 global_template->Set(String::New("load"), FunctionTemplate::New(Load)); | 792 global_template->Set(String::New("load"), FunctionTemplate::New(Load)); |
| 688 global_template->Set(String::New("quit"), FunctionTemplate::New(Quit)); | 793 global_template->Set(String::New("quit"), FunctionTemplate::New(Quit)); |
| 689 global_template->Set(String::New("version"), FunctionTemplate::New(Version)); | 794 global_template->Set(String::New("version"), FunctionTemplate::New(Version)); |
| 690 global_template->Set(String::New("enableProfiler"), | 795 global_template->Set(String::New("enableProfiler"), |
| 691 FunctionTemplate::New(EnableProfiler)); | 796 FunctionTemplate::New(EnableProfiler)); |
| 692 global_template->Set(String::New("disableProfiler"), | 797 global_template->Set(String::New("disableProfiler"), |
| 693 FunctionTemplate::New(DisableProfiler)); | 798 FunctionTemplate::New(DisableProfiler)); |
| 694 | 799 |
| 695 // Bind the handlers for external arrays. | 800 // Bind the handlers for external arrays. |
| 801 global_template->Set(String::New("ArrayBuffer"), | |
| 802 FunctionTemplate::New(ArrayBuffer)); | |
| 696 global_template->Set(String::New("Int8Array"), | 803 global_template->Set(String::New("Int8Array"), |
| 697 FunctionTemplate::New(Int8Array)); | 804 FunctionTemplate::New(Int8Array)); |
| 698 global_template->Set(String::New("Uint8Array"), | 805 global_template->Set(String::New("Uint8Array"), |
| 699 FunctionTemplate::New(Uint8Array)); | 806 FunctionTemplate::New(Uint8Array)); |
| 700 global_template->Set(String::New("Int16Array"), | 807 global_template->Set(String::New("Int16Array"), |
| 701 FunctionTemplate::New(Int16Array)); | 808 FunctionTemplate::New(Int16Array)); |
| 702 global_template->Set(String::New("Uint16Array"), | 809 global_template->Set(String::New("Uint16Array"), |
| 703 FunctionTemplate::New(Uint16Array)); | 810 FunctionTemplate::New(Uint16Array)); |
| 704 global_template->Set(String::New("Int32Array"), | 811 global_template->Set(String::New("Int32Array"), |
| 705 FunctionTemplate::New(Int32Array)); | 812 FunctionTemplate::New(Int32Array)); |
| (...skipping 687 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1393 } | 1500 } |
| 1394 | 1501 |
| 1395 } // namespace v8 | 1502 } // namespace v8 |
| 1396 | 1503 |
| 1397 | 1504 |
| 1398 #ifndef GOOGLE3 | 1505 #ifndef GOOGLE3 |
| 1399 int main(int argc, char* argv[]) { | 1506 int main(int argc, char* argv[]) { |
| 1400 return v8::Shell::Main(argc, argv); | 1507 return v8::Shell::Main(argc, argv); |
| 1401 } | 1508 } |
| 1402 #endif | 1509 #endif |
| OLD | NEW |