Browse documentation

WebAssembly

For contributors building or testing the applications as WebAssembly modules.

Executive Summary

  • Two modules — GocciaRunner and GocciaTestRunner build as wasm32-wasip1 modules that import only WASI preview 1 and use native WebAssembly exceptions (exnref / try_table)
  • Named host gaps — HostPlatform.inc names what a WASI host lacks; code that works around a gap branches on those symbols
  • Bounded recursion — the host's call stack is all native code has; each deep recursion has a limit measured on its own, and recursions combined can still overflow it
  • Files the host offers — fetch reaches the network through a device file, and Intl reads the locale data of the ICU4C linked in from a data file; where a host offers neither, fetch fails without connecting and Intl has no locale data
  • Tested in CI — the wasm job's three runners run every test file on the module against tests/wasm-known-failures.txt
  • Runs in the playground — a visitor can run a vendored version's modules in their browser instead of on the server

Building

GocciaRunner and GocciaTestRunner build for WebAssembly as wasm32-wasip1 modules, which import only WASI preview 1 (wasi_snapshot_preview1), and CI runs them under Node's node:wasi (ADR 0235). Install the target's units with the toolchain (install.sh --lock toolchain.lock --targets wasm32-wasip1), then:

FPC_TARGET_CPU=wasm32 FPC_TARGET_OS=wasip1 ./build.pas runner testrunner

The modules replace build/GocciaRunner and build/GocciaTestRunner, so build without the variables again before running them natively. The host must preopen the directories a script reads. CI and the releases build them with ICU linked in (ICU):

bash scripts/build-wasm-icu.sh build/wasm-icu
GOCCIA_WASM_ICU=build/wasm-icu FPC_TARGET_CPU=wasm32 FPC_TARGET_OS=wasip1 ./build.pas runner testrunner

build.pas links them with wasm-ld (ADR 0244) and compiles them with native WebAssembly exceptions (-CTwasmexceptions, standardized exnref / try_table, ADR 0255): for FPC_TARGET_OS=wasip1 it passes -Xe and -CTwasmexceptions. fpc-toolchain 3.3.1+14 ships wasm-ld in its cross/bin, matching exnref RTL/package units selected by fpc.cfg while keeping wasi-sdk 34's noeh C++ libraries so -fno-exceptions ICU4C links unchanged, and selective goto lowering that keeps br_table jump tables in labeled procedures such as the VM dispatch loops (ADR 0257). wasm-ld leaves out the functions, data and imports nothing references, and --prod's -Xs strips the name section; a development build keeps it, so a trap names its functions, but carries no DWARF, since wasm-ld cannot read the symbols FreePascal writes for it. wasm-ld puts the 8 MB stack at the bottom of linear memory, below the static data, so a stack that overflows traps instead of overwriting that data.

Production builds (--prod) also pass --compress-relocations and -O2 to wasm-ld, and run Binaryen's wasm-opt -O3 --one-caller-inline-max-function-size=100 --strip-target-features when wasm-opt is available (ADR 0256). Install the pinned Binaryen release locally with bash scripts/install-binaryen.sh build/binaryen and put build/binaryen/bin on PATH (or set GOCCIA_WASM_OPT=/path/to/wasm-opt; GOCCIA_WASM_OPT=0 skips wasm-opt and GOCCIA_WASM_OPT=1 requires it, as CI does). wasm-ld keeps the target_features custom section when wasm-opt will run so Binaryen auto-detects the exact WebAssembly feature set emitted by FPC and ICU4C, then --strip-target-features removes it from the final module.

Runtime and browser support

Because the modules use standardized native WebAssembly exceptions (exnref / try_table), hosts must support the finalized exception-handling proposal:

  • Browsers: Chrome/Edge 137+, Firefox 131+, Safari 18.4+ (macOS and iOS).
  • Node.js: 24.15.0+ or 25+ by default (--experimental-wasm-exnref on Node.js 22–24.14).
  • Wasmtime / Bun: Wasmtime 47+ and Bun 1.3+.

In the playground, browsers that lack exnref support (or Worker) automatically run on the server instead of trying to compile an unsupported module.

How a run differs

The module runs without threads, sockets or FFI:

  • Files run one at a time on the main thread.
  • --timeout is checked from the instruction counter, which then counts every instruction, and from the native loops that poll for an interrupt, such as the regular expression matcher and BigInt arithmetic: a run with a timeout takes the slow dispatch path, about 2.2 times as long across AWFY (up to 3.5 times on a tight loop). A run with --max-instructions counts them anyway.
  • fetch goes through the host's fetch device when it offers one (Fetch through the host), and fails without connecting otherwise; provider imports always do. Atomics.wait and Atomics.waitAsync can only time out, asleep until their timeout or the run's deadline, as no other agent runs; $262.agent.start and a nested sandbox run (runScript, the shell's goccia) are refused.
  • Intl has locale data only when the module links ICU and its host offers ICU's data (ICU); FFI refuses, and an OS error carries no code.
  • The host's call stack is all the native recursion has. A recursion through built-ins ends in a RangeError after 300 nested calls, and one that re-enters the interpreter from a built-in (a callback) after 250; proxies forward at most 800 deep, JSON nests at most 1,000 deep, flat 2,500, and source code 256 levels (with syntax trees at most 1,536 tall). These were measured against V8's default WebAssembly stack of 984 KB; other native recursion has no limit of its own and can still overflow the host's stack, which ends the module.
  • Linear memory has no lazy virtual mappings, so the default nursery is 1 MiB (EngineNurseryBytes), the temp root stack (TempRoots), gray stack, remembered list, and chunked call-frame stack (FrameChunks) start small and grow on demand up to their full limits, and the value stack (VStack) uses GetMem instead of AllocMem so WebAssembly's zeroed pages are not eagerly zero-filled.

Fetch through the host

A module has no sockets, so the applications hand each request to their host through a file, keeping the module's imports to WASI preview 1 (ADR 0237). A host that can perform HTTP requests preopens the directory /.goccia holding the file fetch, after the directories the run reads (FreePascal's runtime takes the first preopened directory as the current one). For each request the transport (Goccia.CLI.FetchDevice) opens that file, writes the request, reads the answer to its end and closes it; the host performs the request at the first read. Without the file, no transport is installed and fetch fails without connecting.

The request is one UTF-8 JSON object (followed, when a POST request has a non-empty body, by "bodyLength" in the JSON, a line feed, and exactly that many raw body bytes):

{"version":1,"method":"GET","url":"https://icanhazdadjoke.com/","headers":[["accept","application/json"]],
"redirect":"follow","timeoutMs":5500,"maxResponseBytes":8388608,
"net":[{"allowAll":false,"allow":["icanhazdadjoke.com"],"denyAll":false,"deny":[]}]}

headers are [name, value] pairs whose characters are each one byte. timeoutMs is the time left before the run's deadline or the request's AbortSignal.timeout(), whichever is sooner, plus 500 ms, so that the engine's deadline ends the run or aborts the signal first (0: neither applies). net is the run's net rules, one object per capability layer. The answer is one line of UTF-8 JSON ended by a line feed, then the body's bytes:

{"ok":true,"status":200,"statusText":"OK","url":"https://icanhazdadjoke.com/","redirected":false,"headers":[["content-type","application/json"]],"bodyLength":106}
<106 bytes>

A network error is {"ok":false,"error":"<the TypeError's message>"}, with "deniedScope":"<host[:port]>" when the host refused the destination, which rejects the fetch with PermissionDenied for that scope. Fields the transport does not know are skipped (Goccia.FetchDevice.Protocol).

Everything fetch decides before network work stays in the engine: the device never sees a host --allow-net refuses. A host that follows redirects and resolves names itself checks each hop and each resolved address against net before it requests or connects to it, and answers a refusal with its deniedScope; the Node test host does. The transport holds the final URL a host reports to --allow-net as well, and refuses a name only private allows, as the request's URL or the final one, since no device reports the address it reached. Each refusal rejects with PermissionDenied and is audited as a net.fetch deny. A module has one thread, so the run waits in the read while the host performs the request.

In a browser

The playground's browser runner performs each request with a synchronous XMLHttpRequest in its worker, for the page's own allowed hosts only. A browser cannot do all that the server's client does:

  • It follows redirects itself and reports only the final URL, without disclosing whether a POST stayed POST or was downgraded to GET (including a 303 redirect back to the exact same URL). The request's URL is checked before the request (by the engine and by the page) and the final URL after it; nothing checks a middle hop, which the browser has already requested. Because the final method of an XHR POST is unknown (the browser host omits finalMethod), both the browser host and the engine's transport conservatively require both POST and GET on the final destination before returning a POST response, even when the final URL matches the request URL. redirect: "error" and "manual" fail only once a redirect was followed, and "manual" cannot answer with the redirect itself.
  • It resolves names itself, so nothing checks the address any hop reached, and a name only private allows is refused.
  • CORS applies: a server that does not allow the page's origin fails as a network error, and only the response headers it exposes are seen. icanhazdadjoke.com, the playground's example, sends Access-Control-Allow-Origin: * and exposes none beyond the CORS-safelisted ones.
  • It drops request headers it forbids, such as Host and Connection.
  • A response over HTTP/2 or HTTP/3 has no reason phrase, so its statusText is empty where the server's client, which speaks HTTP/1.1, reports one such as OK.
  • It checks the body against the ceiling once all of it has arrived.

Testing the device

scripts/run-wasi.js --fetch-device offers the device under Node, performing each request in a child Node process, and --fetch-device-log <file> records each request it receives. Both hosts use one codec for the contract, scripts/lib/fetch-device-codec.mjs, the browser's runner included (Running in the browser).

ICU

The modules link ICU4C 79.1 when build.pas gets GOCCIA_WASM_ICU, the directory scripts/build-wasm-icu.sh builds ICU into, and read ICU's data from a file their host offers (ADR 0245). CI builds every module that way, and the releases and the nightly ship them so.

  • Building ICU — scripts/build-wasm-icu.sh <directory> downloads ICU's sources, its data's sources and wasi-sdk 34, each pinned by SHA-256, and builds ICU with wasi-sdk's clang: fpc-toolchain ships wasm-ld and wasi-sdk's sysroot but no C compiler. It needs a C++ compiler, make and python3 on the host, and does nothing when the directory already holds a build of the same script, filter and table. CI caches the directory by their hash.
  • Linking — GOCCIA_WASM_ICU makes build.pas define GOCCIA_STATIC_ICU and add the directory's lib/ to the library path, in a unit directory of its own (-icu). ICU.pas then links ICU's libraries, the C++ runtime and wasilibc, which puts C's malloc on FreePascal's heap and runs ICU's constructors.
  • Resolving functions — a module loads no shared library, so ICU.pas looks each function up by name in scripts/wasm-icu/icu-symbols.c, a table compiled with ICU's headers, which give every entry its versioned name and real signature. node scripts/generate-wasm-icu-symbols.js writes it from the names the sources resolve, and --check, which the wasm job runs, fails when it is out of date.
  • The data — icudt79l.dat, ICU's data for the 20 locales of scripts/generate-intl-data.js and az and si, without what Intl never reads (scripts/wasm-icu/data-filter.json). Another locale is not supported, so locale matching falls back to the default locale (supportedLocalesOf(["fi-FI"]) is []). On its first use of ICU, the module points ICU at /.goccia/icu, which ICU reads the file from, whole, into memory. A host that offers no such directory leaves the module without ICU: Intl then formats without locale data, as a module without ICU does.
  • Hosts — scripts/run-wasi.js --icu-data <directory> preopens a directory holding the file at /.goccia/icu. The wasm32-wasip1 archive carries the file under icu/ and ICU's license as ICU-LICENSE.txt. The playground's browser runner stages it with the modules and reads it when a module first opens it (ADR 0246), so a run that never uses ICU (Intl, or a Temporal calendar other than ISO 8601) never downloads it.

What the host lacks

source/shared/HostPlatform.inc, which Shared.inc and the engine's Goccia.Checks.inc both include, names what a WASI preview 1 host lacks. Code that works around what a host lacks branches on these symbols; a call of a WASI API branches on WASI.

SymbolThe host has noSo
GOCCIA_NO_SOCKETSsocket APIa network request goes to the host's fetch device, or fails before it connects without one; URL and host checks still run
GOCCIA_NO_DYNAMIC_LIBRARIESshared librariesFFI libraries are not found; ICU is there only when linked in (GOCCIA_STATIC_ICU, ICU)
GOCCIA_NO_THREADSthreads (only wasm32-wasip1threads has them)ThreadLaunch runs a thread's work on the caller's thread; the deadline is polled from the instruction counter
GOCCIA_NO_LIBMC math libraryFdLibm, a port of fdlibm 5.3, computes Math's functions, and Goccia.NumberRemainder %
GOCCIA_HIDDEN_CALL_STACKcall stack in linear memorythe native stack guard counts nested calls (NATIVE_DEPTH_MAX in Goccia.Core) instead of reading addresses

The clocks read WASI's clock_time_get to the nanosecond (TimingUtils).

Performance and footprint

On darwin/arm64 under Node.js v24.18.0 WASI, the shipped ICU-linked --prod GocciaRunner module built with fpc-toolchain 3.3.1+14, -CTwasmexceptions, and Binaryen wasm-opt measures:

  • Size: 12.02 MB raw, 3.24 MB gzip-9, 2.29 MB brotli-11 (GocciaTestRunner: 11.98 MB raw, 2.27 MB brotli-11).
  • Startup (empty script): 4.5 ms WebAssembly.compile, 60 ms cold Node WASI subprocess, 18.4 ms cold --output=json timing.total_ns, 5.8 ms warm precompiled instantiate + _start.
  • Execution (14 AWFY workloads, geomean): 0.95x of the native darwin/arm64 interpreter (GOCCIA_JIT=0), and 0.62x of QuickJS-ng v0.18.0 qjs-wasi.wasm under the manifest warm protocol (0.68x when every workload is warmed past V8 TurboFan tier-up, 0.80x under --no-liftoff). With --timeout enabled (the playground's slow dispatch path), execution is 1.35x of untimed qjs-wasi.wasm.

Full per-workload figures and methodology live in ADR 0257.

Publishing

The wasm job of ci.yml uploads the two --prod modules, ICU's data file and ICU's license as the gocciascript-wasm32-wasip1 artifact, and the tagged release and the nightly publish them as gocciascript-<label>-wasm32-wasip1.tar.gz, with GocciaRunner.wasm, GocciaTestRunner.wasm, icu/icudt79l.dat and ICU-LICENSE.txt beside the tests/, benchmarks/ and examples/ every archive carries (.github/scripts/package-release-archive.sh).

The playground

The website vendors each version's wasm32-wasip1 archive when its release has one, and the playground runs that version in the visitor's browser by default instead of on the server. A version without the archive, a browser that cannot run the modules, and a visitor who unticks Run in browser run on the server, and so does a default browser run whose module does not start; a visitor who ticked Run in browser sees that error instead: see Running in the browser, ADR 0236 and ADR 0247.

Testing

scripts/run-wasi.js runs a module under Node's node:wasi, preopening each --dir (the current directory by default) at the same path inside it:

node scripts/run-wasi.js build/GocciaTestRunner -P tests/built-ins/Math

scripts/run-wasm-test-suite.js build/GocciaTestRunner --icu-data build/wasm-icu runs every test file in a module instance of its own and compares the outcome with tests/wasm-known-failures.txt, which names each file that fails on WebAssembly, with ICU linked in and its data offered, and why. It fails on a file the list does not name, and on a listed file that passes, whose entry then goes; a listed file that traps or faults counts as listed only when its reason names the host stack. scripts/check-wasm-modules.js checks that both modules import only WASI, that --timeout ends a script that never stops and one stuck in BigInt arithmetic, and that a directory run in one instance fails a file that times out alone. With --icu-data it checks that GocciaRunner formats with ICU's data when its host offers the file and without locale data when it does not. It also runs scripts/check-wasm-fetch.js, which runs fetch through the device against a local server (a response, a 404, redirects, the ceiling, timeouts, a refused host, a refused middle hop, an unnamed private address and a name only private allows), beside ICU's data when given, and without it. The wasm job of pr.yml and ci.yml builds ICU, or restores it from its cache, builds the modules with --prod and runs both scripts under Node 24 (>=24.15.0); on a pull request it also runs the playground's browser runner on them (Running in the browser).

A file that overflows the host's stack ends its module instance, so a host runs each script in a fresh instance, or starts a new one after such a trap.

The suite takes about 16 minutes on one GitHub runner and has run past 26 on a slow one, more than the job's 30-minute timeout leaves after the build, so the wasm job runs on three, each building the modules and running its shard of the files with --shard <i>/<n> (every n-th file of the sorted list); the first also runs scripts/check-wasm-modules.js, uploads the modules from ci.yml and runs the browser runner from pr.yml. A file that takes longer than 30 seconds is logged as it ends, and the run ends with its five slowest files.