ADR 0079: WebAssembly through the C backend, and JavaScript as a second host behind the foreign boundary¶
Status: Accepted Type: Architecture Created: 2026-11-16 Amends: 0002 and 0070 (their sentences on the retired WebAssembly-text backend) Related-ADRs: 0000 (p2w, and the WASM backend it informed), 0002 (the modes, unchanged on this target), 0006 (the boundary the JavaScript one is shaped on), 0007 (a JavaScript error as a POST exception), 0024 (not amended: a JavaScript handle's lifetime is a callback's), 0070 and 0076 (functions passed to JavaScript). Provenance: the owner (2026-11-16) named two use cases, portable applications and JavaScript, browser and WASI interop, asked not to reinvent the wheel, and aligned the target's triple and the JavaScript surface on the conventions that exist (LLVM's and Rust's triple, Pyodide's names).
Context¶
postpile has a WASM backend that emits WebAssembly text from the IR. It lowers scalars, array element operations and libm calls; it has no runtime, so it refuses exceptions, strings, containers, classes and print, which is every whole program. It is not in the build driver or the CLI. Growing it into a backend for whole programs means writing the runtime again, in WebAssembly, against the runtime-helper pattern: runtime behaviour is written once, in C.
The owner also maintains p2w, a Python-to-WebAssembly compiler built on WASM GC. It covers more of the dynamic language than postpile does, has a JavaScript bridge and a simulated browser for its tests, and boxes every value in a GC struct. p2w's value model and postpile's (unboxed values, counted objects in linear memory) differ at the root: folding one compiler into the other rewrites one of them.
The C backend's output is portable C over libc. Compiled by clang for wasm32-wasip1 and linked by wasm-ld against wasi-libc, whole programs already run in a WASI engine and print what CPython prints. Only the runtime stood in the way, outside the IR. It moves eight-byte slots through pointer arithmetic that assumes eight-byte pointers, and two of the platform calls it makes (a process CPU clock, a process id) do not exist under WASI.
Two use cases ask for WebAssembly. A portable application is one artifact that runs sandboxed in any WASI engine. A browser application calls JavaScript, the DOM included, and is called back by it. The browser application needs a boundary to a foreign object model, and postpile has one: the CPython boundary of ADR 0006. Its operations on an opaque handle already lower to a fixed vocabulary of runtime helpers.
Decision¶
One way to WebAssembly: the reference's C¶
A WebAssembly build is asked for with --target wasm32-wasip1, the triple LLVM, wasi-libc and Rust use for WASI preview 1 (wasm32-wasi is the retired name). It compiles the C backend's translation units and the C runtime with clang for wasm32-wasip1, and links them with wasm-ld against wasi-libc. It is a target of the C backend: the IR and the lowering to C do not change; the C compiler, the link step and the runtime's platform surface do. The modes keep their meaning (ADR 0002), since the frontend decides them.
This buys the reference's coverage and results with no other implementation. Agreement between backends still proves nothing: a WebAssembly build is held to CPython by the stdout differential, run in a WASI engine, over the same corpora as a native build.
The runtime is word-size clean. A slot is eight bytes holding a 64-bit integer, a double or a pointer widened to 64 bits; it is never addressed as an array of void *. Pointers convert through uintptr_t. The rule holds for every 32-bit target; wasm32 is the one that checks it.
wasm32, not memory64. wasi-libc ships no 64-bit build, and not every engine runs memory64. A program whose data needs more than 4 GiB of linear memory is out of reach on this target, and says so when an allocation fails.
The platform surface is named. Each call the runtime makes outside C99 and WASI preview 1 gets a WASI branch that says what it computes instead. A substitute that would change an observable result is a refusal on this target instead.
What the target refuses, and why. pygo is refused because its runtime needs threads, which preview 1 does not have; from python import is refused because no CPython runs in the module. Both are diagnostics at build time, never link errors. Both are provisional: a WASI with threads or a CPython in the module reopens them.
The oracle on a WebAssembly target is CPython built for it¶
A result that comes from the C library, cbrt, exp or pow, is the target's libm's, which is wasi-libc's on wasm32 and may round differently from the host's. CPython built for WebAssembly calls the same family of libm. Pyodide's CPython gives math.cbrt(2.5) as this target's build does, one ulp below CPython on macOS (A197). So the oracle for a WebAssembly build is CPython built for WebAssembly; the tests run Pyodide. The host's CPython stands in where Pyodide is not installed, with the cases where the two libms differ named.
Two hosts, two module shapes¶
An executable (--exe) is a WASI command: _start runs the script and exits, and stdout goes through fd_write. A library or a program that imports JavaScript is a reactor: an exported initialiser runs the module's top level and returns. The instance stays alive for the calls that come later: exported functions and callbacks from JavaScript. A browser host supplies the WASI preview 1 functions the runtime imports, which is a short list, beside the JavaScript bridge.
JavaScript as another host behind ADR 0006's boundary¶
The spelling is Pyodide's: from js import X and import js. Pyodide is CPython compiled to WebAssembly, so a POST program that calls JavaScript runs unmodified under it. Pyodide is the oracle for every operation that crosses into JavaScript: the same source, run by Pyodide and compiled by postpile, against the same JavaScript environment, must produce the same output and the same sequence of calls into that environment. p2w's bridge and Pyodide disagree in places: p2w copies a dict or a list into a JavaScript object, where Pyodide passes a proxy; p2w reads a plain object as a mapping and keeps every function it hands over; A197 lists the rest. There, Pyodide's behaviour is the specification; a difference is refused until it is reproduced.
The names are Pyodide's. import js, with attribute access on the module, and from js import X are both accepted, as Pyodide accepts both. A JavaScript handle is annotated JsProxy, imported from pyodide.ffi as in Pyodide, where JsException and create_proxy come from too; object stays the CPython handle of ADR 0006, which this ADR does not change. js is accepted only on this target, and python is refused on it, so no program holds both kinds.
A handle holds what Pyodide would have handed Python. Pyodide converts a JavaScript number (an int for a safe integer, a float otherwise), string, boolean or undefined to the Python value as it crosses, gives null as the falsy jsnull, and proxies anything else; which one comes back is known only at run time. Statically, the result of every operation on a JsProxy is a JsProxy. At run time it holds either the converted value or the proxy. An operation on it (printing, ==, truth, len, in, iteration, calls, attribute and item access) does what Pyodide does with that value, including the refusals. A plain object is not a mapping: len, in, o[k] and iteration raise TypeError on it. They do the same under Pyodide (A197). The inward conversions are written as in ADR 0006 (int(h), float(h), str(h), bool(h)), which under Pyodide are the builtins applied to the converted value. The data operations and the fixed-arity calls take 0006's shape; on this target they are imports that the JavaScript bridge implements, and the differential checks each against Pyodide.
A JavaScript handle made in a callback ends with it. A handle is an index into a table the bridge keeps. No module global, field or container holds a handle. A function handed to JavaScript is defined at module level and captures nothing. So a handle made while a callback runs is unreachable once the callback returns: the bridge cuts the table back to where it stood when the callback began. A browser program runs callbacks for as long as the page is open; the cut keeps one event's handles from outliving it. The top level's handles live as long as the program, as a CPython handle does under 0006. No new counted kind is needed in ADR 0024; one becomes necessary when a handle can be stored where a callback can reach it after returning, a field or a global.
A JavaScript error is JsException, Pyodide's class, an Exception whose message is the error's text; the bridge catches it at the call and returns the null handle with the exception pending, ADR 0007's convention for a CPython error. An error the bridge raises as Pyodide would (TypeError, AttributeError, IndexError, KeyError, ValueError, OverflowError) is that class, with Pyodide's text. When JavaScript was called from compiled code, a POST exception raised in a callback goes back to the compiled code below it, so the except around that call sees it, as under Pyodide. When JavaScript called on its own, the exception is thrown to JavaScript as an error whose type is the exception's class.
A function passed to JavaScript is valid for the call that passes it, as Pyodide's borrowed proxy is: JavaScript that keeps it and calls it later gets the error Pyodide throws. A callback that outlives the call is made as under Pyodide, with pyodide.ffi.create_proxy(f), and lives until its destroy(); a function stored on a JavaScript object (el.onclick = f) is kept, as Pyodide keeps it. The function's arguments are counted as CPython counts them, with CPython's messages, and convert by the parameters' annotations: a JsProxy is the handle, an int, a str or a bool what Pyodide hands Python for one. A float parameter is refused, since a whole JavaScript number arrives as an int under Pyodide and a float slot does not hold one (A190): the parameter is a JsProxy, converted with float(). A program that imports js is a reactor: pp_main runs the top level and returns, so the instance stays for the callbacks.
Outward conversion is implicit for what Pyodide converts implicitly: int, float, bool, str and None. Keyword arguments to a JavaScript function are one object after the positional ones, Pyodide's f(1, {x: 2}) for f(1, x=2). A container, an instance or bytes passed to JavaScript is refused, since Pyodide passes a live proxy and postpile has none.
The WebAssembly-text backend is retired¶
The backend that emitted WebAssembly text from the IR is removed. The C target ran the cross-backend benchmark's kernels at about five times the old backend's speed in both engines, ran the libm kernel it had to leave out, and covered the shared corpus it was tested on. Nothing in the frontend or the IR existed for it alone.
p2w stays a separate compiler¶
postpile takes from p2w what is not tied to p2w's value model. It takes the bridge's mechanics (strings through linear memory, the callback trampoline, errors in both directions) and the simulated browser p2w's tests run in, and, as before, the demos as acceptance programs and the corpus. What becomes of p2w is not decided here.
Consequences¶
- A whole POST program becomes a portable WebAssembly artifact with nothing to maintain beyond the C backend: each construct the C backend gains, the target gains.
- The browser use case reuses the boundary, the conversion rules and the refusal discipline that the CPython boundary established, and inherits an interpreter as the oracle. The price is that postpile follows Pyodide's JavaScript semantics, including the places where p2w chose differently.
- A JavaScript handle's lifetime is a callback's, which needs nothing of ADR 0024 while a handle cannot be stored where a callback can reach it after returning; making a handle a field type or a global's reopens that.
- The runtime's portability becomes a checked property: an eight-byte pointer assumption fails the WebAssembly differential.
- Untyped Python in the browser stays p2w's ground; postpile refuses what does not type.
Alternatives considered¶
- Grow the WebAssembly-text backend. It means another runtime, written in WebAssembly, held to the C one by tests alone: the duplication the runtime-helper pattern exists to prevent.
- Emit WASM GC from the IR. It would give JavaScript's collector the cycles that cross the boundary and handles with no table. It also means another memory model beside ADR 0024's, for one target.
- Merge p2w into postpile, or the reverse. The value models differ at the root; one compiler would be rewritten.
- No C compiler on the user's machine: the LLVM backend to a
wasm32object. Parked; the owner prefers it. llvmlite already has thewasm32target, the LLVM backend is held byte-for-byte to the C one, and it links the extern runtime instead of inlining it. The runtime would be compiled forwasm32once, when postpile is packaged, and shipped with wasi-libc; awasm-ldwould still link the module. It is parked behind the C target because it needs what the C target builds first (the word-size clean runtime, the WASI shims, the differential it would be held to), and because it adds packaging work the C target does not: a runtime archive per configuration the defines select, plus a linker to ship or find. A runtime not written in C at all is the first alternative above. - Emscripten. It brings its own libc, JavaScript glue and module shape; wasi-libc and a bridge of our own keep the WASI engines and the browser on one artifact.
memory64. It would keep the runtime's pointer assumption, at the cost of wasi-libc support and of engines that do not run it.- p2w's conversion table instead of Pyodide's. p2w has no interpreter behind it. Pyodide does; postpile's results are held to an interpreter.