Skip to content

Commit

Permalink
feat(ecmascript): %TypedArray%.prototype.forEach (#553)
Browse files Browse the repository at this point in the history
  • Loading branch information
yossydev authored Feb 5, 2025
1 parent 9dcd62d commit 701e843
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -723,13 +723,78 @@ impl TypedArrayPrototype {
todo!()
}

// ### [ 23.2.3.15 %TypedArray%.prototype.forEach ( callback [ , thisArg ] )](https://tc39.es/ecma262/multipage/indexed-collections.html#sec-%typedarray%.prototype.foreach)
// The interpretation and use of the arguments of this method are the same as for Array.prototype.forEach as defined in 23.1.3.15.
fn for_each(
_agent: &mut Agent,
_this_value: Value,
_: ArgumentsList,
_gc: GcScope,
agent: &mut Agent,
this_value: Value,
arguments: ArgumentsList,
mut gc: GcScope,
) -> JsResult<Value> {
todo!()
let callback = arguments.get(0).bind(gc.nogc());
let this_arg = arguments.get(1).bind(gc.nogc());
// 1. Let O be the this value.
let o = this_value;
// 2. Let taRecord be ? ValidateTypedArray(O, seq-cst).
let ta_record = validate_typed_array(agent, o, Ordering::SeqCst, gc.nogc())?;
// 3. Let len be TypedArrayLength(taRecord).
let mut o = ta_record.object;
let scoped_o = o.scope(agent, gc.nogc());
let len = match o {
TypedArray::Int8Array(_)
| TypedArray::Uint8Array(_)
| TypedArray::Uint8ClampedArray(_) => {
typed_array_length::<u8>(agent, &ta_record, gc.nogc())
}
TypedArray::Int16Array(_) | TypedArray::Uint16Array(_) => {
typed_array_length::<u16>(agent, &ta_record, gc.nogc())
}
#[cfg(feature = "proposal-float16array")]
TypedArray::Float16Array(_) => typed_array_length::<f16>(agent, &ta_record, gc.nogc()),
TypedArray::Int32Array(_)
| TypedArray::Uint32Array(_)
| TypedArray::Float32Array(_) => {
typed_array_length::<u32>(agent, &ta_record, gc.nogc())
}
TypedArray::BigInt64Array(_)
| TypedArray::BigUint64Array(_)
| TypedArray::Float64Array(_) => {
typed_array_length::<u64>(agent, &ta_record, gc.nogc())
}
};
// 4. If IsCallable(callback) is false, throw a TypeError exception.
let Some(callback) = is_callable(callback, gc.nogc()) else {
return Err(agent.throw_exception_with_static_message(
ExceptionType::TypeError,
"Callback is not callable",
gc.nogc(),
));
};
let callback = callback.scope(agent, gc.nogc());
// 5. Let k be 0.
let mut k = 0;
// 6. Repeat, while k < len,
while k < len {
// a. Let Pk be ! ToString(𝔽(k)).
let pk: PropertyKey = k.try_into().unwrap();
// b. Let kValue be ! Get(O, Pk).
let k_value = unwrap_try(try_get(agent, o, pk, gc.nogc()));
// c. Perform ? Call(callback, thisArg, « kValue, 𝔽(k), O »).
// // SAFETY: pk is Integer, which is what we want for fk as well.
let fk = unsafe { pk.into_value_unchecked() };
call_function(
agent,
callback.get(agent),
this_arg,
Some(ArgumentsList(&[k_value, fk, o.into_value()])),
gc.reborrow(),
)?;
// d. Set k to k + 1.
k += 1;
o = scoped_o.get(agent).bind(gc.nogc());
}
// 7. Return undefined.
Ok(Value::Undefined)
}

fn includes(
Expand Down
54 changes: 8 additions & 46 deletions tests/expectations.json
Original file line number Diff line number Diff line change
Expand Up @@ -3581,7 +3581,7 @@
"built-ins/RegExp/property-escapes/character-class.js": "CRASH",
"built-ins/RegExp/property-escapes/generated/ASCII.js": "CRASH",
"built-ins/RegExp/property-escapes/generated/ASCII_Hex_Digit.js": "CRASH",
"built-ins/RegExp/property-escapes/generated/Alphabetic.js": "TIMEOUT",
"built-ins/RegExp/property-escapes/generated/Alphabetic.js": "CRASH",
"built-ins/RegExp/property-escapes/generated/Any.js": "CRASH",
"built-ins/RegExp/property-escapes/generated/Assigned.js": "CRASH",
"built-ins/RegExp/property-escapes/generated/Bidi_Control.js": "CRASH",
Expand Down Expand Up @@ -3616,7 +3616,7 @@
"built-ins/RegExp/property-escapes/generated/General_Category_-_Final_Punctuation.js": "CRASH",
"built-ins/RegExp/property-escapes/generated/General_Category_-_Format.js": "CRASH",
"built-ins/RegExp/property-escapes/generated/General_Category_-_Initial_Punctuation.js": "CRASH",
"built-ins/RegExp/property-escapes/generated/General_Category_-_Letter.js": "TIMEOUT",
"built-ins/RegExp/property-escapes/generated/General_Category_-_Letter.js": "CRASH",
"built-ins/RegExp/property-escapes/generated/General_Category_-_Letter_Number.js": "CRASH",
"built-ins/RegExp/property-escapes/generated/General_Category_-_Line_Separator.js": "CRASH",
"built-ins/RegExp/property-escapes/generated/General_Category_-_Lowercase_Letter.js": "CRASH",
Expand All @@ -3641,15 +3641,15 @@
"built-ins/RegExp/property-escapes/generated/General_Category_-_Surrogate.js": "CRASH",
"built-ins/RegExp/property-escapes/generated/General_Category_-_Symbol.js": "CRASH",
"built-ins/RegExp/property-escapes/generated/General_Category_-_Titlecase_Letter.js": "CRASH",
"built-ins/RegExp/property-escapes/generated/General_Category_-_Unassigned.js": "TIMEOUT",
"built-ins/RegExp/property-escapes/generated/General_Category_-_Unassigned.js": "CRASH",
"built-ins/RegExp/property-escapes/generated/General_Category_-_Uppercase_Letter.js": "CRASH",
"built-ins/RegExp/property-escapes/generated/Grapheme_Base.js": "TIMEOUT",
"built-ins/RegExp/property-escapes/generated/Grapheme_Base.js": "CRASH",
"built-ins/RegExp/property-escapes/generated/Grapheme_Extend.js": "CRASH",
"built-ins/RegExp/property-escapes/generated/Hex_Digit.js": "CRASH",
"built-ins/RegExp/property-escapes/generated/IDS_Binary_Operator.js": "CRASH",
"built-ins/RegExp/property-escapes/generated/IDS_Trinary_Operator.js": "CRASH",
"built-ins/RegExp/property-escapes/generated/ID_Continue.js": "TIMEOUT",
"built-ins/RegExp/property-escapes/generated/ID_Start.js": "TIMEOUT",
"built-ins/RegExp/property-escapes/generated/ID_Continue.js": "CRASH",
"built-ins/RegExp/property-escapes/generated/ID_Start.js": "CRASH",
"built-ins/RegExp/property-escapes/generated/Ideographic.js": "CRASH",
"built-ins/RegExp/property-escapes/generated/Join_Control.js": "CRASH",
"built-ins/RegExp/property-escapes/generated/Logical_Order_Exception.js": "CRASH",
Expand Down Expand Up @@ -4008,8 +4008,8 @@
"built-ins/RegExp/property-escapes/generated/Uppercase.js": "CRASH",
"built-ins/RegExp/property-escapes/generated/Variation_Selector.js": "CRASH",
"built-ins/RegExp/property-escapes/generated/White_Space.js": "CRASH",
"built-ins/RegExp/property-escapes/generated/XID_Continue.js": "TIMEOUT",
"built-ins/RegExp/property-escapes/generated/XID_Start.js": "TIMEOUT",
"built-ins/RegExp/property-escapes/generated/XID_Continue.js": "CRASH",
"built-ins/RegExp/property-escapes/generated/XID_Start.js": "CRASH",
"built-ins/RegExp/property-escapes/generated/strings/Basic_Emoji-negative-CharacterClass.js": "FAIL",
"built-ins/RegExp/property-escapes/generated/strings/Basic_Emoji-negative-P.js": "FAIL",
"built-ins/RegExp/property-escapes/generated/strings/Basic_Emoji-negative-u.js": "FAIL",
Expand Down Expand Up @@ -9903,44 +9903,8 @@
"built-ins/TypedArray/prototype/findLastIndex/return-negative-one-if-predicate-returns-false-value.js": "CRASH",
"built-ins/TypedArray/prototype/findLastIndex/this-is-not-object.js": "CRASH",
"built-ins/TypedArray/prototype/findLastIndex/this-is-not-typedarray-instance.js": "CRASH",
"built-ins/TypedArray/prototype/forEach/BigInt/arraylength-internal.js": "CRASH",
"built-ins/TypedArray/prototype/forEach/BigInt/callbackfn-arguments-with-thisarg.js": "CRASH",
"built-ins/TypedArray/prototype/forEach/BigInt/callbackfn-arguments-without-thisarg.js": "CRASH",
"built-ins/TypedArray/prototype/forEach/BigInt/callbackfn-detachbuffer.js": "CRASH",
"built-ins/TypedArray/prototype/forEach/BigInt/callbackfn-is-not-callable.js": "CRASH",
"built-ins/TypedArray/prototype/forEach/BigInt/callbackfn-no-interaction-over-non-integer.js": "CRASH",
"built-ins/TypedArray/prototype/forEach/BigInt/callbackfn-not-called-on-empty.js": "CRASH",
"built-ins/TypedArray/prototype/forEach/BigInt/callbackfn-return-does-not-change-instance.js": "CRASH",
"built-ins/TypedArray/prototype/forEach/BigInt/callbackfn-returns-abrupt.js": "CRASH",
"built-ins/TypedArray/prototype/forEach/BigInt/callbackfn-set-value-during-interaction.js": "CRASH",
"built-ins/TypedArray/prototype/forEach/BigInt/callbackfn-this.js": "CRASH",
"built-ins/TypedArray/prototype/forEach/BigInt/detached-buffer.js": "CRASH",
"built-ins/TypedArray/prototype/forEach/BigInt/return-abrupt-from-this-out-of-bounds.js": "CRASH",
"built-ins/TypedArray/prototype/forEach/BigInt/returns-undefined.js": "CRASH",
"built-ins/TypedArray/prototype/forEach/BigInt/values-are-not-cached.js": "CRASH",
"built-ins/TypedArray/prototype/forEach/arraylength-internal.js": "CRASH",
"built-ins/TypedArray/prototype/forEach/callbackfn-arguments-with-thisarg.js": "CRASH",
"built-ins/TypedArray/prototype/forEach/callbackfn-arguments-without-thisarg.js": "CRASH",
"built-ins/TypedArray/prototype/forEach/callbackfn-detachbuffer.js": "CRASH",
"built-ins/TypedArray/prototype/forEach/callbackfn-is-not-callable.js": "CRASH",
"built-ins/TypedArray/prototype/forEach/callbackfn-no-interaction-over-non-integer.js": "CRASH",
"built-ins/TypedArray/prototype/forEach/callbackfn-not-called-on-empty.js": "CRASH",
"built-ins/TypedArray/prototype/forEach/callbackfn-resize.js": "CRASH",
"built-ins/TypedArray/prototype/forEach/callbackfn-return-does-not-change-instance.js": "CRASH",
"built-ins/TypedArray/prototype/forEach/callbackfn-returns-abrupt.js": "CRASH",
"built-ins/TypedArray/prototype/forEach/callbackfn-set-value-during-interaction.js": "CRASH",
"built-ins/TypedArray/prototype/forEach/callbackfn-this.js": "CRASH",
"built-ins/TypedArray/prototype/forEach/detached-buffer.js": "CRASH",
"built-ins/TypedArray/prototype/forEach/invoked-as-func.js": "CRASH",
"built-ins/TypedArray/prototype/forEach/invoked-as-method.js": "CRASH",
"built-ins/TypedArray/prototype/forEach/resizable-buffer-grow-mid-iteration.js": "CRASH",
"built-ins/TypedArray/prototype/forEach/resizable-buffer-shrink-mid-iteration.js": "CRASH",
"built-ins/TypedArray/prototype/forEach/resizable-buffer.js": "CRASH",
"built-ins/TypedArray/prototype/forEach/return-abrupt-from-this-out-of-bounds.js": "CRASH",
"built-ins/TypedArray/prototype/forEach/returns-undefined.js": "CRASH",
"built-ins/TypedArray/prototype/forEach/this-is-not-object.js": "CRASH",
"built-ins/TypedArray/prototype/forEach/this-is-not-typedarray-instance.js": "CRASH",
"built-ins/TypedArray/prototype/forEach/values-are-not-cached.js": "CRASH",
"built-ins/TypedArray/prototype/includes/BigInt/detached-buffer-during-fromIndex-returns-false-for-zero.js": "CRASH",
"built-ins/TypedArray/prototype/includes/BigInt/detached-buffer-during-fromIndex-returns-true-for-undefined.js": "CRASH",
"built-ins/TypedArray/prototype/includes/BigInt/detached-buffer.js": "CRASH",
Expand Down Expand Up @@ -11372,8 +11336,6 @@
"language/block-scope/syntax/redeclaration/inner-block-var-redeclaration-attempt-after-function.js": "FAIL",
"language/block-scope/syntax/redeclaration/var-name-redeclaration-attempt-with-function.js": "FAIL",
"language/block-scope/syntax/redeclaration/var-redeclaration-attempt-after-function.js": "FAIL",
"language/comments/S7.4_A5.js": "TIMEOUT",
"language/comments/S7.4_A6.js": "TIMEOUT",
"language/comments/hashbang/eval-indirect.js": "FAIL",
"language/comments/hashbang/use-strict.js": "CRASH",
"language/computed-property-names/object/accessor/getter-super.js": "CRASH",
Expand Down
10 changes: 5 additions & 5 deletions tests/metrics.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"results": {
"crash": 13225,
"fail": 9061,
"pass": 24456,
"skip": 55,
"timeout": 4,
"crash": 13174,
"fail": 9070,
"pass": 24492,
"skip": 65,
"timeout": 0,
"unresolved": 0
},
"total": 46801
Expand Down
26 changes: 18 additions & 8 deletions tests/skip.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,21 @@
"built-ins/Object/defineProperty/15.2.3.6-4-183.js",
"built-ins/parseFloat/S15.1.2.3_A6.js",
"built-ins/parseInt/S15.1.2.2_A8.js",
"built-ins/RegExp/property-escapes/generated/Alphabetic.js",
"built-ins/RegExp/property-escapes/generated/General_Category_-_Letter.js",
"built-ins/RegExp/property-escapes/generated/General_Category_-_Other_Letter.js",
"built-ins/RegExp/property-escapes/generated/General_Category_-_Unassigned.js",
"built-ins/RegExp/property-escapes/generated/Grapheme_Base.js",
"built-ins/RegExp/property-escapes/generated/ID_Continue.js",
"built-ins/RegExp/property-escapes/generated/ID_Start.js",
"built-ins/RegExp/property-escapes/generated/XID_Continue.js",
"built-ins/RegExp/property-escapes/generated/XID_Start.js",
"built-ins/TypedArray/prototype/copyWithin/coerced-values-end-detached-prototype.js",
"built-ins/TypedArray/prototype/copyWithin/coerced-values-end-detached.js",
"built-ins/TypedArray/prototype/copyWithin/coerced-values-start-detached.js",
"built-ins/RegExp/property-escapes/generated/General_Category_-_Other_Letter.js",
"harness/propertyhelper-verifywritable-array-length.js",
"language/comments/S7.4_A5.js",
"language/comments/S7.4_A6.js",
"language/identifiers/part-unicode-15.1.0-escaped.js",
"language/identifiers/part-unicode-15.1.0.js",
"language/identifiers/part-unicode-16.0.0-class-escaped.js",
Expand All @@ -49,13 +59,13 @@
"language/identifiers/start-unicode-8.0.0.js",
"language/identifiers/start-unicode-9.0.0-escaped.js",
"language/identifiers/start-unicode-9.0.0.js",
"staging/sm/Proxy/ownkeys-linear.js",
"staging/sm/class/compPropNames.js",
"staging/sm/regress/regress-610026.js",
"staging/sm/Array/length-truncate-nonconfigurable-sparse.js",
"staging/sm/Array/length-truncate-with-indexed.js",
"staging/sm/Array/sort_small.js",
"staging/sm/String/normalize-generateddata-input.js",
"staging/sm/class/compPropNames.js",
"staging/sm/JSON/parse-reviver-array-delete.js",
"staging/sm/Array/length-truncate-nonconfigurable-sparse.js",
"staging/sm/Array/length-truncate-with-indexed.js"
"staging/sm/Proxy/ownkeys-linear.js",
"staging/sm/regress/regress-610026.js",
"staging/sm/String/normalize-generateddata-input.js"
]
}
}

0 comments on commit 701e843

Please sign in to comment.