rustc: Add a #[wasm_import_module] attribute #48992
Closed
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
This commit adds a new attribute to the Rust compiler specific to the wasm
target (and no other targets). The
#[wasm_import_module]attribute is used tospecify the module that a name is imported from, and is used like so:
Here the import of the symbol
some_js_functionis tagged with the./foo.jsmodule in the wasm output file. Wasm-the-format includes two fields on all
imports, a module and a field. The field is the symbol name (
some_js_functionabove) and the module has historically unconditionally been
"env". I'm notsure if this
"env"convention has asm.js or LLVM roots, but regardless we'dlike the ability to configure it!
The proposed ES module integration with wasm (aka a wasm module is "just another
ES module") requires that the import module of wasm imports is interpreted as an
ES module import, meaning that you'll need to encode paths, NPM packages, etc.
As a result, we'll need this to be something other than
"env"!Unfortunately neither our version of LLVM nor LLD supports custom import modules
(aka anything not
"env"). My hope is that by the time LLVM 7 is released bothwill have support, but in the meantime this commit adds some primitive
encoding/decoding of wasm files to the compiler. This way rustc postprocesses
the wasm module that LLVM emits to ensure it's got all the imports we'd like to
have in it.
Eventually I'd ideally like to unconditionally require this attribute to be
placed on all
extern { ... }blocks. For now though it seemed prudent to addit as an unstable attribute, so for now it's not required (as that'd force usage
of a feature gate). Hopefully it doesn't take too long to "stabilize" this!
cc rust-lang-nursery/rust-wasm#29