Skip to content

TypedArray lastIndexOf mis-lowered to string path (returns wrong result / crashes 2-arg) #2457

Description

@proggeramlug

Subset of #2447.

Array.prototype.lastIndexOf works for plain arrays but returns the wrong result (and crashes in the 2-arg form) for TypedArray receivers, because at the HIR level lastIndexOf on a TypedArray is lowered to the string lastIndexOf path (js_string_last_index_of) instead of the array path (js_array_last_index_of_jsvalue).

Repro

const ta = new Int32Array([1, 2, 3, 2, 1]);
console.log(ta.lastIndexOf(2));     // Perry: -1   node: 3
console.log(ta.lastIndexOf(2, 2));  // Perry: TypeError "(number).lastIndexOf is not a function"   node: 1

Evidence

perry compile --trace llvm shows ta.lastIndexOf(2) emitting call i32 @js_string_last_index_of, while ta.indexOf(2) correctly emits call i32 @js_array_indexOf_jsvalue.

Fix direction

Route TypedArray (and array) lastIndexOf to js_array_last_index_of_jsvalue in the HIR method-dispatch (crates/perry-hir/src/lower/array_fold.rs / the expr_call/*_array_methods.rs lowering that currently treats lastIndexOf as string-or-array). The runtime side (js_array_last_index_of_jsvalue) already handles plain arrays with full fromIndex clamping; it just needs a TypedArray branch mirroring the indexOf/includes one added in #2456 (as_typed_array + js_typed_array_get). PR #2456 left an explanatory NB comment at that runtime site.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions