WebAssembly
Zig treats wasm as an ordinary target, which makes it one of the shortest paths to shipping compiled code to a browser. These chapters cover what a freestanding module actually is, exporting functions to JavaScript, moving strings and structs across a boundary that only passes numbers, instantiating the module on a page, and building a WASI command. This site runs on the result.
- wasm is an ordinary target. Nothing about the language changes, and the same source builds for your machine and for a browser.
- The boundary only passes numbers. A string crosses as a pointer and a length into linear memory, and both sides have to agree who owns it.
- Freestanding means no libc, no WASI and no allocator you did not bring yourself.
6 chapters.
- What WebAssembly IsA portable binary target for a sandboxed stack machine, and how Zig reaches it.
- Recipe: A WASI CommandCompile a Zig program to wasm and run it from a shell under any WASI runtime.
- Recipe: Calling Zig from JavaScriptBuild a reactor module with exported functions and call them by name from a host.
- Recipe: Passing Data Across the BoundaryMove strings and buffers between JavaScript and wasm through shared linear memory.
- Recipe: A Freestanding ModuleThe smallest wasm target, with no WASI and no libc, and the memory builtins that replace them.
- Recipe: Loading wasm in the BrowserFetch, instantiate, wire up imports and exports, and let a module call back into JavaScript.