Skip to content

fix: allow wasm memory growth for sources over ~4MB#217

Merged
guybedford merged 1 commit into
mainfrom
fix/wasm-memory-growth
Jul 2, 2026
Merged

fix: allow wasm memory growth for sources over ~4MB#217
guybedford merged 1 commit into
mainfrom
fix/wasm-memory-growth

Conversation

@guybedford

@guybedford guybedford commented Jul 2, 2026

Copy link
Copy Markdown
Owner

Fixes #214.

Root cause

The emsdk toolchain update in #203 (shipped in 2.2.0) regressed the wasm memory declaration. The old fastcomp build emitted growable memory (min 1 page, no maximum), but the upstream emscripten build defaults to fixed-size memory:

Build flags initial maximum
2.1.0 0x0 1 page none (growable to 4 GiB engine cap)
2.2.0 0x1 258 pages (~16 MiB) 258 pages (~16 MiB)

With maximum == initial, the unconditional memory.grow() in parse() throws WebAssembly.Memory.grow(): Maximum memory size exceeded for any source that doesn't fit the initial 16 MiB. Since the source is copied in as UTF-16 (4 * (len + 1) bytes), that's the ~4M char threshold reported in the issue. The asm.js build was unaffected.

Fix

Add -s ALLOW_MEMORY_GROWTH=1 to the emcc build, restoring growable memory (max 32768 pages / 2 GiB, emscripten's default ceiling — ~512M chars of source).

The issue repro now passes against the rebuilt dist/lexer.js:

import { parse, init } from 'es-module-lexer';
await init;
const big = 'const x = "' + 'a'.repeat(5 * 1024 * 1024) + '";\n';
parse(big); // OK (previously threw)

Also adds a 5MB large-source regression test, verified to fail against a fresh build off main and pass with this change; chomp test passes (149 tests, wasm + asm).

The emsdk toolchain update in #203 dropped the growable memory the old
fastcomp build emitted: the wasm memory was declared with
maximum == initial (258 pages / ~16MiB), so the unconditional
memory.grow() in parse() throws "Maximum memory size exceeded" for any
source over ~4M chars (stored as 4 bytes per char).

Add -s ALLOW_MEMORY_GROWTH=1 to restore growable memory, and a 5MB
large source regression test.

Fixes #214
@guybedford
guybedford force-pushed the fix/wasm-memory-growth branch from d1c8c21 to cec78bb Compare July 2, 2026 00:50
@guybedford
guybedford enabled auto-merge (squash) July 2, 2026 00:51
@guybedford
guybedford merged commit e9b611e into main Jul 2, 2026
1 check passed
@guybedford
guybedford deleted the fix/wasm-memory-growth branch July 2, 2026 00:52
guybedford added a commit that referenced this pull request Jul 2, 2026
…e readme (#219)

The minimal wasm build added in #211 predates the #217 memory growth fix
and was missing -s ALLOW_MEMORY_GROWTH=1, so parse() on sources over ~4M
chars threw "Maximum memory size exceeded" (the Large source test from
#217 catches this under test:minimal:wasm).

Also documents the es-module-lexer/minimal build in the readme, and
corrects the stale comments claiming dynamic-import n / ip() are dropped
in the minimal build: _ip is exported and read, only at/ss/f/ms go.
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.

parse() throws "WebAssembly.Memory.grow(): Maximum memory size exceeded" on inputs larger than ~4 MB (regression in 2.2.0)

1 participant