WebAssembly
Zig treats wasm as an ordinary target. Thus Zig is one of the shortest paths from compiled code to a browser. These chapters show what a freestanding module is, and how to export functions to JavaScript. They show how to move strings and structs across a boundary that only passes numbers. They also show how to instantiate the module on a page, and how to build 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 must agree who owns the string.
- Freestanding means no libc, no WASI, and no allocator except one that you supply.
6 chapters.
- What WebAssembly IsA portable binary target for a sandboxed stack machine, and how Zig compiles to it.
- Recipe: A WASI CommandCompile a Zig program to wasm and run it from a shell under any WASI runtime.
- Recipe: Call Zig from JavaScriptBuild a reactor module with exported functions and call them by name from a host.
- Recipe: Pass 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: Load wasm in the BrowserFetch, instantiate, connect the imports and exports, and let a module call back into JavaScript.