weblings: Compiling Rust to WASM from inside WASM
摘要
Weblings 是编译到 WASM 的 Rust 编译器工具链,能在浏览器中编译和执行 Rust 代码。网页 UI 包含类似 Rust Playground 的环境以及 Rustlings 练习套件。技术栈包括 rustc WASM 版、Cranelift IR 到 waffle 的转换、WASM 对象文件链接等步骤,部分组件由 AI 辅助开发。作者计划用于计算机内存原理教学和 Rust 学习反馈,并说明选择 Cranelift 而非 LLVM 的考虑,还列出 rubrc 和 rubri 等先例项目。
荐读理由
这个 toolchain 把 rustc 编译到 WASM 并实现 Cranelift IR 到 waffle 翻译和自定义 linker,你可以直接在浏览器 playground 编译执行 Rust 代码,迁移到 web 工具项目中快速验证 WASM 集成。
原文
Weblings
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.
这条对你有帮助吗?