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, | |
Camillo Bruni
2017/03/22 22:08:25
See my comments further down, I don't think we nee
Camillo Bruni
2017/03/23 11:06:16
ignore this for now :)
| |
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(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()->NewNumber(length); | |
Camillo Bruni
2017/03/22 22:08:25
argv[0] = isolate->factory()->NewNumberFromInt64(l
Choongwoo Han
2017/03/23 17:38:48
Done.
| |
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 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
199 if (V8_UNLIKELY(array->WasNeutered())) return Smi::FromInt(-1); | 276 if (V8_UNLIKELY(array->WasNeutered())) return Smi::FromInt(-1); |
200 | 277 |
201 Handle<Object> search_element = args.atOrUndefined(isolate, 1); | 278 Handle<Object> search_element = args.atOrUndefined(isolate, 1); |
202 ElementsAccessor* elements = array->GetElementsAccessor(); | 279 ElementsAccessor* elements = array->GetElementsAccessor(); |
203 Maybe<int64_t> result = elements->LastIndexOfValue( | 280 Maybe<int64_t> result = elements->LastIndexOfValue( |
204 isolate, array, search_element, static_cast<uint32_t>(index)); | 281 isolate, array, search_element, static_cast<uint32_t>(index)); |
205 MAYBE_RETURN(result, isolate->heap()->exception()); | 282 MAYBE_RETURN(result, isolate->heap()->exception()); |
206 return *isolate->factory()->NewNumberFromInt64(result.FromJust()); | 283 return *isolate->factory()->NewNumberFromInt64(result.FromJust()); |
207 } | 284 } |
208 | 285 |
286 BUILTIN(TypedArrayPrototypeSlice) { | |
287 HandleScope scope(isolate); | |
288 | |
289 Handle<JSTypedArray> array; | |
290 const char* method = "%TypedArray%.prototype.slice"; | |
291 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( | |
292 isolate, array, JSTypedArray::Validate(isolate, args.receiver(), method)); | |
293 | |
294 int64_t len = array->length_value(); | |
295 int64_t start = 0; | |
296 int64_t end = len; | |
297 { | |
298 Handle<Object> num = args.atOrUndefined(isolate, 1); | |
299 if (!num->IsUndefined(isolate)) { | |
300 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, num, | |
301 Object::ToInteger(isolate, num)); | |
302 start = CapRelativeIndex(num, 0, len); | |
303 | |
304 num = args.atOrUndefined(isolate, 2); | |
305 if (!num->IsUndefined(isolate)) { | |
306 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, num, | |
307 Object::ToInteger(isolate, num)); | |
308 end = CapRelativeIndex(num, 0, len); | |
309 } | |
310 } | |
311 } | |
312 | |
313 int64_t count = std::max<int64_t>(end - start, 0); | |
314 | |
315 Handle<JSTypedArray> result_array; | |
Camillo Bruni
2017/03/22 22:08:25
Push everything below here into the ElementsAccess
Choongwoo Han
2017/03/22 22:29:55
But, I guess we still need Execution::New call. sp
Camillo Bruni
2017/03/23 11:06:16
You are right... it was too late yesterday night a
| |
316 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( | |
317 isolate, result_array, | |
318 TypedArraySpeciesCreateByLength(isolate, array, method, count)); | |
319 | |
320 // TODO(cwhan.tunz): neutering check of the result_array should be done in | |
321 // TypedArraySpeciesCreate, but currently ValidateTypedArray does not throw | |
322 // for neutered buffer, so this is a temporary neutering check for the result | |
323 // array | |
324 if (V8_UNLIKELY(result_array->WasNeutered())) return *result_array; | |
325 | |
326 // TODO(cwhan.tunz): should throw. | |
327 if (V8_UNLIKELY(array->WasNeutered())) return *result_array; | |
328 | |
329 if (count == 0) return *result_array; | |
330 | |
331 ElementsAccessor* accessor = array->GetElementsAccessor(); | |
332 return *accessor->Slice(array, static_cast<uint32_t>(start), | |
333 static_cast<uint32_t>(end), result_array); | |
334 } | |
335 | |
209 } // namespace internal | 336 } // namespace internal |
210 } // namespace v8 | 337 } // namespace v8 |
OLD | NEW |