Commit b2efe57c authored by cwhan.tunz's avatar cwhan.tunz Committed by Commit bot

[typedarrays] Move %TypedArray%.prototype.indexOf to C++

- Remove TypedArrayIndexOf in src/js/typedarray.js
- Implement it to C++ using the IndexOfValue in ElementsAccessor
- Add buffer neutering check also for %TypedArray%.prototype.includes

BUG=v8:5929

Review-Url: https://codereview.chromium.org/2733193002
Cr-Commit-Position: refs/heads/master@{#43741}
parent 07ee0b17
......@@ -2617,6 +2617,8 @@ void Genesis::InitializeGlobal(Handle<JSGlobalObject> global_object,
Builtins::kTypedArrayPrototypeCopyWithin, 2, false);
SimpleInstallFunction(prototype, "includes",
Builtins::kTypedArrayPrototypeIncludes, 1, false);
SimpleInstallFunction(prototype, "indexOf",
Builtins::kTypedArrayPrototypeIndexOf, 1, false);
}
{ // -- T y p e d A r r a y s
......
......@@ -532,7 +532,6 @@ BUILTIN(TypedArrayPrototypeIncludes) {
if (args.length() < 2) return isolate->heap()->false_value();
int64_t len = array->length_value();
if (len == 0) return isolate->heap()->false_value();
int64_t index = 0;
......@@ -543,13 +542,48 @@ BUILTIN(TypedArrayPrototypeIncludes) {
index = CapRelativeIndex(num, 0, len);
}
// TODO(cwhan.tunz): throw. See the above comment in CopyWithin.
if (V8_UNLIKELY(array->WasNeutered())) return isolate->heap()->false_value();
Handle<Object> search_element = args.at<Object>(1);
ElementsAccessor* elements = array->GetElementsAccessor();
Maybe<bool> result = elements->IncludesValue(isolate, array, search_element,
static_cast<uint32_t>(index),
static_cast<uint32_t>(len));
MAYBE_RETURN(result, isolate->heap()->exception());
return *isolate->factory()->ToBoolean(result.FromJust());
}
BUILTIN(TypedArrayPrototypeIndexOf) {
HandleScope scope(isolate);
Handle<JSTypedArray> array;
const char* method = "%TypedArray%.prototype.indexOf";
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 = 0;
if (args.length() > 2) {
Handle<Object> num;
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
isolate, num, Object::ToInteger(isolate, args.at<Object>(2)));
index = CapRelativeIndex(num, 0, len);
}
// TODO(cwhan.tunz): throw. See the above comment in CopyWithin.
if (V8_UNLIKELY(array->WasNeutered())) return Smi::FromInt(-1);
Handle<Object> search_element = args.at<Object>(1);
ElementsAccessor* elements = array->GetElementsAccessor();
Maybe<int64_t> result = elements->IndexOfValue(isolate, array, search_element,
static_cast<uint32_t>(index),
static_cast<uint32_t>(len));
MAYBE_RETURN(result, isolate->heap()->exception());
return *isolate->factory()->NewNumberFromInt64(result.FromJust());
}
} // namespace internal
} // namespace v8
......@@ -825,6 +825,8 @@ class Isolate;
CPP(TypedArrayPrototypeCopyWithin) \
/* ES7 #sec-%typedarray%.prototype.includes */ \
CPP(TypedArrayPrototypeIncludes) \
/* ES6 #sec-%typedarray%.prototype.indexof */ \
CPP(TypedArrayPrototypeIndexOf) \
\
/* Wasm */ \
TFS(WasmStackGuard, BUILTIN, kNoExtraICState, WasmRuntimeCall, 1) \
......
......@@ -514,40 +514,6 @@ function TypedArraySort(comparefn) {
}
// ES6 section 22.2.3.13
function TypedArrayIndexOf(element, index) {
if (!IS_TYPEDARRAY(this)) throw %make_type_error(kNotTypedArray);
var length = %_TypedArrayGetLength(this);
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);
// ES6 section 22.2.3.16
function TypedArrayLastIndexOf(element, index) {
if (!IS_TYPEDARRAY(this)) throw %make_type_error(kNotTypedArray);
......@@ -780,7 +746,6 @@ utils.InstallFunctions(GlobalTypedArray.prototype, DONT_ENUM, [
"filter", TypedArrayFilter,
"find", TypedArrayFind,
"findIndex", TypedArrayFindIndex,
"indexOf", TypedArrayIndexOf,
"join", TypedArrayJoin,
"lastIndexOf", TypedArrayLastIndexOf,
"forEach", TypedArrayForEach,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment