Repository navigation
Description
Summary
Rune builds for wasm32-unknown-unknown (a page with JavaScript), but not for wasm32-wasip2. The request is for the rune crate (and rune-luau) to compile for wasm32-wasip2, so a Rune app can run as a WebAssembly component in a non-browser runtime such as Wasmtime, drawing through wasi:webgpu.
This is not a request for a full WASI platform backend. With the compile fixes below, an embedder can drive Runtime and Renderer from outside the crate through the public API, the way platform/embed.rs (LayerHost) does on Apple targets.
I tried this at c4eda05889c984fff91ccd906e3c00cdfc4cae24 and it works; details are under "What was verified".
Why it does not build today
target_arch = "wasm32"is used to mean "browser". There are about 340 suchcfgsites across the workspace (about 330 incrates/runeandcrates/rune-staging, the rest inrune-luau,rune-i18n,rune-fx,rune-light), and none checkstarget_os. Onwasm32-wasip2they select thewasm-bindgen/web-sys/js-syspaths, which need JavaScript. WASI has a real libc,std::time::Instant, ICU4X and theimagedecoders, so nearly every site should take the native path there.- winit has no WASI platform.
vendor/winithitscompile_error!("The platform you're compiling for is not supported by winit"), and itsCargo.tomlpullswasm-bindgen,web-sys,js-sysandweb-timefor anytarget_family = "wasm". Rune uses winit types in many files, so the crate cannot build without it.
Proposed change
1. A browser cfg alias. In crates/rune/build.rs (and rune-staging):
println!("cargo::rustc-check-cfg=cfg(browser)");
let target = |key: &str| std::env::var(key).unwrap_or_default();
if target("CARGO_CFG_TARGET_ARCH") == "wasm32" && target("CARGO_CFG_TARGET_OS") != "wasi" {
println!("cargo::rustc-cfg=browser");
}Then replace target_arch = "wasm32" with browser in the source. This was one mechanical search and replace; 328 of the 330 sites in rune and rune-staging needed nothing else. Behaviour on wasm32-unknown-unknown and on native targets does not change.
2. Dependency tables. In crates/rune/Cargo.toml, the web-only table becomes cfg(all(target_arch = "wasm32", not(target_os = "wasi"))) and the native table becomes cfg(any(not(target_arch = "wasm32"), target_os = "wasi")). The same for the web table in crates/rune-luau/Cargo.toml.
3. A few sites where WASI joins an existing "this platform has none" list (about 23 lines in 5 files):
clipboard/native.rs: WASI takes the iOS/Android "no OS clipboard" backend (noarboard).text/system_fonts.rs: WASI joins Android and the web in "no system fonts to ask".overlay/options.rs,overlay/placement.rs: no native display id and no pointer position.frame/host_clock.rs:now_nsfromstd::time::Instanton WASI.
4. winit. A small stub platform in the vendored copy (src/platform_impl/wasi/, about 500 lines, added to vendor/winit.patch): Window::new returns an error, EventLoop::run returns NotSupported, there are no monitors. It exists only so the types resolve; an embedder never creates a winit window. The web-only dependencies in vendor/winit/Cargo.toml are gated to target_os = "unknown".
5. rune-luau. A wasm32-wasip2 branch in build.rs (about 15 lines): WASI libc is already the real libc there, so only c++abi, unwind (from the SDK's lib/wasm32-wasip2/eh) and wasi-emulated-process-clocks are linked. Luau's C++ was built with WASI SDK 34.0 and -fexceptions -fwasm-exceptions -mllvm -wasm-use-legacy-eh=false.
What was verified
All at the commit above, with the changes described:
cargo check -p rune --lib --no-default-features --target wasm32-wasip2passes, also with--features embedded-fonts,svgand with--features tooling.cargo tree --target wasm32-wasip2shows nowasm-bindgen,web-sys,js-sysorweb-time.- A
wasm32-wasip2component ran under Wasmtime 50 and drew a frame with Rune's realRuntime, layout, text system andRenderer: a rounded card with wrapped Latin and Vietnamese text at scale 2, read back as pixels. The component got itswgpudevice from the host throughwasi:webgpu(awgpu 30custom backend) and passed it toRenderer::new; it did not callGpu::try_new. No Rune source change beyond the list above was needed for this. - A component with
rune-luauand the Luau VM received a script as text at run time, drew it, handled a press through theRuntime's input path, and reloaded a second version withsaveHotState/loadHotStatekeeping its state. Error paths worked too (a failed reload keeps the old VM, the watchdog stops a busy handler, arendererror shows the error card), so C++ exceptions work.
Two things an embedder had to know, which a note in the docs would cover:
- A canvas that is not an sRGB format (for example
Bgra8Unorm) needsset_srgb_blend(true)on bothRuntimeandRenderer, asLayerHostdoes. - On wasm32 with wgpu's
custombackend, wgpu types are notSend/Syncunless wgpu'sfragile-send-sync-non-atomic-wasmfeature is on; thestatics inrender.rsandrender/warm.rsneed it. That is sound while the target has no threads.
Not covered, and not asked for here
std::thread::spawnsites (task.rs, the image decode worker, the clipboard worker,spell.rs,quit.rs, tooling,gpu/pick.rs) compile but cannot start a thread onwasm32-wasip2. The frames above did not reach them.spawn_blockingalready falls back to running inline.pollster::block_oninrender.rsandgpu/pick.rs(an embedder that supplies its own device avoids it).- Timers and wakers driven by a host, IME, clipboard, system fonts and accessibility on WASI. These need host interfaces that WASI does not standardise yet.
- I did not rebuild
wasm32-unknown-unknownor native macOS with the changes; they are unchanged by construction, but that is not tested.
I can attach the full patch (about 3,500 lines, almost all of it the mechanical rename) if that helps.
Activity
opencodejr commentedon Nov 25, 2026
Thanks for the detailed write-up and for testing it end to end, including the Luau exception paths.
I checked the current
devhead and the two causes you describe are still there:target_arch = "wasm32"sites checkstarget_os, and no crate sets abrowser-style cfg.vendor/winit/src/platform_impl/has no WASI platform.One thing has changed since
c4eda05: Rune has been split into more crates, and the wasm32 sites are now spread over about 16 of them (rune,rune-core,rune-text,rune-image,rune-gpu,rune-media,rune-edit,rune-spell,rune-kit,procwatch,rune-perfand others, not onlyruneandrune-staging). So the cfg alias would have to be set in each of those crates, and the dependency tables in theirCargo.tomlfiles need the same split. The winit stub would go intovendor/winit.patch, as you suggest.I'm treating this as a feature request for
wasm32-wasip2builds ofruneandrune-luau, limited to compiling and to embedder-driven use as you describe. Thesrgb_blendandfragile-send-sync-non-atomic-wasmnotes are useful for the docs. Your patch would help as a reference, but it's against an older layout, so it won't apply as it is.opencodejr commentedon Nov 25, 2026
I looked at doing this and haven't opened a PR. On current
devit's a larger change than one automated PR should carry:target_arch = "wasm32"sites in 16 crates, so thebrowsercfg would need abuild.rsin each crate, plus the split in each crate'sCargo.toml.platform_impl/wasistub, carried invendor/winit.patch.rune-luau/build.rsneeds a WASI SDK branch.wasm32-unknown-unknown(cargo check-wasm/cargo check-webgl) and native targets need checking afterwards, because apps pindevrevs.A new cfg that every crate has to set, and gating winit's web dependencies on
target_os = "unknown", changes conventions for the whole repo, so I'm leaving that decision to the maintainers. The issue stays open as a feature request. If they go ahead, splitting the work would make it easier to review: first the cfg alias and dependency tables, with acargo check-wasip2alias; then the winit stub; thenrune-luau. Your patch would be a useful reference for that, rebased onto the current crate layout.