Repository navigation
fix(array): validate .with/unshift/toSpliced/sort comparator semantics - #3375
Merged
Merged
Conversation
…2792-2814-2794-2796 # Conflicts: # crates/perry-runtime/src/array/mod.rs
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.
Closes #2792
Closes #2814
Closes #2794
Closes #2796
Brings four
Array/TypedArrayprototype methods into Node parity. All changes are runtime + lowering only (no Cargo/CLAUDE/CHANGELOG edits — maintainer folds those in at merge).Implementation
#2792 —
.withthrows RangeError for out-of-range indexesjs_array_with(array/immutable.rs): NaN coerces to index 0; non-finite (±Infinity) and out-of-range positive/too-negative indexes now throwRangeError: Invalid index : <idx>instead of returning an unchanged copy. Non-finite values are checked before anyas isizecast.js_typed_array_with(typedarray.rs): same coercion/validation, throwingRangeError: Invalid typed array index(Node's typed-array message). Note: the typed-array.withvalue-materialization path (Array.from(u.with(...))) is broken onmainindependently of this change, so the typed-array.withRangeError is implemented but not yet observable end-to-end; the runtime code is correct and activates once that separate pre-existing routing bug is fixed.#2814 — zero and variadic
Array.prototype.unshiftjs_array_unshift_variadic(arr, items, count)inserts N items at the front in source order, returns the (possibly relocated) header.lower_array_method.rs: 0 args returns the current length with no mutation; 1+ args go through the variadic helper. HIR single-value fast path (local_array_methods.rs) is now restricted to exactly 1 arg so 0/2+ fall through to generic dispatch.native_call_method.rs):unshiftnow handles 0 args (returns length) and N args (variadic helper).#[used]keepalive anchor + codegen declaration added (auto-optimize dead-strips codegen-only symbols otherwise).#2794 — omitted
deleteCountforArray.prototype.toSplicedarray_only/local/imported/inline) drop theargs.len() >= 2guard: 0 args -> shallow copy (start 0, deleteCount 0); 1 arg -> delete from start through end (deleteCount+Infinity, clamped in the runtime); 2+ args keep explicitdeleteCount(incl.undefined -> 0).js_array_to_spliced(array/immutable.rs): start/deleteCount normalization rewritten as ECMA ToIntegerOrInfinity (NaN -> 0,+Infinitystart -> len,+InfinitydeleteCount -> delete-through-end) with noas isizecast on non-finite values.#2796 — validate sort comparators before sorting
js_validate_array_comparator(cmp): returns the closure pointer for a function,0forundefined(default sort), or throwsTypeError: The comparison function must be either a function or undefined: <value>for anything else.bigint_set.rs,os_uri_dates.rs,lower_array_method.rs) route the raw NaN-boxed comparator through the validator before unboxing.array/sort.rs,array/immutable.rs,typedarray.rs) treat a null comparator as the default-sort path.native_call_method.rs) for Array and TypedArraysort/toSortedvalidates the raw arg (previously a non-callable likenull/1was silently treated as the default sort).#[used]keepalive anchor + codegen declaration added.Validation
test-files/test_gap_2792_2814_2794_2796_array_validation.tscovers every case from the four issue bodies. The default (auto-optimize) compile output is byte-identical tonode --experimental-strip-types.cargo test --release -p perry-runtime arrayand... typedarrayare green.cargo fmt --all -- --checkclean.test_array_methods.ts,test_gap_array_methods.ts,test_edge_arrays.ts— all byte-identical to Node.