WebAssembly modules use native WebAssembly exceptions
Date: 2026-12-03
Area: build, apps, website
Refines: ADR 0235 and ADR 0244
Related: ADR 0236,
ADR 0245,
ADR 0247
Context
By default FreePascal compiles wasm32-wasip1 with branchful exceptions (-CTbfexceptions): after nearly every call, the compiler emits an i32.load8_u check on an exception flag followed by br_if to unwind. In a --prod GocciaRunner module with ICU4C linked in, that added 79,965 post-call flag checks and 85,980 extra br_if instructions across 116,714 direct call sites.
fpc-toolchain 3.3.1+13 (release 3.3.1-b13, PR frostney/fpc-toolchain#47) ships a matching wasm32-wasip1 RTL and package set built with standardized native WebAssembly exceptions (-CTwasmexceptions, using exnref and try_table) alongside the branchful units. Under -CTwasmexceptions, fpc.cfg selects those units automatically and keeps wasi-sdk 34's noeh/ C++ libraries so -fno-exceptions ICU4C continues to link unchanged.
We measured the switch before changing the build:
-
Disassembly (
wasm-tools print,--prodGocciaRunnerwith ICU4C):br_if:143,435->57,455(-59.9%)- Post-call
i32.load8_u+br_ifchecks:79,965->161(-99.8%; the remaining 161 are ordinary boolean field/byte checks) - Direct
call:116,714->107,819(-7.6%) try_table:0->5,264(all catching FreePascal's single private tagtag 0, zerocatch_all, so host JS exceptions such asWasiExitandWasiKillunwind cleanly)throw:0->3,633
-
Module size (bytes;
--prodwith ICU4C linked, gzip-9, brotli-q 11; QuickJS-ng v0.18.0qjs-wasi.wasmwithout ICU shown for scale):Module Branchful ( 3.3.1+12)Native exnref(3.3.1+13)Delta GocciaRunnerraw16,923,259 16,037,542 -5.23% (-885,717 B) GocciaRunnergzip3,970,476 3,882,241 -2.22% (-88,235 B) GocciaRunnerbrotli-112,652,121 2,602,682 -1.86% (-49,439 B) GocciaTestRunnerraw16,780,179 15,917,040 -5.14% (-863,139 B) GocciaTestRunnergzip3,922,887 3,835,411 -2.23% (-87,476 B) GocciaTestRunnerbrotli-112,621,592 2,572,124 -1.89% (-49,468 B) qjs-wasi.wasm(v0.18.0, no ICU)1,571,399 raw / 544,525 gz / 435,766 br-11 — — -
Startup (empty script, interleaved under Node.js 24.18.0 WASI, medians):
WebAssembly.compile(11 reps):6.26 ms->6.01 ms(-4.0%, non-overlapping ranges;qjs-wasi.wasm:0.86 ms)- Cold Node WASI subprocess (11 reps):
196.10 ms->180.45 ms(-8.0%, non-overlapping ranges;qjs-wasi.wasm:35.22 ms) - Cold
GocciaRunner --output=jsontiming.total_ns(11 reps):29.87 ms->28.35 ms(-5.1%, non-overlapping ranges) - Warm precompiled instantiate +
_start(21 reps):7.06 ms->7.05 ms(neutral;qjs-wasi.wasm:0.77 ms)
-
AWFY execution (14 workloads, warm protocol, 1 warmup + 5 interleaved alternating reps under Node.js 24.18.0 WASI alongside
qjs-wasi.wasmv0.18.0):- Steady-state warm execution is neutral (
0.9971xgeomean across all 14 workloads whenSievewarms to V8 TurboFan with 30 warmup iterations:2.8168x->2.8086xqjs-wasi.wasm;0.9962xacross the 13 non-Sieveworkloads:2.8266x->2.8159x). - Under the default manifest's
warmupIterations: 5(~5 ms),Sievesits on V8's background Liftoff-to-TurboFan tier-up boundary (bimodal ~1,060 us vs ~2,500 us: 1/5 slow in branchful, 4/5 slow inexnref, giving raw geomeans2.8199x->2.9861x). Controlling V8's tier directly confirmsexnrefis faster in both tiers onSieve(-1.9%under--no-liftoff:1144.9->1123.3 us;-2.0%under--liftoff-only:2205.1->2160.6 us) and neutral once warmed (1045.4->1054.5 usat 30 warmup iterations). - Removing branchful exception checks therefore does not explain GocciaScript's ~2.8x runtime ratio against QuickJS-ng in WebAssembly; it is runtime-neutral in steady state while delivering clear module-size and cold-startup wins.
- Steady-state warm execution is neutral (
-
Full WebAssembly test suite (
scripts/run-wasm-test-suite.js,--icu-data, 2,057 files):- Identical pass/known-failure outcomes (
2,022passed,35failed as listed intests/wasm-known-failures.txt,0unexpected,0stale), completing in77 svs88 s(-12.5%wall-clock time).
- Identical pass/known-failure outcomes (
Decision
- Pin
fpc-toolchain3.3.1+13and buildwasm32-wasip1with-CTwasmexceptions.build.paspasses-CTwasmexceptionsalongside-XewheneverFPC_TARGET_OS=wasip1, so shipped modules, CI, and local WASI builds use standardized native WebAssembly exceptions (exnref/try_table). - Require
exnref-capable browsers and runtimes, with automatic server fallback in the playground.- Browsers: Chrome/Edge 137+, Firefox 131+, Safari 18.4+ (macOS and iOS).
website/src/lib/playground-engine.tsprobesWebAssembly.validatewith a 40-byte minimaltag+try_tablemodule before offering browser execution; browsers withoutexnreffall back to running on the server and see the minimum browser requirement rather than failing to compile the module. - Node.js: 24.15.0+ or 25+ by default (
--experimental-wasm-exnrefon Node.js 22–24.14). CI'swasmjobs pin>=24.15.0 <25, andscripts/run-wasi.jsreports the version requirement if compilation fails on a Node.js runtime withoutexnref. - Wasmtime / Bun: Wasmtime 47+ (default-enabled) and Bun 1.3+.
- Browsers: Chrome/Edge 137+, Firefox 131+, Safari 18.4+ (macOS and iOS).
Consequences
- Shipped
--prodmodules shrink by ~5.2% raw (~885 KB onGocciaRunner) and ~1.9% brotli-11 (~49 KB), compile ~4% faster, start cold ~8% faster, and run the 2,057-file WASI test suite ~12.5% faster with zero regressions. - Older browsers (Chrome/Edge < 137, Firefox < 131, Safari < 18.4) and older WASI hosts (Node.js < 24.15 without
--experimental-wasm-exnref, Wasmtime < 47) cannot compile thewasm32-wasip1modules; playground visitors on those browsers transparently run on the server instead. - All 142 audited
i386-win32objects in3.3.1+13match their3.3.1+12SHA-256 hashes, soconfig/win32-safeseh/fpc-3.3.1.manifestadvances its version to3.3.1+13under the existing3.3.1+10object audit.