Skip to content

Commit 3e28e6e

Browse files
committed
Fix web, no-modules, and bundler output types
Make sure the wasm import definition map is hooked up correctly!
1 parent 68c5233 commit 3e28e6e

File tree

2 files changed

+37
-49
lines changed

2 files changed

+37
-49
lines changed

crates/cli-support/src/js/mod.rs

Lines changed: 35 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ impl<'a> Context<'a> {
309309
OutputMode::NoModules { global } => {
310310
js.push_str("const __exports = {};\n");
311311
js.push_str("let wasm;\n");
312-
init = self.gen_init(&module_name, needs_manual_start);
312+
init = self.gen_init(needs_manual_start);
313313
footer.push_str(&format!(
314314
"self.{} = Object.assign(init, __exports);\n",
315315
global
@@ -347,12 +347,17 @@ impl<'a> Context<'a> {
347347
experimental_modules: true,
348348
} => {
349349
imports.push_str(&format!("import * as wasm from './{}_bg';\n", module_name));
350-
if needs_manual_start {
351-
footer.push_str("wasm.__wbindgen_start();\n");
352-
}
353350
for (id, js) in self.wasm_import_definitions.iter() {
354-
drop((id, js));
355-
unimplemented!()
351+
let import = self.module.imports.get_mut(*id);
352+
import.module = format!("./{}.js", module_name);
353+
footer.push_str("\nexport const ");
354+
footer.push_str(&import.name);
355+
footer.push_str(" = ");
356+
footer.push_str(js.trim());
357+
footer.push_str(";\n");
358+
}
359+
if needs_manual_start {
360+
footer.push_str("\nwasm.__wbindgen_start();\n");
356361
}
357362
}
358363

@@ -363,7 +368,7 @@ impl<'a> Context<'a> {
363368
OutputMode::Web => {
364369
self.imports_post.push_str("const __exports = {};\n");
365370
self.imports_post.push_str("let wasm;\n");
366-
init = self.gen_init(&module_name, needs_manual_start);
371+
init = self.gen_init(needs_manual_start);
367372
footer.push_str("export default init;\n");
368373
}
369374
}
@@ -488,11 +493,7 @@ impl<'a> Context<'a> {
488493
)
489494
}
490495

491-
fn gen_init(&mut self, module_name: &str, needs_manual_start: bool) -> (String, String) {
492-
for (id, js) in self.wasm_import_definitions.iter() {
493-
drop((id, js));
494-
unimplemented!()
495-
}
496+
fn gen_init(&mut self, needs_manual_start: bool) -> (String, String) {
496497
let mem = self.module.memories.get(self.memory);
497498
let (init_memory1, init_memory2) = if mem.import.is_some() {
498499
let mut memory = String::from("new WebAssembly.Memory({");
@@ -519,44 +520,32 @@ impl<'a> Context<'a> {
519520
};
520521
let ts = Self::ts_for_init_fn(mem.import.is_some());
521522

522-
// Generate extra initialization for the `imports` object if necessary
523-
// based on the values in `direct_imports` we find. These functions are
524-
// intended to be imported directly to the wasm module and we need to
525-
// ensure that the modules are actually imported from and inserted into
526-
// the object correctly.
527-
// let mut map = BTreeMap::new();
528-
// for &(module, name) in self.direct_imports.values() {
529-
// map.entry(module).or_insert(BTreeSet::new()).insert(name);
530-
// }
531-
let imports_init = String::new();
532-
// for (module, names) in map {
533-
// drop((module, names));
534-
// panic!()
535-
// // imports_init.push_str("imports['");
536-
// // imports_init.push_str(module);
537-
// // imports_init.push_str("'] = { ");
538-
// // for (i, name) in names.into_iter().enumerate() {
539-
// // if i != 0 {
540-
// // imports_init.push_str(", ");
541-
// // }
542-
// // let import = Import::Module {
543-
// // module,
544-
// // name,
545-
// // field: None,
546-
// // };
547-
// // let identifier = self.import_identifier(import);
548-
// // imports_init.push_str(name);
549-
// // imports_init.push_str(": ");
550-
// // imports_init.push_str(&identifier);
551-
// // }
552-
// // imports_init.push_str(" };\n");
553-
// }
523+
// Initialize the `imports` object for all import definitions that we're
524+
// directed to wire up.
525+
let mut imports_init = String::new();
526+
let module_name = "wbg";
527+
if self.wasm_import_definitions.len() > 0 {
528+
imports_init.push_str("imports.");
529+
imports_init.push_str(module_name);
530+
imports_init.push_str(" = {};\n");
531+
}
532+
for (id, js) in self.wasm_import_definitions.iter() {
533+
let import = self.module.imports.get_mut(*id);
534+
import.module = module_name.to_string();
535+
imports_init.push_str("imports.");
536+
imports_init.push_str(module_name);
537+
imports_init.push_str(".");
538+
imports_init.push_str(&import.name);
539+
imports_init.push_str(" = ");
540+
imports_init.push_str(js.trim());
541+
imports_init.push_str(";\n");
542+
}
554543

555544
let js = format!(
556545
"\
557546
function init(module{init_memory_arg}) {{
558547
let result;
559-
const imports = {{ './{module}': __exports }};
548+
const imports = {{}};
560549
{imports_init}
561550
if (module instanceof URL || typeof module === 'string' || module instanceof Request) {{
562551
{init_memory2}
@@ -598,7 +587,6 @@ impl<'a> Context<'a> {
598587
}}
599588
",
600589
init_memory_arg = init_memory_arg,
601-
module = module_name,
602590
init_memory1 = init_memory1,
603591
init_memory2 = init_memory2,
604592
start = if needs_manual_start {
@@ -2163,7 +2151,7 @@ impl<'a> Context<'a> {
21632151
}
21642152
// errors
21652153
if (val instanceof Error) {
2166-
return `${val.name}: ${val.message}\n${val.stack}`;
2154+
return `${val.name}: ${val.message}\\n${val.stack}`;
21672155
}
21682156
// TODO we could test for more things here, like `Set`s and `Map`s.
21692157
return className;

crates/cli-support/src/js/rust2js.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -934,8 +934,8 @@ impl<'a, 'b> Rust2Js<'a, 'b> {
934934
}} catch (e) {{\n\
935935
let error = (function () {{
936936
try {{
937-
return e instanceof Error
938-
? `${{e.message}}\n\nStack:\n${{e.stack}}`
937+
return e instanceof Error \
938+
? `${{e.message}}\\n\\nStack:\\n${{e.stack}}` \
939939
: e.toString();
940940
}} catch(_) {{
941941
return \"<failed to stringify thrown value>\";

0 commit comments

Comments
 (0)