Skip to content

a[i] = v past the end of an array grows it to i + 1, as in JavaScript - #979

Merged
ASDAlexander77 merged 4 commits into
mainfrom
array-write-grows
Nov 21, 2026
Merged

ASDAlexander77 merged 4 commits into
mainfrom
array-write-grows

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

a[i] = v with i at or past the end of an array now grows it to i + 1, as in JavaScript (a[a.length] = v appends). The slots in between read as the element type's default.

Before this change, the length stayed the same and the write went past the end of the block. Under gc it landed in whatever memory was there; under rc and own it crashed.

  • How it works: before the store, MLIRGen compares the index with the length. Only when the index is past the end does it call a private .array_grow.<type> function, which runs SetLengthOf. That function is shared by all writes to arrays of the same type, so the inline code stays small: inlining the growth at every write took largeControlFlowGraph from 5 s to 71 s.
  • Memory models: gc, rc and none. Under -mm=own the ownership passes read the grown slot as a keep, so own keeps the old behaviour for now; this is tracked in TSLANG 152.
  • Test: 00array_write_grows.ts (compile, jit, jit rc).

Verified: ctest 8788/0 after rebasing onto #977 and #978. Before the rebase: the wasm32 rc and none tester suites were clean, and the full tsc sweep was identical to the baseline (3256 pass).

🤖 Generated with Claude Code

ASDAlexander77 and others added 4 commits November 21, 2026 18:18
`a[a.length] = v` and `s[i] = ...` in a loop over an empty array left the length as it was and wrote past the end
of the block: under gc into whatever lay there, under rc and own a crash (TSLANG 152). The element store, which
takes the element's address anew after the right side ran, now grows the array first when the index is at or
past the length (mlirGenGrowArrayForWrite: SetLengthOf, which zeroes the slots in between - a hole reads as the
element type's default). A negative index grows nothing; an array with no place to update (made for the
statement) is left as it was. Test 00array_write_grows.ts (compile, JIT, rc, own).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…rown slot as a keep); TSLANG 152

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
SetLengthOf lowers to a capacity check, a reallocation and the zeroing of the new slots; made at every element
write, the sweep's largeControlFlowGraph (10000 `data[0] = 0`) took 71 s instead of 5 s, all of it lowering to
LLVM. The length check stays at the write; the growth is a call of `.array_grow.<type>(array, length)`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…growth not there yet, TSLANG 152)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit 852fc3a into main Nov 21, 2026
10 checks passed
@ASDAlexander77
ASDAlexander77 deleted the array-write-grows branch November 21, 2026 19:22
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