Commit ada192fe authored by Mike Stanton's avatar Mike Stanton Committed by Commit Bot

[Torque] Port Array.prototype.find and findIndex to Torque

Happily, with the port of Array.prototype.find and findIndex, we can
remove a large set of library functions from array-builtins-gen.cc.

BUG: v8:7672
Change-Id: I74e07fe00162b34b2246c868386d4551ba4dc032
Reviewed-on: https://chromium-review.googlesource.com/c/1484296
Commit-Queue: Michael Stanton <mvstanton@chromium.org>
Reviewed-by: 's avatarTobias Tebbi <tebbi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#59902}
parent 1937e2b1
......@@ -902,6 +902,8 @@ torque_files = [
"src/builtins/array-copywithin.tq",
"src/builtins/array-every.tq",
"src/builtins/array-filter.tq",
"src/builtins/array-find.tq",
"src/builtins/array-findindex.tq",
"src/builtins/array-foreach.tq",
"src/builtins/array-join.tq",
"src/builtins/array-lastindexof.tq",
......@@ -933,6 +935,8 @@ torque_namespaces = [
"array",
"array-copywithin",
"array-filter",
"array-find",
"array-findindex",
"array-foreach",
"array-join",
"array-map",
......
// Copyright 2018 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.
namespace array_find {
transitioning javascript builtin
ArrayFindLoopEagerDeoptContinuation(implicit context: Context)(
receiver: Object, callback: Object, thisArg: Object, initialK: Object,
length: Object): Object {
// All continuation points in the optimized find implementation are
// after the ToObject(O) call that ensures we are dealing with a
// JSReceiver.
//
// Also, this great mass of casts is necessary because the signature
// of Torque javascript builtins requires Object type for all parameters
// other than {context}.
const jsreceiver = Cast<JSReceiver>(receiver) otherwise unreachable;
const callbackfn = Cast<Callable>(callback) otherwise unreachable;
const numberK = Cast<Number>(initialK) otherwise unreachable;
const numberLength = Cast<Number>(length) otherwise unreachable;
return ArrayFindLoopContinuation(
jsreceiver, callbackfn, thisArg, jsreceiver, numberK, numberLength);
}
transitioning javascript builtin
ArrayFindLoopLazyDeoptContinuation(implicit context: Context)(
receiver: Object, callback: Object, thisArg: Object, initialK: Object,
length: Object, result: Object): Object {
// This deopt continuation point is never actually called, it just
// exists to make stack traces correct from a ThrowTypeError if the
// callback was found to be non-callable.
unreachable;
}
// Continuation that is called after a lazy deoptimization from TF that
// happens right after the callback and it's returned value must be handled
// before iteration continues.
transitioning javascript builtin
ArrayFindLoopAfterCallbackLazyDeoptContinuation(implicit context: Context)(
receiver: Object, callback: Object, thisArg: Object, initialK: Object,
length: Object, foundValue: Object, isFound: Object): Object {
// All continuation points in the optimized find implementation are
// after the ToObject(O) call that ensures we are dealing with a
// JSReceiver.
const jsreceiver = Cast<JSReceiver>(receiver) otherwise unreachable;
const callbackfn = Cast<Callable>(callback) otherwise unreachable;
let numberK = Cast<Number>(initialK) otherwise unreachable;
const numberLength = Cast<Number>(length) otherwise unreachable;
// This custom lazy deopt point is right after the callback. find() needs
// to pick up at the next step, which is returning the element if the
// callback value is truthy. Otherwise, continue the search by calling the
// continuation.
if (ToBoolean(isFound)) {
return foundValue;
}
return ArrayFindLoopContinuation(
jsreceiver, callbackfn, thisArg, jsreceiver, numberK, numberLength);
}
transitioning builtin ArrayFindLoopContinuation(implicit context: Context)(
receiver: JSReceiver, callbackfn: Callable, thisArg: Object,
o: JSReceiver, initialK: Number, length: Number): Object {
// 5. Let k be 0.
// 6. Repeat, while k < len
for (let k: Number = initialK; k < length; k++) {
// 6a. Let Pk be ! ToString(k).
// k is guaranteed to be a positive integer, hence ToString is
// side-effect free and HasProperty/GetProperty do the conversion inline.
// 6b. i. Let kValue be ? Get(O, Pk).
const value: Object = GetProperty(o, k);
// 6c. Let testResult be ToBoolean(? Call(predicate, T, <<kValue, k,
// O>>)).
const testResult: Object =
Call(context, callbackfn, thisArg, value, k, o);
// 6d. If testResult is true, return kValue.
if (ToBoolean(testResult)) {
return value;
}
// 6e. Increase k by 1. (done by the loop).
}
return Undefined;
}
transitioning macro FastArrayFind(implicit context: Context)(
o: JSReceiver, len: Number, callbackfn: Callable, thisArg: Object): Object
labels Bailout(Smi) {
let k: Smi = 0;
const smiLen = Cast<Smi>(len) otherwise goto Bailout(k);
const fastO = Cast<FastJSArray>(o) otherwise goto Bailout(k);
let fastOW = FastJSArrayWitness{fastO};
// Build a fast loop over the smi array.
for (; k < smiLen; k++) {
fastOW.Recheck() otherwise goto Bailout(k);
// Ensure that we haven't walked beyond a possibly updated length.
if (k >= fastOW.Get().length) goto Bailout(k);
const value: Object = fastOW.LoadElementOrUndefined(k);
const testResult: Object =
Call(context, callbackfn, thisArg, value, k, fastOW.Get());
if (ToBoolean(testResult)) {
return value;
}
}
return Undefined;
}
// https://tc39.github.io/ecma262/#sec-array.prototype.find
transitioning javascript builtin
ArrayPrototypeFind(implicit context: Context)(receiver: Object, ...arguments):
Object {
try {
if (IsNullOrUndefined(receiver)) {
goto NullOrUndefinedError;
}
// 1. Let O be ? ToObject(this value).
const o: JSReceiver = ToObject_Inline(context, receiver);
// 2. Let len be ? ToLength(? Get(O, "length")).
const len: Number = GetLengthProperty(o);
// 3. If IsCallable(callbackfn) is false, throw a TypeError exception.
if (arguments.length == 0) {
goto NotCallableError;
}
const callbackfn =
Cast<Callable>(arguments[0]) otherwise NotCallableError;
// 4. If thisArg is present, let T be thisArg; else let T be undefined.
const thisArg: Object = arguments.length > 1 ? arguments[1] : Undefined;
// Special cases.
try {
return FastArrayFind(o, len, callbackfn, thisArg)
otherwise Bailout;
}
label Bailout(k: Smi) deferred {
return ArrayFindLoopContinuation(o, callbackfn, thisArg, o, k, len);
}
}
label NotCallableError deferred {
ThrowTypeError(kCalledNonCallable, arguments[0]);
}
label NullOrUndefinedError deferred {
ThrowTypeError(kCalledOnNullOrUndefined, 'Array.prototype.find');
}
}
}
// Copyright 2018 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.
namespace array_findindex {
transitioning javascript builtin
ArrayFindIndexLoopEagerDeoptContinuation(implicit context: Context)(
receiver: Object, callback: Object, thisArg: Object, initialK: Object,
length: Object): Object {
// All continuation points in the optimized findIndex implementation are
// after the ToObject(O) call that ensures we are dealing with a
// JSReceiver.
//
// Also, this great mass of casts is necessary because the signature
// of Torque javascript builtins requires Object type for all parameters
// other than {context}.
const jsreceiver = Cast<JSReceiver>(receiver) otherwise unreachable;
const callbackfn = Cast<Callable>(callback) otherwise unreachable;
const numberK = Cast<Number>(initialK) otherwise unreachable;
const numberLength = Cast<Number>(length) otherwise unreachable;
return ArrayFindIndexLoopContinuation(
jsreceiver, callbackfn, thisArg, jsreceiver, numberK, numberLength);
}
transitioning javascript builtin
ArrayFindIndexLoopLazyDeoptContinuation(implicit context: Context)(
receiver: Object, callback: Object, thisArg: Object, initialK: Object,
length: Object, result: Object): Object {
// This deopt continuation point is never actually called, it just
// exists to make stack traces correct from a ThrowTypeError if the
// callback was found to be non-callable.
unreachable;
}
// Continuation that is called after a lazy deoptimization from TF that
// happens right after the callback and it's returned value must be handled
// before iteration continues.
transitioning javascript builtin
ArrayFindIndexLoopAfterCallbackLazyDeoptContinuation(implicit context:
Context)(
receiver: Object, callback: Object, thisArg: Object, initialK: Object,
length: Object, foundValue: Object, isFound: Object): Object {
// All continuation points in the optimized findIndex implementation are
// after the ToObject(O) call that ensures we are dealing with a
// JSReceiver.
const jsreceiver = Cast<JSReceiver>(receiver) otherwise unreachable;
const callbackfn = Cast<Callable>(callback) otherwise unreachable;
let numberK = Cast<Number>(initialK) otherwise unreachable;
const numberLength = Cast<Number>(length) otherwise unreachable;
// This custom lazy deopt point is right after the callback. find() needs
// to pick up at the next step, which is returning the element if the
// callback value is truthy. Otherwise, continue the search by calling the
// continuation.
if (ToBoolean(isFound)) {
return foundValue;
}
return ArrayFindIndexLoopContinuation(
jsreceiver, callbackfn, thisArg, jsreceiver, numberK, numberLength);
}
transitioning builtin ArrayFindIndexLoopContinuation(implicit context:
Context)(
receiver: JSReceiver, callbackfn: Callable, thisArg: Object,
o: JSReceiver, initialK: Number, length: Number): Number {
// 5. Let k be 0.
// 6. Repeat, while k < len
for (let k: Number = initialK; k < length; k++) {
// 6a. Let Pk be ! ToString(k).
// k is guaranteed to be a positive integer, hence ToString is
// side-effect free and HasProperty/GetProperty do the conversion inline.
// 6b. i. Let kValue be ? Get(O, Pk).
const value: Object = GetProperty(o, k);
// 6c. Let testResult be ToBoolean(? Call(predicate, T, <<kValue, k,
// O>>)).
const testResult: Object =
Call(context, callbackfn, thisArg, value, k, o);
// 6d. If testResult is true, return k.
if (ToBoolean(testResult)) {
return k;
}
// 6e. Increase k by 1. (done by the loop).
}
return Convert<Smi>(-1);
}
transitioning macro FastArrayFindIndex(implicit context: Context)(
o: JSReceiver, len: Number, callbackfn: Callable, thisArg: Object): Number
labels Bailout(Smi) {
let k: Smi = 0;
const smiLen = Cast<Smi>(len) otherwise goto Bailout(k);
const fastO = Cast<FastJSArray>(o) otherwise goto Bailout(k);
let fastOW = FastJSArrayWitness{fastO};
// Build a fast loop over the smi array.
for (; k < smiLen; k++) {
fastOW.Recheck() otherwise goto Bailout(k);
// Ensure that we haven't walked beyond a possibly updated length.
if (k >= fastOW.Get().length) goto Bailout(k);
const value: Object = fastOW.LoadElementOrUndefined(k);
const testResult: Object =
Call(context, callbackfn, thisArg, value, k, fastOW.Get());
if (ToBoolean(testResult)) {
return k;
}
}
return -1;
}
// https://tc39.github.io/ecma262/#sec-array.prototype.findIndex
transitioning javascript builtin
ArrayPrototypeFindIndex(implicit context:
Context)(receiver: Object, ...arguments): Object {
try {
if (IsNullOrUndefined(receiver)) {
goto NullOrUndefinedError;
}
// 1. Let O be ? ToObject(this value).
const o: JSReceiver = ToObject_Inline(context, receiver);
// 2. Let len be ? ToLength(? Get(O, "length")).
const len: Number = GetLengthProperty(o);
// 3. If IsCallable(callbackfn) is false, throw a TypeError exception.
if (arguments.length == 0) {
goto NotCallableError;
}
const callbackfn =
Cast<Callable>(arguments[0]) otherwise NotCallableError;
// 4. If thisArg is present, let T be thisArg; else let T be undefined.
const thisArg: Object = arguments.length > 1 ? arguments[1] : Undefined;
// Special cases.
try {
return FastArrayFindIndex(o, len, callbackfn, thisArg)
otherwise Bailout;
}
label Bailout(k: Smi) deferred {
return ArrayFindIndexLoopContinuation(
o, callbackfn, thisArg, o, k, len);
}
}
label NotCallableError deferred {
ThrowTypeError(kCalledNonCallable, arguments[0]);
}
label NullOrUndefinedError deferred {
ThrowTypeError(kCalledOnNullOrUndefined, 'Array.prototype.findIndex');
}
}
}
......@@ -1526,6 +1526,15 @@ struct FastJSArrayWitness {
}
}
LoadElementOrUndefined(implicit context: Context)(k: Smi): Object {
try {
return this.LoadElementNoHole(k) otherwise FoundHole;
}
label FoundHole {
return Undefined;
}
}
stable: JSArray;
unstable: FastJSArray;
map: Map;
......
This diff is collapsed.
......@@ -23,8 +23,6 @@ class ArrayBuiltinsAssembler : public CodeStubAssembler {
typedef std::function<void(ArrayBuiltinsAssembler* masm)> PostLoopAction;
enum class MissingPropertyMode { kSkip, kUseUndefined };
void FindResultGenerator();
Node* FindProcessor(Node* k_value, Node* k);
......@@ -51,12 +49,6 @@ class ArrayBuiltinsAssembler : public CodeStubAssembler {
void ReducePostLoopAction();
void FilterResultGenerator();
Node* FilterProcessor(Node* k_value, Node* k);
void MapResultGenerator();
void TypedArrayMapResultGenerator();
Node* SpecCompliantMapProcessor(Node* k_value, Node* k);
......@@ -106,27 +98,11 @@ class ArrayBuiltinsAssembler : public CodeStubAssembler {
TNode<Object> receiver, Node* callbackfn,
Node* this_arg, TNode<IntPtrT> argc);
void GenerateIteratingArrayBuiltinBody(
const char* name, const BuiltinResultGenerator& generator,
const CallResultProcessor& processor, const PostLoopAction& action,
const Callable& slow_case_continuation,
MissingPropertyMode missing_property_mode,
ForEachDirection direction = ForEachDirection::kForward);
void InitIteratingArrayBuiltinLoopContinuation(
TNode<Context> context, TNode<Object> receiver, Node* callbackfn,
Node* this_arg, Node* a, TNode<JSReceiver> o, Node* initial_k,
TNode<Number> len, Node* to);
void GenerateIteratingTypedArrayBuiltinBody(
const char* name, const BuiltinResultGenerator& generator,
const CallResultProcessor& processor, const PostLoopAction& action,
ForEachDirection direction = ForEachDirection::kForward);
void GenerateIteratingArrayBuiltinLoopContinuation(
const CallResultProcessor& processor, const PostLoopAction& action,
MissingPropertyMode missing_property_mode,
ForEachDirection direction = ForEachDirection::kForward);
void TailCallArrayConstructorStub(
const Callable& callable, TNode<Context> context,
TNode<JSFunction> target, TNode<HeapObject> allocation_site_or_undefined,
......@@ -167,18 +143,6 @@ class ArrayBuiltinsAssembler : public CodeStubAssembler {
Label* detached, ForEachDirection direction,
TNode<JSTypedArray> typed_array);
void VisitAllFastElementsOneKind(ElementsKind kind,
const CallResultProcessor& processor,
Label* array_changed, ParameterMode mode,
ForEachDirection direction,
MissingPropertyMode missing_property_mode,
TNode<Smi> length);
void HandleFastElements(const CallResultProcessor& processor,
const PostLoopAction& action, Label* slow,
ForEachDirection direction,
MissingPropertyMode missing_property_mode);
// Perform ArraySpeciesCreate (ES6 #sec-arrayspeciescreate).
// This version is specialized to create a zero length array
// of the elements kind of the input array.
......
......@@ -352,27 +352,6 @@ namespace internal {
TFS(ExtractFastJSArray, kSource, kBegin, kCount) \
/* ES6 #sec-array.prototype.entries */ \
TFJ(ArrayPrototypeEntries, 0, kReceiver) \
/* ES6 #sec-array.prototype.find */ \
TFS(ArrayFindLoopContinuation, kReceiver, kCallbackFn, kThisArg, kArray, \
kObject, kInitialK, kLength, kTo) \
TFJ(ArrayFindLoopEagerDeoptContinuation, 4, kReceiver, kCallbackFn, \
kThisArg, kInitialK, kLength) \
TFJ(ArrayFindLoopLazyDeoptContinuation, 5, kReceiver, kCallbackFn, kThisArg, \
kInitialK, kLength, kResult) \
TFJ(ArrayFindLoopAfterCallbackLazyDeoptContinuation, 6, kReceiver, \
kCallbackFn, kThisArg, kInitialK, kLength, kFoundValue, kIsFound) \
TFJ(ArrayPrototypeFind, SharedFunctionInfo::kDontAdaptArgumentsSentinel) \
/* ES6 #sec-array.prototype.findIndex */ \
TFS(ArrayFindIndexLoopContinuation, kReceiver, kCallbackFn, kThisArg, \
kArray, kObject, kInitialK, kLength, kTo) \
TFJ(ArrayFindIndexLoopEagerDeoptContinuation, 4, kReceiver, kCallbackFn, \
kThisArg, kInitialK, kLength) \
TFJ(ArrayFindIndexLoopLazyDeoptContinuation, 5, kReceiver, kCallbackFn, \
kThisArg, kInitialK, kLength, kResult) \
TFJ(ArrayFindIndexLoopAfterCallbackLazyDeoptContinuation, 6, kReceiver, \
kCallbackFn, kThisArg, kInitialK, kLength, kFoundValue, kIsFound) \
TFJ(ArrayPrototypeFindIndex, \
SharedFunctionInfo::kDontAdaptArgumentsSentinel) \
/* ES6 #sec-array.prototype.keys */ \
TFJ(ArrayPrototypeKeys, 0, kReceiver) \
/* ES6 #sec-array.prototype.values */ \
......
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