Commit 041a996d authored by Mike Stanton's avatar Mike Stanton Committed by Commit Bot

[Torque] More TypedArray builtins in Torque.

This CL contains find, findIndex, every and some. Now that we've
established the pattern on the torque side for iterating array
builtins, it's a very easy port, which nonetheless decreases
code size in the snapshot, w00t!

Bug: v8:8906
Change-Id: I3082d8e3e298e55733a42d6b441e5812b7f12f3d
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1496976
Commit-Queue: Michael Stanton <mvstanton@chromium.org>
Reviewed-by: 's avatarJakob Gruber <jgruber@chromium.org>
Reviewed-by: 's avatarSimon Zünd <szuend@chromium.org>
Cr-Commit-Position: refs/heads/master@{#60128}
parent 1b7eacdc
......@@ -927,11 +927,15 @@ torque_files = [
"src/builtins/string-startswith.tq",
"src/builtins/typed-array.tq",
"src/builtins/typed-array-createtypedarray.tq",
"src/builtins/typed-array-every.tq",
"src/builtins/typed-array-filter.tq",
"src/builtins/typed-array-find.tq",
"src/builtins/typed-array-findindex.tq",
"src/builtins/typed-array-foreach.tq",
"src/builtins/typed-array-reduce.tq",
"src/builtins/typed-array-reduceright.tq",
"src/builtins/typed-array-slice.tq",
"src/builtins/typed-array-some.tq",
"src/builtins/typed-array-subarray.tq",
"test/torque/test-torque.tq",
"third_party/v8/builtins/array-sort.tq",
......@@ -965,11 +969,15 @@ torque_namespaces = [
"test",
"typed-array",
"typed-array-createtypedarray",
"typed-array-every",
"typed-array-filter",
"typed-array-find",
"typed-array-findindex",
"typed-array-foreach",
"typed-array-reduce",
"typed-array-reduceright",
"typed-array-slice",
"typed-array-some",
"typed-array-subarray",
]
......
......@@ -31,66 +31,6 @@ ArrayBuiltinsAssembler::ArrayBuiltinsAssembler(
to_(this, MachineRepresentation::kTagged, SmiConstant(0)),
fully_spec_compliant_(this, {&k_, &a_, &to_}) {}
void ArrayBuiltinsAssembler::FindResultGenerator() {
a_.Bind(UndefinedConstant());
}
Node* ArrayBuiltinsAssembler::FindProcessor(Node* k_value, Node* k) {
Node* value = CallJS(CodeFactory::Call(isolate()), context(), callbackfn(),
this_arg(), k_value, k, o());
Label false_continue(this), return_true(this);
BranchIfToBooleanIsTrue(value, &return_true, &false_continue);
BIND(&return_true);
ReturnFromBuiltin(k_value);
BIND(&false_continue);
return a();
}
void ArrayBuiltinsAssembler::FindIndexResultGenerator() {
a_.Bind(SmiConstant(-1));
}
Node* ArrayBuiltinsAssembler::FindIndexProcessor(Node* k_value, Node* k) {
Node* value = CallJS(CodeFactory::Call(isolate()), context(), callbackfn(),
this_arg(), k_value, k, o());
Label false_continue(this), return_true(this);
BranchIfToBooleanIsTrue(value, &return_true, &false_continue);
BIND(&return_true);
ReturnFromBuiltin(k);
BIND(&false_continue);
return a();
}
void ArrayBuiltinsAssembler::SomeResultGenerator() {
a_.Bind(FalseConstant());
}
Node* ArrayBuiltinsAssembler::SomeProcessor(Node* k_value, Node* k) {
Node* value = CallJS(CodeFactory::Call(isolate()), context(), callbackfn(),
this_arg(), k_value, k, o());
Label false_continue(this), return_true(this);
BranchIfToBooleanIsTrue(value, &return_true, &false_continue);
BIND(&return_true);
ReturnFromBuiltin(TrueConstant());
BIND(&false_continue);
return a();
}
void ArrayBuiltinsAssembler::EveryResultGenerator() {
a_.Bind(TrueConstant());
}
Node* ArrayBuiltinsAssembler::EveryProcessor(Node* k_value, Node* k) {
Node* value = CallJS(CodeFactory::Call(isolate()), context(), callbackfn(),
this_arg(), k_value, k, o());
Label true_continue(this), return_false(this);
BranchIfToBooleanIsTrue(value, &true_continue, &return_false);
BIND(&return_false);
ReturnFromBuiltin(FalseConstant());
BIND(&true_continue);
return a();
}
void ArrayBuiltinsAssembler::TypedArrayMapResultGenerator() {
// 6. Let A be ? TypedArraySpeciesCreate(O, len).
TNode<JSTypedArray> original_array = CAST(o());
......@@ -914,81 +854,6 @@ TF_BUILTIN(ArrayFrom, ArrayPopulatorAssembler) {
args.PopAndReturn(array.value());
}
// ES #sec-get-%typedarray%.prototype.find
TF_BUILTIN(TypedArrayPrototypeFind, ArrayBuiltinsAssembler) {
TNode<IntPtrT> argc =
ChangeInt32ToIntPtr(Parameter(Descriptor::kJSActualArgumentsCount));
CodeStubArguments args(this, argc);
TNode<Context> context = CAST(Parameter(Descriptor::kContext));
TNode<Object> receiver = args.GetReceiver();
Node* callbackfn = args.GetOptionalArgumentValue(0);
Node* this_arg = args.GetOptionalArgumentValue(1);
InitIteratingArrayBuiltinBody(context, receiver, callbackfn, this_arg, argc);
GenerateIteratingTypedArrayBuiltinBody(
"%TypedArray%.prototype.find",
&ArrayBuiltinsAssembler::FindResultGenerator,
&ArrayBuiltinsAssembler::FindProcessor,
&ArrayBuiltinsAssembler::NullPostLoopAction);
}
// ES #sec-get-%typedarray%.prototype.findIndex
TF_BUILTIN(TypedArrayPrototypeFindIndex, ArrayBuiltinsAssembler) {
TNode<IntPtrT> argc =
ChangeInt32ToIntPtr(Parameter(Descriptor::kJSActualArgumentsCount));
CodeStubArguments args(this, argc);
TNode<Context> context = CAST(Parameter(Descriptor::kContext));
TNode<Object> receiver = args.GetReceiver();
Node* callbackfn = args.GetOptionalArgumentValue(0);
Node* this_arg = args.GetOptionalArgumentValue(1);
InitIteratingArrayBuiltinBody(context, receiver, callbackfn, this_arg, argc);
GenerateIteratingTypedArrayBuiltinBody(
"%TypedArray%.prototype.findIndex",
&ArrayBuiltinsAssembler::FindIndexResultGenerator,
&ArrayBuiltinsAssembler::FindIndexProcessor,
&ArrayBuiltinsAssembler::NullPostLoopAction);
}
TF_BUILTIN(TypedArrayPrototypeSome, ArrayBuiltinsAssembler) {
TNode<IntPtrT> argc =
ChangeInt32ToIntPtr(Parameter(Descriptor::kJSActualArgumentsCount));
CodeStubArguments args(this, argc);
TNode<Context> context = CAST(Parameter(Descriptor::kContext));
TNode<Object> receiver = args.GetReceiver();
Node* callbackfn = args.GetOptionalArgumentValue(0);
Node* this_arg = args.GetOptionalArgumentValue(1);
InitIteratingArrayBuiltinBody(context, receiver, callbackfn, this_arg, argc);
GenerateIteratingTypedArrayBuiltinBody(
"%TypedArray%.prototype.some",
&ArrayBuiltinsAssembler::SomeResultGenerator,
&ArrayBuiltinsAssembler::SomeProcessor,
&ArrayBuiltinsAssembler::NullPostLoopAction);
}
TF_BUILTIN(TypedArrayPrototypeEvery, ArrayBuiltinsAssembler) {
TNode<IntPtrT> argc =
ChangeInt32ToIntPtr(Parameter(Descriptor::kJSActualArgumentsCount));
CodeStubArguments args(this, argc);
TNode<Context> context = CAST(Parameter(Descriptor::kContext));
TNode<Object> receiver = args.GetReceiver();
Node* callbackfn = args.GetOptionalArgumentValue(0);
Node* this_arg = args.GetOptionalArgumentValue(1);
InitIteratingArrayBuiltinBody(context, receiver, callbackfn, this_arg, argc);
GenerateIteratingTypedArrayBuiltinBody(
"%TypedArray%.prototype.every",
&ArrayBuiltinsAssembler::EveryResultGenerator,
&ArrayBuiltinsAssembler::EveryProcessor,
&ArrayBuiltinsAssembler::NullPostLoopAction);
}
TF_BUILTIN(TypedArrayPrototypeMap, ArrayBuiltinsAssembler) {
TNode<IntPtrT> argc =
ChangeInt32ToIntPtr(Parameter(Descriptor::kJSActualArgumentsCount));
......
......@@ -1120,12 +1120,6 @@ namespace internal {
CPP(TypedArrayPrototypeCopyWithin) \
/* ES6 #sec-%typedarray%.prototype.fill */ \
CPP(TypedArrayPrototypeFill) \
/* ES6 %TypedArray%.prototype.find */ \
TFJ(TypedArrayPrototypeFind, \
SharedFunctionInfo::kDontAdaptArgumentsSentinel) \
/* ES6 %TypedArray%.prototype.findIndex */ \
TFJ(TypedArrayPrototypeFindIndex, \
SharedFunctionInfo::kDontAdaptArgumentsSentinel) \
/* ES7 #sec-%typedarray%.prototype.includes */ \
CPP(TypedArrayPrototypeIncludes) \
/* ES6 #sec-%typedarray%.prototype.indexof */ \
......@@ -1138,12 +1132,6 @@ namespace internal {
TFJ(TypedArrayPrototypeSet, SharedFunctionInfo::kDontAdaptArgumentsSentinel) \
/* ES6 #sec-get-%typedarray%.prototype-@@tostringtag */ \
TFJ(TypedArrayPrototypeToStringTag, 0, kReceiver) \
/* ES6 %TypedArray%.prototype.every */ \
TFJ(TypedArrayPrototypeEvery, \
SharedFunctionInfo::kDontAdaptArgumentsSentinel) \
/* ES6 %TypedArray%.prototype.some */ \
TFJ(TypedArrayPrototypeSome, \
SharedFunctionInfo::kDontAdaptArgumentsSentinel) \
/* ES6 %TypedArray%.prototype.map */ \
TFJ(TypedArrayPrototypeMap, SharedFunctionInfo::kDontAdaptArgumentsSentinel) \
/* ES6 %TypedArray%.of */ \
......
// Copyright 2019 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include 'src/builtins/builtins-typed-array-gen.h'
namespace typed_array_every {
const kBuiltinName: constexpr string = '%TypedArray%.prototype.every';
transitioning macro EveryAllElements(implicit context: Context)(
array: typed_array::AttachedJSTypedArray, callbackfn: Callable,
thisArg: Object): Boolean {
let witness = typed_array::NewAttachedJSTypedArrayWitness(array);
const length: Smi = Convert<Smi>(witness.Get().length);
for (let k: Smi = 0; k < length; k++) {
// BUG(4895): We should throw on detached buffers rather than simply exit.
witness.Recheck() otherwise break;
const value: Object = witness.Load(k);
const result =
Call(context, callbackfn, thisArg, value, k, witness.GetStable());
if (!ToBoolean(result)) {
return False;
}
}
return True;
}
// https://tc39.github.io/ecma262/#sec-%typedarray%.prototype.every
transitioning javascript builtin
TypedArrayPrototypeEvery(implicit context: Context)(
receiver: Object, ...arguments): Object {
// arguments[0] = callback
// arguments[1] = thisArg
try {
const array: JSTypedArray = Cast<JSTypedArray>(receiver)
otherwise NotTypedArray;
const uarray = typed_array::EnsureAttached(array) otherwise IsDetached;
const callbackfn = Cast<Callable>(arguments[0]) otherwise NotCallable;
const thisArg = arguments[1];
return EveryAllElements(uarray, callbackfn, thisArg);
}
label NotCallable deferred {
ThrowTypeError(kCalledNonCallable, arguments[0]);
}
label NotTypedArray deferred {
ThrowTypeError(kNotTypedArray, kBuiltinName);
}
label IsDetached deferred {
ThrowTypeError(kDetachedOperation, kBuiltinName);
}
}
}
// Copyright 2019 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include 'src/builtins/builtins-typed-array-gen.h'
namespace typed_array_find {
const kBuiltinName: constexpr string = '%TypedArray%.prototype.find';
transitioning macro FindAllElements(implicit context: Context)(
array: typed_array::AttachedJSTypedArray, callbackfn: Callable,
thisArg: Object): Object {
let witness = typed_array::NewAttachedJSTypedArrayWitness(array);
const length: Smi = Convert<Smi>(witness.Get().length);
for (let k: Smi = 0; k < length; k++) {
// BUG(4895): We should throw on detached buffers rather than simply exit.
witness.Recheck() otherwise break;
const value: Object = witness.Load(k);
const result =
Call(context, callbackfn, thisArg, value, k, witness.GetStable());
if (ToBoolean(result)) {
return value;
}
}
return Undefined;
}
// https://tc39.github.io/ecma262/#sec-%typedarray%.prototype.find
transitioning javascript builtin
TypedArrayPrototypeFind(implicit context:
Context)(receiver: Object, ...arguments): Object {
// arguments[0] = callback
// arguments[1] = thisArg
try {
const array: JSTypedArray = Cast<JSTypedArray>(receiver)
otherwise NotTypedArray;
const uarray = typed_array::EnsureAttached(array) otherwise IsDetached;
const callbackfn = Cast<Callable>(arguments[0]) otherwise NotCallable;
const thisArg = arguments[1];
return FindAllElements(uarray, callbackfn, thisArg);
}
label NotCallable deferred {
ThrowTypeError(kCalledNonCallable, arguments[0]);
}
label NotTypedArray deferred {
ThrowTypeError(kNotTypedArray, kBuiltinName);
}
label IsDetached deferred {
ThrowTypeError(kDetachedOperation, kBuiltinName);
}
}
}
// Copyright 2019 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include 'src/builtins/builtins-typed-array-gen.h'
namespace typed_array_findindex {
const kBuiltinName: constexpr string = '%TypedArray%.prototype.findIndex';
transitioning macro FindIndexAllElements(implicit context: Context)(
array: typed_array::AttachedJSTypedArray, callbackfn: Callable,
thisArg: Object): Number {
let witness = typed_array::NewAttachedJSTypedArrayWitness(array);
const length: Smi = Convert<Smi>(witness.Get().length);
for (let k: Smi = 0; k < length; k++) {
// BUG(4895): We should throw on detached buffers rather than simply exit.
witness.Recheck() otherwise break;
const value: Object = witness.Load(k);
const result =
Call(context, callbackfn, thisArg, value, k, witness.GetStable());
if (ToBoolean(result)) {
return k;
}
}
return -1;
}
// https://tc39.github.io/ecma262/#sec-%typedarray%.prototype.findIndex
transitioning javascript builtin
TypedArrayPrototypeFindIndex(implicit context: Context)(
receiver: Object, ...arguments): Object {
// arguments[0] = callback
// arguments[1] = thisArg.
try {
const array: JSTypedArray = Cast<JSTypedArray>(receiver)
otherwise NotTypedArray;
const uarray = typed_array::EnsureAttached(array) otherwise IsDetached;
const callbackfn = Cast<Callable>(arguments[0]) otherwise NotCallable;
const thisArg = arguments[1];
return FindIndexAllElements(uarray, callbackfn, thisArg);
}
label NotCallable deferred {
ThrowTypeError(kCalledNonCallable, arguments[0]);
}
label NotTypedArray deferred {
ThrowTypeError(kNotTypedArray, kBuiltinName);
}
label IsDetached deferred {
ThrowTypeError(kDetachedOperation, kBuiltinName);
}
}
}
// Copyright 2019 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include 'src/builtins/builtins-typed-array-gen.h'
namespace typed_array_some {
const kBuiltinName: constexpr string = '%TypedArray%.prototype.some';
transitioning macro SomeAllElements(implicit context: Context)(
array: typed_array::AttachedJSTypedArray, callbackfn: Callable,
thisArg: Object): Boolean {
let witness = typed_array::NewAttachedJSTypedArrayWitness(array);
const length: Smi = Convert<Smi>(witness.Get().length);
for (let k: Smi = 0; k < length; k++) {
// BUG(4895): We should throw on detached buffers rather than simply exit.
witness.Recheck() otherwise break;
const value: Object = witness.Load(k);
const result =
Call(context, callbackfn, thisArg, value, k, witness.GetStable());
if (ToBoolean(result)) {
return True;
}
}
return False;
}
// https://tc39.github.io/ecma262/#sec-%typedarray%.prototype.some
transitioning javascript builtin
TypedArrayPrototypeSome(implicit context:
Context)(receiver: Object, ...arguments): Object {
// arguments[0] = callback
// arguments[1] = thisArg.
try {
const array: JSTypedArray = Cast<JSTypedArray>(receiver)
otherwise NotTypedArray;
const uarray = typed_array::EnsureAttached(array) otherwise IsDetached;
const callbackfn = Cast<Callable>(arguments[0]) otherwise NotCallable;
const thisArg = arguments[1];
return SomeAllElements(uarray, callbackfn, thisArg);
}
label NotCallable deferred {
ThrowTypeError(kCalledNonCallable, arguments[0]);
}
label NotTypedArray deferred {
ThrowTypeError(kNotTypedArray, kBuiltinName);
}
label IsDetached deferred {
ThrowTypeError(kDetachedOperation, kBuiltinName);
}
}
}
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