| 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 | |
| 812 RUNTIME_FUNCTION(MaybeObject*, Runtime_ArrayBufferInitialize) { | 794 RUNTIME_FUNCTION(MaybeObject*, Runtime_ArrayBufferInitialize) { |
| 813 HandleScope scope(isolate); | 795 HandleScope scope(isolate); |
| 814 ASSERT(args.length() == 2); | 796 ASSERT(args.length() == 2); |
| 815 CONVERT_ARG_HANDLE_CHECKED(JSArrayBuffer, holder, 0); | 797 CONVERT_ARG_HANDLE_CHECKED(JSArrayBuffer, holder, 0); |
| 816 CONVERT_ARG_HANDLE_CHECKED(Object, byteLength, 1); | 798 CONVERT_ARG_HANDLE_CHECKED(Object, byteLength, 1); |
| 817 size_t allocated_length; | 799 size_t allocated_length; |
| 818 if (byteLength->IsSmi()) { | 800 if (byteLength->IsSmi()) { |
| 819 allocated_length = Smi::cast(*byteLength)->value(); | 801 allocated_length = Smi::cast(*byteLength)->value(); |
| 820 } else { | 802 } else { |
| 821 ASSERT(byteLength->IsHeapNumber()); | 803 ASSERT(byteLength->IsHeapNumber()); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 855 HandleScope scope(isolate); | 837 HandleScope scope(isolate); |
| 856 ASSERT(args.length() == 3); | 838 ASSERT(args.length() == 3); |
| 857 CONVERT_ARG_HANDLE_CHECKED(JSArrayBuffer, source, 0); | 839 CONVERT_ARG_HANDLE_CHECKED(JSArrayBuffer, source, 0); |
| 858 CONVERT_ARG_HANDLE_CHECKED(JSArrayBuffer, target, 1); | 840 CONVERT_ARG_HANDLE_CHECKED(JSArrayBuffer, target, 1); |
| 859 CONVERT_DOUBLE_ARG_CHECKED(first, 2); | 841 CONVERT_DOUBLE_ARG_CHECKED(first, 2); |
| 860 size_t start = static_cast<size_t>(first); | 842 size_t start = static_cast<size_t>(first); |
| 861 size_t target_length = NumberToSize(isolate, target->byte_length()); | 843 size_t target_length = NumberToSize(isolate, target->byte_length()); |
| 862 | 844 |
| 863 if (target_length == 0) return isolate->heap()->undefined_value(); | 845 if (target_length == 0) return isolate->heap()->undefined_value(); |
| 864 | 846 |
| 865 size_t source_byte_length = NumberToSize(isolate, source->byte_length()); | 847 ASSERT(NumberToSize(isolate, source->byte_length()) - target_length >= start); |
| 866 CHECK(start <= source_byte_length); | |
| 867 CHECK(source_byte_length - start >= target_length); | |
| 868 uint8_t* source_data = reinterpret_cast<uint8_t*>(source->backing_store()); | 848 uint8_t* source_data = reinterpret_cast<uint8_t*>(source->backing_store()); |
| 869 uint8_t* target_data = reinterpret_cast<uint8_t*>(target->backing_store()); | 849 uint8_t* target_data = reinterpret_cast<uint8_t*>(target->backing_store()); |
| 870 CopyBytes(target_data, source_data + start, target_length); | 850 CopyBytes(target_data, source_data + start, target_length); |
| 871 return isolate->heap()->undefined_value(); | 851 return isolate->heap()->undefined_value(); |
| 872 } | 852 } |
| 873 | 853 |
| 874 | 854 |
| 875 RUNTIME_FUNCTION(MaybeObject*, Runtime_ArrayBufferIsView) { | 855 RUNTIME_FUNCTION(MaybeObject*, Runtime_ArrayBufferIsView) { |
| 876 HandleScope scope(isolate); | 856 HandleScope scope(isolate); |
| 877 ASSERT(args.length() == 1); | 857 ASSERT(args.length() == 1); |
| 878 CONVERT_ARG_CHECKED(Object, object, 0); | 858 CONVERT_ARG_CHECKED(Object, object, 0); |
| 879 return object->IsJSArrayBufferView() | 859 return object->IsJSArrayBufferView() |
| 880 ? isolate->heap()->true_value() | 860 ? isolate->heap()->true_value() |
| 881 : isolate->heap()->false_value(); | 861 : isolate->heap()->false_value(); |
| 882 } | 862 } |
| 883 | 863 |
| 884 | 864 |
| 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 | |
| 898 void Runtime::ArrayIdToTypeAndSize( | 865 void Runtime::ArrayIdToTypeAndSize( |
| 899 int arrayId, ExternalArrayType* array_type, size_t* element_size) { | 866 int arrayId, ExternalArrayType* array_type, size_t* element_size) { |
| 900 switch (arrayId) { | 867 switch (arrayId) { |
| 901 #define ARRAY_ID_CASE(Type, type, TYPE, ctype, size) \ | 868 #define ARRAY_ID_CASE(Type, type, TYPE, ctype, size) \ |
| 902 case ARRAY_ID_##TYPE: \ | 869 case ARRAY_ID_##TYPE: \ |
| 903 *array_type = kExternal##Type##Array; \ | 870 *array_type = kExternal##Type##Array; \ |
| 904 *element_size = size; \ | 871 *element_size = size; \ |
| 905 break; | 872 break; |
| 906 | 873 |
| 907 TYPED_ARRAYS(ARRAY_ID_CASE) | 874 TYPED_ARRAYS(ARRAY_ID_CASE) |
| (...skipping 23 matching lines...) Expand all Loading... |
| 931 ExternalArrayType array_type = kExternalInt8Array; // Bogus initialization. | 898 ExternalArrayType array_type = kExternalInt8Array; // Bogus initialization. |
| 932 size_t element_size = 1; // Bogus initialization. | 899 size_t element_size = 1; // Bogus initialization. |
| 933 Runtime::ArrayIdToTypeAndSize(arrayId, &array_type, &element_size); | 900 Runtime::ArrayIdToTypeAndSize(arrayId, &array_type, &element_size); |
| 934 | 901 |
| 935 holder->set_buffer(*buffer); | 902 holder->set_buffer(*buffer); |
| 936 holder->set_byte_offset(*byte_offset_object); | 903 holder->set_byte_offset(*byte_offset_object); |
| 937 holder->set_byte_length(*byte_length_object); | 904 holder->set_byte_length(*byte_length_object); |
| 938 | 905 |
| 939 size_t byte_offset = NumberToSize(isolate, *byte_offset_object); | 906 size_t byte_offset = NumberToSize(isolate, *byte_offset_object); |
| 940 size_t byte_length = NumberToSize(isolate, *byte_length_object); | 907 size_t byte_length = NumberToSize(isolate, *byte_length_object); |
| 941 size_t array_buffer_byte_length = | 908 ASSERT(byte_length % element_size == 0); |
| 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); | |
| 947 size_t length = byte_length / element_size; | 909 size_t length = byte_length / element_size; |
| 948 | 910 |
| 949 if (length > static_cast<unsigned>(Smi::kMaxValue)) { | 911 if (length > static_cast<unsigned>(Smi::kMaxValue)) { |
| 950 return isolate->Throw(*isolate->factory()-> | 912 return isolate->Throw(*isolate->factory()-> |
| 951 NewRangeError("invalid_typed_array_length", | 913 NewRangeError("invalid_typed_array_length", |
| 952 HandleVector<Object>(NULL, 0))); | 914 HandleVector<Object>(NULL, 0))); |
| 953 } | 915 } |
| 954 | 916 |
| 955 Handle<Object> length_obj = isolate->factory()->NewNumberFromSize(length); | 917 Handle<Object> length_obj = isolate->factory()->NewNumberFromSize(length); |
| 956 holder->set_length(*length_obj); | 918 holder->set_length(*length_obj); |
| (...skipping 14054 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 15011 // Handle last resort GC and make sure to allow future allocations | 14973 // Handle last resort GC and make sure to allow future allocations |
| 15012 // to grow the heap without causing GCs (if possible). | 14974 // to grow the heap without causing GCs (if possible). |
| 15013 isolate->counters()->gc_last_resort_from_js()->Increment(); | 14975 isolate->counters()->gc_last_resort_from_js()->Increment(); |
| 15014 isolate->heap()->CollectAllGarbage(Heap::kNoGCFlags, | 14976 isolate->heap()->CollectAllGarbage(Heap::kNoGCFlags, |
| 15015 "Runtime::PerformGC"); | 14977 "Runtime::PerformGC"); |
| 15016 } | 14978 } |
| 15017 } | 14979 } |
| 15018 | 14980 |
| 15019 | 14981 |
| 15020 } } // namespace v8::internal | 14982 } } // namespace v8::internal |
| OLD | NEW |