| OLD | NEW |
| 1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "src/v8.h" | 5 #include "src/v8.h" |
| 6 | 6 |
| 7 #include "src/arguments.h" | 7 #include "src/arguments.h" |
| 8 #include "src/messages.h" | 8 #include "src/messages.h" |
| 9 #include "src/runtime/runtime.h" | 9 #include "src/runtime/runtime.h" |
| 10 #include "src/runtime/runtime-utils.h" | 10 #include "src/runtime/runtime-utils.h" |
| 11 | 11 |
| 12 | 12 |
| 13 namespace v8 { | 13 namespace v8 { |
| 14 namespace internal { | 14 namespace internal { |
| 15 | 15 |
| 16 void Runtime::SetupArrayBuffer(Isolate* isolate, | 16 void Runtime::SetupArrayBuffer(Isolate* isolate, |
| 17 Handle<JSArrayBuffer> array_buffer, | 17 Handle<JSArrayBuffer> array_buffer, |
| 18 bool is_external, void* data, | 18 bool is_external, void* data, |
| 19 size_t allocated_length) { | 19 size_t allocated_length, SharedFlag shared) { |
| 20 DCHECK(array_buffer->GetInternalFieldCount() == | 20 DCHECK(array_buffer->GetInternalFieldCount() == |
| 21 v8::ArrayBuffer::kInternalFieldCount); | 21 v8::ArrayBuffer::kInternalFieldCount); |
| 22 for (int i = 0; i < v8::ArrayBuffer::kInternalFieldCount; i++) { | 22 for (int i = 0; i < v8::ArrayBuffer::kInternalFieldCount; i++) { |
| 23 array_buffer->SetInternalField(i, Smi::FromInt(0)); | 23 array_buffer->SetInternalField(i, Smi::FromInt(0)); |
| 24 } | 24 } |
| 25 array_buffer->set_backing_store(data); | 25 array_buffer->set_backing_store(data); |
| 26 array_buffer->set_bit_field(0); | 26 array_buffer->set_bit_field(0); |
| 27 array_buffer->set_is_external(is_external); | 27 array_buffer->set_is_external(is_external); |
| 28 array_buffer->set_is_neuterable(true); | 28 array_buffer->set_is_neuterable(shared == NOT_SHARED); |
| 29 array_buffer->set_is_shared(shared == SHARED); |
| 29 | 30 |
| 30 Handle<Object> byte_length = | 31 Handle<Object> byte_length = |
| 31 isolate->factory()->NewNumberFromSize(allocated_length); | 32 isolate->factory()->NewNumberFromSize(allocated_length); |
| 32 CHECK(byte_length->IsSmi() || byte_length->IsHeapNumber()); | 33 CHECK(byte_length->IsSmi() || byte_length->IsHeapNumber()); |
| 33 array_buffer->set_byte_length(*byte_length); | 34 array_buffer->set_byte_length(*byte_length); |
| 34 | 35 |
| 35 if (data && !is_external) { | 36 if (data && !is_external) { |
| 36 isolate->heap()->RegisterNewArrayBuffer(data, allocated_length); | 37 isolate->heap()->RegisterNewArrayBuffer(data, allocated_length); |
| 37 } | 38 } |
| 38 } | 39 } |
| 39 | 40 |
| 40 | 41 |
| 41 bool Runtime::SetupArrayBufferAllocatingData(Isolate* isolate, | 42 bool Runtime::SetupArrayBufferAllocatingData(Isolate* isolate, |
| 42 Handle<JSArrayBuffer> array_buffer, | 43 Handle<JSArrayBuffer> array_buffer, |
| 43 size_t allocated_length, | 44 size_t allocated_length, |
| 44 bool initialize) { | 45 bool initialize, |
| 46 SharedFlag shared) { |
| 47 v8::ArrayBuffer::Allocator* allocator = |
| 48 shared == SHARED ? isolate->shared_array_buffer_allocator() |
| 49 : isolate->array_buffer_allocator(); |
| 50 CHECK(allocator != NULL); |
| 45 void* data; | 51 void* data; |
| 46 CHECK(isolate->array_buffer_allocator() != NULL); | |
| 47 // Prevent creating array buffers when serializing. | 52 // Prevent creating array buffers when serializing. |
| 48 DCHECK(!isolate->serializer_enabled()); | 53 DCHECK(!isolate->serializer_enabled()); |
| 49 if (allocated_length != 0) { | 54 if (allocated_length != 0) { |
| 50 if (initialize) { | 55 if (initialize) { |
| 51 data = isolate->array_buffer_allocator()->Allocate(allocated_length); | 56 data = allocator->Allocate(allocated_length); |
| 52 } else { | 57 } else { |
| 53 data = isolate->array_buffer_allocator()->AllocateUninitialized( | 58 data = allocator->AllocateUninitialized(allocated_length); |
| 54 allocated_length); | |
| 55 } | 59 } |
| 56 if (data == NULL) return false; | 60 if (data == NULL) return false; |
| 57 } else { | 61 } else { |
| 58 data = NULL; | 62 data = NULL; |
| 59 } | 63 } |
| 60 | 64 |
| 61 SetupArrayBuffer(isolate, array_buffer, false, data, allocated_length); | 65 SetupArrayBuffer(isolate, array_buffer, false, data, allocated_length, |
| 66 shared); |
| 62 return true; | 67 return true; |
| 63 } | 68 } |
| 64 | 69 |
| 65 | 70 |
| 66 void Runtime::NeuterArrayBuffer(Handle<JSArrayBuffer> array_buffer) { | 71 void Runtime::NeuterArrayBuffer(Handle<JSArrayBuffer> array_buffer) { |
| 67 array_buffer->Neuter(); | 72 array_buffer->Neuter(); |
| 68 } | 73 } |
| 69 | 74 |
| 70 | 75 |
| 71 RUNTIME_FUNCTION(Runtime_ArrayBufferInitialize) { | 76 RUNTIME_FUNCTION(Runtime_ArrayBufferInitialize) { |
| 72 HandleScope scope(isolate); | 77 HandleScope scope(isolate); |
| 73 DCHECK(args.length() == 2); | 78 DCHECK(args.length() == 3); |
| 74 CONVERT_ARG_HANDLE_CHECKED(JSArrayBuffer, holder, 0); | 79 CONVERT_ARG_HANDLE_CHECKED(JSArrayBuffer, holder, 0); |
| 75 CONVERT_NUMBER_ARG_HANDLE_CHECKED(byteLength, 1); | 80 CONVERT_NUMBER_ARG_HANDLE_CHECKED(byteLength, 1); |
| 81 CONVERT_BOOLEAN_ARG_CHECKED(is_shared, 2); |
| 76 if (!holder->byte_length()->IsUndefined()) { | 82 if (!holder->byte_length()->IsUndefined()) { |
| 77 // ArrayBuffer is already initialized; probably a fuzz test. | 83 // ArrayBuffer is already initialized; probably a fuzz test. |
| 78 return *holder; | 84 return *holder; |
| 79 } | 85 } |
| 80 size_t allocated_length = 0; | 86 size_t allocated_length = 0; |
| 81 if (!TryNumberToSize(isolate, *byteLength, &allocated_length)) { | 87 if (!TryNumberToSize(isolate, *byteLength, &allocated_length)) { |
| 82 THROW_NEW_ERROR_RETURN_FAILURE( | 88 THROW_NEW_ERROR_RETURN_FAILURE( |
| 83 isolate, NewRangeError(MessageTemplate::kInvalidArrayBufferLength)); | 89 isolate, NewRangeError(MessageTemplate::kInvalidArrayBufferLength)); |
| 84 } | 90 } |
| 85 if (!Runtime::SetupArrayBufferAllocatingData(isolate, holder, | 91 if (!Runtime::SetupArrayBufferAllocatingData( |
| 86 allocated_length)) { | 92 isolate, holder, allocated_length, true, |
| 93 is_shared ? SHARED : NOT_SHARED)) { |
| 87 THROW_NEW_ERROR_RETURN_FAILURE( | 94 THROW_NEW_ERROR_RETURN_FAILURE( |
| 88 isolate, NewRangeError(MessageTemplate::kInvalidArrayBufferLength)); | 95 isolate, NewRangeError(MessageTemplate::kInvalidArrayBufferLength)); |
| 89 } | 96 } |
| 90 return *holder; | 97 return *holder; |
| 91 } | 98 } |
| 92 | 99 |
| 93 | 100 |
| 94 RUNTIME_FUNCTION(Runtime_ArrayBufferGetByteLength) { | 101 RUNTIME_FUNCTION(Runtime_ArrayBufferGetByteLength) { |
| 95 SealHandleScope shs(isolate); | 102 SealHandleScope shs(isolate); |
| 96 DCHECK(args.length() == 1); | 103 DCHECK(args.length() == 1); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 | 138 |
| 132 | 139 |
| 133 RUNTIME_FUNCTION(Runtime_ArrayBufferNeuter) { | 140 RUNTIME_FUNCTION(Runtime_ArrayBufferNeuter) { |
| 134 HandleScope scope(isolate); | 141 HandleScope scope(isolate); |
| 135 DCHECK(args.length() == 1); | 142 DCHECK(args.length() == 1); |
| 136 CONVERT_ARG_HANDLE_CHECKED(JSArrayBuffer, array_buffer, 0); | 143 CONVERT_ARG_HANDLE_CHECKED(JSArrayBuffer, array_buffer, 0); |
| 137 if (array_buffer->backing_store() == NULL) { | 144 if (array_buffer->backing_store() == NULL) { |
| 138 CHECK(Smi::FromInt(0) == array_buffer->byte_length()); | 145 CHECK(Smi::FromInt(0) == array_buffer->byte_length()); |
| 139 return isolate->heap()->undefined_value(); | 146 return isolate->heap()->undefined_value(); |
| 140 } | 147 } |
| 148 // Shared array buffers should never be neutered. |
| 149 DCHECK(!array_buffer->is_shared()); |
| 141 DCHECK(!array_buffer->is_external()); | 150 DCHECK(!array_buffer->is_external()); |
| 142 void* backing_store = array_buffer->backing_store(); | 151 void* backing_store = array_buffer->backing_store(); |
| 143 size_t byte_length = NumberToSize(isolate, array_buffer->byte_length()); | 152 size_t byte_length = NumberToSize(isolate, array_buffer->byte_length()); |
| 144 array_buffer->set_is_external(true); | 153 array_buffer->set_is_external(true); |
| 145 Runtime::NeuterArrayBuffer(array_buffer); | 154 Runtime::NeuterArrayBuffer(array_buffer); |
| 146 isolate->heap()->UnregisterArrayBuffer(backing_store); | 155 isolate->heap()->UnregisterArrayBuffer(backing_store); |
| 147 isolate->array_buffer_allocator()->Free(backing_store, byte_length); | 156 isolate->array_buffer_allocator()->Free(backing_store, byte_length); |
| 148 return isolate->heap()->undefined_value(); | 157 return isolate->heap()->undefined_value(); |
| 149 } | 158 } |
| 150 | 159 |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 233 | 242 |
| 234 Handle<ExternalArray> elements = isolate->factory()->NewExternalArray( | 243 Handle<ExternalArray> elements = isolate->factory()->NewExternalArray( |
| 235 static_cast<int>(length), array_type, | 244 static_cast<int>(length), array_type, |
| 236 static_cast<uint8_t*>(buffer->backing_store()) + byte_offset); | 245 static_cast<uint8_t*>(buffer->backing_store()) + byte_offset); |
| 237 Handle<Map> map = | 246 Handle<Map> map = |
| 238 JSObject::GetElementsTransitionMap(holder, external_elements_kind); | 247 JSObject::GetElementsTransitionMap(holder, external_elements_kind); |
| 239 JSObject::SetMapAndElements(holder, map, elements); | 248 JSObject::SetMapAndElements(holder, map, elements); |
| 240 DCHECK(IsExternalArrayElementsKind(holder->map()->elements_kind())); | 249 DCHECK(IsExternalArrayElementsKind(holder->map()->elements_kind())); |
| 241 } else { | 250 } else { |
| 242 Handle<JSArrayBuffer> buffer = isolate->factory()->NewJSArrayBuffer(); | 251 Handle<JSArrayBuffer> buffer = isolate->factory()->NewJSArrayBuffer(); |
| 243 Runtime::SetupArrayBuffer(isolate, buffer, true, NULL, byte_length); | 252 Runtime::SetupArrayBuffer(isolate, buffer, true, NULL, byte_length, |
| 253 NOT_SHARED); |
| 244 holder->set_buffer(*buffer); | 254 holder->set_buffer(*buffer); |
| 245 Handle<FixedTypedArrayBase> elements = | 255 Handle<FixedTypedArrayBase> elements = |
| 246 isolate->factory()->NewFixedTypedArray(static_cast<int>(length), | 256 isolate->factory()->NewFixedTypedArray(static_cast<int>(length), |
| 247 array_type); | 257 array_type); |
| 248 holder->set_elements(*elements); | 258 holder->set_elements(*elements); |
| 249 } | 259 } |
| 250 return isolate->heap()->undefined_value(); | 260 return isolate->heap()->undefined_value(); |
| 251 } | 261 } |
| 252 | 262 |
| 253 | 263 |
| (...skipping 459 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 713 DATA_VIEW_SETTER(Uint16, uint16_t) | 723 DATA_VIEW_SETTER(Uint16, uint16_t) |
| 714 DATA_VIEW_SETTER(Int16, int16_t) | 724 DATA_VIEW_SETTER(Int16, int16_t) |
| 715 DATA_VIEW_SETTER(Uint32, uint32_t) | 725 DATA_VIEW_SETTER(Uint32, uint32_t) |
| 716 DATA_VIEW_SETTER(Int32, int32_t) | 726 DATA_VIEW_SETTER(Int32, int32_t) |
| 717 DATA_VIEW_SETTER(Float32, float) | 727 DATA_VIEW_SETTER(Float32, float) |
| 718 DATA_VIEW_SETTER(Float64, double) | 728 DATA_VIEW_SETTER(Float64, double) |
| 719 | 729 |
| 720 #undef DATA_VIEW_SETTER | 730 #undef DATA_VIEW_SETTER |
| 721 } // namespace internal | 731 } // namespace internal |
| 722 } // namespace v8 | 732 } // namespace v8 |
| OLD | NEW |