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