Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(84)

Unified Diff: src/builtins/builtins-typedarray.cc

Issue 2671233002: [typedarrays] move %TypedArray%.prototype.copyWithin to C++ (Closed)
Patch Set: make win32 happy Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/builtins/builtins.h ('k') | src/js/array.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/builtins/builtins-typedarray.cc
diff --git a/src/builtins/builtins-typedarray.cc b/src/builtins/builtins-typedarray.cc
index e394df4e7abfe2d357dc6dc2fbeb649010996243..14d09ff2ef850abda837b2ad70e92f79fdc0d461 100644
--- a/src/builtins/builtins-typedarray.cc
+++ b/src/builtins/builtins-typedarray.cc
@@ -167,5 +167,95 @@ 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;
+}
+
+int64_t CapRelativeIndex(Handle<Object> num, int64_t minimum, int64_t maximum) {
+ int64_t relative;
+ if (V8_LIKELY(num->IsSmi())) {
+ relative = Smi::cast(*num)->value();
+ } else {
+ DCHECK(num->IsHeapNumber());
+ double fp = HeapNumber::cast(*num)->value();
+ if (V8_UNLIKELY(!std::isfinite(fp))) {
+ // +Infinity / -Infinity
+ DCHECK(!std::isnan(fp));
+ return fp < 0 ? minimum : maximum;
+ }
+ relative = static_cast<int64_t>(fp);
+ }
+ return relative < 0 ? std::max<int64_t>(relative + maximum, minimum)
+ : std::min<int64_t>(relative, maximum);
+}
+
+} // 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));
Choongwoo Han 2017/02/12 07:38:21 Validation check in here can be violated later.
+
+ 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> num;
+ ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
+ isolate, num, Object::ToInteger(isolate, args.at<Object>(1)));
Choongwoo Han 2017/02/12 07:38:21 This ToInteger call may neuter the buf. See https:
+ to = CapRelativeIndex(num, 0, len);
+
+ if (args.length() > 2) {
+ ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
+ isolate, num, Object::ToInteger(isolate, args.at<Object>(2)));
+ from = CapRelativeIndex(num, 0, len);
+
+ Handle<Object> end = args.atOrUndefined(isolate, 3);
+ if (!end->IsUndefined(isolate)) {
+ ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, num,
+ Object::ToInteger(isolate, end));
+ final = CapRelativeIndex(num, 0, 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
« no previous file with comments | « src/builtins/builtins.h ('k') | src/js/array.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698