-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
[DRAFT] add wasm-bindgen support #23493
base: main
Are you sure you want to change the base?
[DRAFT] add wasm-bindgen support #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 |
tools/link.py
Outdated
@@ -1932,6 +1933,11 @@ def phase_post_link(options, state, in_wasm, wasm_target, target, js_syms, base_ | |||
|
|||
settings.TARGET_JS_NAME = os.path.basename(state.js_target) | |||
|
|||
if settings.WASM_BINDGEN: | |||
phase_wasm_bindgen(in_wasm) | |||
settings.PRE_JS_FILES += [os.path.abspath(get_emscripten_temp_dir() + '/wbg_out/wbg_pre.js')] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can assume that get_emscripten_temp_dir()
is absolute.
Also, we an in_temp
helper for generated temporarily files. See its use elsewhere in this file.
What do you think about using bindgen
instead of the (IMHO less obvious) wbg
acronym?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed the unnecessary os.path.abspath
calls, and happy to use bindgen
over wbg
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks like in_temp()
doesn't handle the nested directory in the temp directory.
This is an early draft PR for the purposes of gathering feedback early. There are also pending changes to wasm-bindgen.
How this works:
wasm32-unknown-emscripten
into a.a
file..a
file.wasm-ld
to link the C++ and Rust into a.wasm
file..wasm
file, producing a new.wasm
file, alibrary.js
file, and apre.js
file..js
, integrating the wasm-bindgen.js
files.You can see a demo more easily at https://github.com/walkingeyerobot/cxx-rust-demo.
library_wbg.js
andpre.js
are approximately what will be produced by wasm-bindgen for consumption by Emscripten.Some TODOs:
wasm-ld
so they're not removed in the final.wasm
but 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.wasm
file itself in the form of_describe
functions. These functions are then removed after JS generation..js
files 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.-unknown
and-emscripten
that I'll have to fix.I'm mostly looking for feedback on the first point about exported symbols and about the general addition of
-sWASM_BINDGEN
to Emscripten. Again, this is very early, but it's a pretty big feature, so I thought it best to start discussions now.cc @daxpedda, who I've been working with on the wasm-bindgen side.