@@ -309,7 +309,7 @@ impl<'a> Context<'a> {
309
309
OutputMode :: NoModules { global } => {
310
310
js. push_str ( "const __exports = {};\n " ) ;
311
311
js. push_str ( "let wasm;\n " ) ;
312
- init = self . gen_init ( & module_name , needs_manual_start) ;
312
+ init = self . gen_init ( needs_manual_start) ;
313
313
footer. push_str ( & format ! (
314
314
"self.{} = Object.assign(init, __exports);\n " ,
315
315
global
@@ -347,12 +347,17 @@ impl<'a> Context<'a> {
347
347
experimental_modules : true ,
348
348
} => {
349
349
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
- }
353
350
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 ( "\n export 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 ( "\n wasm.__wbindgen_start();\n " ) ;
356
361
}
357
362
}
358
363
@@ -363,7 +368,7 @@ impl<'a> Context<'a> {
363
368
OutputMode :: Web => {
364
369
self . imports_post . push_str ( "const __exports = {};\n " ) ;
365
370
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) ;
367
372
footer. push_str ( "export default init;\n " ) ;
368
373
}
369
374
}
@@ -488,11 +493,7 @@ impl<'a> Context<'a> {
488
493
)
489
494
}
490
495
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 ) {
496
497
let mem = self . module . memories . get ( self . memory ) ;
497
498
let ( init_memory1, init_memory2) = if mem. import . is_some ( ) {
498
499
let mut memory = String :: from ( "new WebAssembly.Memory({" ) ;
@@ -519,44 +520,32 @@ impl<'a> Context<'a> {
519
520
} ;
520
521
let ts = Self :: ts_for_init_fn ( mem. import . is_some ( ) ) ;
521
522
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
+ }
554
543
555
544
let js = format ! (
556
545
"\
557
546
function init(module{init_memory_arg}) {{
558
547
let result;
559
- const imports = {{ './{module}': __exports }};
548
+ const imports = {{}};
560
549
{imports_init}
561
550
if (module instanceof URL || typeof module === 'string' || module instanceof Request) {{
562
551
{init_memory2}
@@ -598,7 +587,6 @@ impl<'a> Context<'a> {
598
587
}}
599
588
" ,
600
589
init_memory_arg = init_memory_arg,
601
- module = module_name,
602
590
init_memory1 = init_memory1,
603
591
init_memory2 = init_memory2,
604
592
start = if needs_manual_start {
@@ -2163,7 +2151,7 @@ impl<'a> Context<'a> {
2163
2151
}
2164
2152
// errors
2165
2153
if (val instanceof Error) {
2166
- return `${val.name}: ${val.message}\n ${val.stack}`;
2154
+ return `${val.name}: ${val.message}\\ n${val.stack}`;
2167
2155
}
2168
2156
// TODO we could test for more things here, like `Set`s and `Map`s.
2169
2157
return className;
0 commit comments