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

[typedarrays] Move %TypedArray%.prototype.includes to C++ builtins

- Remove TypedArrayIncludes in src/js/typedarray.js
- Implement it to C++ using the IncludesValue implementation
  in ElementsAccessor

BUG=v8:5929

Review-Url: https://codereview.chromium.org/2732823002
Cr-Commit-Position: refs/heads/master@{#43625}
parent 4951ff88
......@@ -2612,6 +2612,8 @@ void Genesis::InitializeGlobal(Handle<JSGlobalObject> global_object,
// TODO(caitp): alphasort accessors/methods
SimpleInstallFunction(prototype, "copyWithin",
Builtins::kTypedArrayPrototypeCopyWithin, 2, false);
SimpleInstallFunction(prototype, "includes",
Builtins::kTypedArrayPrototypeIncludes, 1, false);
}
{ // -- T y p e d A r r a y s
......
......@@ -6,6 +6,7 @@
#include "src/builtins/builtins.h"
#include "src/code-stub-assembler.h"
#include "src/counters.h"
#include "src/elements.h"
#include "src/objects-inl.h"
namespace v8 {
......@@ -456,5 +457,35 @@ BUILTIN(TypedArrayPrototypeCopyWithin) {
return *array;
}
BUILTIN(TypedArrayPrototypeIncludes) {
HandleScope scope(isolate);
Handle<JSTypedArray> array;
const char* method = "%TypedArray%.prototype.includes";
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
isolate, array, JSTypedArray::Validate(isolate, args.receiver(), method));
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;
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);
}
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));
return *isolate->factory()->ToBoolean(result.FromJust());
}
} // namespace internal
} // namespace v8
......@@ -823,6 +823,8 @@ class Isolate;
TFJ(TypedArrayPrototypeValues, 0) \
/* ES6 #sec-%typedarray%.prototype.copywithin */ \
CPP(TypedArrayPrototypeCopyWithin) \
/* ES7 #sec-%typedarray%.prototype.includes */ \
CPP(TypedArrayPrototypeIncludes) \
\
/* Wasm */ \
TFS(WasmStackGuard, BUILTIN, kNoExtraICState, WasmRuntimeCall, 1) \
......
......@@ -717,39 +717,6 @@ function TypedArraySlice(start, end) {
}
// ES2016 draft, section 22.2.3.14
function TypedArrayIncludes(searchElement, fromIndex) {
if (!IS_TYPEDARRAY(this)) throw %make_type_error(kNotTypedArray);
var length = %_TypedArrayGetLength(this);
if (length === 0) return false;
var n = TO_INTEGER(fromIndex);
var k;
if (n >= 0) {
k = n;
} else {
k = length + n;
if (k < 0) {
k = 0;
}
}
while (k < length) {
var elementK = this[k];
if (%SameValueZero(searchElement, elementK)) {
return true;
}
++k;
}
return false;
}
%FunctionSetLength(TypedArrayIncludes, 1);
// ES6 draft 08-24-14, section 22.2.2.2
function TypedArrayOf() {
var length = arguments.length;
......@@ -830,7 +797,6 @@ utils.InstallFunctions(GlobalTypedArray.prototype, DONT_ENUM, [
"filter", TypedArrayFilter,
"find", TypedArrayFind,
"findIndex", TypedArrayFindIndex,
"includes", TypedArrayIncludes,
"indexOf", TypedArrayIndexOf,
"join", TypedArrayJoin,
"lastIndexOf", TypedArrayLastIndexOf,
......
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