| 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 479 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 490 | 490 |
| 491 | 491 |
| 492 void ExternalArrayWeakCallback(v8::Persistent<v8::Value> object, void* data) { | 492 void ExternalArrayWeakCallback(v8::Persistent<v8::Value> object, void* data) { |
| 493 free(data); | 493 free(data); |
| 494 object.Dispose(); | 494 object.Dispose(); |
| 495 } | 495 } |
| 496 | 496 |
| 497 | 497 |
| 498 v8::Handle<v8::Value> CreateExternalArray(const v8::Arguments& args, | 498 v8::Handle<v8::Value> CreateExternalArray(const v8::Arguments& args, |
| 499 v8::ExternalArrayType type, | 499 v8::ExternalArrayType type, |
| 500 int element_size) { | 500 size_t element_size) { |
| 501 ASSERT(element_size == 1 || element_size == 2 || element_size == 4 || |
| 502 element_size == 8); |
| 501 if (args.Length() != 1) { | 503 if (args.Length() != 1) { |
| 502 return v8::ThrowException( | 504 return v8::ThrowException( |
| 503 v8::String::New("Array constructor needs one parameter.")); | 505 v8::String::New("Array constructor needs one parameter.")); |
| 504 } | 506 } |
| 505 int length = args[0]->Int32Value(); | 507 size_t length = 0; |
| 506 void* data = malloc(length * element_size); | 508 if (args[0]->IsUint32()) { |
| 507 memset(data, 0, length * element_size); | 509 length = args[0]->Uint32Value(); |
| 510 } else if (args[0]->IsNumber()) { |
| 511 double raw_length = args[0]->NumberValue(); |
| 512 if (raw_length < 0) { |
| 513 return v8::ThrowException( |
| 514 v8::String::New("Array length must not be negative.")); |
| 515 } |
| 516 if (raw_length > v8::internal::ExternalArray::kMaxLength) { |
| 517 return v8::ThrowException( |
| 518 v8::String::New("Array length exceeds maximum length.")); |
| 519 } |
| 520 length = static_cast<size_t>(raw_length); |
| 521 } else { |
| 522 return v8::ThrowException( |
| 523 v8::String::New("Array length must be a number.")); |
| 524 } |
| 525 if (length > static_cast<size_t>(v8::internal::ExternalArray::kMaxLength)) { |
| 526 return v8::ThrowException( |
| 527 v8::String::New("Array length exceeds maximum length.")); |
| 528 } |
| 529 void* data = calloc(length, element_size); |
| 530 if (data == NULL) { |
| 531 return v8::ThrowException(v8::String::New("Memory allocation failed.")); |
| 532 } |
| 508 v8::Handle<v8::Object> array = v8::Object::New(); | 533 v8::Handle<v8::Object> array = v8::Object::New(); |
| 509 v8::Persistent<v8::Object> persistent_array = | 534 v8::Persistent<v8::Object> persistent_array = |
| 510 v8::Persistent<v8::Object>::New(array); | 535 v8::Persistent<v8::Object>::New(array); |
| 511 persistent_array.MakeWeak(data, ExternalArrayWeakCallback); | 536 persistent_array.MakeWeak(data, ExternalArrayWeakCallback); |
| 512 persistent_array.MarkIndependent(); | 537 persistent_array.MarkIndependent(); |
| 513 array->SetIndexedPropertiesToExternalArrayData(data, type, length); | 538 array->SetIndexedPropertiesToExternalArrayData(data, type, length); |
| 514 array->Set(v8::String::New("length"), v8::Int32::New(length), | 539 array->Set(v8::String::New("length"), v8::Int32::New(length), |
| 515 v8::ReadOnly); | 540 v8::ReadOnly); |
| 516 array->Set(v8::String::New("BYTES_PER_ELEMENT"), | 541 array->Set(v8::String::New("BYTES_PER_ELEMENT"), |
| 517 v8::Int32::New(element_size)); | 542 v8::Int32::New(element_size)); |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 676 printf("^"); | 701 printf("^"); |
| 677 } | 702 } |
| 678 printf("\n"); | 703 printf("\n"); |
| 679 v8::String::Utf8Value stack_trace(try_catch->StackTrace()); | 704 v8::String::Utf8Value stack_trace(try_catch->StackTrace()); |
| 680 if (stack_trace.length() > 0) { | 705 if (stack_trace.length() > 0) { |
| 681 const char* stack_trace_string = ToCString(stack_trace); | 706 const char* stack_trace_string = ToCString(stack_trace); |
| 682 printf("%s\n", stack_trace_string); | 707 printf("%s\n", stack_trace_string); |
| 683 } | 708 } |
| 684 } | 709 } |
| 685 } | 710 } |
| OLD | NEW |