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

Unified Diff: src/js/typedarray.js

Issue 2243523002: [builtins] IndexOf/LastIndexOf implementation for typedarrays (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Ensure -0 is never returned Created 4 years, 4 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 | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/js/typedarray.js
diff --git a/src/js/typedarray.js b/src/js/typedarray.js
index 204e0918b24a45550be3440ac389cd2178751838..46e238789d7b6dc2d78e71fcab1b1bc8f69e24fe 100644
--- a/src/js/typedarray.js
+++ b/src/js/typedarray.js
@@ -28,9 +28,7 @@ var InnerArrayFilter;
var InnerArrayFind;
var InnerArrayFindIndex;
var InnerArrayForEach;
-var InnerArrayIndexOf;
var InnerArrayJoin;
-var InnerArrayLastIndexOf;
var InnerArrayReduce;
var InnerArrayReduceRight;
var InnerArraySome;
@@ -79,9 +77,7 @@ utils.Import(function(from) {
InnerArrayFind = from.InnerArrayFind;
InnerArrayFindIndex = from.InnerArrayFindIndex;
InnerArrayForEach = from.InnerArrayForEach;
- InnerArrayIndexOf = from.InnerArrayIndexOf;
adamk 2016/08/15 20:38:54 I suspect these were the last importers of these f
InnerArrayJoin = from.InnerArrayJoin;
- InnerArrayLastIndexOf = from.InnerArrayLastIndexOf;
InnerArrayReduce = from.InnerArrayReduce;
InnerArrayReduceRight = from.InnerArrayReduceRight;
InnerArraySome = from.InnerArraySome;
@@ -572,7 +568,31 @@ function TypedArrayIndexOf(element, index) {
if (!IS_TYPEDARRAY(this)) throw %make_type_error(kNotTypedArray);
var length = %_TypedArrayGetLength(this);
- return InnerArrayIndexOf(this, element, index, length);
+
+ if (length === 0) return -1;
+ if (!IS_NUMBER(element)) return -1;
+ var n = TO_INTEGER(index);
+
+ var k;
+ if (n === 0) {
+ k = 0;
+ } else if (n > 0) {
+ k = n;
+ } else {
+ k = length + n;
+ if (k < 0) {
+ k = 0;
+ }
+ }
+
+ while (k < length) {
+ var elementK = this[k];
+ if (element === elementK) {
+ return k;
+ }
+ ++k;
+ }
+ return -1;
}
%FunctionSetLength(TypedArrayIndexOf, 1);
@@ -583,8 +603,36 @@ function TypedArrayLastIndexOf(element, index) {
var length = %_TypedArrayGetLength(this);
- return InnerArrayLastIndexOf(this, element, index, length,
- arguments.length);
+ if (length === 0) return -1;
+ if (!IS_NUMBER(element)) return -1;
+ var n;
+ if (arguments.length < 2) {
+ n = length - 1;
+ } else {
+ n = TO_INTEGER(index);
+ }
+
+ var k;
+ if (n >= 0) {
+ if (length <= n) {
+ k = length - 1;
+ } else if (n === 0) {
+ k = 0;
+ } else {
+ k = n;
+ }
+ } else {
+ k = length + n;
+ }
+
+ while (k >= 0) {
+ var elementK = this[k];
+ if (element === elementK) {
+ return k;
+ }
+ --k;
+ }
+ return -1;
}
%FunctionSetLength(TypedArrayLastIndexOf, 1);
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698