Description
This is a tracking issue for the #[wasm_import_module]
attribute and the wasm_import_module
feature. This attribute is applied to extern { ... }
blocks like so:
#[wasm_import_module = "foo"]
extern {
fn foo();
fn bar();
// ...
}
The WebAssembly specification requires that all imported values from the host environment have a two-level namespace:
All imports include two opaque names: a module name and an import name, which are required to be valid UTF-8. The interpretation of these names is up to the host environment but designed to allow a host environments, like the Web, to support a two-level namespace.
Additionally only globals (like static variables), functions, memories, and tables can be imported. The extern { ... }
language block is used to import globals and functions, memories and tables cannot currently be manually imported.
Each field of the wasm import needs to be configurable by Rust as both fields have semantic meaning. Typically the first field of the import is the "module" interpreted as an ES module, and the second field is what to import from that module.
Currently LLVM and LLD will default imports to the "env" module. This means that if you compile this code:
extern {
fn foo();
}
it will generate a wasm file that imports the function "foo" from the module "env". By using #[wasm_import_module]
we can customize what happens here:
#[wasm_import_module = "somewhere else"]
extern {
fn foo();
}
This attribute must be of the form #[wasm_import_module = "string"]
, no other forms are accepted. It can only be attached to an extern
block. All items in the block are considered to come from the same module. Through the usage of #[link_name]
we can then configure both fields of WebAssembly imports arbitrarily:
#[wasm_import_module = "somewhere else"]
extern {
#[link_name = "some_other_name"]
fn foo();
}
The #[wasm_import_module]
is accepted on non-wasm platforms but has no effect, it is simply an ignored attribute.
Helpful links:
- Implementation
- Test for a well-formed attribute
- Test that it is emitted correctly in the wasm file
- Test that it is currently unstable
Open questions and TODO
- Documentation in the reference
- Should the attribute instead be
#[link(wasm_import_module = "...")]
?