Rain Post Wasm — Wasm

Ship a Rust rain tool to phones, a village server and the edge: size, headers, WASI, threads. 16 levels, played right in the browser with nothing to install.

What you learn in this game

WebAssembly is fast inside, but its door costs something. Every time JavaScript calls a module function, values have to be translated and the glue has to run. At the Sukamaju rain post, Mr. Aji's tool is called mangsa: written in Rust, built with wasm-pack, and run in real Chromium and WebKit. The crossing count on the board is counted by the module itself.

wasm-pack build wraps the Rust output into a package in pkg/: the .wasm file, the JavaScript glue wasm-bindgen writes, and TypeScript types. The glue's shape depends on --target, because each place loads WebAssembly its own way. Every combination in this level was really built with wasm-pack 0.15.0 and opened in Chromium and WebKit.

Node runs WebAssembly with the same V8 engine as Chromium, but loads the file differently: there is no page and no URL, there is the disk. A .mjs file is an ES module. Inside it import is valid and require does not exist.

For a cheap phone on a weak signal every kilobyte counts. The size of a .wasm is decided by how rustc optimises the code and by what comes along from the libraries. [profile.release] in Cargo.toml sets the optimisation of a release build. Every combination in this level was really built with rustc 1.99.1, then measured.

Rustc stops at the edges of crates and libraries; binaryen sees the final file. Many WebAssembly projects run wasm-opt as their last step. A .wasm file also has custom sections it does not need to run, function names for example.

Tools like twiggy show which functions take room in a .wasm file. Mr. Aji's laptop reads the same list straight from the code and name sections of the built file. Size is not only a matter of compiler settings: the features the code uses decide what comes along.