What is a known working setup for using Rust with emscripten (wasm32-unknown-emscripten)?

#1

Hello I’m trying to use emscripten with Rust, through the wasm32-unknown-emscripten target, ultimately to a port a large application (why not wasm32-unknown-unknown instead? I might try that next, but would like use Emscripten for the reasons in this post: Wasm unknown vs emscripten)

So I reached the point where I’m able to compile with --target wasm32-unknown-emscripten, progress… but the application crashes immediately. I tracked it down to the winit dependency, used by the glutin SDL wrapper. The problem isn’t anything specific with my app, it even reproduces with the winit “hello world” example (window)! Filed this issue to winit:

https://github.com/tomaka/winit/issues/760 missing function: emscripten_set_mousemove_callback

There is also a closed issue on the Emscripten issue tracker with some insights, same error message, but apparently a different cause: https://github.com/emscripten-core/emscripten/issues/7525. Note that I reproduced the issue on a fresh reinstall, so there are not any stale caches etc. causing the issue.

The Emscripten developers raised the question, maybe the Rust compiler stores precompiled libraries which are outdated and need to rebuilt? I updated to Rust stable 1.32, and it installed info: installing component 'rust-std' for 'wasm32-unknown-emscripten', is it possible these are compiled with an outdated Emscripten SDK?

Ultimately my question is: what is a simple known-working configuration for using Emscripten with Rust? If I can setup a trivial example working program, I can backtrack and isolate what is causing the issues here.

What I know doesn’t work: emsdk 1.38.22 with winit 0.18.1, testing the example as described in winit#760:

rustup target install wasm32-unknown-emscripten
cargo build --example window --target wasm32-unknown-emscripten

This rules out using glutin, at least with this emsdk. What crates and versions are people using Rust with Emscripten actually using? Older versions of emsdk? Other compatible crates? Is this even a winit problem or emscripten problem or rust-std problem? What actually works? Any help appreciated.

Compiling to the web with Rust and emscripten
#2

First glance:

  • You will get issues linking code using 64bit integer operations: Emscripten does not support native-int64, but will emulate this via floating-point (provided by environment) rust-wasm32-unknown-unknown does support native int64.

  • In the past I analyzed the handling of dynamic memory using emscripten, rustc or setup of clang+musl-libc https://frehberg.wordpress.com/webassembly-and-dynamic-memory/ emscripten These build systems are using different dyn-memory strategies. You will not be able to link so called “apps”, but you might be able to link object files being produced by emscripten and rustc, never tried.

Hope that helps, Frank

#3

Thanks for the info frehberg. But I’m still stuck on getting wasm32-unknown-emscripten to work at all. It compiles, producing a .wasm and .js file, but loading into a browser it immediately fails with the “missing function: emscripten_set_mousemove_callback” error.

To be clear, all I’m looking for is a minimal working example of using wasm32-unknown-emscripten.

What versions of emsdk, rustc, and what crates are known to work without an error?

  • emsdk 1.38.22 + winit 0.18.1 + the included “window” example is currently nonfunctional (https://github.com/tomaka/winit/issues/760).
  • Are their older good compatible versions of emsdk?
  • Is wasm32-unknown-emscripten even expected to work correctly anymore?

Rust + WebAssembly, including the rustwasm book https://rustwasm.github.io/book/, seems to have moved onto wasm32-unknown-unknown. But what is going on with wasm32-unknown-emscripten?

#4

To close the loop on this, for anyone who has the same questions, this is the current situation, my results of testing wasm32-unknown-emscripten with various Emscripten SDKs versions and the latest winit:

  • emsdk 1.38.22+ - 1.38.17: broke compatibility with winit 0.18.1, due to callback function changes
  • emsdk 1.38.16 - 1.37.13: ???
  • emsdk 1.37.12: almost works with winit 0.18.1, except for a bug with subnormals in rustc/emscripten
    • winit#760 assertion failed: validate_hidpi_factor(dpi_factor)
    • Workaround in iceiix/winit#1, removes the is_normal() check
    • The window sample in winit now works with this change

The bigger question of course is why is this broken, is no one using it?

Found the answer on /r/WebAssembly:

… LLVM’s native wasm support. Rust is moving away from the emscripten-based toolchain to it instead.

and:

Just wanted to note that while emscripten is still supported, all the community effort is for emscriptenless WASM, and most if not all current WASM Rust projects use the non-emcripten backend. Emscripten is supported, but Rust has, as far as I can tell, already left.

Could not find an official announcement on this change in direction, but long story short, wasm32-unknown-unknown is the way to go. The emscripten target is not as actively maintained and is bitrotting, using it is an uphill battle.