.nib guide std nib 0.3.1-pre

Compiling to WebAssembly

The wasm32 module and the wasm32-wasip2 component — the same lowering as native, a 32-bit memory, and a host the program cannot see past.

nibc build --target wasm32 [--wire W] [-o OUT.wasm] [--rc] [--fibres] FILE
nibc build --target wasm32-wasip2 [--wire W] [-o OUT.wasm] [--host HOST.wasm] FILE

Both use the lowering the native backend uses. The build prints one line per function — compiled (N splits: …) or fell back to the walker — CODE reason — then a count and the module's digest, and writes OUT.wasm with OUT.wasm.manifest.json beside it: the ABI name (nib-aot-abi-1), the module's SHA-256, and each body's tier, splits and transition count. Exit status 0 means a module was written; 3 means the entry was refused.

A module is all or nothing

Only the wire's entry becomes code; every function it calls is inlined into it. Other bodies are lowered only to report on them. If any construct the entry reaches is outside the subset, the entry is refused, nothing is written, and nibc --run remains the way to run the program — nothing is ever walked inside a module. The refusals form a closed set of 29 codes (EPerform, EParkCarries, ETryPark, EHeapEq, …), each a sentence about one construct. Three — EPerform, EParkCarries, ECapRaises — are limits of the plain module's import ABI rather than unfinished work.

The plain module

Every local is an i64 and memory is wasm32's: a block's address is an i32 widened into the word. The header is the native one (kind, length, aux, count, then the payload at +16), and Ints are narrowed to 63 bits after each operation. The module exports handle (i64) -> i64. Each operation a wire binds is an import nib.<iface>_<meth> (i64) -> i64, preceded by nib.park, which lets the host suspend the call; a parking body also exports resume, frames and blocks, so the host can save a call and continue it. wasmtime run --invoke handle OUT.wasm ARG runs one that performs nothing.

Counted and fibred

  • --rc gives blocks owner counts (the native heap, compiled from the same nibrt crate to a wasm object and linked by nibc itself): loops that ran out of heap run in their live set, and Vec.push/Map.set on a unique container update in place.
  • --fibres builds the entry as fibres in regions a guest runtime owns: a park returns to the runtime instead of calling out, a perform may carry two arguments of any kind, and memory may grow to 1 GiB.

The wasip2 component

--target wasm32-wasip2 builds the fibre module, adds canonical-ABI glue, and composes it with a host component (system/tools/nib-wasm-rt, Rust) through wasm-tools and wac into one runnable component. The host answers the same sixteen operations the native host does — sockets, printing, the clock, seats, switches, forks — and does no HTTP of its own: framing and routing stay in compiled nib.

wasmtime run -S inherit-network=y OUT.wasm

The host is found by --host, then NIB_WASM_HOST, then the installed share directory. Measured on the example site (Node 22 for the module, wasmtime for the component): 48,400 req/s plain, 44,200 with counting; the native build of the same site serves about 2.6× its wasmtime throughput.