The WebAssembly target¶
postpile builds WebAssembly from the C backend's output: --target wasm32-wasip1 compiles the same translation units and the same C runtime for WASI preview 1 on 32-bit WebAssembly, and links them against wasi-libc (ADR 0079). It is a target of the C backend, so every construct the C backend compiles, WebAssembly gets, with the C backend's results.
ppc build --exe --target wasm32-wasip1 hello.py # hello.wasm, a WASI command
wasmtime hello.wasm
ppc build --target wasm32-wasip1 kernels.py # kernels.wasm, a WASI reactor
Two module shapes¶
- An executable (
--exe) is a WASI command:_startruns the script and exits, andprintwrites throughfd_write. Any WASI engine runs it: wasmtime, node'snode:wasi, a browser with a preview 1 shim. - A library is a WASI reactor: the host calls
_initializeonce, then the module's exportedpp_<name>functions, as a native caller calls them through the C ABI. It also exportsmallocandfree, through which the host places an argument in the module's linear memory. Onwasm32an__pp_arrayis 32 bytes: the data pointer, four bytes of padding,ndim, the shape and strides pointers, and the byte offset.
JavaScript¶
A program can call JavaScript with Pyodide's spelling, so the same file runs under Pyodide unchanged and Pyodide is what the compiled program is held to (ADR 0079):
import js
from pyodide.ffi import JsProxy
def paint(ctx: JsProxy, colour: str) -> None:
ctx.fillStyle = colour
ctx.fillRect(0, 0, 600, 400)
canvas = js.document.getElementById("chart")
paint(canvas.getContext("2d"), "#f5f5f5")
js.console.log("done")
import js and from js import document give handles on JavaScript values, annotated JsProxy; attribute reads and stores, calls, Cls.new(...), len, in, [] and iteration work on them, and int(), float(), str() and bool() convert one to a POST value. Each does what Pyodide does, including Pyodide's errors: a plain object is not a mapping, a missing attribute is an AttributeError, a JavaScript exception is catchable as Exception. A program that imports js builds for this target only, and cannot also import python.
Build it as an executable and load it in a page with postpile's browser host, wasi.mjs, and its bridge, bridge.mjs, both in the installed package's postpile/js/ directory (python -c "from importlib.resources import files; print(files('postpile') / 'js')"):
<canvas id="chart" width="600" height="400"></canvas>
<script type="module">
import { run } from './wasi.mjs';
await run(fetch('app.wasm'));
</script>
A function of the program can be handed to JavaScript, as under Pyodide:
from pyodide.ffi import JsProxy, create_proxy
def on_click(event: JsProxy) -> None:
print("clicked")
js.document.body.addEventListener("click", create_proxy(on_click))
Passed straight to a call, a function is borrowed for that call and destroyed when it returns, so JavaScript that keeps it and calls it later gets Pyodide's error. create_proxy(f) keeps it until destroy() is called on the proxy; a function stored on an object (el.onclick = f) is kept. The parameters of such a function are JsProxy, int, str or bool; a JavaScript number goes to a JsProxy parameter and is converted with float(), since a whole one arrives as an int under Pyodide. An exception it raises reaches the compiled code that called into JavaScript, or is thrown to JavaScript when JavaScript called on its own. A program that imports js is a reactor: wasi.mjs runs the top level with pp_main. The module stays for the callbacks, whose handles are released when each returns.
A JavaScript error is JsException, imported from pyodide.ffi as under Pyodide and caught by except JsException or except Exception. Keyword arguments to a JavaScript function arrive as Pyodide passes them, one object after the positional arguments: f(1, x=2) is f(1, {x: 2}).
A library's functions are callable from a page: load instantiates the module and returns the exports, an int crossing as a BigInt and a float as a Number.
import { load } from './wasi.mjs';
const lib = await load(fetch('kernels.wasm'));
lib.pp_add(2n, 40n); // 42n
Two things are not there yet: a lambda or a closure as a callback, which needs a handle counted where the closure can hold it; a library function that takes or returns a str or an array from a page.
Toolchain¶
The default compiler is zig's (zig cc), from PATH or from the ziglang wheel of the bundled extra: one download with clang, wasm-ld, wasi-libc and compiler-rt. A clang with a WASI sysroot works too, through --cc and --cflags --sysroot=…. ppc backends reports what the target would build with. The tests run modules with the wasmtime package of the wasm extra.
What the target cannot hold¶
from python import: no CPython runs inside the module.- pygo: its runtime needs threads, which WASI preview 1 does not have.
- An extension module and the
--prefixinstall layout, which are for a host process.
Each is refused with a diagnostic at build time.
Fidelity¶
A WebAssembly build is held to CPython by the same tests as a native one: the p2w programs as commands in debug and release, and the lpython and shared corpora as reactors (tests/c_e2e/test_wasi_target.py). The oracle for a result that comes from the C library, a cbrt or an exp, is CPython built for WebAssembly: on wasm32 the libm is wasi-libc's, as it is for Pyodide's CPython, and the two agree where the host's libm rounds differently.
The runtime keeps every list element and instance field in an eight-byte slot whatever the pointer width, so a 32-bit target lays data out as a 64-bit one does; a pointer is widened to 64 bits through uintptr_t when it is stored.
Speed¶
python -m benchmarks cross runs each kernel as a reactor in wasmtime and in node; python -m benchmarks.wholeprog --wasi adds the low-level programs and their C versions, all built for the target. The figures, with how they were taken, are in notes/measurements.md.
Before this target¶
Until 2026-11-16 postpile had a separate WebAssembly backend that emitted WebAssembly text from the IR. It lowered scalar and array kernels, had no runtime, and so refused every whole program; it ran the cross benchmark's kernels at about a tenth of the speed of this target. ADR 0079 retired it.