Weblings is a Rust compiler toolchain compiled to WASM. This means that you can compile and execute Rust code in your browser!
Tech stack tl;dr rust code -> rustc -> cranelift IR -> waffle -> wasm object files -> linker -> wasm executable -> browser execution
The web UI
The primary way to interface with this WASM toolchain is through the web UI, which you can access at https://weblings.forest-anderson.ca. It has two sections:
- Playground: Similar to the Rust Playground, but the code compiles and runs in your browser
- Rustlings: The beloved Rustlings excercise suite, brought to you in your browser!
The stack
A few things had to be set up to get this toolchain working:
- rustc compiled to WASM: @bjorn3 has set up branches that patch rustc so that it can run inside WASM correctly. Very awesome! I build the rustc binaries for WASM in this repo.
- ⭐ Cranelift IR -> waffle translation: a crate to go from Cranelift's intermediate representation (CLIF) to waffle's own intermediate representation, which waffle can then translate to WASM. This solution was inspired by disussion in this issue on bytecodealliance/wasmtime.
- ⭐ A wasm
linker:
a crate to link the compiled WASM object to the bundled std sysroot rlibs.
There are some alternatives, like
wasm-ldfrom LLVM. I was hoping that the Wild linker might have WASM linking support, but that's still on the way.
⭐ - these steps were written with AI tooling to achieve the goals of this project, but I'd love to see them included in Cranelift/Wild, and hope to find a way to upstream them!
Why?
I've set up this toolchain so that I can build out some tooling that I'll be using in upcoming projects to teach computer memory principles. I've been exploring visualizing memory segments during C code execution, and I'll be hopefully expanding that to Rust soon. Also, I want more ways to get quick feedback when learning Rust concepts!
There is also the question of why to use Cranelift instead of LLVM as the compiler backend. Here are a few things on my mind:
- LLVM would be a larger toolchain to include (I think)
- Cranelift is faster to get (I did not test numbers)
- The whole toolchain in Rust is pretty neat!
Prior art
- rubrc: (website) Uses rustc compiled to WASM, with LLVM as the backend, also compiled to WASM.
- rubri: (website) Uses Miri to create a Rust interpreter that can run in WASM.
(If you know of others, please make a PR!)
AI Assistance
AI tooling was used to create the Leptos web UI, the Cranelift -> Waffle
IR translation, the WASM linker,
and various other scripts. *.md files in this repo are human made.
0 Comments
Log in to join the conversation.No comments yet. Be the first to share your thoughts.