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 Handle<Object> byte_length = |
| 672 isolate->factory()->NewNumber(static_cast<double>(allocated_length)); |
| 673 CHECK(byte_length->IsSmi() || byte_length->IsHeapNumber()); |
| 674 array_buffer->set_byte_length(*byte_length); |
| 675 return true; |
| 676 } |
| 677 |
| 678 |
| 679 bool Runtime::SetupArrayBufferAllocatingData( |
| 680 Isolate* isolate, |
| 681 Handle<JSArrayBuffer> array_buffer, |
| 682 size_t allocated_length) { |
| 683 void* data; |
| 684 if (allocated_length != 0) { |
| 685 data = malloc(allocated_length); |
| 686 if (data == NULL) return false; |
| 687 memset(data, 0, allocated_length); |
| 688 } else { |
| 689 data = NULL; |
| 690 } |
| 691 |
| 692 if (!SetupArrayBuffer(isolate, array_buffer, data, allocated_length)) |
| 693 return false; |
| 694 |
| 695 v8::Isolate* external_isolate = reinterpret_cast<v8::Isolate*>(isolate); |
| 696 v8::Persistent<v8::Value> weak_handle = v8::Persistent<v8::Value>::New( |
| 697 external_isolate, v8::Utils::ToLocal(Handle<Object>::cast(array_buffer))); |
| 698 weak_handle.MakeWeak(external_isolate, data, ArrayBufferWeakCallback); |
| 699 weak_handle.MarkIndependent(external_isolate); |
| 700 isolate->heap()->AdjustAmountOfExternalAllocatedMemory(allocated_length); |
| 701 |
| 702 return true; |
| 703 } |
| 704 |
| 705 |
665 RUNTIME_FUNCTION(MaybeObject*, Runtime_ArrayBufferInitialize) { | 706 RUNTIME_FUNCTION(MaybeObject*, Runtime_ArrayBufferInitialize) { |
666 HandleScope scope(isolate); | 707 HandleScope scope(isolate); |
667 ASSERT(args.length() == 2); | 708 ASSERT(args.length() == 2); |
668 CONVERT_ARG_HANDLE_CHECKED(JSArrayBuffer, holder, 0); | 709 CONVERT_ARG_HANDLE_CHECKED(JSArrayBuffer, holder, 0); |
669 CONVERT_ARG_HANDLE_CHECKED(Object, byteLength, 1); | 710 CONVERT_ARG_HANDLE_CHECKED(Object, byteLength, 1); |
670 size_t allocated_length; | 711 size_t allocated_length; |
671 if (byteLength->IsSmi()) { | 712 if (byteLength->IsSmi()) { |
672 allocated_length = Smi::cast(*byteLength)->value(); | 713 allocated_length = Smi::cast(*byteLength)->value(); |
673 } else { | 714 } else { |
674 ASSERT(byteLength->IsHeapNumber()); | 715 ASSERT(byteLength->IsHeapNumber()); |
675 double value = HeapNumber::cast(*byteLength)->value(); | 716 double value = HeapNumber::cast(*byteLength)->value(); |
676 | 717 |
677 ASSERT(value >= 0); | 718 ASSERT(value >= 0); |
678 | 719 |
679 if (value > std::numeric_limits<size_t>::max()) { | 720 if (value > std::numeric_limits<size_t>::max()) { |
680 return isolate->Throw( | 721 return isolate->Throw( |
681 *isolate->factory()->NewRangeError("invalid_array_buffer_length", | 722 *isolate->factory()->NewRangeError("invalid_array_buffer_length", |
682 HandleVector<Object>(NULL, 0))); | 723 HandleVector<Object>(NULL, 0))); |
683 } | 724 } |
684 | 725 |
685 allocated_length = static_cast<size_t>(value); | 726 allocated_length = static_cast<size_t>(value); |
686 } | 727 } |
687 | 728 |
688 void* data; | 729 if (!Runtime::SetupArrayBufferAllocatingData(isolate, |
689 if (allocated_length != 0) { | 730 holder, allocated_length)) { |
690 data = malloc(allocated_length); | |
691 | |
692 if (data == NULL) { | |
693 return isolate->Throw(*isolate->factory()-> | 731 return isolate->Throw(*isolate->factory()-> |
694 NewRangeError("invalid_array_buffer_length", | 732 NewRangeError("invalid_array_buffer_length", |
695 HandleVector<Object>(NULL, 0))); | 733 HandleVector<Object>(NULL, 0))); |
696 } | |
697 | |
698 memset(data, 0, allocated_length); | |
699 } else { | |
700 data = NULL; | |
701 } | 734 } |
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 | 735 |
721 return *holder; | 736 return *holder; |
722 } | 737 } |
723 | 738 |
724 | 739 |
725 RUNTIME_FUNCTION(MaybeObject*, Runtime_ArrayBufferGetByteLength) { | 740 RUNTIME_FUNCTION(MaybeObject*, Runtime_ArrayBufferGetByteLength) { |
726 NoHandleAllocation ha(isolate); | 741 NoHandleAllocation ha(isolate); |
727 ASSERT(args.length() == 1); | 742 ASSERT(args.length() == 1); |
728 CONVERT_ARG_CHECKED(JSArrayBuffer, holder, 0); | 743 CONVERT_ARG_CHECKED(JSArrayBuffer, holder, 0); |
729 return holder->byte_length(); | 744 return holder->byte_length(); |
(...skipping 12620 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
13350 // Handle last resort GC and make sure to allow future allocations | 13365 // Handle last resort GC and make sure to allow future allocations |
13351 // to grow the heap without causing GCs (if possible). | 13366 // to grow the heap without causing GCs (if possible). |
13352 isolate->counters()->gc_last_resort_from_js()->Increment(); | 13367 isolate->counters()->gc_last_resort_from_js()->Increment(); |
13353 isolate->heap()->CollectAllGarbage(Heap::kNoGCFlags, | 13368 isolate->heap()->CollectAllGarbage(Heap::kNoGCFlags, |
13354 "Runtime::PerformGC"); | 13369 "Runtime::PerformGC"); |
13355 } | 13370 } |
13356 } | 13371 } |
13357 | 13372 |
13358 | 13373 |
13359 } } // namespace v8::internal | 13374 } } // namespace v8::internal |
OLD | NEW |