- Published on
A Tiny WebAssembly Playground
- Authors

- Name
- Garfield Zhu
- @_AlohaYo_
@Author: Garfield Zhu
A Tiny WebAssembly Playground
I wanted to edit a small WebAssembly program on the page and see what it returned. This playground does that: change a line, press Run WASM, and get a number back. The number also sets a triangle's color, so you can see the result.
WebAssembly runs binary code, which is no fun to edit by hand. The editor uses WAT, WebAssembly's readable text format. This function takes a number, multiplies it by seven, and keeps the result between 0 and 254:
(module
(func (export "shade") (param i32) (result i32)
local.get 0
i32.const 7
i32.mul
i32.const 255
i32.rem_u
)
)
With the input set to 12, it returns 84. Change i32.const 7 to i32.const 3, run it again, and the result becomes 36. Try it here:
Edit the WASM, then run it
One exported i32 → i32 function. No imports, no DOM, no surprise.
Change i32.const 7 to i32.const 3, then press Run WASM. With input 12, the result goes from 84 to 36, so the triangle changes color. ⌘/Ctrl + Enter works too.
Preparing the browser runtime…
Preparing the preview…
The code can still run while WebGPU is checked.
The WASM return value becomes a color and is sent to a WebGPU uniform buffer. The triangle is intentionally humble.
From text to a running function
The component keeps the WAT in a text editor and the input in a number field. When you press Run WASM, a small parser turns the supported instructions into binary bytes, then the browser runs the exported shade function. WebAssembly.instantiate() is the browser call that loads those bytes:
const bytes = compileEditableWat(source)
const { instance } = await WebAssembly.instantiate(bytes)
const result = instance.exports.shade(input)
The parser only supports one function and a handful of i32 instructions. That keeps the code small, and it is all this example needs.
JavaScript shows the returned number. When WebGPU is available, the same number sets the triangle's color. When it is not, the number still appears and the editor still runs.
This playground edits WAT rather than Rust. A Rust program needs to be compiled to WebAssembly first; here I wanted to keep the browser-side part small and easy to try. For a Rust example, see my WASM Game of Life.