Skip to content

TypedArray: remaining prototype methods (fill/copyWithin/reduce/join/indexOf/slice/…) + instanceof #3148

Description

@proggeramlug

Summary

Follow-up to PR #2499 (which wired TypedArray.prototype map/filter/every/some/forEach/find/findIndex). The remaining %TypedArray%.prototype methods are still unwired or broken, and x instanceof Int32Array is false for any typed array.

Concrete gaps (Perry vs Node)

const a = new Int32Array([1,2,3,4,5]);
a.fill(7);                 // Perry: TypeError "(number).fill is not a function"   Node: [7,7,7,7,7]
a.copyWithin(0,3);         // Perry: "(number).copyWithin is not a function"       Node: [4,5,3,4,5]
a.reverse();               // in-place; Node returns same TA reversed
a.reduce((s,x)=>s+x,0);    // Node 15
a.join("-");               // Node "1-2-3-4-5"
a.indexOf(3);              // Node 2
a.lastIndexOf(3);          // Node 2
a.includes(3);             // Node true
a.slice(1,3);              // Node Int32Array [2,3]   (same-kind TA)
a.subarray(1,3);           // Node Int32Array view [2,3]
a.set([9,9], 1);           // writes 9,9 at offset 1
for (const v of a.keys()) {}    // 0..n-1
for (const v of a.values()) {}  // elements
for (const [i,v] of a.entries()) {}

new Int32Array([1,2,3]) instanceof Int32Array   // Perry: false   Node: true

Where the code lives (pattern is established by PR #2499)

The established pattern: the generic js_array_* helper detects a typed-array receiver via crate::typedarray::lookup_typed_array_kind(arr as usize).is_some() and delegates to a js_typed_array_* impl that reads/writes via load_at/store_at (element-typed).

  • crates/perry-runtime/src/typedarray.rs — add js_typed_array_{fill,copy_within,reverse,reduce,reduce_right,join,index_of,last_index_of,includes,slice,subarray,set,keys,values,entries} (mirror the map/filter/find_last impls already there). map/filter/slice/subarray return a same-kind TypedArray.
  • crates/perry-runtime/src/array/{concat_reverse.rs (js_array_fill, js_array_reverse), immutable.rs (js_array_copy_within), iter_methods.rs (reduce, join), search.rs (indexOf/lastIndexOf/includes)} — add the lookup_typed_array_kind delegation guard at the top (mirror array/sort.rs lines ~25 and PR fix(runtime): TypedArray map/filter/every/some/forEach/find/findIndex read element-typed storage #2499's guards in iter_methods.rs).
  • Lowering routing for fill/copyWithin/reverse: these currently error "(number).fill is not a function" because crates/perry-hir/src/lower/expr_call/local_array_methods.rs does NOT route them to the generic Expr::Array* variants for TA receivers — add them to the typed-array-allowed method match there (the is_typed_array arm around the map/filter block).
  • instanceof Int32Array: crates/perry-runtime/src/object/instanceof.rs (js_instanceof / js_instanceof_dynamic) + the codegen class-id map in crates/perry-codegen/src/expr/instance_misc1.rs — add reserved class-ids per TA kind detected via lookup_typed_array_kind.

Acceptance criteria

  • All methods above byte-match node --experimental-strip-types for Int32Array AND Float64Array (different element kinds).
  • map/filter/slice/subarray return a same-kind TypedArray; x instanceof Int32Array true (and false for the wrong kind / non-TA).
  • Regular-array versions of these methods unaffected (the guard only fires for genuine TA pointers).
  • scripts/test262_subset.py --dir built-ins/TypedArray --max 300 improves; no regression in built-ins/Array.

Refs: PR #2499 (the pattern to copy), Test262 radar #799.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew capability or improvementparityCompatibility gap with Node.js, ECMAScript, or the supported ecosystem

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions