Skip to content

Truly independent web browser

LibJS: Iterate TypedArray find indices without collecting them

Shannon Boothd3a491b6caf0
diff --git a/Libraries/LibJS/Rust/src/runtime/typed_array_prototype.rs b/Libraries/LibJS/Rust/src/runtime/typed_array_prototype.rs
index e0e20c96aba..8f5c2b14dfd 100644
--- a/Libraries/LibJS/Rust/src/runtime/typed_array_prototype.rs
+++ b/Libraries/LibJS/Rust/src/runtime/typed_array_prototype.rs
@@ -158,15 +158,20 @@ fn find_via_predicate(
// 1. If IsCallable(predicate) is false, throw a TypeError exception.
let predicate = callback_from_args(vm, prototype_name)?;
+ let mut ascending_indices;
+ let mut descending_indices;
+
// 2. If direction is ascending, then
- let indices: Vec<u32> = if direction == Direction::Ascending {
+ let indices: &mut dyn Iterator<Item = u32> = if direction == Direction::Ascending {
// a. Let indices be a List of the integers in the interval from 0 (inclusive) to len (exclusive), in ascending order.
- (0..length).collect()
+ ascending_indices = 0..length;
+ &mut ascending_indices
}
// 3. Else,
else {
// a. Let indices be a List of the integers in the interval from 0 (inclusive) to len (exclusive), in descending order.
- (0..length).rev().collect()
+ descending_indices = (0..length).rev();
+ &mut descending_indices
};
// 4. For each integer k of indices, do
diff --git a/Tests/LibJS/Runtime/builtins/TypedArray/typed-array-length-above-int32-max.js b/Tests/LibJS/Runtime/builtins/TypedArray/typed-array-length-above-int32-max.js
index 518e90b5b13..40f2956ef13 100644
--- a/Tests/LibJS/Runtime/builtins/TypedArray/typed-array-length-above-int32-max.js
+++ b/Tests/LibJS/Runtime/builtins/TypedArray/typed-array-length-above-int32-max.js
@@ -10,6 +10,13 @@ test("copying into a fixed-length view", () => {
});
});
+test("find, findIndex, findLast and findLastIndex", () => {
+ expect(typedArray.find(value => value === 0)).toBe(0);
+ expect(typedArray.findIndex(value => value === 0)).toBe(0);
+ expect(typedArray.findLast(value => value === 22)).toBe(22);
+ expect(typedArray.findLastIndex(value => value === 22)).toBe(length - 1);
+});
+
test("lastIndexOf", () => {
expect(typedArray.lastIndexOf(22)).toBe(length - 1);
expect(typedArray.lastIndexOf(11, -2)).toBe(length - 2);