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

[Torque] Array.prototype.filter moves to Torque.

Change-Id: Ifc71ae885b2a08b898ace7f75a8df0ca2b9c3a3d
Reviewed-on: https://chromium-review.googlesource.com/c/1275820
Commit-Queue: Michael Stanton <mvstanton@chromium.org>
Reviewed-by: 's avatarTobias Tebbi <tebbi@chromium.org>
Reviewed-by: 's avatarDaniel Clifford <danno@chromium.org>
Cr-Commit-Position: refs/heads/master@{#58643}
parent 1ab4b006
......@@ -854,6 +854,7 @@ torque_files = [
"src/builtins/arguments.tq",
"src/builtins/array.tq",
"src/builtins/array-copywithin.tq",
"src/builtins/array-filter.tq",
"src/builtins/array-foreach.tq",
"src/builtins/array-join.tq",
"src/builtins/array-lastindexof.tq",
......
// 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 {
transitioning javascript builtin
ArrayFilterLoopEagerDeoptContinuation(implicit context: Context)(
receiver: Object, callback: Object, thisArg: Object, array: Object,
initialK: Object, length: Object, initialTo: Object): Object {
// All continuation points in the optimized filter 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: JSReceiver =
Cast<JSReceiver>(receiver) otherwise unreachable;
const callbackfn: Callable = Cast<Callable>(callback) otherwise unreachable;
const outputArray: JSReceiver =
Cast<JSReceiver>(array) otherwise unreachable;
const numberK: Number = Cast<Number>(initialK) otherwise unreachable;
const numberTo: Number = Cast<Number>(initialTo) otherwise unreachable;
const numberLength: Number = Cast<Number>(length) otherwise unreachable;
return ArrayFilterLoopContinuation(
jsreceiver, callbackfn, thisArg, outputArray, jsreceiver, numberK,
numberLength, numberTo);
}
transitioning javascript builtin
ArrayFilterLoopLazyDeoptContinuation(implicit context: Context)(
receiver: Object, callback: Object, thisArg: Object, array: Object,
initialK: Object, length: Object, valueK: Object, initialTo: Object,
result: Object): Object {
// All continuation points in the optimized filter implementation are
// after the ToObject(O) call that ensures we are dealing with a
// JSReceiver.
const jsreceiver: JSReceiver =
Cast<JSReceiver>(receiver) otherwise unreachable;
const callbackfn: Callable = Cast<Callable>(callback) otherwise unreachable;
const outputArray: JSReceiver =
Cast<JSReceiver>(array) otherwise unreachable;
let numberK: Number = Cast<Number>(initialK) otherwise unreachable;
let numberTo: Number = Cast<Number>(initialTo) otherwise unreachable;
const numberLength: Number = Cast<Number>(length) otherwise unreachable;
// This custom lazy deopt point is right after the callback. filter() needs
// to pick up at the next step, which is setting the callback result in
// the output array. After incrementing k and to, we can glide into the loop
// continuation builtin.
if (ToBoolean(result)) {
CreateDataProperty(outputArray, numberTo, valueK);
numberTo = numberTo + 1;
}
numberK = numberK + 1;
return ArrayFilterLoopContinuation(
jsreceiver, callbackfn, thisArg, outputArray, jsreceiver, numberK,
numberLength, numberTo);
}
transitioning builtin ArrayFilterLoopContinuation(implicit context: Context)(
receiver: JSReceiver, callbackfn: Callable, thisArg: Object,
array: JSReceiver, o: JSReceiver, initialK: Number, length: Number,
initialTo: Number): Object {
let to: Number = initialTo;
// 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. Let kPresent be ? HasProperty(O, Pk).
const kPresent: Boolean = HasProperty_Inline(o, k);
// 6c. If kPresent is true, then
if (kPresent == True) {
// 6c. i. Let kValue be ? Get(O, Pk).
const kValue: Object = GetProperty(o, k);
// 6c. ii. Perform ? Call(callbackfn, T, <kValue, k, O>).
const result: Object = Call(context, callbackfn, thisArg, kValue, k, o);
// iii. If selected is true, then...
if (ToBoolean(result)) {
// 1. Perform ? CreateDataPropertyOrThrow(A, ToString(to), kValue).
CreateDataProperty(array, to, kValue);
// 2. Increase to by 1.
to = to + 1;
}
}
// 6d. Increase k by 1. (done by the loop).
}
return array;
}
transitioning macro
FilterVisitAllElements<FixedArrayType: type>(implicit context: Context)(
kind: constexpr ElementsKind, o: JSArray, len: Smi, callbackfn: Callable,
thisArg: Object, a: JSArray) labels Bailout(Smi, Smi) {
let k: Smi = 0;
let to: Smi = 0;
const oFastWitness: FastJSArrayWitness =
MakeWitness(Cast<FastJSArray>(o) otherwise goto Bailout(k, to));
const aFastWitness: FastJSArrayWitness =
MakeWitness(Cast<FastJSArray>(a) otherwise goto Bailout(k, to));
// Build a fast loop over the smi array.
for (; k < len; k = k + 1) {
let oFast: FastJSArray =
Testify(oFastWitness) otherwise goto Bailout(k, to);
// Ensure that we haven't walked beyond a possibly updated length.
if (k >= oFast.length) goto Bailout(k, to);
try {
const value: Object =
LoadElementNoHole<FixedArrayType>(oFast, k) otherwise FoundHole;
const result: Object =
Call(context, callbackfn, thisArg, value, k, oFast);
if (ToBoolean(result)) {
try {
// Since the call to {callbackfn} is observable, we can't
// use the Bailout label until we've successfully stored.
// Hence the {SlowStore} label.
const aFast: FastJSArray =
Testify(aFastWitness) otherwise SlowStore;
if (aFast.length != to) goto SlowStore;
BuildAppendJSArray(kind, aFast, value)
otherwise SlowStore;
}
label SlowStore {
CreateDataProperty(a, to, value);
}
to = to + 1;
}
}
label FoundHole {}
}
}
transitioning macro FastArrayFilter(implicit context: Context)(
o: JSReceiver, len: Number, callbackfn: Callable, thisArg: Object,
array: JSReceiver): Object
labels Bailout(Smi, Smi) {
let k: Smi = 0;
let to: Smi = 0;
const smiLen: Smi = Cast<Smi>(len) otherwise goto Bailout(k, to);
const fastArray: FastJSArray =
Cast<FastJSArray>(array) otherwise goto Bailout(k, to);
let fastO: FastJSArray = Cast<FastJSArray>(o) otherwise goto Bailout(k, to);
EnsureArrayPushable(fastArray.map) otherwise goto Bailout(k, to);
const elementsKind: ElementsKind = fastO.map.elements_kind;
if (IsElementsKindLessThanOrEqual(elementsKind, HOLEY_SMI_ELEMENTS)) {
FilterVisitAllElements<FixedArray>(
HOLEY_SMI_ELEMENTS, fastO, smiLen, callbackfn, thisArg, fastArray)
otherwise Bailout;
} else if (IsElementsKindLessThanOrEqual(elementsKind, HOLEY_ELEMENTS)) {
FilterVisitAllElements<FixedArray>(
HOLEY_ELEMENTS, fastO, smiLen, callbackfn, thisArg, fastArray)
otherwise Bailout;
} else {
assert(IsDoubleElementsKind(elementsKind));
FilterVisitAllElements<FixedDoubleArray>(
HOLEY_DOUBLE_ELEMENTS, fastO, smiLen, callbackfn, thisArg, fastArray)
otherwise Bailout;
}
return array;
}
// This method creates a 0-length array with the ElementsKind of the
// receiver if possible, otherwise, calls the species constructor.
macro FilterSpeciesCreate(implicit context: Context)(receiver: JSReceiver):
JSReceiver {
const len: Smi = 0;
try {
if (IsArraySpeciesProtectorCellInvalid()) goto Slow;
const o: FastJSArray = Cast<FastJSArray>(receiver) otherwise Slow;
const newMap: Map = LoadJSArrayElementsMap(
o.map.elements_kind, LoadNativeContext(context));
return AllocateJSArray(PACKED_SMI_ELEMENTS, newMap, len, len);
}
label Slow {
return ArraySpeciesCreate(context, receiver, len);
}
}
// https://tc39.github.io/ecma262/#sec-array.prototype.filter
transitioning javascript builtin
ArrayFilter(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 TypeError;
}
const callbackfn: Callable =
Cast<Callable>(arguments[0]) otherwise TypeError;
// 4. If thisArg is present, let T be thisArg; else let T be undefined.
const thisArg: Object = arguments.length > 1 ? arguments[1] : Undefined;
const array: JSReceiver = FilterSpeciesCreate(o);
// Special cases.
let k: Number = 0;
let to: Number = 0;
try {
return FastArrayFilter(o, len, callbackfn, thisArg, array)
otherwise Bailout;
}
label Bailout(kValue: Smi, toValue: Smi) deferred {
k = kValue;
to = toValue;
}
return ArrayFilterLoopContinuation(
o, callbackfn, thisArg, array, o, k, len, to);
}
label TypeError deferred {
ThrowTypeError(context, kCalledNonCallable, arguments[0]);
}
label NullOrUndefinedError deferred {
ThrowTypeError(
context, kCalledOnNullOrUndefined, 'Array.prototype.filter');
}
}
}
......@@ -332,6 +332,9 @@ extern macro ThrowTypeError(
Context, constexpr MessageTemplate, Object, Object, Object): never;
extern macro ArraySpeciesCreate(Context, Object, Number): JSReceiver;
extern macro ArrayCreate(implicit context: Context)(Number): JSArray;
extern macro BuildAppendJSArray(
constexpr ElementsKind, FastJSArray, Object): void labels Bailout;
extern macro EnsureArrayPushable(Map): ElementsKind
labels Bailout;
extern macro EnsureArrayLengthWritable(Map) labels Bailout;
......@@ -684,6 +687,27 @@ CastHeapObject<FastJSArray>(implicit context: Context)(o: HeapObject):
return %RawObjectCast<FastJSArray>(o);
}
struct FastJSArrayWitness {
array: HeapObject;
map: Map;
}
macro MakeWitness(array: FastJSArray): FastJSArrayWitness {
return FastJSArrayWitness{array, array.map};
}
macro Testify(witness: FastJSArrayWitness): FastJSArray labels CastError {
if (witness.array.map != witness.map) goto CastError;
// We don't need to check elements kind or whether the prototype
// has changed away from the default JSArray prototype, because
// if the map remains the same then those properties hold.
//
// However, we have to make sure there are no elements in the
// prototype chain.
if (IsNoElementsProtectorCellInvalid()) goto CastError;
return %RawObjectCast<FastJSArray>(witness.array);
}
CastHeapObject<FastJSArrayForCopy>(implicit context: Context)(o: HeapObject):
FastJSArrayForCopy
labels CastError {
......@@ -760,6 +784,7 @@ extern macro ChangeInt32ToIntPtr(int32): intptr; // Sign-extends.
extern macro ChangeUint32ToWord(uint32): uintptr; // Doesn't sign-extend.
extern macro LoadNativeContext(Context): NativeContext;
extern macro LoadJSArrayElementsMap(constexpr ElementsKind, Context): Map;
extern macro LoadJSArrayElementsMap(ElementsKind, Context): Map;
extern macro ChangeNonnegativeNumberToUintPtr(Number): uintptr;
extern macro NumberConstant(constexpr float64): Number;
......@@ -1166,7 +1191,7 @@ macro TorqueCopyElements(
count);
}
transitioning macro LoadElementNoHole<T: type>(a: JSArray, index: Smi): Object
macro LoadElementNoHole<T: type>(a: JSArray, index: Smi): Object
labels IfHole;
LoadElementNoHole<FixedArray>(implicit context: Context)(
......
......@@ -126,79 +126,6 @@ Node* ArrayBuiltinsAssembler::FindProcessor(Node* k_value, Node* k) {
BIND(&ok);
}
void ArrayBuiltinsAssembler::FilterResultGenerator() {
// 7. Let A be ArraySpeciesCreate(O, 0).
// This version of ArraySpeciesCreate will create with the correct
// ElementsKind in the fast case.
GenerateArraySpeciesCreate();
}
Node* ArrayBuiltinsAssembler::FilterProcessor(Node* k_value, Node* k) {
// ii. Let selected be ToBoolean(? Call(callbackfn, T, kValue, k, O)).
Node* selected = CallJS(CodeFactory::Call(isolate()), context(),
callbackfn(), this_arg(), k_value, k, o());
Label true_continue(this, &to_), false_continue(this);
BranchIfToBooleanIsTrue(selected, &true_continue, &false_continue);
BIND(&true_continue);
// iii. If selected is true, then...
{
Label after_work(this, &to_);
Node* kind = nullptr;
// If a() is a JSArray, we can have a fast path.
Label fast(this);
Label runtime(this);
Label object_push_pre(this), object_push(this), double_push(this);
BranchIfFastJSArray(CAST(a()), context(), &fast, &runtime);
BIND(&fast);
{
GotoIf(WordNotEqual(LoadJSArrayLength(a()), to_.value()), &runtime);
kind = EnsureArrayPushable(LoadMap(a()), &runtime);
GotoIf(IsElementsKindGreaterThan(kind, HOLEY_SMI_ELEMENTS),
&object_push_pre);
BuildAppendJSArray(HOLEY_SMI_ELEMENTS, a(), k_value, &runtime);
Goto(&after_work);
}
BIND(&object_push_pre);
{
Branch(IsElementsKindGreaterThan(kind, HOLEY_ELEMENTS), &double_push,
&object_push);
}
BIND(&object_push);
{
BuildAppendJSArray(HOLEY_ELEMENTS, a(), k_value, &runtime);
Goto(&after_work);
}
BIND(&double_push);
{
BuildAppendJSArray(HOLEY_DOUBLE_ELEMENTS, a(), k_value, &runtime);
Goto(&after_work);
}
BIND(&runtime);
{
// 1. Perform ? CreateDataPropertyOrThrow(A, ToString(to), kValue).
CallRuntime(Runtime::kCreateDataProperty, context(), a(), to_.value(),
k_value);
Goto(&after_work);
}
BIND(&after_work);
{
// 2. Increase to by 1.
to_.Bind(NumberInc(to_.value()));
Goto(&false_continue);
}
}
BIND(&false_continue);
return a();
}
void ArrayBuiltinsAssembler::MapResultGenerator() {
GenerateArraySpeciesCreate(len_);
}
......@@ -832,53 +759,6 @@ Node* ArrayBuiltinsAssembler::FindProcessor(Node* k_value, Node* k) {
}
}
// Perform ArraySpeciesCreate (ES6 #sec-arrayspeciescreate).
// This version is specialized to create a zero length array
// of the elements kind of the input array.
void ArrayBuiltinsAssembler::GenerateArraySpeciesCreate() {
Label runtime(this, Label::kDeferred), done(this);
TNode<Smi> len = SmiConstant(0);
TNode<Map> original_map = LoadMap(o());
GotoIfNot(
InstanceTypeEqual(LoadMapInstanceType(original_map), JS_ARRAY_TYPE),
&runtime);
GotoIfNot(IsPrototypeInitialArrayPrototype(context(), original_map),
&runtime);
Node* species_protector = ArraySpeciesProtectorConstant();
Node* value =
LoadObjectField(species_protector, PropertyCell::kValueOffset);
TNode<Smi> const protector_invalid =
SmiConstant(Isolate::kProtectorInvalid);
GotoIf(WordEqual(value, protector_invalid), &runtime);
// Respect the ElementsKind of the input array.
TNode<Int32T> elements_kind = LoadMapElementsKind(original_map);
GotoIfNot(IsFastElementsKind(elements_kind), &runtime);
TNode<Context> native_context = LoadNativeContext(context());
TNode<Map> array_map =
LoadJSArrayElementsMap(elements_kind, native_context);
TNode<JSArray> array =
AllocateJSArray(GetInitialFastElementsKind(), array_map, len, len,
nullptr, CodeStubAssembler::SMI_PARAMETERS);
a_.Bind(array);
Goto(&done);
BIND(&runtime);
{
// 5. Let A be ? ArraySpeciesCreate(O, len).
TNode<JSReceiver> constructor =
CAST(CallRuntime(Runtime::kArraySpeciesConstructor, context(), o()));
a_.Bind(Construct(context(), constructor, len));
Goto(&fully_spec_compliant_);
}
BIND(&done);
}
// Perform ArraySpeciesCreate (ES6 #sec-arrayspeciescreate).
void ArrayBuiltinsAssembler::GenerateArraySpeciesCreate(TNode<Number> len) {
Label runtime(this, Label::kDeferred), done(this);
......@@ -2248,101 +2128,6 @@ TF_BUILTIN(TypedArrayPrototypeReduceRight, ArrayBuiltinsAssembler) {
ForEachDirection::kReverse);
}
TF_BUILTIN(ArrayFilterLoopContinuation, ArrayBuiltinsAssembler) {
TNode<Context> context = CAST(Parameter(Descriptor::kContext));
TNode<Object> receiver = CAST(Parameter(Descriptor::kReceiver));
Node* callbackfn = Parameter(Descriptor::kCallbackFn);
Node* this_arg = Parameter(Descriptor::kThisArg);
Node* array = Parameter(Descriptor::kArray);
TNode<JSReceiver> object = CAST(Parameter(Descriptor::kObject));
Node* initial_k = Parameter(Descriptor::kInitialK);
TNode<Number> len = CAST(Parameter(Descriptor::kLength));
Node* to = Parameter(Descriptor::kTo);
InitIteratingArrayBuiltinLoopContinuation(context, receiver, callbackfn,
this_arg, array, object, initial_k,
len, to);
GenerateIteratingArrayBuiltinLoopContinuation(
&ArrayBuiltinsAssembler::FilterProcessor,
&ArrayBuiltinsAssembler::NullPostLoopAction, MissingPropertyMode::kSkip);
}
TF_BUILTIN(ArrayFilterLoopEagerDeoptContinuation, ArrayBuiltinsAssembler) {
TNode<Context> context = CAST(Parameter(Descriptor::kContext));
TNode<Object> receiver = CAST(Parameter(Descriptor::kReceiver));
Node* callbackfn = Parameter(Descriptor::kCallbackFn);
Node* this_arg = Parameter(Descriptor::kThisArg);
Node* array = Parameter(Descriptor::kArray);
Node* initial_k = Parameter(Descriptor::kInitialK);
TNode<Number> len = CAST(Parameter(Descriptor::kLength));
Node* to = Parameter(Descriptor::kTo);
Return(CallBuiltin(Builtins::kArrayFilterLoopContinuation, context, receiver,
callbackfn, this_arg, array, receiver, initial_k, len,
to));
}
TF_BUILTIN(ArrayFilterLoopLazyDeoptContinuation, ArrayBuiltinsAssembler) {
TNode<Context> context = CAST(Parameter(Descriptor::kContext));
TNode<Object> receiver = CAST(Parameter(Descriptor::kReceiver));
Node* callbackfn = Parameter(Descriptor::kCallbackFn);
Node* this_arg = Parameter(Descriptor::kThisArg);
Node* array = Parameter(Descriptor::kArray);
Node* initial_k = Parameter(Descriptor::kInitialK);
TNode<Number> len = CAST(Parameter(Descriptor::kLength));
Node* value_k = Parameter(Descriptor::kValueK);
Node* result = Parameter(Descriptor::kResult);
VARIABLE(to, MachineRepresentation::kTagged, Parameter(Descriptor::kTo));
// This custom lazy deopt point is right after the callback. filter() needs
// to pick up at the next step, which is setting the callback result in
// the output array. After incrementing k and to, we can glide into the loop
// continuation builtin.
Label true_continue(this, &to), false_continue(this);
// iii. If selected is true, then...
BranchIfToBooleanIsTrue(result, &true_continue, &false_continue);
BIND(&true_continue);
{
// 1. Perform ? CreateDataPropertyOrThrow(A, ToString(to), kValue).
CallRuntime(Runtime::kCreateDataProperty, context, array, to.value(),
value_k);
// 2. Increase to by 1.
to.Bind(NumberInc(to.value()));
Goto(&false_continue);
}
BIND(&false_continue);
// Increment k.
initial_k = NumberInc(initial_k);
Return(CallBuiltin(Builtins::kArrayFilterLoopContinuation, context, receiver,
callbackfn, this_arg, array, receiver, initial_k, len,
to.value()));
}
TF_BUILTIN(ArrayFilter, 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);
GenerateIteratingArrayBuiltinBody(
"Array.prototype.filter", &ArrayBuiltinsAssembler::FilterResultGenerator,
&ArrayBuiltinsAssembler::FilterProcessor,
&ArrayBuiltinsAssembler::NullPostLoopAction,
Builtins::CallableFor(isolate(), Builtins::kArrayFilterLoopContinuation),
MissingPropertyMode::kSkip);
}
TF_BUILTIN(ArrayMapLoopContinuation, ArrayBuiltinsAssembler) {
TNode<Context> context = CAST(Parameter(Descriptor::kContext));
TNode<Object> receiver = CAST(Parameter(Descriptor::kReceiver));
......
......@@ -363,14 +363,6 @@ namespace internal {
TFJ(ArraySomeLoopLazyDeoptContinuation, 5, kReceiver, kCallbackFn, kThisArg, \
kInitialK, kLength, kResult) \
TFJ(ArraySome, SharedFunctionInfo::kDontAdaptArgumentsSentinel) \
/* ES6 #sec-array.prototype.filter */ \
TFS(ArrayFilterLoopContinuation, kReceiver, kCallbackFn, kThisArg, kArray, \
kObject, kInitialK, kLength, kTo) \
TFJ(ArrayFilter, SharedFunctionInfo::kDontAdaptArgumentsSentinel) \
TFJ(ArrayFilterLoopEagerDeoptContinuation, 6, kReceiver, kCallbackFn, \
kThisArg, kArray, kInitialK, kLength, kTo) \
TFJ(ArrayFilterLoopLazyDeoptContinuation, 8, kReceiver, kCallbackFn, \
kThisArg, kArray, kInitialK, kLength, kValueK, kTo, kResult) \
/* ES6 #sec-array.prototype.foreach */ \
TFS(ArrayMapLoopContinuation, kReceiver, kCallbackFn, kThisArg, kArray, \
kObject, kInitialK, kLength, kTo) \
......
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