Timber Yard Wasm — Wasm

One Rust nota rule for the office PC and the buyer's phone: WIT, Wasmtime, jco, the canonical ABI. 15 levels, right in the browser.

What you learn in this game

Toko Kayu Margono, at the mouth of Akasia Lane, sells planks and beams by the cubic metre. Its receipt rule lives in two places: an old program on the office PC and Mr. Piksel's phone page, and the two are often a few hundred rupiah apart. Ms. Raras writes the rule once, in Rust, so the office and the phones use the same one. A plain WebAssembly module (a core module) only takes and returns numbers. Text has to go through memory: write its bytes, send their address and length. Everything you see in this store runs for real: the modules are built by rustc 1.99, and the page runs in your own browser.

A component is a core module wrapped in a contract. The contract is written in WIT (Wasm Interface Type): which functions exist, and which rich types they take, such as string, list, record, option and result. Wasmtime runs a component directly. --invoke writes the call in the WAVE format: strings in quotes, records in curly braces, none for an empty option.

Almost everyone thinks WebAssembly runs in any browser. That is true for core modules. Components are a newer format, and browsers do not know them yet. jco from the Bytecode Alliance translates a component into a core module for the browser, plus JavaScript that packs strings and lists into its memory.

WIT is a contract: written once, read by every language. package gives a name and a version, interface gathers types and functions, and world names what the component gives and asks for. Rust reads the contract through wit-bindgen 0.62: generate! turns the WIT into a Guest trait that Raras fills in, and export! plugs it in.

WIT has rich types: record (an object with fixed fields), list<T> (a sequence), string, option<T>, result<T, E>, variant and enum, and the numbers u8 to u64, s8 to s64, f32, f64, bool and char. Every language has its match. In Rust list<T> is Vec<T>, in JavaScript an array.

WASI is the system interface for WebAssembly: files, clocks, the screen, exiting a program. WASI 0.2 and 0.3 are built from components: wasi:cli, wasi:io and wasi:clocks are plain WIT interfaces that a component imports and its host gives. The host decides what it gives. Wasmtime gives all of them by default. A page in the browser gives none unless you add them.