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

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

Issue 2744283002: [typedarrays] Implement %TypedArray%.prototype.lastIndexOf in C++ (Closed)
Patch Set: Correct integral value check Created 3 years, 9 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/elements.h » ('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 ce534e9b3979887923c94fe3675377d0762331e9..4ff840376a6cc7ac1d730cab12c028150075407b 100644
--- a/src/builtins/builtins-typedarray.cc
+++ b/src/builtins/builtins-typedarray.cc
@@ -585,5 +585,39 @@ BUILTIN(TypedArrayPrototypeIndexOf) {
return *isolate->factory()->NewNumberFromInt64(result.FromJust());
}
+BUILTIN(TypedArrayPrototypeLastIndexOf) {
+ HandleScope scope(isolate);
+
+ Handle<JSTypedArray> array;
+ const char* method = "%TypedArray%.prototype.lastIndexOf";
+ ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
+ isolate, array, JSTypedArray::Validate(isolate, args.receiver(), method));
+
+ int64_t len = array->length_value();
+ if (len == 0) return Smi::FromInt(-1);
+
+ int64_t index = len - 1;
+ if (args.length() > 2) {
+ Handle<Object> num;
+ ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
+ isolate, num, Object::ToInteger(isolate, args.at<Object>(2)));
+ // Set a negative value (-1) for returning -1 if num is negative and
+ // len + num is still negative. Upper bound is len - 1.
+ index = std::min<int64_t>(CapRelativeIndex(num, -1, len), len - 1);
+ }
+
+ if (index < 0) return Smi::FromInt(-1);
+
+ // TODO(cwhan.tunz): throw. See the above comment in CopyWithin.
+ if (V8_UNLIKELY(array->WasNeutered())) return Smi::FromInt(-1);
+
+ Handle<Object> search_element = args.atOrUndefined(isolate, 1);
+ ElementsAccessor* elements = array->GetElementsAccessor();
+ Maybe<int64_t> result = elements->LastIndexOfValue(
+ isolate, array, search_element, static_cast<uint32_t>(index));
+ MAYBE_RETURN(result, isolate->heap()->exception());
+ return *isolate->factory()->NewNumberFromInt64(result.FromJust());
+}
+
} // namespace internal
} // namespace v8
« no previous file with comments | « src/builtins/builtins.h ('k') | src/elements.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698