Index: src/builtins/builtins-typedarray.cc |
diff --git a/src/builtins/builtins-typedarray.cc b/src/builtins/builtins-typedarray.cc |
index ab1ebbc69e207c354f2cab9e8d8c6caae0179578..e471465e64ce7c6edb845b1241661620ef70ae3b 100644 |
--- a/src/builtins/builtins-typedarray.cc |
+++ b/src/builtins/builtins-typedarray.cc |
@@ -167,5 +167,82 @@ void Builtins::Generate_TypedArrayPrototypeKeys( |
state, "%TypedArray%.prototype.keys()"); |
} |
+BUILTIN(TypedArrayPrototypeCopyWithin) { |
+ HandleScope scope(isolate); |
+ Handle<Object> receiver = args.receiver(); |
+ Handle<JSTypedArray> obj; |
+ |
+ { // ValiadateTypedArray |
+ if (V8_UNLIKELY(!receiver->IsJSTypedArray())) { |
+ const MessageTemplate::Template message = MessageTemplate::kNotTypedArray; |
+ THROW_NEW_ERROR_RETURN_FAILURE(isolate, NewTypeError(message)); |
+ } |
+ |
+ obj = Handle<JSTypedArray>::cast(receiver); |
+ if (V8_UNLIKELY(obj->WasNeutered())) { |
+ const MessageTemplate::Template message = |
+ MessageTemplate::kDetachedOperation; |
+ Handle<String> operation = isolate->factory()->NewStringFromAsciiChecked( |
+ "%TypedArray%.prototype.copyWithin"); |
+ THROW_NEW_ERROR_RETURN_FAILURE(isolate, NewTypeError(message, operation)); |
+ } |
+ } |
+ |
+ // Let len be O.[[ArrayLength]] |
+ size_t len = obj->length_value(); |
Benedikt Meurer
2017/02/06 03:54:33
How about using int64_t for these to avoid sprinkl
caitp
2017/02/06 14:21:13
Done
|
+ size_t to = 0; |
+ size_t from = 0; |
+ size_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()); |
+ |
+ to = relative < 0 |
+ ? static_cast<size_t>(std::max<int64_t>(relative + len, 0)) |
+ : static_cast<size_t>(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()); |
+ from = relative < 0 |
+ ? static_cast<size_t>(std::max<int64_t>(relative + len, 0)) |
+ : static_cast<size_t>(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()); |
+ |
+ final = relative < 0 |
+ ? static_cast<size_t>(std::max<int64_t>(relative + len, 0)) |
+ : static_cast<size_t>(std::min<int64_t>(relative, len)); |
+ } |
+ } |
+ } |
+ |
+ int64_t lcount = |
+ std::min(static_cast<int64_t>(final) - static_cast<int64_t>(from), |
+ static_cast<int64_t>(len) - static_cast<int64_t>(to)); |
+ if (lcount <= 0) return *obj; |
+ |
+ size_t count = static_cast<size_t>(lcount); |
+ Handle<FixedTypedArrayBase> elements( |
+ FixedTypedArrayBase::cast(obj->elements())); |
+ size_t element_size = obj->element_size(); |
+ |
+ uint8_t* data = static_cast<uint8_t*>(elements->DataPtr()); |
+ std::memmove(data + (to * element_size), data + (from * element_size), |
+ (count * element_size)); |
+ |
+ return *obj; |
+} |
+ |
} // namespace internal |
} // namespace v8 |