mirror of
https://github.com/rustwasm/wasm-bindgen.git
synced 2025-01-07 05:31:37 +03:00
Fix direct imports in --target web
Currently the import object constructed for the `--target web` output only ever includes the current module as an one of the modules included. With `wasm-bindgen`'s optimization to import directly from modules, however, it's possible to have more modules imported from in the generated wasm file. This commit ensures that the imports are hooked up in the `--target web` es6 emulation mode, ensuring there aren't extraneous errors about import objects.
This commit is contained in:
parent
8174973c81
commit
22eb34d9ab
@ -13,7 +13,7 @@ use crate::{
|
|||||||
};
|
};
|
||||||
use failure::{bail, Error, ResultExt};
|
use failure::{bail, Error, ResultExt};
|
||||||
use std::{
|
use std::{
|
||||||
collections::{BTreeMap, HashMap, HashSet},
|
collections::{BTreeMap, HashMap, HashSet, BTreeSet},
|
||||||
env, fs,
|
env, fs,
|
||||||
};
|
};
|
||||||
use walrus::{MemoryId, Module};
|
use walrus::{MemoryId, Module};
|
||||||
@ -918,13 +918,41 @@ impl<'a> Context<'a> {
|
|||||||
} else {
|
} else {
|
||||||
""
|
""
|
||||||
};
|
};
|
||||||
|
|
||||||
let ts = Self::ts_for_init_fn(mem.import.is_some());
|
let ts = Self::ts_for_init_fn(mem.import.is_some());
|
||||||
|
|
||||||
|
// Generate extra initialization for the `imports` object if necessary
|
||||||
|
// based on the values in `direct_imports` we find. These functions are
|
||||||
|
// intended to be imported directly to the wasm module and we need to
|
||||||
|
// ensure that the modules are actually imported from and inserted into
|
||||||
|
// the object correctly.
|
||||||
|
let mut map = BTreeMap::new();
|
||||||
|
for &(module, name) in self.direct_imports.values() {
|
||||||
|
map.entry(module).or_insert(BTreeSet::new()).insert(name);
|
||||||
|
}
|
||||||
|
let mut imports_init = String::new();
|
||||||
|
for (module, names) in map {
|
||||||
|
imports_init.push_str("imports['");
|
||||||
|
imports_init.push_str(module);
|
||||||
|
imports_init.push_str("'] = { ");
|
||||||
|
for (i, name) in names.into_iter().enumerate() {
|
||||||
|
if i != 0 {
|
||||||
|
imports_init.push_str(", ");
|
||||||
|
}
|
||||||
|
let import = Import::Module { module, name, field: None };
|
||||||
|
let identifier = self.import_identifier(import);
|
||||||
|
imports_init.push_str(name);
|
||||||
|
imports_init.push_str(": ");
|
||||||
|
imports_init.push_str(&identifier);
|
||||||
|
}
|
||||||
|
imports_init.push_str(" };\n");
|
||||||
|
}
|
||||||
|
|
||||||
let js = format!(
|
let js = format!(
|
||||||
"\
|
"\
|
||||||
function init(module{init_memory_arg}) {{
|
function init(module{init_memory_arg}) {{
|
||||||
let result;
|
let result;
|
||||||
const imports = {{ './{module}': __exports }};
|
const imports = {{ './{module}': __exports }};
|
||||||
|
{imports_init}
|
||||||
if (module instanceof URL || typeof module === 'string' || module instanceof Request) {{
|
if (module instanceof URL || typeof module === 'string' || module instanceof Request) {{
|
||||||
{init_memory2}
|
{init_memory2}
|
||||||
const response = fetch(module);
|
const response = fetch(module);
|
||||||
@ -973,6 +1001,7 @@ impl<'a> Context<'a> {
|
|||||||
} else {
|
} else {
|
||||||
""
|
""
|
||||||
},
|
},
|
||||||
|
imports_init = imports_init,
|
||||||
);
|
);
|
||||||
|
|
||||||
(js, ts)
|
(js, ts)
|
||||||
|
@ -39,3 +39,4 @@ fn can_log_html_strings() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub mod snippets;
|
pub mod snippets;
|
||||||
|
pub mod modules;
|
||||||
|
3
tests/headless/modules.js
Normal file
3
tests/headless/modules.js
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
export function get_five() {
|
||||||
|
return 5;
|
||||||
|
}
|
12
tests/headless/modules.rs
Normal file
12
tests/headless/modules.rs
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
use wasm_bindgen::prelude::*;
|
||||||
|
use wasm_bindgen_test::*;
|
||||||
|
|
||||||
|
#[wasm_bindgen(raw_module = "./tests/headless/modules.js")]
|
||||||
|
extern "C" {
|
||||||
|
fn get_five() -> u32;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[wasm_bindgen_test]
|
||||||
|
fn test_get_five() {
|
||||||
|
assert_eq!(get_five(), 5);
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user