1
Fork 0

rustc: Stabilize #[wasm_import_module] as #[link(...)]

This commit stabilizes the `#[wasm_import_module]` attribute as
`#[link(wasm_import_module = "...")]`. Tracked by #52090 this new directive in
the `#[link]` attribute is used to configured the module name that the imports
are listed with. The WebAssembly specification indicates two utf-8 names are
associated with all imported items, one for the module the item comes from and
one for the item itself. The item itself is configurable in Rust via its
identifier or `#[link_name = "..."]`, but the module name was previously not
configurable and defaulted to `"env"`. This commit ensures that this is also
configurable.

Closes #52090
This commit is contained in:
Alex Crichton 2018-07-16 11:31:14 -07:00
parent 29ee65411c
commit b9024f8a75
16 changed files with 167 additions and 162 deletions

View file

@ -226,13 +226,22 @@ pub fn provide(providers: &mut Providers) {
pub fn provide_extern(providers: &mut Providers) {
providers.wasm_import_module_map = |tcx, cnum| {
// Build up a map from DefId to a `NativeLibrary` structure, where
// `NativeLibrary` internally contains information about
// `#[link(wasm_import_module = "...")]` for example.
let native_libs = tcx.native_libraries(cnum);
let mut def_id_to_native_lib = FxHashMap();
for lib in native_libs.iter() {
if let Some(id) = lib.foreign_module {
def_id_to_native_lib.insert(id, lib);
}
}
let mut ret = FxHashMap();
for lib in tcx.foreign_modules(cnum).iter() {
let attrs = tcx.get_attrs(lib.def_id);
let mut module = None;
for attr in attrs.iter().filter(|a| a.check_name("wasm_import_module")) {
module = attr.value_str();
}
let module = def_id_to_native_lib
.get(&lib.def_id)
.and_then(|s| s.wasm_import_module);
let module = match module {
Some(s) => s,
None => continue,
@ -244,7 +253,7 @@ pub fn provide_extern(providers: &mut Providers) {
}
Lrc::new(ret)
}
};
}
fn wasm_import_module(tcx: TyCtxt, id: DefId) -> Option<CString> {