OLD | NEW |
1 // Copyright 2016 the V8 project authors. All rights reserved. | 1 // Copyright 2016 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/builtins/builtins-utils.h" | 5 #include "src/builtins/builtins-utils.h" |
6 #include "src/builtins/builtins.h" | 6 #include "src/builtins/builtins.h" |
7 #include "src/counters.h" | 7 #include "src/counters.h" |
8 #include "src/elements.h" | 8 #include "src/elements.h" |
9 #include "src/objects-inl.h" | 9 #include "src/objects-inl.h" |
10 | 10 |
(...skipping 23 matching lines...) Expand all Loading... |
34 // +Infinity / -Infinity | 34 // +Infinity / -Infinity |
35 DCHECK(!std::isnan(fp)); | 35 DCHECK(!std::isnan(fp)); |
36 return fp < 0 ? minimum : maximum; | 36 return fp < 0 ? minimum : maximum; |
37 } | 37 } |
38 relative = static_cast<int64_t>(fp); | 38 relative = static_cast<int64_t>(fp); |
39 } | 39 } |
40 return relative < 0 ? std::max<int64_t>(relative + maximum, minimum) | 40 return relative < 0 ? std::max<int64_t>(relative + maximum, minimum) |
41 : std::min<int64_t>(relative, maximum); | 41 : std::min<int64_t>(relative, maximum); |
42 } | 42 } |
43 | 43 |
| 44 // ES7 section 22.2.4.6 TypedArrayCreate ( constructor, argumentList ) |
| 45 MaybeHandle<JSTypedArray> TypedArrayCreate(Isolate* isolate, |
| 46 Handle<JSFunction> default_ctor, |
| 47 int argc, Handle<Object>* argv, |
| 48 const char* method_name) { |
| 49 // 1. Let newTypedArray be ? Construct(constructor, argumentList). |
| 50 Handle<Object> new_obj; |
| 51 ASSIGN_RETURN_ON_EXCEPTION( |
| 52 isolate, new_obj, Execution::New(default_ctor, argc, argv), JSTypedArray); |
| 53 |
| 54 // 2. Perform ? ValidateTypedArray(newTypedArray). |
| 55 Handle<JSTypedArray> new_array; |
| 56 ASSIGN_RETURN_ON_EXCEPTION( |
| 57 isolate, new_array, JSTypedArray::Validate(isolate, new_obj, method_name), |
| 58 JSTypedArray); |
| 59 |
| 60 // 3. If argumentList is a List of a single Number, then |
| 61 // If newTypedArray.[[ArrayLength]] < size, throw a TypeError exception. |
| 62 DCHECK_IMPLIES(argc == 1, argv[0]->IsSmi()); |
| 63 if (argc == 1 && new_array->length_value() < argv[0]->Number()) { |
| 64 const MessageTemplate::Template message = |
| 65 MessageTemplate::kTypedArrayTooShort; |
| 66 THROW_NEW_ERROR(isolate, NewTypeError(message), JSTypedArray); |
| 67 } |
| 68 |
| 69 // 4. Return newTypedArray. |
| 70 return new_array; |
| 71 } |
| 72 |
| 73 // ES7 section 22.2.4.7 TypedArraySpeciesCreate ( exemplar, argumentList ) |
| 74 MaybeHandle<JSTypedArray> TypedArraySpeciesCreate(Isolate* isolate, |
| 75 Handle<JSTypedArray> exemplar, |
| 76 int argc, |
| 77 Handle<Object>* argv, |
| 78 const char* method_name) { |
| 79 // 1. Assert: exemplar is an Object that has a [[TypedArrayName]] internal |
| 80 // slot. |
| 81 DCHECK(exemplar->IsJSTypedArray()); |
| 82 |
| 83 // 2. Let defaultConstructor be the intrinsic object listed in column one of |
| 84 // Table 51 for exemplar.[[TypedArrayName]]. |
| 85 Handle<JSFunction> default_ctor = isolate->uint8_array_fun(); |
| 86 switch (exemplar->type()) { |
| 87 #define TYPED_ARRAY_CTOR(Type, type, TYPE, ctype, size) \ |
| 88 case kExternal##Type##Array: { \ |
| 89 default_ctor = isolate->type##_array_fun(); \ |
| 90 break; \ |
| 91 } |
| 92 |
| 93 TYPED_ARRAYS(TYPED_ARRAY_CTOR) |
| 94 #undef TYPED_ARRAY_CTOR |
| 95 default: |
| 96 UNREACHABLE(); |
| 97 } |
| 98 |
| 99 // 3. Let constructor be ? SpeciesConstructor(exemplar, defaultConstructor). |
| 100 Handle<Object> ctor; |
| 101 ASSIGN_RETURN_ON_EXCEPTION( |
| 102 isolate, ctor, |
| 103 Object::SpeciesConstructor(isolate, exemplar, default_ctor), |
| 104 JSTypedArray); |
| 105 |
| 106 // 4. Return ? TypedArrayCreate(constructor, argumentList). |
| 107 return TypedArrayCreate(isolate, Handle<JSFunction>::cast(ctor), argc, argv, |
| 108 method_name); |
| 109 } |
| 110 |
| 111 MaybeHandle<JSTypedArray> TypedArraySpeciesCreateByLength( |
| 112 Isolate* isolate, Handle<JSTypedArray> exemplar, const char* method_name, |
| 113 int64_t length) { |
| 114 const int argc = 1; |
| 115 ScopedVector<Handle<Object>> argv(argc); |
| 116 argv[0] = isolate->factory()->NewNumberFromInt64(length); |
| 117 return TypedArraySpeciesCreate(isolate, exemplar, argc, argv.start(), |
| 118 method_name); |
| 119 } |
| 120 |
44 } // namespace | 121 } // namespace |
45 | 122 |
46 BUILTIN(TypedArrayPrototypeCopyWithin) { | 123 BUILTIN(TypedArrayPrototypeCopyWithin) { |
47 HandleScope scope(isolate); | 124 HandleScope scope(isolate); |
48 | 125 |
49 Handle<JSTypedArray> array; | 126 Handle<JSTypedArray> array; |
50 const char* method = "%TypedArray%.prototype.copyWithin"; | 127 const char* method = "%TypedArray%.prototype.copyWithin"; |
51 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( | 128 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
52 isolate, array, JSTypedArray::Validate(isolate, args.receiver(), method)); | 129 isolate, array, JSTypedArray::Validate(isolate, args.receiver(), method)); |
53 | 130 |
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
263 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( | 340 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
264 isolate, array, JSTypedArray::Validate(isolate, args.receiver(), method)); | 341 isolate, array, JSTypedArray::Validate(isolate, args.receiver(), method)); |
265 | 342 |
266 if (V8_UNLIKELY(array->WasNeutered())) return *array; | 343 if (V8_UNLIKELY(array->WasNeutered())) return *array; |
267 | 344 |
268 ElementsAccessor* elements = array->GetElementsAccessor(); | 345 ElementsAccessor* elements = array->GetElementsAccessor(); |
269 elements->Reverse(*array); | 346 elements->Reverse(*array); |
270 return *array; | 347 return *array; |
271 } | 348 } |
272 | 349 |
| 350 BUILTIN(TypedArrayPrototypeSlice) { |
| 351 HandleScope scope(isolate); |
| 352 |
| 353 Handle<JSTypedArray> array; |
| 354 const char* method = "%TypedArray%.prototype.slice"; |
| 355 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
| 356 isolate, array, JSTypedArray::Validate(isolate, args.receiver(), method)); |
| 357 |
| 358 int64_t len = array->length_value(); |
| 359 int64_t start = 0; |
| 360 int64_t end = len; |
| 361 { |
| 362 Handle<Object> num = args.atOrUndefined(isolate, 1); |
| 363 if (!num->IsUndefined(isolate)) { |
| 364 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, num, |
| 365 Object::ToInteger(isolate, num)); |
| 366 start = CapRelativeIndex(num, 0, len); |
| 367 |
| 368 num = args.atOrUndefined(isolate, 2); |
| 369 if (!num->IsUndefined(isolate)) { |
| 370 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, num, |
| 371 Object::ToInteger(isolate, num)); |
| 372 end = CapRelativeIndex(num, 0, len); |
| 373 } |
| 374 } |
| 375 } |
| 376 |
| 377 int64_t count = std::max<int64_t>(end - start, 0); |
| 378 |
| 379 Handle<JSTypedArray> result_array; |
| 380 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
| 381 isolate, result_array, |
| 382 TypedArraySpeciesCreateByLength(isolate, array, method, count)); |
| 383 |
| 384 // TODO(cwhan.tunz): neutering check of the result_array should be done in |
| 385 // TypedArraySpeciesCreate, but currently ValidateTypedArray does not throw |
| 386 // for neutered buffer, so this is a temporary neutering check for the result |
| 387 // array |
| 388 if (V8_UNLIKELY(result_array->WasNeutered())) return *result_array; |
| 389 |
| 390 // TODO(cwhan.tunz): should throw. |
| 391 if (V8_UNLIKELY(array->WasNeutered())) return *result_array; |
| 392 |
| 393 if (count == 0) return *result_array; |
| 394 |
| 395 ElementsAccessor* accessor = array->GetElementsAccessor(); |
| 396 return *accessor->Slice(array, static_cast<uint32_t>(start), |
| 397 static_cast<uint32_t>(end), result_array); |
| 398 } |
| 399 |
273 } // namespace internal | 400 } // namespace internal |
274 } // namespace v8 | 401 } // namespace v8 |
OLD | NEW |