| 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 == SharedFlag::kNotShared); |
| 29 array_buffer->set_is_shared(shared == SharedFlag::kShared); |
| 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) { |
| 45 void* data; | 47 void* data; |
| 46 CHECK(isolate->array_buffer_allocator() != NULL); | 48 CHECK(isolate->array_buffer_allocator() != NULL); |
| 47 // Prevent creating array buffers when serializing. | 49 // Prevent creating array buffers when serializing. |
| 48 DCHECK(!isolate->serializer_enabled()); | 50 DCHECK(!isolate->serializer_enabled()); |
| 49 if (allocated_length != 0) { | 51 if (allocated_length != 0) { |
| 50 if (initialize) { | 52 if (initialize) { |
| 51 data = isolate->array_buffer_allocator()->Allocate(allocated_length); | 53 data = isolate->array_buffer_allocator()->Allocate(allocated_length); |
| 52 } else { | 54 } else { |
| 53 data = isolate->array_buffer_allocator()->AllocateUninitialized( | 55 data = isolate->array_buffer_allocator()->AllocateUninitialized( |
| 54 allocated_length); | 56 allocated_length); |
| 55 } | 57 } |
| 56 if (data == NULL) return false; | 58 if (data == NULL) return false; |
| 57 } else { | 59 } else { |
| 58 data = NULL; | 60 data = NULL; |
| 59 } | 61 } |
| 60 | 62 |
| 61 SetupArrayBuffer(isolate, array_buffer, false, data, allocated_length); | 63 SetupArrayBuffer(isolate, array_buffer, false, data, allocated_length, |
| 64 shared); |
| 62 return true; | 65 return true; |
| 63 } | 66 } |
| 64 | 67 |
| 65 | 68 |
| 66 void Runtime::NeuterArrayBuffer(Handle<JSArrayBuffer> array_buffer) { | 69 void Runtime::NeuterArrayBuffer(Handle<JSArrayBuffer> array_buffer) { |
| 67 array_buffer->Neuter(); | 70 array_buffer->Neuter(); |
| 68 } | 71 } |
| 69 | 72 |
| 70 | 73 |
| 71 RUNTIME_FUNCTION(Runtime_ArrayBufferInitialize) { | 74 RUNTIME_FUNCTION(Runtime_ArrayBufferInitialize) { |
| 72 HandleScope scope(isolate); | 75 HandleScope scope(isolate); |
| 73 DCHECK(args.length() == 2); | 76 DCHECK(args.length() == 3); |
| 74 CONVERT_ARG_HANDLE_CHECKED(JSArrayBuffer, holder, 0); | 77 CONVERT_ARG_HANDLE_CHECKED(JSArrayBuffer, holder, 0); |
| 75 CONVERT_NUMBER_ARG_HANDLE_CHECKED(byteLength, 1); | 78 CONVERT_NUMBER_ARG_HANDLE_CHECKED(byteLength, 1); |
| 79 CONVERT_BOOLEAN_ARG_CHECKED(is_shared, 2); |
| 76 if (!holder->byte_length()->IsUndefined()) { | 80 if (!holder->byte_length()->IsUndefined()) { |
| 77 // ArrayBuffer is already initialized; probably a fuzz test. | 81 // ArrayBuffer is already initialized; probably a fuzz test. |
| 78 return *holder; | 82 return *holder; |
| 79 } | 83 } |
| 80 size_t allocated_length = 0; | 84 size_t allocated_length = 0; |
| 81 if (!TryNumberToSize(isolate, *byteLength, &allocated_length)) { | 85 if (!TryNumberToSize(isolate, *byteLength, &allocated_length)) { |
| 82 THROW_NEW_ERROR_RETURN_FAILURE( | 86 THROW_NEW_ERROR_RETURN_FAILURE( |
| 83 isolate, NewRangeError(MessageTemplate::kInvalidArrayBufferLength)); | 87 isolate, NewRangeError(MessageTemplate::kInvalidArrayBufferLength)); |
| 84 } | 88 } |
| 85 if (!Runtime::SetupArrayBufferAllocatingData(isolate, holder, | 89 if (!Runtime::SetupArrayBufferAllocatingData( |
| 86 allocated_length)) { | 90 isolate, holder, allocated_length, true, |
| 91 is_shared ? SharedFlag::kShared : SharedFlag::kNotShared)) { |
| 87 THROW_NEW_ERROR_RETURN_FAILURE( | 92 THROW_NEW_ERROR_RETURN_FAILURE( |
| 88 isolate, NewRangeError(MessageTemplate::kInvalidArrayBufferLength)); | 93 isolate, NewRangeError(MessageTemplate::kInvalidArrayBufferLength)); |
| 89 } | 94 } |
| 90 return *holder; | 95 return *holder; |
| 91 } | 96 } |
| 92 | 97 |
| 93 | 98 |
| 94 RUNTIME_FUNCTION(Runtime_ArrayBufferGetByteLength) { | 99 RUNTIME_FUNCTION(Runtime_ArrayBufferGetByteLength) { |
| 95 SealHandleScope shs(isolate); | 100 SealHandleScope shs(isolate); |
| 96 DCHECK(args.length() == 1); | 101 DCHECK(args.length() == 1); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 | 136 |
| 132 | 137 |
| 133 RUNTIME_FUNCTION(Runtime_ArrayBufferNeuter) { | 138 RUNTIME_FUNCTION(Runtime_ArrayBufferNeuter) { |
| 134 HandleScope scope(isolate); | 139 HandleScope scope(isolate); |
| 135 DCHECK(args.length() == 1); | 140 DCHECK(args.length() == 1); |
| 136 CONVERT_ARG_HANDLE_CHECKED(JSArrayBuffer, array_buffer, 0); | 141 CONVERT_ARG_HANDLE_CHECKED(JSArrayBuffer, array_buffer, 0); |
| 137 if (array_buffer->backing_store() == NULL) { | 142 if (array_buffer->backing_store() == NULL) { |
| 138 CHECK(Smi::FromInt(0) == array_buffer->byte_length()); | 143 CHECK(Smi::FromInt(0) == array_buffer->byte_length()); |
| 139 return isolate->heap()->undefined_value(); | 144 return isolate->heap()->undefined_value(); |
| 140 } | 145 } |
| 146 // Shared array buffers should never be neutered. |
| 147 DCHECK(!array_buffer->is_shared()); |
| 141 DCHECK(!array_buffer->is_external()); | 148 DCHECK(!array_buffer->is_external()); |
| 142 void* backing_store = array_buffer->backing_store(); | 149 void* backing_store = array_buffer->backing_store(); |
| 143 size_t byte_length = NumberToSize(isolate, array_buffer->byte_length()); | 150 size_t byte_length = NumberToSize(isolate, array_buffer->byte_length()); |
| 144 array_buffer->set_is_external(true); | 151 array_buffer->set_is_external(true); |
| 145 Runtime::NeuterArrayBuffer(array_buffer); | 152 Runtime::NeuterArrayBuffer(array_buffer); |
| 146 isolate->heap()->UnregisterArrayBuffer(backing_store); | 153 isolate->heap()->UnregisterArrayBuffer(backing_store); |
| 147 isolate->array_buffer_allocator()->Free(backing_store, byte_length); | 154 isolate->array_buffer_allocator()->Free(backing_store, byte_length); |
| 148 return isolate->heap()->undefined_value(); | 155 return isolate->heap()->undefined_value(); |
| 149 } | 156 } |
| 150 | 157 |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 233 | 240 |
| 234 Handle<ExternalArray> elements = isolate->factory()->NewExternalArray( | 241 Handle<ExternalArray> elements = isolate->factory()->NewExternalArray( |
| 235 static_cast<int>(length), array_type, | 242 static_cast<int>(length), array_type, |
| 236 static_cast<uint8_t*>(buffer->backing_store()) + byte_offset); | 243 static_cast<uint8_t*>(buffer->backing_store()) + byte_offset); |
| 237 Handle<Map> map = | 244 Handle<Map> map = |
| 238 JSObject::GetElementsTransitionMap(holder, external_elements_kind); | 245 JSObject::GetElementsTransitionMap(holder, external_elements_kind); |
| 239 JSObject::SetMapAndElements(holder, map, elements); | 246 JSObject::SetMapAndElements(holder, map, elements); |
| 240 DCHECK(IsExternalArrayElementsKind(holder->map()->elements_kind())); | 247 DCHECK(IsExternalArrayElementsKind(holder->map()->elements_kind())); |
| 241 } else { | 248 } else { |
| 242 Handle<JSArrayBuffer> buffer = isolate->factory()->NewJSArrayBuffer(); | 249 Handle<JSArrayBuffer> buffer = isolate->factory()->NewJSArrayBuffer(); |
| 243 Runtime::SetupArrayBuffer(isolate, buffer, true, NULL, byte_length); | 250 Runtime::SetupArrayBuffer(isolate, buffer, true, NULL, byte_length, |
| 251 SharedFlag::kNotShared); |
| 244 holder->set_buffer(*buffer); | 252 holder->set_buffer(*buffer); |
| 245 Handle<FixedTypedArrayBase> elements = | 253 Handle<FixedTypedArrayBase> elements = |
| 246 isolate->factory()->NewFixedTypedArray(static_cast<int>(length), | 254 isolate->factory()->NewFixedTypedArray(static_cast<int>(length), |
| 247 array_type); | 255 array_type); |
| 248 holder->set_elements(*elements); | 256 holder->set_elements(*elements); |
| 249 } | 257 } |
| 250 return isolate->heap()->undefined_value(); | 258 return isolate->heap()->undefined_value(); |
| 251 } | 259 } |
| 252 | 260 |
| 253 | 261 |
| (...skipping 459 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 713 DATA_VIEW_SETTER(Uint16, uint16_t) | 721 DATA_VIEW_SETTER(Uint16, uint16_t) |
| 714 DATA_VIEW_SETTER(Int16, int16_t) | 722 DATA_VIEW_SETTER(Int16, int16_t) |
| 715 DATA_VIEW_SETTER(Uint32, uint32_t) | 723 DATA_VIEW_SETTER(Uint32, uint32_t) |
| 716 DATA_VIEW_SETTER(Int32, int32_t) | 724 DATA_VIEW_SETTER(Int32, int32_t) |
| 717 DATA_VIEW_SETTER(Float32, float) | 725 DATA_VIEW_SETTER(Float32, float) |
| 718 DATA_VIEW_SETTER(Float64, double) | 726 DATA_VIEW_SETTER(Float64, double) |
| 719 | 727 |
| 720 #undef DATA_VIEW_SETTER | 728 #undef DATA_VIEW_SETTER |
| 721 } // namespace internal | 729 } // namespace internal |
| 722 } // namespace v8 | 730 } // namespace v8 |
| OLD | NEW |