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" |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
152 array_buffer->set_is_external(true); | 152 array_buffer->set_is_external(true); |
153 Runtime::NeuterArrayBuffer(array_buffer); | 153 Runtime::NeuterArrayBuffer(array_buffer); |
154 isolate->heap()->UnregisterArrayBuffer( | 154 isolate->heap()->UnregisterArrayBuffer( |
155 isolate->heap()->InNewSpace(*array_buffer), backing_store); | 155 isolate->heap()->InNewSpace(*array_buffer), backing_store); |
156 isolate->array_buffer_allocator()->Free(backing_store, byte_length); | 156 isolate->array_buffer_allocator()->Free(backing_store, byte_length); |
157 return isolate->heap()->undefined_value(); | 157 return isolate->heap()->undefined_value(); |
158 } | 158 } |
159 | 159 |
160 | 160 |
161 void Runtime::ArrayIdToTypeAndSize(int arrayId, ExternalArrayType* array_type, | 161 void Runtime::ArrayIdToTypeAndSize(int arrayId, ExternalArrayType* array_type, |
| 162 ElementsKind* external_elements_kind, |
162 ElementsKind* fixed_elements_kind, | 163 ElementsKind* fixed_elements_kind, |
163 size_t* element_size) { | 164 size_t* element_size) { |
164 switch (arrayId) { | 165 switch (arrayId) { |
165 #define ARRAY_ID_CASE(Type, type, TYPE, ctype, size) \ | 166 #define ARRAY_ID_CASE(Type, type, TYPE, ctype, size) \ |
166 case ARRAY_ID_##TYPE: \ | 167 case ARRAY_ID_##TYPE: \ |
167 *array_type = kExternal##Type##Array; \ | 168 *array_type = kExternal##Type##Array; \ |
| 169 *external_elements_kind = EXTERNAL_##TYPE##_ELEMENTS; \ |
168 *fixed_elements_kind = TYPE##_ELEMENTS; \ | 170 *fixed_elements_kind = TYPE##_ELEMENTS; \ |
169 *element_size = size; \ | 171 *element_size = size; \ |
170 break; | 172 break; |
171 | 173 |
172 TYPED_ARRAYS(ARRAY_ID_CASE) | 174 TYPED_ARRAYS(ARRAY_ID_CASE) |
173 #undef ARRAY_ID_CASE | 175 #undef ARRAY_ID_CASE |
174 | 176 |
175 default: | 177 default: |
176 UNREACHABLE(); | 178 UNREACHABLE(); |
177 } | 179 } |
178 } | 180 } |
179 | 181 |
180 | 182 |
181 RUNTIME_FUNCTION(Runtime_TypedArrayInitialize) { | 183 RUNTIME_FUNCTION(Runtime_TypedArrayInitialize) { |
182 HandleScope scope(isolate); | 184 HandleScope scope(isolate); |
183 DCHECK(args.length() == 6); | 185 DCHECK(args.length() == 6); |
184 CONVERT_ARG_HANDLE_CHECKED(JSTypedArray, holder, 0); | 186 CONVERT_ARG_HANDLE_CHECKED(JSTypedArray, holder, 0); |
185 CONVERT_SMI_ARG_CHECKED(arrayId, 1); | 187 CONVERT_SMI_ARG_CHECKED(arrayId, 1); |
186 CONVERT_ARG_HANDLE_CHECKED(Object, maybe_buffer, 2); | 188 CONVERT_ARG_HANDLE_CHECKED(Object, maybe_buffer, 2); |
187 CONVERT_NUMBER_ARG_HANDLE_CHECKED(byte_offset_object, 3); | 189 CONVERT_NUMBER_ARG_HANDLE_CHECKED(byte_offset_object, 3); |
188 CONVERT_NUMBER_ARG_HANDLE_CHECKED(byte_length_object, 4); | 190 CONVERT_NUMBER_ARG_HANDLE_CHECKED(byte_length_object, 4); |
189 CONVERT_BOOLEAN_ARG_CHECKED(initialize, 5); | 191 CONVERT_BOOLEAN_ARG_CHECKED(initialize, 5); |
190 | 192 |
191 RUNTIME_ASSERT(arrayId >= Runtime::ARRAY_ID_FIRST && | 193 RUNTIME_ASSERT(arrayId >= Runtime::ARRAY_ID_FIRST && |
192 arrayId <= Runtime::ARRAY_ID_LAST); | 194 arrayId <= Runtime::ARRAY_ID_LAST); |
193 | 195 |
194 ExternalArrayType array_type = kExternalInt8Array; // Bogus initialization. | 196 ExternalArrayType array_type = kExternalInt8Array; // Bogus initialization. |
195 size_t element_size = 1; // Bogus initialization. | 197 size_t element_size = 1; // Bogus initialization. |
| 198 ElementsKind external_elements_kind = |
| 199 EXTERNAL_INT8_ELEMENTS; // Bogus initialization. |
196 ElementsKind fixed_elements_kind = INT8_ELEMENTS; // Bogus initialization. | 200 ElementsKind fixed_elements_kind = INT8_ELEMENTS; // Bogus initialization. |
197 Runtime::ArrayIdToTypeAndSize(arrayId, &array_type, &fixed_elements_kind, | 201 Runtime::ArrayIdToTypeAndSize(arrayId, &array_type, &external_elements_kind, |
198 &element_size); | 202 &fixed_elements_kind, &element_size); |
199 RUNTIME_ASSERT(holder->map()->elements_kind() == fixed_elements_kind); | 203 RUNTIME_ASSERT(holder->map()->elements_kind() == fixed_elements_kind); |
200 | 204 |
201 size_t byte_offset = 0; | 205 size_t byte_offset = 0; |
202 size_t byte_length = 0; | 206 size_t byte_length = 0; |
203 RUNTIME_ASSERT(TryNumberToSize(isolate, *byte_offset_object, &byte_offset)); | 207 RUNTIME_ASSERT(TryNumberToSize(isolate, *byte_offset_object, &byte_offset)); |
204 RUNTIME_ASSERT(TryNumberToSize(isolate, *byte_length_object, &byte_length)); | 208 RUNTIME_ASSERT(TryNumberToSize(isolate, *byte_length_object, &byte_length)); |
205 | 209 |
206 if (maybe_buffer->IsJSArrayBuffer()) { | 210 if (maybe_buffer->IsJSArrayBuffer()) { |
207 Handle<JSArrayBuffer> buffer = Handle<JSArrayBuffer>::cast(maybe_buffer); | 211 Handle<JSArrayBuffer> buffer = Handle<JSArrayBuffer>::cast(maybe_buffer); |
208 size_t array_buffer_byte_length = | 212 size_t array_buffer_byte_length = |
(...skipping 21 matching lines...) Expand all Loading... |
230 } | 234 } |
231 Handle<Object> length_obj = isolate->factory()->NewNumberFromSize(length); | 235 Handle<Object> length_obj = isolate->factory()->NewNumberFromSize(length); |
232 holder->set_length(*length_obj); | 236 holder->set_length(*length_obj); |
233 holder->set_byte_offset(*byte_offset_object); | 237 holder->set_byte_offset(*byte_offset_object); |
234 holder->set_byte_length(*byte_length_object); | 238 holder->set_byte_length(*byte_length_object); |
235 | 239 |
236 if (!maybe_buffer->IsNull()) { | 240 if (!maybe_buffer->IsNull()) { |
237 Handle<JSArrayBuffer> buffer = Handle<JSArrayBuffer>::cast(maybe_buffer); | 241 Handle<JSArrayBuffer> buffer = Handle<JSArrayBuffer>::cast(maybe_buffer); |
238 holder->set_buffer(*buffer); | 242 holder->set_buffer(*buffer); |
239 | 243 |
240 Handle<FixedTypedArrayBase> elements = | 244 Handle<ExternalArray> elements = isolate->factory()->NewExternalArray( |
241 isolate->factory()->NewFixedTypedArrayWithExternalPointer( | 245 static_cast<int>(length), array_type, |
242 static_cast<int>(length), array_type, | 246 static_cast<uint8_t*>(buffer->backing_store()) + byte_offset); |
243 static_cast<uint8_t*>(buffer->backing_store()) + byte_offset); | 247 Handle<Map> map = |
244 holder->set_elements(*elements); | 248 JSObject::GetElementsTransitionMap(holder, external_elements_kind); |
| 249 JSObject::SetMapAndElements(holder, map, elements); |
| 250 DCHECK(IsExternalArrayElementsKind(holder->map()->elements_kind())); |
245 } else { | 251 } else { |
246 Handle<JSArrayBuffer> buffer = isolate->factory()->NewJSArrayBuffer(); | 252 Handle<JSArrayBuffer> buffer = isolate->factory()->NewJSArrayBuffer(); |
247 Runtime::SetupArrayBuffer(isolate, buffer, true, NULL, byte_length, | 253 Runtime::SetupArrayBuffer(isolate, buffer, true, NULL, byte_length, |
248 SharedFlag::kNotShared); | 254 SharedFlag::kNotShared); |
249 holder->set_buffer(*buffer); | 255 holder->set_buffer(*buffer); |
250 Handle<FixedTypedArrayBase> elements = | 256 Handle<FixedTypedArrayBase> elements = |
251 isolate->factory()->NewFixedTypedArray(static_cast<int>(length), | 257 isolate->factory()->NewFixedTypedArray(static_cast<int>(length), |
252 array_type, initialize); | 258 array_type, initialize); |
253 holder->set_elements(*elements); | 259 holder->set_elements(*elements); |
254 } | 260 } |
(...skipping 12 matching lines...) Expand all Loading... |
267 CONVERT_ARG_HANDLE_CHECKED(JSTypedArray, holder, 0); | 273 CONVERT_ARG_HANDLE_CHECKED(JSTypedArray, holder, 0); |
268 CONVERT_SMI_ARG_CHECKED(arrayId, 1); | 274 CONVERT_SMI_ARG_CHECKED(arrayId, 1); |
269 CONVERT_ARG_HANDLE_CHECKED(Object, source, 2); | 275 CONVERT_ARG_HANDLE_CHECKED(Object, source, 2); |
270 CONVERT_NUMBER_ARG_HANDLE_CHECKED(length_obj, 3); | 276 CONVERT_NUMBER_ARG_HANDLE_CHECKED(length_obj, 3); |
271 | 277 |
272 RUNTIME_ASSERT(arrayId >= Runtime::ARRAY_ID_FIRST && | 278 RUNTIME_ASSERT(arrayId >= Runtime::ARRAY_ID_FIRST && |
273 arrayId <= Runtime::ARRAY_ID_LAST); | 279 arrayId <= Runtime::ARRAY_ID_LAST); |
274 | 280 |
275 ExternalArrayType array_type = kExternalInt8Array; // Bogus initialization. | 281 ExternalArrayType array_type = kExternalInt8Array; // Bogus initialization. |
276 size_t element_size = 1; // Bogus initialization. | 282 size_t element_size = 1; // Bogus initialization. |
| 283 ElementsKind external_elements_kind = |
| 284 EXTERNAL_INT8_ELEMENTS; // Bogus intialization. |
277 ElementsKind fixed_elements_kind = INT8_ELEMENTS; // Bogus initialization. | 285 ElementsKind fixed_elements_kind = INT8_ELEMENTS; // Bogus initialization. |
278 Runtime::ArrayIdToTypeAndSize(arrayId, &array_type, &fixed_elements_kind, | 286 Runtime::ArrayIdToTypeAndSize(arrayId, &array_type, &external_elements_kind, |
279 &element_size); | 287 &fixed_elements_kind, &element_size); |
280 | 288 |
281 RUNTIME_ASSERT(holder->map()->elements_kind() == fixed_elements_kind); | 289 RUNTIME_ASSERT(holder->map()->elements_kind() == fixed_elements_kind); |
282 | 290 |
283 Handle<JSArrayBuffer> buffer = isolate->factory()->NewJSArrayBuffer(); | 291 Handle<JSArrayBuffer> buffer = isolate->factory()->NewJSArrayBuffer(); |
284 size_t length = 0; | 292 size_t length = 0; |
285 if (source->IsJSTypedArray() && | 293 if (source->IsJSTypedArray() && |
286 JSTypedArray::cast(*source)->type() == array_type) { | 294 JSTypedArray::cast(*source)->type() == array_type) { |
287 length_obj = handle(JSTypedArray::cast(*source)->length(), isolate); | 295 length_obj = handle(JSTypedArray::cast(*source)->length(), isolate); |
288 length = JSTypedArray::cast(*source)->length_value(); | 296 length = JSTypedArray::cast(*source)->length_value(); |
289 } else { | 297 } else { |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
325 isolate, NewRangeError(MessageTemplate::kInvalidArrayBufferLength)); | 333 isolate, NewRangeError(MessageTemplate::kInvalidArrayBufferLength)); |
326 } | 334 } |
327 | 335 |
328 holder->set_buffer(*buffer); | 336 holder->set_buffer(*buffer); |
329 holder->set_byte_offset(Smi::FromInt(0)); | 337 holder->set_byte_offset(Smi::FromInt(0)); |
330 Handle<Object> byte_length_obj( | 338 Handle<Object> byte_length_obj( |
331 isolate->factory()->NewNumberFromSize(byte_length)); | 339 isolate->factory()->NewNumberFromSize(byte_length)); |
332 holder->set_byte_length(*byte_length_obj); | 340 holder->set_byte_length(*byte_length_obj); |
333 holder->set_length(*length_obj); | 341 holder->set_length(*length_obj); |
334 | 342 |
335 Handle<FixedTypedArrayBase> elements = | 343 Handle<ExternalArray> elements = isolate->factory()->NewExternalArray( |
336 isolate->factory()->NewFixedTypedArrayWithExternalPointer( | 344 static_cast<int>(length), array_type, |
337 static_cast<int>(length), array_type, | 345 static_cast<uint8_t*>(buffer->backing_store())); |
338 static_cast<uint8_t*>(buffer->backing_store())); | 346 Handle<Map> map = |
339 holder->set_elements(*elements); | 347 JSObject::GetElementsTransitionMap(holder, external_elements_kind); |
| 348 JSObject::SetMapAndElements(holder, map, elements); |
340 | 349 |
341 if (source->IsJSTypedArray()) { | 350 if (source->IsJSTypedArray()) { |
342 Handle<JSTypedArray> typed_array(JSTypedArray::cast(*source)); | 351 Handle<JSTypedArray> typed_array(JSTypedArray::cast(*source)); |
343 | 352 |
344 if (typed_array->type() == holder->type()) { | 353 if (typed_array->type() == holder->type()) { |
345 uint8_t* backing_store = | 354 uint8_t* backing_store = |
346 static_cast<uint8_t*>(typed_array->GetBuffer()->backing_store()); | 355 static_cast<uint8_t*>(typed_array->GetBuffer()->backing_store()); |
347 size_t source_byte_offset = | 356 size_t source_byte_offset = |
348 NumberToSize(isolate, typed_array->byte_offset()); | 357 NumberToSize(isolate, typed_array->byte_offset()); |
349 memcpy(buffer->backing_store(), backing_store + source_byte_offset, | 358 memcpy(buffer->backing_store(), backing_store + source_byte_offset, |
(...skipping 390 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
740 DATA_VIEW_SETTER(Uint16, uint16_t) | 749 DATA_VIEW_SETTER(Uint16, uint16_t) |
741 DATA_VIEW_SETTER(Int16, int16_t) | 750 DATA_VIEW_SETTER(Int16, int16_t) |
742 DATA_VIEW_SETTER(Uint32, uint32_t) | 751 DATA_VIEW_SETTER(Uint32, uint32_t) |
743 DATA_VIEW_SETTER(Int32, int32_t) | 752 DATA_VIEW_SETTER(Int32, int32_t) |
744 DATA_VIEW_SETTER(Float32, float) | 753 DATA_VIEW_SETTER(Float32, float) |
745 DATA_VIEW_SETTER(Float64, double) | 754 DATA_VIEW_SETTER(Float64, double) |
746 | 755 |
747 #undef DATA_VIEW_SETTER | 756 #undef DATA_VIEW_SETTER |
748 } // namespace internal | 757 } // namespace internal |
749 } // namespace v8 | 758 } // namespace v8 |
OLD | NEW |