| OLD | NEW |
| 1 // Copyright 2012 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 |
| (...skipping 408 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 419 | 419 |
| 420 // Hold a reference to the ArrayBuffer so its buffer doesn't get collected. | 420 // Hold a reference to the ArrayBuffer so its buffer doesn't get collected. |
| 421 array->Set(String::New(kArrayBufferReferencePropName), args[0], ReadOnly); | 421 array->Set(String::New(kArrayBufferReferencePropName), args[0], ReadOnly); |
| 422 } | 422 } |
| 423 | 423 |
| 424 if (is_array_buffer_construct) { | 424 if (is_array_buffer_construct) { |
| 425 array->Set(String::New(kArrayBufferMarkerPropName), True(), ReadOnly); | 425 array->Set(String::New(kArrayBufferMarkerPropName), True(), ReadOnly); |
| 426 } | 426 } |
| 427 | 427 |
| 428 Persistent<Object> persistent_array = Persistent<Object>::New(array); | 428 Persistent<Object> persistent_array = Persistent<Object>::New(array); |
| 429 persistent_array.MakeWeak(data, ExternalArrayWeakCallback); | |
| 430 persistent_array.MarkIndependent(); | |
| 431 if (data == NULL && length != 0) { | 429 if (data == NULL && length != 0) { |
| 432 data = calloc(length, element_size); | 430 // Prepend the size of the allocated chunk to the data itself. |
| 431 int total_size = length * element_size + sizeof(size_t); |
| 432 data = malloc(total_size); |
| 433 if (data == NULL) { | 433 if (data == NULL) { |
| 434 return ThrowException(String::New("Memory allocation failed.")); | 434 return ThrowException(String::New("Memory allocation failed.")); |
| 435 } | 435 } |
| 436 *reinterpret_cast<size_t*>(data) = total_size; |
| 437 data = reinterpret_cast<size_t*>(data) + 1; |
| 438 memset(data, 0, length * element_size); |
| 439 V8::AdjustAmountOfExternalAllocatedMemory(total_size); |
| 436 } | 440 } |
| 441 persistent_array.MakeWeak(data, ExternalArrayWeakCallback); |
| 442 persistent_array.MarkIndependent(); |
| 437 | 443 |
| 438 array->SetIndexedPropertiesToExternalArrayData( | 444 array->SetIndexedPropertiesToExternalArrayData( |
| 439 reinterpret_cast<uint8_t*>(data) + offset, type, | 445 reinterpret_cast<uint8_t*>(data) + offset, type, |
| 440 static_cast<int>(length)); | 446 static_cast<int>(length)); |
| 441 array->Set(String::New("length"), | 447 array->Set(String::New("length"), |
| 442 Int32::New(static_cast<int32_t>(length)), ReadOnly); | 448 Int32::New(static_cast<int32_t>(length)), ReadOnly); |
| 443 array->Set(String::New("BYTES_PER_ELEMENT"), | 449 array->Set(String::New("BYTES_PER_ELEMENT"), |
| 444 Int32::New(static_cast<int32_t>(element_size))); | 450 Int32::New(static_cast<int32_t>(element_size))); |
| 445 return array; | 451 return array; |
| 446 } | 452 } |
| 447 | 453 |
| 448 | 454 |
| 449 void Shell::ExternalArrayWeakCallback(Persistent<Value> object, void* data) { | 455 void Shell::ExternalArrayWeakCallback(Persistent<Value> object, void* data) { |
| 450 HandleScope scope; | 456 HandleScope scope; |
| 451 Handle<String> prop_name = String::New(kArrayBufferReferencePropName); | 457 Handle<String> prop_name = String::New(kArrayBufferReferencePropName); |
| 452 Handle<Object> converted_object = object->ToObject(); | 458 Handle<Object> converted_object = object->ToObject(); |
| 453 Local<Value> prop_value = converted_object->Get(prop_name); | 459 Local<Value> prop_value = converted_object->Get(prop_name); |
| 454 if (data != NULL && !prop_value->IsObject()) { | 460 if (data != NULL && !prop_value->IsObject()) { |
| 461 data = reinterpret_cast<size_t*>(data) - 1; |
| 462 V8::AdjustAmountOfExternalAllocatedMemory( |
| 463 -(*reinterpret_cast<size_t*>(data))); |
| 455 free(data); | 464 free(data); |
| 456 } | 465 } |
| 457 object.Dispose(); | 466 object.Dispose(); |
| 458 } | 467 } |
| 459 | 468 |
| 460 | 469 |
| 461 Handle<Value> Shell::ArrayBuffer(const Arguments& args) { | 470 Handle<Value> Shell::ArrayBuffer(const Arguments& args) { |
| 462 return CreateExternalArray(args, v8::kExternalByteArray, 0); | 471 return CreateExternalArray(args, v8::kExternalByteArray, 0); |
| 463 } | 472 } |
| 464 | 473 |
| (...skipping 1074 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1539 } | 1548 } |
| 1540 | 1549 |
| 1541 } // namespace v8 | 1550 } // namespace v8 |
| 1542 | 1551 |
| 1543 | 1552 |
| 1544 #ifndef GOOGLE3 | 1553 #ifndef GOOGLE3 |
| 1545 int main(int argc, char* argv[]) { | 1554 int main(int argc, char* argv[]) { |
| 1546 return v8::Shell::Main(argc, argv); | 1555 return v8::Shell::Main(argc, argv); |
| 1547 } | 1556 } |
| 1548 #endif | 1557 #endif |
| OLD | NEW |