Add initial integration with wasm-bindgen#23493
Conversation
Does rustc then read the wasm to find those function names, and pass those names to In general if we need to read metadata-type info from the wasm, then we have a minimal parser in tools/webassembly.py. If we need something more complex, a binaryen pass is an option. |
|
wasm-bindgen itself is two pieces: a library that allows you to annotate your rust code marking things to be exported, and a tool that consumes a .wasm file and reads those annotations to produce a companion js file. rustc knows about those function names because wasm-bindgen as a library provided the annotations. If rustc invokes the linker itself, it's able to pass that information along. However, because we need to also build C++, we're only using rustc to compile and not drive the whole process, so we need to have it output that information elsewhere. One (very naive) possibility is to have rustc invoke a fake linker that just writes the |
Export all C symbols, except perhaps those from system libraries.
Automatically infer what symbols to export for wasm-bindgen
| return run_binaryen_command('wasm-opt', infile, outfile, args=args, **kwargs) | ||
|
|
||
|
|
||
| def run_wasm_bindgen(infile, outfile=None, args=[], **kwargs): # noqa |
There was a problem hiding this comment.
This only has a single caller which only passes one argument so I think we can just remove all the other arguments here?
|
Codesize failures probably just mean you need a rebase/merge. |
sbc100
left a comment
There was a problem hiding this comment.
What would you like the comment message to look like? Do you want to update the PR description with that, or write it in a comment?
Something like this? Feel free to edit. Adds wasm-bindgen support. When |
|
Actually now that I think about it this should probably also come with a ChangeLog entry.. |
|
I added a ChangeLog entry. |
| ---------------------- | ||
| - The `-sUSE_PTHREADS` and `-sMEMORY64` flags have been deprecated in favor of the | ||
| more standard `-pthread` and `-m64` (or `--target=wasm64`) flags. (#27025) | ||
| - Adds wasm-bindgen support. When `-sWASM_BINDGEN` is set, Emscripten will call out to `wasm-bindgen` in the users's path and integrate the wasm-bindgen JS with the normal Emscripten JS. Some wasm-bindgen features may not yet be fully supported. |
There was a problem hiding this comment.
Sorry to be a pain, can you wrap this at 80 cols.
| ---------------------- | ||
| - The `-sUSE_PTHREADS` and `-sMEMORY64` flags have been deprecated in favor of the | ||
| more standard `-pthread` and `-m64` (or `--target=wasm64`) flags. (#27025) | ||
| - Adds wasm-bindgen support. When `-sWASM_BINDGEN` is set, Emscripten will call out to `wasm-bindgen` in the users's path and integrate the wasm-bindgen JS with the normal Emscripten JS. Some wasm-bindgen features may not yet be fully supported. |
There was a problem hiding this comment.
And add (#23493) to the end of the line
This is an early draft PR for the purposes of gathering feedback early. There are also pending changes to wasm-bindgen.This is ready for review.How this works:
wasm32-unknown-emscripteninto a.afile (staticlib). This.afile includes some annotations needed by wasm-bindgen later..afile.wasm-ldto link the C++ and Rust together into a single.wasmfile..wasmfile, removing the annotations needed by wasm-bindgen and producing a new.wasmfile, alibrary.jsfile, and apre.jsfile..js, integrating the wasm-bindgen.jsfiles.You can see a demo more easily at https://github.com/walkingeyerobot/cxx-rust-demo.
Some TODOs:
Figure out how to pass the exported symbols from the rust compiler to Emscripten. These are symbols that need to be passed towasm-ldso they're not removed in the final.wasmbut that may not necessarily be present after wasm-bindgen processes the.wasm. wasm-bindgen at compile time puts the information it needs to generate JS inside the.wasmfile itself in the form of_describefunctions. These functions are then removed after JS generation.Merge the.jsfiles produced by wasm-bindgen. This shouldn't be that hard; I just haven't gotten around to it yet. This would simplify the code for both Emscripten and wasm-bindgen.Get wasm-bindgen tests to pass. Early efforts here have revealed some very odd compiler differences between-unknownand-emscriptenthat I'll have to fix.Have this work end-to-end via wasm-pack. I'll have a draft PR for this soon (tm).My work here didn't pan out, but there's a new PR for this here: feat: support wasm32-unknown-emscripten target wasm-bindgen/wasm-pack#1583I'm mostly looking for feedback on the first point about exported symbols and about the general addition of-sWASM_BINDGENto Emscripten. Again, this is very early, but it's a pretty big feature, so I thought it best to start discussions now.cc @daxpedda @guybedford @RReverser, who I've been working with on the wasm-bindgen side.
(updated May 18 2026 to be more accurate as to the current state of things)