Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(64)

Side by Side Diff: src/runtime/runtime-typedarray.cc

Issue 1069883002: WIP SharedArrayBuffer implementation (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: update MakeTypeError calls Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/runtime/runtime.h" 8 #include "src/runtime/runtime.h"
9 #include "src/runtime/runtime-utils.h" 9 #include "src/runtime/runtime-utils.h"
10 10
11 11
12 namespace v8 { 12 namespace v8 {
13 namespace internal { 13 namespace internal {
14 14
15 void Runtime::FreeArrayBuffer(Isolate* isolate, 15 void Runtime::FreeArrayBuffer(Isolate* isolate,
16 JSArrayBuffer* phantom_array_buffer) { 16 JSArrayBuffer* phantom_array_buffer) {
17 if (phantom_array_buffer->should_be_freed()) { 17 if (phantom_array_buffer->should_be_freed()) {
18 DCHECK(phantom_array_buffer->is_external()); 18 DCHECK(phantom_array_buffer->is_external());
19 free(phantom_array_buffer->backing_store()); 19 free(phantom_array_buffer->backing_store());
20 } 20 }
21 if (phantom_array_buffer->is_external()) return; 21 if (phantom_array_buffer->is_external()) return;
22 22
23 size_t allocated_length = 23 size_t allocated_length =
24 NumberToSize(isolate, phantom_array_buffer->byte_length()); 24 NumberToSize(isolate, phantom_array_buffer->byte_length());
25 25
26 reinterpret_cast<v8::Isolate*>(isolate) 26 reinterpret_cast<v8::Isolate*>(isolate)
27 ->AdjustAmountOfExternalAllocatedMemory( 27 ->AdjustAmountOfExternalAllocatedMemory(
28 -static_cast<int64_t>(allocated_length)); 28 -static_cast<int64_t>(allocated_length));
29 CHECK(V8::ArrayBufferAllocator() != NULL); 29 if (phantom_array_buffer->is_shared()) {
30 V8::ArrayBufferAllocator()->Free(phantom_array_buffer->backing_store(), 30 CHECK(V8::SharedArrayBufferAllocator() != NULL);
31 allocated_length); 31 V8::SharedArrayBufferAllocator()->Free(
32 phantom_array_buffer->backing_store(), allocated_length);
33 } else {
34 CHECK(V8::ArrayBufferAllocator() != NULL);
35 V8::ArrayBufferAllocator()->Free(phantom_array_buffer->backing_store(),
36 allocated_length);
37 }
32 } 38 }
33 39
34 40
35 void Runtime::SetupArrayBuffer(Isolate* isolate, 41 void Runtime::SetupArrayBuffer(Isolate* isolate,
36 Handle<JSArrayBuffer> array_buffer, 42 Handle<JSArrayBuffer> array_buffer,
37 bool is_external, void* data, 43 bool is_external, void* data,
38 size_t allocated_length) { 44 size_t allocated_length) {
39 DCHECK(array_buffer->GetInternalFieldCount() == 45 DCHECK(array_buffer->GetInternalFieldCount() ==
40 v8::ArrayBuffer::kInternalFieldCount); 46 v8::ArrayBuffer::kInternalFieldCount);
41 for (int i = 0; i < v8::ArrayBuffer::kInternalFieldCount; i++) { 47 for (int i = 0; i < v8::ArrayBuffer::kInternalFieldCount; i++) {
(...skipping 21 matching lines...) Expand all
63 ->set_weak_next(*array_buffer); 69 ->set_weak_next(*array_buffer);
64 isolate->heap()->set_last_array_buffer_in_list(*array_buffer); 70 isolate->heap()->set_last_array_buffer_in_list(*array_buffer);
65 } 71 }
66 } 72 }
67 73
68 74
69 bool Runtime::SetupArrayBufferAllocatingData(Isolate* isolate, 75 bool Runtime::SetupArrayBufferAllocatingData(Isolate* isolate,
70 Handle<JSArrayBuffer> array_buffer, 76 Handle<JSArrayBuffer> array_buffer,
71 size_t allocated_length, 77 size_t allocated_length,
72 bool initialize) { 78 bool initialize) {
79 bool is_shared = array_buffer->is_shared();
Jarin 2015/04/29 04:46:01 This should be array_buffer->is_shared() == SHARED
binji 2015/04/29 18:27:22 Done.
73 void* data; 80 void* data;
74 CHECK(V8::ArrayBufferAllocator() != NULL); 81 CHECK(is_shared ? V8::SharedArrayBufferAllocator() != NULL
82 : V8::ArrayBufferAllocator() != NULL);
75 // Prevent creating array buffers when serializing. 83 // Prevent creating array buffers when serializing.
76 DCHECK(!isolate->serializer_enabled()); 84 DCHECK(!isolate->serializer_enabled());
77 if (allocated_length != 0) { 85 if (allocated_length != 0) {
78 if (initialize) { 86 if (is_shared) {
79 data = V8::ArrayBufferAllocator()->Allocate(allocated_length); 87 if (initialize) {
88 data = V8::SharedArrayBufferAllocator()->Allocate(allocated_length);
89 } else {
90 data = V8::SharedArrayBufferAllocator()->AllocateUninitialized(
91 allocated_length);
92 }
80 } else { 93 } else {
81 data = 94 if (initialize) {
82 V8::ArrayBufferAllocator()->AllocateUninitialized(allocated_length); 95 data = V8::ArrayBufferAllocator()->Allocate(allocated_length);
96 } else {
97 data =
98 V8::ArrayBufferAllocator()->AllocateUninitialized(allocated_length);
99 }
83 } 100 }
84 if (data == NULL) return false; 101 if (data == NULL) return false;
85 } else { 102 } else {
86 data = NULL; 103 data = NULL;
87 } 104 }
88 105
89 SetupArrayBuffer(isolate, array_buffer, false, data, allocated_length); 106 SetupArrayBuffer(isolate, array_buffer, false, data, allocated_length);
90 107
91 reinterpret_cast<v8::Isolate*>(isolate) 108 reinterpret_cast<v8::Isolate*>(isolate)
92 ->AdjustAmountOfExternalAllocatedMemory(allocated_length); 109 ->AdjustAmountOfExternalAllocatedMemory(allocated_length);
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
126 143
127 144
128 RUNTIME_FUNCTION(Runtime_ArrayBufferGetByteLength) { 145 RUNTIME_FUNCTION(Runtime_ArrayBufferGetByteLength) {
129 SealHandleScope shs(isolate); 146 SealHandleScope shs(isolate);
130 DCHECK(args.length() == 1); 147 DCHECK(args.length() == 1);
131 CONVERT_ARG_CHECKED(JSArrayBuffer, holder, 0); 148 CONVERT_ARG_CHECKED(JSArrayBuffer, holder, 0);
132 return holder->byte_length(); 149 return holder->byte_length();
133 } 150 }
134 151
135 152
153 RUNTIME_FUNCTION(Runtime_ArrayBufferIsShared) {
154 SealHandleScope shs(isolate);
155 DCHECK(args.length() == 1);
156 CONVERT_ARG_CHECKED(JSArrayBuffer, holder, 0);
157 return isolate->heap()->ToBoolean(holder->is_shared());
158 }
159
160
136 RUNTIME_FUNCTION(Runtime_ArrayBufferSliceImpl) { 161 RUNTIME_FUNCTION(Runtime_ArrayBufferSliceImpl) {
137 HandleScope scope(isolate); 162 HandleScope scope(isolate);
138 DCHECK(args.length() == 3); 163 DCHECK(args.length() == 3);
139 CONVERT_ARG_HANDLE_CHECKED(JSArrayBuffer, source, 0); 164 CONVERT_ARG_HANDLE_CHECKED(JSArrayBuffer, source, 0);
140 CONVERT_ARG_HANDLE_CHECKED(JSArrayBuffer, target, 1); 165 CONVERT_ARG_HANDLE_CHECKED(JSArrayBuffer, target, 1);
141 CONVERT_NUMBER_ARG_HANDLE_CHECKED(first, 2); 166 CONVERT_NUMBER_ARG_HANDLE_CHECKED(first, 2);
142 RUNTIME_ASSERT(!source.is_identical_to(target)); 167 RUNTIME_ASSERT(!source.is_identical_to(target));
143 size_t start = 0; 168 size_t start = 0;
144 RUNTIME_ASSERT(TryNumberToSize(isolate, *first, &start)); 169 RUNTIME_ASSERT(TryNumberToSize(isolate, *first, &start));
145 size_t target_length = NumberToSize(isolate, target->byte_length()); 170 size_t target_length = NumberToSize(isolate, target->byte_length());
(...skipping 19 matching lines...) Expand all
165 190
166 191
167 RUNTIME_FUNCTION(Runtime_ArrayBufferNeuter) { 192 RUNTIME_FUNCTION(Runtime_ArrayBufferNeuter) {
168 HandleScope scope(isolate); 193 HandleScope scope(isolate);
169 DCHECK(args.length() == 1); 194 DCHECK(args.length() == 1);
170 CONVERT_ARG_HANDLE_CHECKED(JSArrayBuffer, array_buffer, 0); 195 CONVERT_ARG_HANDLE_CHECKED(JSArrayBuffer, array_buffer, 0);
171 if (array_buffer->backing_store() == NULL) { 196 if (array_buffer->backing_store() == NULL) {
172 CHECK(Smi::FromInt(0) == array_buffer->byte_length()); 197 CHECK(Smi::FromInt(0) == array_buffer->byte_length());
173 return isolate->heap()->undefined_value(); 198 return isolate->heap()->undefined_value();
174 } 199 }
200 // Shared array buffers should never be neutered.
201 DCHECK(!array_buffer->is_shared());
jochen (gone - plz use gerrit) 2015/04/28 18:31:46 why? What about ArrayBuffer.transfer?
Jarin 2015/04/29 04:46:01 The whole idea of SharedArrayBuffer is that it can
binji 2015/04/29 18:27:22 I asked about this on the spec doc, but I think it
175 DCHECK(!array_buffer->is_external()); 202 DCHECK(!array_buffer->is_external());
176 void* backing_store = array_buffer->backing_store(); 203 void* backing_store = array_buffer->backing_store();
177 size_t byte_length = NumberToSize(isolate, array_buffer->byte_length()); 204 size_t byte_length = NumberToSize(isolate, array_buffer->byte_length());
178 array_buffer->set_is_external(true); 205 array_buffer->set_is_external(true);
179 Runtime::NeuterArrayBuffer(array_buffer); 206 Runtime::NeuterArrayBuffer(array_buffer);
180 V8::ArrayBufferAllocator()->Free(backing_store, byte_length); 207 V8::ArrayBufferAllocator()->Free(backing_store, byte_length);
181 return isolate->heap()->undefined_value(); 208 return isolate->heap()->undefined_value();
182 } 209 }
183 210
184 211
(...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after
435 462
436 if (!args[1]->IsJSTypedArray()) 463 if (!args[1]->IsJSTypedArray())
437 return Smi::FromInt(TYPED_ARRAY_SET_NON_TYPED_ARRAY); 464 return Smi::FromInt(TYPED_ARRAY_SET_NON_TYPED_ARRAY);
438 465
439 CONVERT_ARG_HANDLE_CHECKED(JSTypedArray, target_obj, 0); 466 CONVERT_ARG_HANDLE_CHECKED(JSTypedArray, target_obj, 0);
440 CONVERT_ARG_HANDLE_CHECKED(JSTypedArray, source_obj, 1); 467 CONVERT_ARG_HANDLE_CHECKED(JSTypedArray, source_obj, 1);
441 CONVERT_NUMBER_ARG_HANDLE_CHECKED(offset_obj, 2); 468 CONVERT_NUMBER_ARG_HANDLE_CHECKED(offset_obj, 2);
442 469
443 Handle<JSTypedArray> target(JSTypedArray::cast(*target_obj)); 470 Handle<JSTypedArray> target(JSTypedArray::cast(*target_obj));
444 Handle<JSTypedArray> source(JSTypedArray::cast(*source_obj)); 471 Handle<JSTypedArray> source(JSTypedArray::cast(*source_obj));
472
473 if (target->is_shared() != source->is_shared()) {
474 THROW_NEW_ERROR_RETURN_FAILURE(
475 isolate,
476 NewTypeError("invalid_argument", HandleVector<Object>(NULL, 0)));
477 }
478
445 size_t offset = 0; 479 size_t offset = 0;
446 RUNTIME_ASSERT(TryNumberToSize(isolate, *offset_obj, &offset)); 480 RUNTIME_ASSERT(TryNumberToSize(isolate, *offset_obj, &offset));
447 size_t target_length = NumberToSize(isolate, target->length()); 481 size_t target_length = NumberToSize(isolate, target->length());
448 size_t source_length = NumberToSize(isolate, source->length()); 482 size_t source_length = NumberToSize(isolate, source->length());
449 size_t target_byte_length = NumberToSize(isolate, target->byte_length()); 483 size_t target_byte_length = NumberToSize(isolate, target->byte_length());
450 size_t source_byte_length = NumberToSize(isolate, source->byte_length()); 484 size_t source_byte_length = NumberToSize(isolate, source->byte_length());
451 if (offset > target_length || offset + source_length > target_length || 485 if (offset > target_length || offset + source_length > target_length ||
452 offset + source_length < offset) { // overflow 486 offset + source_length < offset) { // overflow
453 THROW_NEW_ERROR_RETURN_FAILURE( 487 THROW_NEW_ERROR_RETURN_FAILURE(
454 isolate, NewRangeError("typed_array_set_source_too_large", 488 isolate, NewRangeError("typed_array_set_source_too_large",
(...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after
749 DATA_VIEW_SETTER(Uint16, uint16_t) 783 DATA_VIEW_SETTER(Uint16, uint16_t)
750 DATA_VIEW_SETTER(Int16, int16_t) 784 DATA_VIEW_SETTER(Int16, int16_t)
751 DATA_VIEW_SETTER(Uint32, uint32_t) 785 DATA_VIEW_SETTER(Uint32, uint32_t)
752 DATA_VIEW_SETTER(Int32, int32_t) 786 DATA_VIEW_SETTER(Int32, int32_t)
753 DATA_VIEW_SETTER(Float32, float) 787 DATA_VIEW_SETTER(Float32, float)
754 DATA_VIEW_SETTER(Float64, double) 788 DATA_VIEW_SETTER(Float64, double)
755 789
756 #undef DATA_VIEW_SETTER 790 #undef DATA_VIEW_SETTER
757 } 791 }
758 } // namespace v8::internal 792 } // namespace v8::internal
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698