Skip to content

Compile for wasm32-wasip2: wasm32 is treated as "browser", and winit has no WASI platform #911

Open

Description

@ducan-ne

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

  1. target_arch = "wasm32" is used to mean "browser". There are about 340 such cfg sites across the workspace (about 330 in crates/rune and crates/rune-staging, the rest in rune-luau, rune-i18n, rune-fx, rune-light), and none checks target_os. On wasm32-wasip2 they select the wasm-bindgen / web-sys / js-sys paths, which need JavaScript. WASI has a real libc, std::time::Instant, ICU4X and the image decoders, so nearly every site should take the native path there.
  2. winit has no WASI platform. vendor/winit hits compile_error!("The platform you're compiling for is not supported by winit"), and its Cargo.toml pulls wasm-bindgen, web-sys, js-sys and web-time for any target_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 (no arboard).
  • 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_ns from std::time::Instant on 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-wasip2 passes, also with --features embedded-fonts,svg and with --features tooling. cargo tree --target wasm32-wasip2 shows no wasm-bindgen, web-sys, js-sys or web-time.
  • A wasm32-wasip2 component ran under Wasmtime 50 and drew a frame with Rune's real Runtime, layout, text system and Renderer: a rounded card with wrapped Latin and Vietnamese text at scale 2, read back as pixels. The component got its wgpu device from the host through wasi:webgpu (a wgpu 30 custom backend) and passed it to Renderer::new; it did not call Gpu::try_new. No Rune source change beyond the list above was needed for this.
  • A component with rune-luau and the Luau VM received a script as text at run time, drew it, handled a press through the Runtime's input path, and reloaded a second version with saveHotState / loadHotState keeping its state. Error paths worked too (a failed reload keeps the old VM, the watchdog stops a busy handler, a render error 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) needs set_srgb_blend(true) on both Runtime and Renderer, as LayerHost does.
  • On wasm32 with wgpu's custom backend, wgpu types are not Send/Sync unless wgpu's fragile-send-sync-non-atomic-wasm feature is on; the statics in render.rs and render/warm.rs need it. That is sound while the target has no threads.

Not covered, and not asked for here

  • std::thread::spawn sites (task.rs, the image decode worker, the clipboard worker, spell.rs, quit.rs, tooling, gpu/pick.rs) compile but cannot start a thread on wasm32-wasip2. The frames above did not reach them. spawn_blocking already falls back to running inline.
  • pollster::block_on in render.rs and gpu/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-unknown or 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

  1. opencodejr commented on Nov 25, 2026

    @opencodejr
    Contributor

    Thanks for the detailed write-up and for testing it end to end, including the Luau exception paths.

    I checked the current dev head and the two causes you describe are still there:

    • None of the target_arch = "wasm32" sites checks target_os, and no crate sets a browser-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-perf and others, not only rune and rune-staging). So the cfg alias would have to be set in each of those crates, and the dependency tables in their Cargo.toml files need the same split. The winit stub would go into vendor/winit.patch, as you suggest.

    I'm treating this as a feature request for wasm32-wasip2 builds of rune and rune-luau, limited to compiling and to embedder-driven use as you describe. The srgb_blend and fragile-send-sync-non-atomic-wasm notes 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.

  2. opencodejr commented on Nov 25, 2026

    @opencodejr
    Contributor

    I looked at doing this and haven't opened a PR. On current dev it's a larger change than one automated PR should carry:

    • There are 954 target_arch = "wasm32" sites in 16 crates, so the browser cfg would need a build.rs in each crate, plus the split in each crate's Cargo.toml.
    • winit needs a new platform_impl/wasi stub, carried in vendor/winit.patch.
    • rune-luau/build.rs needs a WASI SDK branch.
    • Both wasm32-unknown-unknown (cargo check-wasm / cargo check-webgl) and native targets need checking afterwards, because apps pin dev revs.

    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 a cargo check-wasip2 alias; then the winit stub; then rune-luau. Your patch would be a useful reference for that, rebased onto the current crate layout.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions