@@ -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,34 @@ 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 ) -> String {
851
+ let ( memory_doc, memory_param) = if has_memory {
852
+ (
853
+ "* @param {WebAssembly.Memory} maybe_memory\n " ,
854
+ ", maybe_memory: WebAssembly.Memory" ,
855
+ )
856
+ } else {
857
+ ( "" , "" )
858
+ } ;
859
+ format ! (
860
+ "\n \
861
+ /**\n \
862
+ * If `module_or_path` is {{RequestInfo}}, makes a request and\n \
863
+ * for everything else, calls `WebAssembly.instantiate` directly.\n \
864
+ *\n \
865
+ * @param {{RequestInfo | BufferSource | WebAssembly.Module}} module_or_path\n \
866
+ {}\
867
+ *\n \
868
+ * @returns {{Promise<any>}}\n \
869
+ */\n \
870
+ export function init \
871
+ (module_or_path: RequestInfo | BufferSource | WebAssembly.Module{}): Promise<any>;
872
+ " ,
873
+ memory_doc, memory_param
874
+ )
875
+ }
876
+
877
+ fn gen_init ( & mut self , module_name : & str , needs_manual_start : bool ) -> ( String , String ) {
846
878
let mem = self . module . memories . get ( self . memory ) ;
847
879
let ( init_memory1, init_memory2) = if mem. import . is_some ( ) {
848
880
let mut memory = String :: from ( "new WebAssembly.Memory({" ) ;
@@ -862,10 +894,16 @@ impl<'a> Context<'a> {
862
894
} else {
863
895
( String :: new ( ) , String :: new ( ) )
864
896
} ;
897
+ let init_memory_arg = if mem. import . is_some ( ) {
898
+ ", maybe_memory"
899
+ } else {
900
+ ""
901
+ } ;
865
902
866
- format ! (
903
+ let ts = Self :: ts_for_init_fn ( mem. import . is_some ( ) ) ;
904
+ let js = format ! (
867
905
"\
868
- function init(module_or_path, maybe_memory ) {{
906
+ function init(module_or_path{init_memory_arg} ) {{
869
907
let result;
870
908
const imports = {{ './{module}': __exports }};
871
909
if (module_or_path instanceof URL || typeof module_or_path === 'string' || module_or_path instanceof Request) {{
@@ -903,6 +941,7 @@ impl<'a> Context<'a> {
903
941
}});
904
942
}}
905
943
" ,
944
+ init_memory_arg = init_memory_arg,
906
945
module = module_name,
907
946
init_memory1 = init_memory1,
908
947
init_memory2 = init_memory2,
@@ -911,7 +950,9 @@ impl<'a> Context<'a> {
911
950
} else {
912
951
""
913
952
} ,
914
- )
953
+ ) ;
954
+
955
+ ( js, ts)
915
956
}
916
957
917
958
fn bind (
0 commit comments