40 lines
1 KiB
Markdown
40 lines
1 KiB
Markdown
|
|
# WASI
|
||
|
|
|
||
|
|
Build a C++26 program for the `wasm32-wasi` target and run it in a browser.
|
||
|
|
|
||
|
|
## Prerequisites
|
||
|
|
|
||
|
|
- `wasi-libc`, `wasi-libc++`, `wasi-libc++abi` (Arch packages — provide
|
||
|
|
`/usr/share/wasi-sysroot/`, which `crafter-build` finds automatically).
|
||
|
|
Other distros: install the WASI SDK and set `cfg.sysroot` in `project.cpp`.
|
||
|
|
|
||
|
|
## Build
|
||
|
|
|
||
|
|
```bash
|
||
|
|
crafter-build
|
||
|
|
```
|
||
|
|
|
||
|
|
Output lands in `bin/wasi-hello-wasm32-wasip1-native/`:
|
||
|
|
|
||
|
|
- `wasi-hello.wasm` — the compiled module
|
||
|
|
- `index.html` + `runtime.js` — a minimal in-browser WASI shim, dropped in by
|
||
|
|
`EnableWasiBrowserRuntime(cfg)` in `project.cpp`. Stdout goes to the browser
|
||
|
|
console.
|
||
|
|
|
||
|
|
## Run in a browser
|
||
|
|
|
||
|
|
```bash
|
||
|
|
./serve.sh # any static file server works
|
||
|
|
```
|
||
|
|
|
||
|
|
Open `http://localhost:8080/`. The string from `main()` shows up in the
|
||
|
|
DevTools console.
|
||
|
|
|
||
|
|
## Run in a standalone runtime
|
||
|
|
|
||
|
|
If you don't need the browser, drop the `EnableWasiBrowserRuntime(cfg)` line
|
||
|
|
from `project.cpp` and run the `.wasm` directly:
|
||
|
|
|
||
|
|
```bash
|
||
|
|
wasmtime bin/wasi-hello-wasm32-wasip1-native/wasi-hello.wasm
|
||
|
|
```
|