Browse documentation

The playground can run a version's WebAssembly modules in the browser

Date: 2026-12-02 Area: website, apps Related: ADR 0235, ADR 0146

Context

The playground runs each script by starting a vendored engine binary in a server function. Since ADR 0235, GocciaRunner and GocciaTestRunner also build as wasm32-wasip1 modules, and releases and the nightly publish them as gocciascript-<label>-wasm32-wasip1.tar.gz. A module that runs in the visitor's browser takes that work off the server.

The module cannot yet do everything the server's binary does: fetch fails without connecting and Intl has no locale data (ADR 0235), and the playground's own examples use both. A --prod module is about 24 MB, a few MB compressed. A browser has no WASI of its own, so the page has to supply a preview 1 host for the functions the modules import.

Decision

  • Running in the browser is the visitor's choice, off by default. The playground offers it for a version whose release published WebAssembly modules, and the server stays the default engine until the modules have Intl data and the page answers fetch through the embedder transport (ADR 0146). The banner says which engine ran.
  • The modules are vendored with the binaries and served as static files. website/scripts/fetch-binaries.ts takes them from the release's wasm32-wasip1 archive at build time and stages them under public/engines/<tag>/, in a directory named by a prefix of the archive's SHA-256. A URL therefore never changes content and is served as immutable. Staging refuses a module that imports a function the browser's WASI host lacks, which would fail every browser run, and the version then has no browser engine, as a release without the archive (every release before ADR 0235) has none; the deploy floor does not look at the modules. A page opened before a redeploy that finds its module gone reads the current URL from /api/versions and tries once more.
  • A browser run reads like a server run. website/src/lib/runner-protocol.ts builds the runner's arguments and turns its JSON report into the response body for both the API routes and the browser, so the output lines, the error and the timing come from the same code.
  • A worker runs the module; the page holds the limits. A dedicated worker compiles each module once and instantiates it afresh for every run, so a trap leaves nothing behind. The page gives loading a deadline, and once the module starts it applies the server's kill timer (the timeout plus its grace): past it, it terminates the worker and answers as the server answers a killed process, with the output the worker had passed on.
  • The WASI host is our own, limited to the imports the modules use. website/src/lib/wasi-shim.ts implements the modules' wasi_snapshot_preview1 imports over one in-memory directory, with stdin from a buffer and stdout and stderr captured up to the server's caps. @bjorn3/browser_wasi_shim 0.4.2, its latest release (June 2025), implements all of preview 1, those imports included, at about 22 KB minified against our 11 KB. It is not worse where it looks worse: without cross-origin isolation both spin in poll_oneoff, its UTF-16 argument sizing never meets a non-ASCII argument here, and the modules printed the same whether stdout looked like a terminal (its choice) or a pipe (ours, as under Node's WASI). What decided it is that the output caps, which stop a run, and the output passed on as it is written would need custom descriptors with the library too, leaving it little to do; a host this small is tested call by call in bun test and against the real modules; and it adds no dependency whose fixes since its last release are unreleased.

Consequences

  • Each vendored version with modules adds about 48 MB of static files to a deployment, and a visitor's first browser run of it downloads one module, a few MB compressed; the checkbox gives its gzip size.
  • A sleep in the module, such as Atomics.wait running to its timeout, spins the worker's thread. Cross-origin isolation would let it block, at the price of headers that constrain every page of the site.
  • We own WASI correctness for the imports the modules use. On every pull request the wasm job runs website/src/__tests__/wasm-runner.test.ts on the modules it builds, which fails when a module imports a function the host does not provide; the host then grows by that function. A release whose modules need one anyway is vendored without its browser engine.
  • Browser runs go through neither the API's rate limit nor its response cache; each sends one client event with its outcome instead.
  • Only the nightly has modules until a release publishes the archive; older stable versions keep running on the server only.