fix(runtime): TypedArray lastIndexOf routes to array path, not string (#2457) - #2501
Merged
Merged
Conversation
…ath (#2457) `new Int32Array([1,2,3,2,1]).lastIndexOf(2)` threw `TypeError: (number).lastIndexOf is not a function`, while `.indexOf` / `.includes` worked. The HIR lowered `lastIndexOf` on a known-not-string / typed-array local to the *string* `lastIndexOf` (`js_string_last_index_of`); at runtime the typed-array pointer reads as a number, so the string dispatch failed. `indexOf`/`includes` avoided this via dedicated `Expr::ArrayIndexOf`/`ArrayIncludes` lowering — `lastIndexOf` had no equivalent. Add a parallel `Expr::ArrayLastIndexOf { array, value, from_index }`: * HIR (`local_array_methods`): emit it for a known-not-string / typed-array local, and add `lastIndexOf` to the ambiguous-method set so an *unknown*-typed local (which may be a string) still falls through to the string path. * Codegen (`misc_methods`): lower to `js_array_last_index_of_jsvalue` (value + optional fromIndex with the spec clamping), mirroring the `lower_array_method::lastIndexOf` arm. * Runtime (`array/search`): re-add the typed-array branch to `js_array_last_index_of_jsvalue` (now reached) — backward strict-equality scan over the typed store via `js_typed_array_get`. * Wire the new variant through the walkers, collectors, monomorph substitute, and stable-hash (exhaustive matches). Uint8Array/Buffer keep their existing byte-level runtime dispatch (they skip the array block). Verified byte-for-byte vs node: Int32Array/Float64Array `lastIndexOf` (+ fromIndex, +NaN), and regular-array / string / any-typed `lastIndexOf` all unchanged. Adds a `typed_array_last_index_of` unit test.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #2457.
Problem
new Int32Array([1,2,3,2,1]).lastIndexOf(2)threwTypeError: (number).lastIndexOf is not a function, while.indexOf/.includesworked (fixed in #2456).A
--trace llvmshowed the divergence:ta.indexOf→js_array_indexOf_jsvalue(array path), butta.lastIndexOf→js_string_last_index_of(string path). The HIR lowerslastIndexOfon a known-not-string / typed-array local to the string method; at runtime the typed-array pointer reads as a number, so the string dispatch throws.indexOf/includesavoided this via dedicatedExpr::ArrayIndexOf/ArrayIncludeslowering —lastIndexOfhad no equivalent.Fix
Add a parallel
Expr::ArrayLastIndexOf { array, value, from_index }:local_array_methods): emit it for a known-not-string / typed-array local; addlastIndexOfto the ambiguous-method set so an unknown-typed local (possibly a string) still falls through to the string path.misc_methods): lower tojs_array_last_index_of_jsvalue(value + optionalfromIndexwith spec clamping), mirroring thelower_array_method::lastIndexOfarm.array/search): re-add the typed-array branch tojs_array_last_index_of_jsvalue(now reached) — backward strict-equality scan over the typed store viajs_typed_array_get.Uint8Array/Bufferkeep their existing byte-level runtime dispatch (they skip the array block).Testing
cargo test -p perry-runtime --lib→ 795 passed, 0 failed (addstyped_array_last_index_of).node, all match:Int32Array/Float64ArraylastIndexOf(incl.fromIndexandNaN),any-typed-paramlastIndexOf(unchanged — no mis-routing),Uint8ArraylastIndexOf(byte dispatch unchanged).Follow-up to #2456 (TypedArray indexOf/includes); part of #2447.