@@ -311,14 +311,15 @@ impl<'a> Context<'a> {
311
311
/// `--target no-modules`, `--target web`, or for bundlers. This is the very
312
312
/// last step performed in `finalize`.
313
313
fn finalize_js ( & mut self , module_name : & str , needs_manual_start : bool ) -> ( String , String ) {
314
+ let mut ts = self . typescript . clone ( ) ;
314
315
let mut js = String :: new ( ) ;
315
316
if self . config . mode . no_modules ( ) {
316
317
js. push_str ( "(function() {\n " ) ;
317
318
}
318
319
319
320
// Depending on the output mode, generate necessary glue to actually
320
321
// import the wasm file in one way or another.
321
- let mut init = String :: new ( ) ;
322
+ let mut init = ( String :: new ( ) , String :: new ( ) ) ;
322
323
match & self . config . mode {
323
324
// In `--target no-modules` mode we need to both expose a name on
324
325
// the global object as well as generate our own custom start
@@ -371,6 +372,10 @@ impl<'a> Context<'a> {
371
372
}
372
373
}
373
374
375
+ let ( init_js, init_ts) = init;
376
+
377
+ ts. push_str ( & init_ts) ;
378
+
374
379
// Emit all the JS for importing all our functionality
375
380
js. push_str ( & self . imports ) ;
376
381
js. push_str ( "\n " ) ;
@@ -382,7 +387,7 @@ impl<'a> Context<'a> {
382
387
js. push_str ( "\n " ) ;
383
388
384
389
// Generate the initialization glue, if there was any
385
- js. push_str ( & init ) ;
390
+ js. push_str ( & init_js ) ;
386
391
js. push_str ( "\n " ) ;
387
392
js. push_str ( & self . footer ) ;
388
393
js. push_str ( "\n " ) ;
@@ -394,7 +399,7 @@ impl<'a> Context<'a> {
394
399
js = js. replace ( "\n \n \n " , "\n \n " ) ;
395
400
}
396
401
397
- ( js, self . typescript . clone ( ) )
402
+ ( js, ts )
398
403
}
399
404
400
405
fn wire_up_initial_intrinsics ( & mut self ) -> Result < ( ) , Error > {
@@ -842,7 +847,43 @@ impl<'a> Context<'a> {
842
847
Ok ( ( ) )
843
848
}
844
849
845
- fn gen_init ( & mut self , module_name : & str , needs_manual_start : bool ) -> String {
850
+ fn ts_for_init_fn ( has_memory : bool ) -> & ' static str {
851
+ macro_rules! initial_ts {
852
+ ( ) => ( "
853
+ \n \
854
+ /**\n \
855
+ * If `module_or_path` is {RequestInfo}, makes a request and\n \
856
+ * for everything else, calls `WebAssembly.instantiate` directly.\n \
857
+ *\n \
858
+ * @param {RequestInfo | BufferSource | WebAssembly.Module} module_or_path\n \
859
+ ") ;
860
+ }
861
+ if has_memory {
862
+ concat ! ( initial_ts!( ) , "\
863
+ * @param {WebAssembly.Memory} maybe_memory\n \
864
+ *\n \
865
+ * @returns {Promise<WebAssembly.ResultObject | WebAssembly.Instance>}\n \
866
+ */\n \
867
+ export function init (\
868
+ module_or_path: RequestInfo | BufferSource | WebAssembly.Module,\
869
+ maybe_memory: WebAssembly.Memory\
870
+ ): Promise<WebAssembly.ResultObject | WebAssembly.Instance>;
871
+ " )
872
+ } else {
873
+ concat ! (
874
+ initial_ts!( ) ,
875
+ "\
876
+ *\n \
877
+ * @returns {Promise<WebAssembly.ResultObject | WebAssembly.Instance>}\n \
878
+ */\n \
879
+ export function init (module_or_path: RequestInfo | BufferSource | WebAssembly.Module): \
880
+ Promise<WebAssembly.ResultObject | WebAssembly.Instance>;
881
+ "
882
+ )
883
+ }
884
+ }
885
+
886
+ fn gen_init ( & mut self , module_name : & str , needs_manual_start : bool ) -> ( String , String ) {
846
887
let mem = self . module . memories . get ( self . memory ) ;
847
888
let ( init_memory1, init_memory2) = if mem. import . is_some ( ) {
848
889
let mut memory = String :: from ( "new WebAssembly.Memory({" ) ;
@@ -862,10 +903,16 @@ impl<'a> Context<'a> {
862
903
} else {
863
904
( String :: new ( ) , String :: new ( ) )
864
905
} ;
906
+ let init_memory_arg = if mem. import . is_some ( ) {
907
+ ", maybe_memory"
908
+ } else {
909
+ ""
910
+ } ;
865
911
866
- format ! (
912
+ let ts = Self :: ts_for_init_fn ( mem. import . is_some ( ) ) ;
913
+ let js = format ! (
867
914
"\
868
- function init(module_or_path, maybe_memory ) {{
915
+ function init(module_or_path{init_memory_arg} ) {{
869
916
let result;
870
917
const imports = {{ './{module}': __exports }};
871
918
if (module_or_path instanceof URL || typeof module_or_path === 'string' || module_or_path instanceof Request) {{
@@ -903,6 +950,7 @@ impl<'a> Context<'a> {
903
950
}});
904
951
}}
905
952
" ,
953
+ init_memory_arg = init_memory_arg,
906
954
module = module_name,
907
955
init_memory1 = init_memory1,
908
956
init_memory2 = init_memory2,
@@ -911,7 +959,9 @@ impl<'a> Context<'a> {
911
959
} else {
912
960
""
913
961
} ,
914
- )
962
+ ) ;
963
+
964
+ ( js, ts. into ( ) )
915
965
}
916
966
917
967
fn bind (
0 commit comments