Skip to content

fix: handle negative indices in array slice#131

Merged
cs01 merged 1 commit intomainfrom
fix/array-slice-negative-indices
Mar 9, 2026
Merged

fix: handle negative indices in array slice#131
cs01 merged 1 commit intomainfrom
fix/array-slice-negative-indices

Conversation

@cs01
Copy link
Owner

@cs01 cs01 commented Mar 8, 2026

Summary

  • arr.slice(0, -1), arr.slice(-2), etc. now work correctly for number[], string[], and object[] arrays
  • Previously, negative indices were passed directly to fptosi → i32 without adjustment, producing wrong results
  • Added normalizeSliceIndex() helper that matches string slice behavior: detect negative → add length → clamp to [0, len]
  • Also clamps final slice length to ≥0 to prevent negative-length memcpy

Test plan

  • Added tests/fixtures/arrays/array-slice-negative.ts covering negative start, negative end, and both for number[] and string[]
  • All 434 tests pass (433 existing + 1 new)
  • Self-hosting verification passes (Stage 0 + Stage 1)

🤖 Generated with Claude Code

…bject arrays

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@cs01 cs01 merged commit 9b204f0 into main Mar 9, 2026
12 checks passed
@cs01 cs01 deleted the fix/array-slice-negative-indices branch March 9, 2026 04:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant