Chromium Code Reviews| Index: src/builtins/builtins-typedarray.cc |
| diff --git a/src/builtins/builtins-typedarray.cc b/src/builtins/builtins-typedarray.cc |
| index e394df4e7abfe2d357dc6dc2fbeb649010996243..f91dfccd4a719cad91c824ce841016fca8527121 100644 |
| --- a/src/builtins/builtins-typedarray.cc |
| +++ b/src/builtins/builtins-typedarray.cc |
| @@ -167,5 +167,86 @@ void Builtins::Generate_TypedArrayPrototypeKeys( |
| state, "%TypedArray%.prototype.keys()"); |
| } |
| +namespace { |
| + |
| +MaybeHandle<JSTypedArray> ValiadateTypedArray(Isolate* isolate, |
| + Handle<Object> receiver, |
| + const char* method_name) { |
| + if (V8_UNLIKELY(!receiver->IsJSTypedArray())) { |
| + const MessageTemplate::Template message = MessageTemplate::kNotTypedArray; |
| + THROW_NEW_ERROR(isolate, NewTypeError(message), JSTypedArray); |
| + } |
| + |
| + Handle<JSTypedArray> array = Handle<JSTypedArray>::cast(receiver); |
| + if (V8_UNLIKELY(array->WasNeutered())) { |
| + const MessageTemplate::Template message = MessageTemplate::kNotTypedArray; |
| + Handle<String> operation = |
| + isolate->factory()->NewStringFromAsciiChecked(method_name); |
| + THROW_NEW_ERROR(isolate, NewTypeError(message, operation), JSTypedArray); |
| + } |
| + |
| + return array; |
| +} |
| +} // namespace |
| + |
| +BUILTIN(TypedArrayPrototypeCopyWithin) { |
| + HandleScope scope(isolate); |
| + |
| + Handle<JSTypedArray> array; |
| + const char* method = "%TypedArray%.prototype.copyWithin"; |
| + ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
| + isolate, array, ValiadateTypedArray(isolate, args.receiver(), method)); |
| + |
| + int64_t len = array->length_value(); |
| + int64_t to = 0; |
| + int64_t from = 0; |
| + int64_t final = len; |
| + |
| + if (V8_LIKELY(args.length() > 1)) { |
| + Handle<Object> target_int; |
| + ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
| + isolate, target_int, Object::ToInteger(isolate, args.at<Object>(1))); |
| + int64_t relative = static_cast<int64_t>(target_int->Number()); |
|
Benedikt Meurer
2017/02/06 14:35:23
I think this is wrong for infinities (also below).
Camillo Bruni
2017/02/06 14:43:35
nit: only if you feel motivated ;) could you add a
caitp
2017/02/06 14:44:40
That's a good point, didn't realize ToInteger() ke
caitp
2017/02/06 14:57:12
Because of the issue with -Infiity/+Infinity, we'd
caitp
2017/02/06 16:27:53
Tests added, and I've tried to add a new helper fo
|
| + |
| + to = relative < 0 ? std::max<int64_t>(relative + len, 0) |
| + : std::min<int64_t>(relative, len); |
| + |
| + if (args.length() > 2) { |
| + Handle<Object> start; |
| + Handle<Object> end; |
| + ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
| + isolate, start, Object::ToInteger(isolate, args.at<Object>(2))); |
| + relative = static_cast<int64_t>(start->Number()); |
|
Camillo Bruni
2017/02/06 14:43:35
ditto.
|
| + from = relative < 0 ? std::max<int64_t>(relative + len, 0) |
| + : std::min<int64_t>(relative, len); |
| + |
| + end = args.atOrUndefined(isolate, 3); |
| + if (!end->IsUndefined(isolate)) { |
| + ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, end, |
| + Object::ToInteger(isolate, end)); |
| + relative = static_cast<int64_t>(end->Number()); |
|
Camillo Bruni
2017/02/06 14:43:35
ditto.
|
| + |
| + final = relative < 0 ? std::max<int64_t>(relative + len, 0) |
| + : std::min<int64_t>(relative, len); |
| + } |
| + } |
| + } |
| + |
| + int64_t count = std::min<int64_t>(final - from, len - to); |
| + if (count <= 0) return *array; |
| + |
| + Handle<FixedTypedArrayBase> elements( |
| + FixedTypedArrayBase::cast(array->elements())); |
| + size_t element_size = array->element_size(); |
| + to = to * element_size; |
| + from = from * element_size; |
| + count = count * element_size; |
| + |
| + uint8_t* data = static_cast<uint8_t*>(elements->DataPtr()); |
| + std::memmove(data + to, data + from, count); |
| + |
| + return *array; |
| +} |
| + |
| } // namespace internal |
| } // namespace v8 |