Commit af324e75 authored by Igor Sheludko's avatar Igor Sheludko Committed by Commit Bot

[builtins] Use uintptr for iteration in TypedArray builtins, pt.2

The CL fixes the following builtins:
  %TypedArray%.prototype.filter

Bug: v8:4153
Change-Id: Ifb4a464c91c38c586598359ffcded9cc4e7d8f4a
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1864943
Commit-Queue: Igor Sheludko <ishell@chromium.org>
Reviewed-by: 's avatarJakob Kummerow <jkummerow@chromium.org>
Cr-Commit-Position: refs/heads/master@{#64375}
parent dc75b63c
...@@ -32,15 +32,15 @@ ArrayBuiltinsAssembler::ArrayBuiltinsAssembler( ...@@ -32,15 +32,15 @@ ArrayBuiltinsAssembler::ArrayBuiltinsAssembler(
void ArrayBuiltinsAssembler::TypedArrayMapResultGenerator() { void ArrayBuiltinsAssembler::TypedArrayMapResultGenerator() {
// 6. Let A be ? TypedArraySpeciesCreate(O, len). // 6. Let A be ? TypedArraySpeciesCreate(O, len).
TNode<JSTypedArray> original_array = CAST(o()); TNode<JSTypedArray> original_array = CAST(o());
TNode<Smi> length = CAST(len_); // TODO(v8:4153): Consider making |len_| an UintPtrT.
TNode<UintPtrT> length = ChangeNonnegativeNumberToUintPtr(len_);
const char* method_name = "%TypedArray%.prototype.map"; const char* method_name = "%TypedArray%.prototype.map";
TNode<JSTypedArray> a = TypedArraySpeciesCreateByLength( TNode<JSTypedArray> a = TypedArraySpeciesCreateByLength(
context(), method_name, original_array, length); context(), method_name, original_array, length);
// In the Spec and our current implementation, the length check is already // In the Spec and our current implementation, the length check is already
// performed in TypedArraySpeciesCreate. // performed in TypedArraySpeciesCreate.
CSA_ASSERT(this, UintPtrLessThanOrEqual(SmiUntag(CAST(len_)), CSA_ASSERT(this, UintPtrLessThanOrEqual(length, LoadJSTypedArrayLength(a)));
LoadJSTypedArrayLength(a)));
fast_typed_array_target_ = fast_typed_array_target_ =
Word32Equal(LoadElementsKind(original_array), LoadElementsKind(a)); Word32Equal(LoadElementsKind(original_array), LoadElementsKind(a));
a_ = a; a_ = a;
......
...@@ -35,6 +35,7 @@ namespace growable_fixed_array { ...@@ -35,6 +35,7 @@ namespace growable_fixed_array {
} }
array: FixedArray; array: FixedArray;
// TODO(v8:4153): make capacity and length uintptr
capacity: intptr; capacity: intptr;
length: intptr; length: intptr;
} }
......
...@@ -400,15 +400,16 @@ namespace typed_array { ...@@ -400,15 +400,16 @@ namespace typed_array {
@export @export
transitioning macro TypedArraySpeciesCreateByLength(implicit context: transitioning macro TypedArraySpeciesCreateByLength(implicit context:
Context)( Context)(
methodName: constexpr string, exemplar: JSTypedArray, methodName: constexpr string, exemplar: JSTypedArray, length: uintptr):
length: PositiveSmi): JSTypedArray { JSTypedArray {
const numArgs: constexpr int31 = 1; const numArgs: constexpr int31 = 1;
// TODO(v8:4153): pass length further as uintptr.
const typedArray: JSTypedArray = TypedArraySpeciesCreate( const typedArray: JSTypedArray = TypedArraySpeciesCreate(
methodName, numArgs, exemplar, length, Undefined, Undefined); methodName, numArgs, exemplar, Convert<Number>(length), Undefined,
if (typedArray.length < Convert<uintptr>(length)) deferred { Undefined);
if (typedArray.length < length) deferred {
ThrowTypeError(kTypedArrayTooShort); ThrowTypeError(kTypedArrayTooShort);
} }
return typedArray; return typedArray;
} }
} }
...@@ -18,8 +18,7 @@ namespace typed_array { ...@@ -18,8 +18,7 @@ namespace typed_array {
const src = typed_array::EnsureAttached(array) otherwise IsDetached; const src = typed_array::EnsureAttached(array) otherwise IsDetached;
// 3. Let len be O.[[ArrayLength]]. // 3. Let len be O.[[ArrayLength]].
// TODO(v8:4153): Support huge TypedArrays here. const len: uintptr = src.length;
const len = Cast<Smi>(Convert<Number>(src.length)) otherwise unreachable;
// 4. If IsCallable(callbackfn) is false, throw a TypeError exception. // 4. If IsCallable(callbackfn) is false, throw a TypeError exception.
const callbackfn = Cast<Callable>(arguments[0]) const callbackfn = Cast<Callable>(arguments[0])
...@@ -29,13 +28,15 @@ namespace typed_array { ...@@ -29,13 +28,15 @@ namespace typed_array {
const thisArg: JSAny = arguments[1]; const thisArg: JSAny = arguments[1];
// 6. Let kept be a new empty List. // 6. Let kept be a new empty List.
// TODO(v8:4153): Support huge TypedArrays here. (growable fixed arrays
// can't be longer than kMaxSmiValue).
let kept = growable_fixed_array::NewGrowableFixedArray(); let kept = growable_fixed_array::NewGrowableFixedArray();
let witness = typed_array::NewAttachedJSTypedArrayWitness(src); let witness = typed_array::NewAttachedJSTypedArrayWitness(src);
// 7. Let k be 0. // 7. Let k be 0.
// 8. Let captured be 0. // 8. Let captured be 0.
// 9. Repeat, while k < len // 9. Repeat, while k < len
for (let k: Smi = 0; k < len; k++) { for (let k: uintptr = 0; k < len; k++) {
witness.Recheck() otherwise IsDetached; witness.Recheck() otherwise IsDetached;
// a. Let Pk be ! ToString(k). // a. Let Pk be ! ToString(k).
...@@ -44,8 +45,11 @@ namespace typed_array { ...@@ -44,8 +45,11 @@ namespace typed_array {
// c. Let selected be ToBoolean(? Call(callbackfn, T, « kValue, k, O // c. Let selected be ToBoolean(? Call(callbackfn, T, « kValue, k, O
// »)). // »)).
const selected: JSAny = // TODO(v8:4153): Consider versioning this loop for Smi and non-Smi
Call(context, callbackfn, thisArg, value, k, witness.GetStable()); // indices to optimize Convert<Number>(k) for the most common case.
const selected: JSAny = Call(
context, callbackfn, thisArg, value, Convert<Number>(k),
witness.GetStable());
// d. If selected is true, then // d. If selected is true, then
// i. Append kValue to the end of kept. // i. Append kValue to the end of kept.
...@@ -56,15 +60,19 @@ namespace typed_array { ...@@ -56,15 +60,19 @@ namespace typed_array {
} }
// 10. Let A be ? TypedArraySpeciesCreate(O, captured). // 10. Let A be ? TypedArraySpeciesCreate(O, captured).
const lengthSmi = Convert<PositiveSmi>(kept.length); const typedArray: JSTypedArray = TypedArraySpeciesCreateByLength(
const typedArray: JSTypedArray = kBuiltinNameFilter, array, Unsigned(kept.length));
TypedArraySpeciesCreateByLength(kBuiltinNameFilter, array, lengthSmi);
// 11. Let n be 0. // 11. Let n be 0.
// 12. For each element e of kept, do // 12. For each element e of kept, do
// a. Perform ! Set(A, ! ToString(n), e, true). // a. Perform ! Set(A, ! ToString(n), e, true).
// b. Increment n by 1. // b. Increment n by 1.
TypedArrayCopyElements(context, typedArray, kept.ToJSArray(), lengthSmi); // TODO(v8:4153): Consider passing growable typed array directly to
// TypedArrayCopyElements() to avoid JSArray materialization. Or collect
// indices instead of values the loop above.
const lengthNumber = Convert<Number>(Unsigned(kept.length));
TypedArrayCopyElements(
context, typedArray, kept.ToJSArray(), lengthNumber);
// 13. Return A. // 13. Return A.
return typedArray; return typedArray;
......
...@@ -84,8 +84,8 @@ namespace typed_array { ...@@ -84,8 +84,8 @@ namespace typed_array {
const count = Convert<PositiveSmi>(IntPtrMax(final - k, 0)); const count = Convert<PositiveSmi>(IntPtrMax(final - k, 0));
// 9. Let A be ? TypedArraySpeciesCreate(O, « count »). // 9. Let A be ? TypedArraySpeciesCreate(O, « count »).
const dest: JSTypedArray = const dest: JSTypedArray = TypedArraySpeciesCreateByLength(
TypedArraySpeciesCreateByLength(kBuiltinNameSlice, src, count); kBuiltinNameSlice, src, Convert<uintptr>(count));
if (count > 0) { if (count > 0) {
try { try {
......
...@@ -112,12 +112,6 @@ namespace typed_array { ...@@ -112,12 +112,6 @@ namespace typed_array {
return lf(context, this.unstable, k); return lf(context, this.unstable, k);
} }
// TODO(v8:4153): Remove this version once all builtins use uintptr indices
// for iteration.
Load(implicit context: Context)(k: Smi): JSAny {
return this.Load(Unsigned(SmiUntag(k)));
}
stable: JSTypedArray; stable: JSTypedArray;
unstable: AttachedJSTypedArray; unstable: AttachedJSTypedArray;
loadfn: LoadFn; loadfn: LoadFn;
......
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