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 773 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
784 } | 784 } |
785 | 785 |
786 SetupArrayBuffer(isolate, array_buffer, false, data, allocated_length); | 786 SetupArrayBuffer(isolate, array_buffer, false, data, allocated_length); |
787 | 787 |
788 isolate->heap()->AdjustAmountOfExternalAllocatedMemory(allocated_length); | 788 isolate->heap()->AdjustAmountOfExternalAllocatedMemory(allocated_length); |
789 | 789 |
790 return true; | 790 return true; |
791 } | 791 } |
792 | 792 |
793 | 793 |
794 void Runtime::NeuterArrayBuffer(Handle<JSArrayBuffer> array_buffer) { | |
795 Isolate* isolate = array_buffer->GetIsolate(); | |
796 for (Handle<Object> view_obj(array_buffer->weak_first_view(), isolate); | |
797 !view_obj->IsUndefined();) { | |
798 Handle<JSArrayBufferView> view(JSArrayBufferView::cast(*view_obj)); | |
799 if (view->IsJSTypedArray()) { | |
800 JSTypedArray::cast(*view)->Neuter(); | |
801 } else if (view->IsJSDataView()) { | |
802 JSDataView::cast(*view)->Neuter(); | |
803 } else { | |
804 UNREACHABLE(); | |
805 } | |
806 view_obj = handle(view->weak_next(), isolate); | |
807 } | |
808 array_buffer->Neuter(); | |
809 } | |
810 | |
811 | |
794 RUNTIME_FUNCTION(MaybeObject*, Runtime_ArrayBufferInitialize) { | 812 RUNTIME_FUNCTION(MaybeObject*, Runtime_ArrayBufferInitialize) { |
795 HandleScope scope(isolate); | 813 HandleScope scope(isolate); |
796 ASSERT(args.length() == 2); | 814 ASSERT(args.length() == 2); |
797 CONVERT_ARG_HANDLE_CHECKED(JSArrayBuffer, holder, 0); | 815 CONVERT_ARG_HANDLE_CHECKED(JSArrayBuffer, holder, 0); |
798 CONVERT_ARG_HANDLE_CHECKED(Object, byteLength, 1); | 816 CONVERT_ARG_HANDLE_CHECKED(Object, byteLength, 1); |
799 size_t allocated_length; | 817 size_t allocated_length; |
800 if (byteLength->IsSmi()) { | 818 if (byteLength->IsSmi()) { |
801 allocated_length = Smi::cast(*byteLength)->value(); | 819 allocated_length = Smi::cast(*byteLength)->value(); |
802 } else { | 820 } else { |
803 ASSERT(byteLength->IsHeapNumber()); | 821 ASSERT(byteLength->IsHeapNumber()); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
837 HandleScope scope(isolate); | 855 HandleScope scope(isolate); |
838 ASSERT(args.length() == 3); | 856 ASSERT(args.length() == 3); |
839 CONVERT_ARG_HANDLE_CHECKED(JSArrayBuffer, source, 0); | 857 CONVERT_ARG_HANDLE_CHECKED(JSArrayBuffer, source, 0); |
840 CONVERT_ARG_HANDLE_CHECKED(JSArrayBuffer, target, 1); | 858 CONVERT_ARG_HANDLE_CHECKED(JSArrayBuffer, target, 1); |
841 CONVERT_DOUBLE_ARG_CHECKED(first, 2); | 859 CONVERT_DOUBLE_ARG_CHECKED(first, 2); |
842 size_t start = static_cast<size_t>(first); | 860 size_t start = static_cast<size_t>(first); |
843 size_t target_length = NumberToSize(isolate, target->byte_length()); | 861 size_t target_length = NumberToSize(isolate, target->byte_length()); |
844 | 862 |
845 if (target_length == 0) return isolate->heap()->undefined_value(); | 863 if (target_length == 0) return isolate->heap()->undefined_value(); |
846 | 864 |
847 ASSERT(NumberToSize(isolate, source->byte_length()) - target_length >= start); | 865 size_t source_byte_length = NumberToSize(isolate, source->byte_length()); |
866 CHECK(start <= source_byte_length); | |
Jakob Kummerow
2014/03/17 17:29:05
Why CHECK rather than RUNTIME_ASSERT? Is a crash b
| |
867 CHECK(source_byte_length - start >= target_length); | |
848 uint8_t* source_data = reinterpret_cast<uint8_t*>(source->backing_store()); | 868 uint8_t* source_data = reinterpret_cast<uint8_t*>(source->backing_store()); |
849 uint8_t* target_data = reinterpret_cast<uint8_t*>(target->backing_store()); | 869 uint8_t* target_data = reinterpret_cast<uint8_t*>(target->backing_store()); |
850 CopyBytes(target_data, source_data + start, target_length); | 870 CopyBytes(target_data, source_data + start, target_length); |
851 return isolate->heap()->undefined_value(); | 871 return isolate->heap()->undefined_value(); |
852 } | 872 } |
853 | 873 |
854 | 874 |
855 RUNTIME_FUNCTION(MaybeObject*, Runtime_ArrayBufferIsView) { | 875 RUNTIME_FUNCTION(MaybeObject*, Runtime_ArrayBufferIsView) { |
856 HandleScope scope(isolate); | 876 HandleScope scope(isolate); |
857 ASSERT(args.length() == 1); | 877 ASSERT(args.length() == 1); |
858 CONVERT_ARG_CHECKED(Object, object, 0); | 878 CONVERT_ARG_CHECKED(Object, object, 0); |
859 return object->IsJSArrayBufferView() | 879 return object->IsJSArrayBufferView() |
860 ? isolate->heap()->true_value() | 880 ? isolate->heap()->true_value() |
861 : isolate->heap()->false_value(); | 881 : isolate->heap()->false_value(); |
862 } | 882 } |
863 | 883 |
864 | 884 |
885 RUNTIME_FUNCTION(MaybeObject*, Runtime_ArrayBufferNeuter) { | |
886 HandleScope scope(isolate); | |
887 CONVERT_ARG_HANDLE_CHECKED(JSArrayBuffer, array_buffer, 0); | |
888 ASSERT(!array_buffer->is_external()); | |
889 void* backing_store = array_buffer->backing_store(); | |
890 size_t byte_length = NumberToSize(isolate, array_buffer->byte_length()); | |
891 array_buffer->set_is_external(true); | |
892 Runtime::NeuterArrayBuffer(array_buffer); | |
893 V8::ArrayBufferAllocator()->Free(backing_store, byte_length); | |
894 return isolate->heap()->undefined_value(); | |
895 } | |
896 | |
897 | |
865 void Runtime::ArrayIdToTypeAndSize( | 898 void Runtime::ArrayIdToTypeAndSize( |
866 int arrayId, ExternalArrayType* array_type, size_t* element_size) { | 899 int arrayId, ExternalArrayType* array_type, size_t* element_size) { |
867 switch (arrayId) { | 900 switch (arrayId) { |
868 #define ARRAY_ID_CASE(Type, type, TYPE, ctype, size) \ | 901 #define ARRAY_ID_CASE(Type, type, TYPE, ctype, size) \ |
869 case ARRAY_ID_##TYPE: \ | 902 case ARRAY_ID_##TYPE: \ |
870 *array_type = kExternal##Type##Array; \ | 903 *array_type = kExternal##Type##Array; \ |
871 *element_size = size; \ | 904 *element_size = size; \ |
872 break; | 905 break; |
873 | 906 |
874 TYPED_ARRAYS(ARRAY_ID_CASE) | 907 TYPED_ARRAYS(ARRAY_ID_CASE) |
(...skipping 23 matching lines...) Expand all Loading... | |
898 ExternalArrayType array_type = kExternalInt8Array; // Bogus initialization. | 931 ExternalArrayType array_type = kExternalInt8Array; // Bogus initialization. |
899 size_t element_size = 1; // Bogus initialization. | 932 size_t element_size = 1; // Bogus initialization. |
900 Runtime::ArrayIdToTypeAndSize(arrayId, &array_type, &element_size); | 933 Runtime::ArrayIdToTypeAndSize(arrayId, &array_type, &element_size); |
901 | 934 |
902 holder->set_buffer(*buffer); | 935 holder->set_buffer(*buffer); |
903 holder->set_byte_offset(*byte_offset_object); | 936 holder->set_byte_offset(*byte_offset_object); |
904 holder->set_byte_length(*byte_length_object); | 937 holder->set_byte_length(*byte_length_object); |
905 | 938 |
906 size_t byte_offset = NumberToSize(isolate, *byte_offset_object); | 939 size_t byte_offset = NumberToSize(isolate, *byte_offset_object); |
907 size_t byte_length = NumberToSize(isolate, *byte_length_object); | 940 size_t byte_length = NumberToSize(isolate, *byte_length_object); |
908 ASSERT(byte_length % element_size == 0); | 941 size_t array_buffer_byte_length = |
942 NumberToSize(isolate, buffer->byte_length()); | |
943 CHECK(byte_offset <= array_buffer_byte_length); | |
944 CHECK(array_buffer_byte_length - byte_offset >= byte_length); | |
945 | |
946 CHECK_EQ(0, byte_length % element_size); | |
909 size_t length = byte_length / element_size; | 947 size_t length = byte_length / element_size; |
910 | 948 |
911 if (length > static_cast<unsigned>(Smi::kMaxValue)) { | 949 if (length > static_cast<unsigned>(Smi::kMaxValue)) { |
912 return isolate->Throw(*isolate->factory()-> | 950 return isolate->Throw(*isolate->factory()-> |
913 NewRangeError("invalid_typed_array_length", | 951 NewRangeError("invalid_typed_array_length", |
914 HandleVector<Object>(NULL, 0))); | 952 HandleVector<Object>(NULL, 0))); |
915 } | 953 } |
916 | 954 |
917 Handle<Object> length_obj = isolate->factory()->NewNumberFromSize(length); | 955 Handle<Object> length_obj = isolate->factory()->NewNumberFromSize(length); |
918 holder->set_length(*length_obj); | 956 holder->set_length(*length_obj); |
(...skipping 14055 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
14974 // Handle last resort GC and make sure to allow future allocations | 15012 // Handle last resort GC and make sure to allow future allocations |
14975 // to grow the heap without causing GCs (if possible). | 15013 // to grow the heap without causing GCs (if possible). |
14976 isolate->counters()->gc_last_resort_from_js()->Increment(); | 15014 isolate->counters()->gc_last_resort_from_js()->Increment(); |
14977 isolate->heap()->CollectAllGarbage(Heap::kNoGCFlags, | 15015 isolate->heap()->CollectAllGarbage(Heap::kNoGCFlags, |
14978 "Runtime::PerformGC"); | 15016 "Runtime::PerformGC"); |
14979 } | 15017 } |
14980 } | 15018 } |
14981 | 15019 |
14982 | 15020 |
14983 } } // namespace v8::internal | 15021 } } // namespace v8::internal |
OLD | NEW |