Skip to content

RFC: Rust considering switching to using WebAssembly exception handling #1119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
hoodmane opened this issue Apr 7, 2025 · 3 comments
Open
Labels
c: wasm WebAssembly export target question Not a problem with the library, but a question regarding usage.

Comments

@hoodmane
Copy link

hoodmane commented Apr 7, 2025

Rust is considering switching to using WebAssembly exception handling. See this major change proposal. We will initially leave in nightly support for opting out. How would such a change affect this project?

@Bromeon Bromeon added question Not a problem with the library, but a question regarding usage. c: wasm WebAssembly export target labels Apr 7, 2025
@PgBiel
Copy link
Contributor

PgBiel commented Apr 17, 2025

Hello,
In principle, if I understood correctly, we have been using your workaround for Wasm exceptions at rust-lang/rust#112195 (comment) ever since Godot 4.3 started compiling with Wasm exceptions (#438 (comment)), requiring GDExtension users to do so as well. Therefore, a change to further promote Wasm exceptions would likely either not affect or maybe even benefit this project, if they become the default, as that would mean one less flag that we would need to compile to Wasm, which is always welcome.

Something that would make a larger difference would be full support for panic=unwind, but I'm not really sure what's blocking that right now (I think std support? Not sure). But I'm not sure if that's related.

@hoodmane
Copy link
Author

You can try it out now with RUSTFLAGS=-Zemscripten-wasm-eh cargo build -Zbuild-std if you are interested.

we have been using your workaround for Wasm exceptions at rust-lang/rust#112195 (comment)

I didn't think that workaround was good enough since code gen for the try intrinsic comes out wrong.

Something that would make a larger difference would be full support for panic=unwind

As far as I am aware, panic=unwind is fully supported. But perhaps this is related to the fact that the workaround linked above doesn't actually work as far as I'm aware? Could you say more about what problems you are having?

@PgBiel
Copy link
Contributor

PgBiel commented Apr 18, 2025

You can try it out now with RUSTFLAGS=-Zemscripten-wasm-eh cargo build -Zbuild-std if you are interested.

Thanks. This compiles, but gives me the following error on run:

Chromium:

Uncaught (in promise) LinkError: WebAssembly.instantiate(): Import #5 "env" "__cpp_exception": tag import requires a WebAssembly.Tag

Firefox:

Uncaught (in promise) LinkError: import object field '__cpp_exception' is not a Tag

The error still occurs even if I add -Cllvm-args=-enable-emscripten-cxx-exceptions=0 from your workaround alongside -Zemscripten-wasm-eh. It only goes away and works if I remove -Zemscripten-wasm-eh and keep the former, as before.

Do you have any ideas on this?

Flags used were as follows

[target.wasm32-unknown-emscripten]
rustflags = [
    "-C", "link-args=-sSIDE_MODULE=2",
    "-Zlink-native-libraries=no",
    "-Zemscripten-wasm-eh"
    # "-Cllvm-args=-enable-emscripten-cxx-exceptions=0", # uncommenting doesn't help
]

we have been using your workaround for Wasm exceptions at rust-lang/rust#112195 (comment)

I didn't think that workaround was good enough since code gen for the try intrinsic comes out wrong.

Something that would make a larger difference would be full support for panic=unwind

(...) But perhaps this is related to the fact that the workaround linked above doesn't actually work as far as I'm aware? Could you say more about what problems you are having?

The workaround very much worked for our purposes. The problem is that upstream Godot switched to Wasm exceptions, so our code compiled with old / js exceptions wouldn't even run because it would expect functions named invoke_(signature) to exist for each function declared on the Rust side, but since our code would run as a library / side module and Godot would run as the entrypoint program (which i suppose is where such functions would be created for deduplication reasons), these functions would never be generated by emscripten.

By using your workaround, the compiled code would successfully run alongside Godot, as it wouldn't generate those calls to invoke_xxxx anymore.

That is, solving unwinding panic was only secondary compared to making the code run in the first place, haha.

As far as I am aware, panic=unwind is fully supported.

I just tested it and it's still aborting regardless, but I'm not using -Zemscripten-wasm-eh yet as it doesn't run due to the above. If we can find a way to run it, I can try again and post my results.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c: wasm WebAssembly export target question Not a problem with the library, but a question regarding usage.
Projects
None yet
Development

No branches or pull requests

3 participants