Browse documentation

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, --prod GocciaRunner with ICU4C):

    • br_if: 143,435 -> 57,455 (-59.9%)
    • Post-call i32.load8_u + br_if checks: 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 tag tag 0, zero catch_all, so host JS exceptions such as WasiExit and WasiKill unwind cleanly)
    • throw: 0 -> 3,633
  • Module size (bytes; --prod with ICU4C linked, gzip -9, brotli -q 11; QuickJS-ng v0.18.0 qjs-wasi.wasm without ICU shown for scale):

    ModuleBranchful (3.3.1+12)Native exnref (3.3.1+13)Delta
    GocciaRunner raw16,923,25916,037,542-5.23% (-885,717 B)
    GocciaRunner gzip3,970,4763,882,241-2.22% (-88,235 B)
    GocciaRunner brotli-112,652,1212,602,682-1.86% (-49,439 B)
    GocciaTestRunner raw16,780,17915,917,040-5.14% (-863,139 B)
    GocciaTestRunner gzip3,922,8873,835,411-2.23% (-87,476 B)
    GocciaTestRunner brotli-112,621,5922,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=json timing.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.wasm v0.18.0):

    • Steady-state warm execution is neutral (0.9971x geomean across all 14 workloads when Sieve warms to V8 TurboFan with 30 warmup iterations: 2.8168x -> 2.8086x qjs-wasi.wasm; 0.9962x across the 13 non-Sieve workloads: 2.8266x -> 2.8159x).
    • Under the default manifest's warmupIterations: 5 (~5 ms), Sieve sits 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 in exnref, giving raw geomeans 2.8199x -> 2.9861x). Controlling V8's tier directly confirms exnref is faster in both tiers on Sieve (-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 us at 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.
  • Full WebAssembly test suite (scripts/run-wasm-test-suite.js, --icu-data, 2,057 files):

    • Identical pass/known-failure outcomes (2,022 passed, 35 failed as listed in tests/wasm-known-failures.txt, 0 unexpected, 0 stale), completing in 77 s vs 88 s (-12.5% wall-clock time).

Decision

  • Pin fpc-toolchain 3.3.1+13 and build wasm32-wasip1 with -CTwasmexceptions. build.pas passes -CTwasmexceptions alongside -Xe whenever FPC_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.ts probes WebAssembly.validate with a 40-byte minimal tag + try_table module before offering browser execution; browsers without exnref fall 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-exnref on Node.js 22–24.14). CI's wasm jobs pin >=24.15.0 <25, and scripts/run-wasi.js reports the version requirement if compilation fails on a Node.js runtime without exnref.
    • Wasmtime / Bun: Wasmtime 47+ (default-enabled) and Bun 1.3+.

Consequences

  • Shipped --prod modules shrink by ~5.2% raw (~885 KB on GocciaRunner) 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 the wasm32-wasip1 modules; playground visitors on those browsers transparently run on the server instead.
  • All 142 audited i386-win32 objects in 3.3.1+13 match their 3.3.1+12 SHA-256 hashes, so config/win32-safeseh/fpc-3.3.1.manifest advances its version to 3.3.1+13 under the existing 3.3.1+10 object audit.