writing-webassembly
Skill2 starsChanged 4 months agoMITView on GitHub
Write, build, test, debug or review WebAssembly (Wasm, WASM), WASI programs, and WAT or WAST text. Covers WASI's capability model and its missing temp directory (Rust's std::env::temp_dir and tempfile::tempdir panic on WASI), WASI 0.1/0.2/0.3 and wasm32-wasip1/wasip2/wasip3 targets, running tests under Wasmtime, WAT operand order, WAST tests, embedding untrusted Wasm with fuel limits, and offline Nix builds. Use for .wat, .wast or .wasm files, wasm32 targets, WASI or component-model work, or a Wasm runtime host; not merely for serving an already-built .wasm file.
In this repository34 other files it gives its agents.
- capture-collaboration-evidenceSkill
- clarify-before-buildingSkill
- cross-platform-project-build-archSkill
- deep-code-reviewSkill
- dispatchSkill
- erect-agent-stackSkill
- fix-zig-deps-hashSkill
- gather-project-intentionsSkill
- handoffSkill
- hulk-bug-handoffSkill
- i18nSkill
- llvm-guided-optimizationSkill
- mechatron-ciSkill
- memoriesSkill
- mficSkill
- multi-implementation-contractsSkill
- no-ai-slopSkill
- onboardSkill
- performance-profilingSkill
- planning-workSkill
- scaffold-zig-projectSkill
- ship-itSkill
- test-isolationSkill
- universal-app-frontendSkill
- upgrading-zig-16-to-17Skill
- walking-decisionsSkill
- writing-bendSkill
- writing-elmSkill
- writing-leanSkill
- writing-nixSkill
- writing-rocSkill
- writing-rustSkill
- writing-zigSkill
- zig-microbenchmarksSkill
The file
We split this skill into the rules it gives an agent
5 rules. About 0 are common, 3 are distinctive, and 2 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈3
Distinctive · estimate
This author's own choices. Worth a look.
2
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 30
Which runtime and version?: Read the project's pins and run wasmtime --version.
- Distinctive: line 33
Read WASI for the version table, targets and runtime flags.
- Distinctive: line 67
Run the test suite under the WASI runner, not only on the host, before reporting it green.
- Very specific: line 75
Test hand-written WAT with WAST scripts run by wasmtime wast, against the unchanged production module.
- Distinctive: line 93
When a host runs guest code (plugins, hot-reloaded applications), give every call a fuel or epoch budget sized per call rather than per …
≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Writing WebAssembly
Researched on 2026-11-27 with nixpkgs' wasmtime 50.0.1, wasm-tools 1.263.0 and Rust 1.99.1. The examples build and run under wasmtime offline in this repository's CI.
Review after: 2027-02-27. WASI is under active development. Past that date, re-verify every fact marked volatile in WASI against its linked source before relying on it, in particular whether WASI has gained a temporary-directory facility, then update this date and the research date.
Know which WebAssembly you are targeting
Decide these before writing code, because the answers change the toolchain, the binary format and what the guest can do:
- Browser or WASI? Browser code targets plain Wasm
(
wasm32-unknown-unknown) and talks to JavaScript. WASI code runs in a runtime such as Wasmtime and gets system access only through WASI. - Which WASI? 0.1 (
wasm32-wasip1) is legacy but the most widely supported; 0.2 (wasm32-wasip2) and the current 0.3 build components on the component model. A core module and a component are different binary formats; check which one a runtime or tool expects. - Which runtime and version? Read the project's pins and run
wasmtime --version. Runtime flags and proposal support change monthly.
Read WASI for the version table, targets and runtime flags.
WASI grants nothing by default
A WASI guest sees only what the host passes in: directories preopened with
--dir HOST::GUEST, environment variables passed with --env, and network
access only if enabled. Wasmtime does not inherit the host environment, so a
TMPDIR set on the host is invisible to the guest. A path outside every
preopen fails with No such file or directory, not a permission error.
There is no temporary directory
This is the WASI failure agents hit most often. WASI has no temp-directory
facility, and on the Rust WASI targets std::env::temp_dir() panics with
not supported by WASI yet without ever reading TMPDIR. Everything built on
it panics the same way, including tempfile::tempdir() and
NamedTempFile::new(). Setting TMPDIR cannot fix it.
Have the host preopen a directory and tell the guest its path:
wasmtime run --dir "$host_tmp::/tmp" --env APP_TEST_TMPDIR=/tmp app.wasm
# Cargo: export CARGO_TARGET_WASM32_WASIP1_RUNNER="wasmtime run --dir $host_tmp::/tmp --env APP_TEST_TMPDIR=/tmp"In the guest, route every temporary file through one helper that reads that
variable and calls tempfile::Builder::new().tempdir_in(dir), or call
tempfile::env::override_temp_dir(dir) once at startup. Then no test can
reach tempdir() by accident. temp_dir_trap.rs
shows the panic and the fix side by side.
Test under the real runtime
- Run the test suite under the WASI runner, not only on the host, before reporting it green. Filesystem, environment, threading and panic behavior differ, and host runs hide all of it.
- On the Rust WASI targets panics abort: a failing assertion ends the whole
test binary with a Wasm
unreachabletrap (exit 134 under wasmtime).std::thread::spawnand process spawning return errors. - Inside a Nix build, set
HOME="$TMPDIR"(or pass-C cache=n) so wasmtime can create its cache. - Test hand-written WAT with WAST scripts run by
wasmtime wast, against the unchanged production module. Make test hosts validate the values they receive, and gate CI on the real host. See WAT and testing.
WAT essentials
- Instructions pop operands in push order: the left operand goes first.
Negation is
i32.const 0, then the value, theni32.sub; the reverse order computesvalue - 0and keeps the sign. The folded form(i32.sub (i32.const 0) (local.get $x))makes the order visible. - Signedness lives in the instruction (
div_s/div_u,lt_s/lt_u,shr_s/shr_u). Integer division by zero and signedINT_MIN / -1trap. - Validate with
wasm-tools validateand convert withwasm-tools parseandwasm-tools print.
Embedding untrusted Wasm
When a host runs guest code (plugins, hot-reloaded applications), give every
call a fuel or epoch budget sized per call rather than per request, and bound
memory and stack. Treat a trap as an error the host reports. Keep guest
.wat/.wasm files out of the host's Nix source closure so a guest edit does
not rebuild the host. In browsers, recreate JavaScript views of linear memory
after anything that can grow it. Read embedding.
Determinism
Floating-point NaN bit patterns and relaxed-SIMD results can differ between
engines. Simulations and replays should keep state in integers or fixed
point. Wasmtime offers -W nan-canonicalization and
-W relaxed-simd-deterministic, and fuel gives a reproducible execution
bound.
Nix builds
nixpkgs packages wasmtime, wasm-tools, wabt, binaryen, wazero and
the bindings generators. Plain pkgs.rustc cannot build for WASI; use
pkgs.pkgsCross.wasi32.buildPackages.rustc with
-C linker=${pkgs.lld}/bin/wasm-ld -C linker-flavor=wasm-ld, as
default.nix does, or add the target to an existing
rust-overlay toolchain. See WASI.
Worked examples
default.nix defines three checks that CI builds and runs offline; each fails if its example misbehaves.
- math.wat with math-tests.wast: operand order, negation, a checked add that traps on overflow, and division traps, asserted against the production module.
- sandbox.rs: a WASI program writing inside a preopened directory whose path arrives through the environment, with paths outside it denied and a run without the preopen failing.
- temp_dir_trap.rs:
std::env::temp_dir()panicking even withTMPDIRset, and the injected-directory fix.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

