Chromium Code Reviews| 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 644 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 655 size_t allocated_length = NumberToSize( | 655 size_t allocated_length = NumberToSize( |
| 656 isolate, JSArrayBuffer::cast(*internal_object)->byte_length()); | 656 isolate, JSArrayBuffer::cast(*internal_object)->byte_length()); |
| 657 isolate->heap()->AdjustAmountOfExternalAllocatedMemory( | 657 isolate->heap()->AdjustAmountOfExternalAllocatedMemory( |
| 658 -static_cast<intptr_t>(allocated_length)); | 658 -static_cast<intptr_t>(allocated_length)); |
| 659 if (data != NULL) | 659 if (data != NULL) |
| 660 free(data); | 660 free(data); |
| 661 object.Dispose(external_isolate); | 661 object.Dispose(external_isolate); |
| 662 } | 662 } |
| 663 | 663 |
| 664 | 664 |
| 665 bool Runtime::SetupArrayBuffer(Isolate* isolate, | |
| 666 Handle<JSArrayBuffer> array_buffer, | |
| 667 void* data, | |
| 668 size_t allocated_length) { | |
| 669 array_buffer->set_backing_store(data); | |
| 670 | |
| 671 Object* byte_length; | |
| 672 { | |
| 673 MaybeObject* maybe_byte_length = | |
| 674 isolate->heap()->NumberFromDouble( | |
|
rossberg
2013/04/25 11:00:55
Use the handlified abstraction isolate->factory()-
Dmitry Lomov (no reviews)
2013/04/25 11:48:01
Done.
| |
| 675 static_cast<double>(allocated_length)); | |
| 676 if (!maybe_byte_length->ToObject(&byte_length)) return false; | |
| 677 } | |
| 678 CHECK(byte_length->IsSmi() || byte_length->IsHeapNumber()); | |
| 679 array_buffer->set_byte_length(byte_length); | |
| 680 return true; | |
| 681 } | |
| 682 | |
| 683 | |
| 684 bool Runtime::AllocateAndSetupArrayBuffer( | |
|
rossberg
2013/04/25 11:00:55
Can we rename this to something like SetUpArrayBuf
Dmitry Lomov (no reviews)
2013/04/25 11:48:01
Done.
| |
| 685 Isolate* isolate, | |
| 686 Handle<JSArrayBuffer> array_buffer, | |
| 687 size_t allocated_length) { | |
| 688 void* data; | |
| 689 if (allocated_length != 0) { | |
| 690 data = malloc(allocated_length); | |
| 691 | |
| 692 if (data == NULL) { | |
|
rossberg
2013/04/25 11:00:55
Nit: I'd put the return on the same line and remov
Dmitry Lomov (no reviews)
2013/04/25 11:48:01
Done.
| |
| 693 return false; | |
| 694 } | |
| 695 | |
| 696 memset(data, 0, allocated_length); | |
| 697 } else { | |
| 698 data = NULL; | |
| 699 } | |
| 700 | |
| 701 if (!SetupArrayBuffer(isolate, array_buffer, data, allocated_length)) | |
|
rossberg
2013/04/25 11:00:55
Nit: spurious extra space
Dmitry Lomov (no reviews)
2013/04/25 11:48:01
Done.
| |
| 702 return false; | |
| 703 | |
| 704 v8::Isolate* external_isolate = reinterpret_cast<v8::Isolate*>(isolate); | |
| 705 v8::Persistent<v8::Value> weak_handle = v8::Persistent<v8::Value>::New( | |
| 706 external_isolate, v8::Utils::ToLocal(Handle<Object>::cast(array_buffer))); | |
| 707 weak_handle.MakeWeak(external_isolate, data, ArrayBufferWeakCallback); | |
| 708 weak_handle.MarkIndependent(external_isolate); | |
| 709 isolate->heap()->AdjustAmountOfExternalAllocatedMemory(allocated_length); | |
| 710 | |
| 711 return true; | |
| 712 } | |
| 713 | |
| 714 | |
| 665 RUNTIME_FUNCTION(MaybeObject*, Runtime_ArrayBufferInitialize) { | 715 RUNTIME_FUNCTION(MaybeObject*, Runtime_ArrayBufferInitialize) { |
| 666 HandleScope scope(isolate); | 716 HandleScope scope(isolate); |
| 667 ASSERT(args.length() == 2); | 717 ASSERT(args.length() == 2); |
| 668 CONVERT_ARG_HANDLE_CHECKED(JSArrayBuffer, holder, 0); | 718 CONVERT_ARG_HANDLE_CHECKED(JSArrayBuffer, holder, 0); |
| 669 CONVERT_ARG_HANDLE_CHECKED(Object, byteLength, 1); | 719 CONVERT_ARG_HANDLE_CHECKED(Object, byteLength, 1); |
| 670 size_t allocated_length; | 720 size_t allocated_length; |
| 671 if (byteLength->IsSmi()) { | 721 if (byteLength->IsSmi()) { |
| 672 allocated_length = Smi::cast(*byteLength)->value(); | 722 allocated_length = Smi::cast(*byteLength)->value(); |
| 673 } else { | 723 } else { |
| 674 ASSERT(byteLength->IsHeapNumber()); | 724 ASSERT(byteLength->IsHeapNumber()); |
| 675 double value = HeapNumber::cast(*byteLength)->value(); | 725 double value = HeapNumber::cast(*byteLength)->value(); |
| 676 | 726 |
| 677 ASSERT(value >= 0); | 727 ASSERT(value >= 0); |
| 678 | 728 |
| 679 if (value > std::numeric_limits<size_t>::max()) { | 729 if (value > std::numeric_limits<size_t>::max()) { |
| 680 return isolate->Throw( | 730 return isolate->Throw( |
| 681 *isolate->factory()->NewRangeError("invalid_array_buffer_length", | 731 *isolate->factory()->NewRangeError("invalid_array_buffer_length", |
| 682 HandleVector<Object>(NULL, 0))); | 732 HandleVector<Object>(NULL, 0))); |
| 683 } | 733 } |
| 684 | 734 |
| 685 allocated_length = static_cast<size_t>(value); | 735 allocated_length = static_cast<size_t>(value); |
| 686 } | 736 } |
| 687 | 737 |
| 688 void* data; | 738 if (!Runtime::AllocateAndSetupArrayBuffer(isolate, |
| 689 if (allocated_length != 0) { | 739 holder, allocated_length)) { |
| 690 data = malloc(allocated_length); | |
| 691 | |
| 692 if (data == NULL) { | |
| 693 return isolate->Throw(*isolate->factory()-> | 740 return isolate->Throw(*isolate->factory()-> |
| 694 NewRangeError("invalid_array_buffer_length", | 741 NewRangeError("invalid_array_buffer_length", |
| 695 HandleVector<Object>(NULL, 0))); | 742 HandleVector<Object>(NULL, 0))); |
| 696 } | |
| 697 | |
| 698 memset(data, 0, allocated_length); | |
| 699 } else { | |
| 700 data = NULL; | |
| 701 } | 743 } |
| 702 holder->set_backing_store(data); | |
| 703 | |
| 704 Object* byte_length; | |
| 705 { | |
| 706 MaybeObject* maybe_byte_length = | |
| 707 isolate->heap()->NumberFromDouble( | |
| 708 static_cast<double>(allocated_length)); | |
| 709 if (!maybe_byte_length->ToObject(&byte_length)) return maybe_byte_length; | |
| 710 } | |
| 711 CHECK(byte_length->IsSmi() || byte_length->IsHeapNumber()); | |
| 712 holder->set_byte_length(byte_length); | |
| 713 | |
| 714 v8::Isolate* external_isolate = reinterpret_cast<v8::Isolate*>(isolate); | |
| 715 v8::Persistent<v8::Value> weak_handle = v8::Persistent<v8::Value>::New( | |
| 716 external_isolate, v8::Utils::ToLocal(Handle<Object>::cast(holder))); | |
| 717 weak_handle.MakeWeak(external_isolate, data, ArrayBufferWeakCallback); | |
| 718 weak_handle.MarkIndependent(external_isolate); | |
| 719 isolate->heap()->AdjustAmountOfExternalAllocatedMemory(allocated_length); | |
| 720 | 744 |
| 721 return *holder; | 745 return *holder; |
| 722 } | 746 } |
| 723 | 747 |
| 724 | 748 |
| 725 RUNTIME_FUNCTION(MaybeObject*, Runtime_ArrayBufferGetByteLength) { | 749 RUNTIME_FUNCTION(MaybeObject*, Runtime_ArrayBufferGetByteLength) { |
| 726 NoHandleAllocation ha(isolate); | 750 NoHandleAllocation ha(isolate); |
| 727 ASSERT(args.length() == 1); | 751 ASSERT(args.length() == 1); |
| 728 CONVERT_ARG_CHECKED(JSArrayBuffer, holder, 0); | 752 CONVERT_ARG_CHECKED(JSArrayBuffer, holder, 0); |
| 729 return holder->byte_length(); | 753 return holder->byte_length(); |
| (...skipping 12561 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 13291 // Handle last resort GC and make sure to allow future allocations | 13315 // Handle last resort GC and make sure to allow future allocations |
| 13292 // to grow the heap without causing GCs (if possible). | 13316 // to grow the heap without causing GCs (if possible). |
| 13293 isolate->counters()->gc_last_resort_from_js()->Increment(); | 13317 isolate->counters()->gc_last_resort_from_js()->Increment(); |
| 13294 isolate->heap()->CollectAllGarbage(Heap::kNoGCFlags, | 13318 isolate->heap()->CollectAllGarbage(Heap::kNoGCFlags, |
| 13295 "Runtime::PerformGC"); | 13319 "Runtime::PerformGC"); |
| 13296 } | 13320 } |
| 13297 } | 13321 } |
| 13298 | 13322 |
| 13299 | 13323 |
| 13300 } } // namespace v8::internal | 13324 } } // namespace v8::internal |
| OLD | NEW |