A toy WASM interpreter, designed to run on microcontrollers with minimal resources available.
- Create the following projects:
shared
for sharing types between the runner and the wasm binary (e.g. for enums)sys
for the FFI interface. This should have a safe wrapper around severalextern "C"
functions.- 1 or more crate that get compiled to
wasm32-unknown-unknown
, using theshared
andsys
crates. - A
runner
crate that runs your WASM interpreter. This references theshared
crate.
- Create a FFI
- In your
runner
crate, make sure you can parse the FFI calls (ProcessAction::CallExtern { function, args }
) - Add the function to your
sys
crate
- In your
- Run your WASM
- Parse it with
Wasm::parse
- Spawn a new process with
wasm.spawn("start_fn_name")
- Continuously poll
process.step()
and handle theProcessAction
result
- Parse it with
See runner/desktop
and projects/sys
for an example.
You need the following tools:
- Rust nightly (any nightly will do)
- Target
wasm32-unknown-unknown
- Target
- For rp2040:
- target
thumbv6m-none-eabi
cargo install elf2uf2-rs --locked
- For debugging:
cargo install cargo-embed --locked
- target
First build projects/blink
. This will produce a wasm in the projects/blink/target/wasm32-unknown-unknown
folder. (For microcontrollers, cargo build --release
is recommended)
Then build one of:
- Desktop runner:
cargo run -- ./projects/blink/target/wasm32-unknown-unknown/release/blink.wasm
- RP2040:
./flash.sh
or./flash.ps1
for flashing in DAP mode (mounted as a file system)cargo embed
for flashing and debugging through a SWD debugger
The crate that parses wasm and provides handles to processes.
Projects are applications compiled to wasm. This folder is structured as followed:
blink
is a simple blink script.shared
is a shared library. This is used to have the same type definitions betweenprojects/blink
and any of the runners.sys
is the safe FFI interface for the scripts.
Runners are binaries that run on certain platforms, and run a given binary.
There are currently 2 runners:
desktop
: Runs on a pc as a CLI tool. Takes the WASM file as the first arg.rp2040
: Runs a WASM file on a rp2040 (hal) microcontroller. The WASM file is currently embedded throughinclude_bytes!()
https://webassembly.github.io/wabt/demo/wasm2wat/
Upload a .wasm here to inspect the contents