Skip to content

Commit fa674df

Browse files
authored
Merge pull request #1412 from c410-f3r/init-ts
Add TS type for init fn
2 parents a1c457b + b6317e3 commit fa674df

File tree

4 files changed

+59
-8
lines changed

4 files changed

+59
-8
lines changed

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

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -311,14 +311,15 @@ impl<'a> Context<'a> {
311311
/// `--target no-modules`, `--target web`, or for bundlers. This is the very
312312
/// last step performed in `finalize`.
313313
fn finalize_js(&mut self, module_name: &str, needs_manual_start: bool) -> (String, String) {
314+
let mut ts = self.typescript.clone();
314315
let mut js = String::new();
315316
if self.config.mode.no_modules() {
316317
js.push_str("(function() {\n");
317318
}
318319

319320
// Depending on the output mode, generate necessary glue to actually
320321
// import the wasm file in one way or another.
321-
let mut init = String::new();
322+
let mut init = (String::new(), String::new());
322323
match &self.config.mode {
323324
// In `--target no-modules` mode we need to both expose a name on
324325
// the global object as well as generate our own custom start
@@ -371,6 +372,10 @@ impl<'a> Context<'a> {
371372
}
372373
}
373374

375+
let (init_js, init_ts) = init;
376+
377+
ts.push_str(&init_ts);
378+
374379
// Emit all the JS for importing all our functionality
375380
js.push_str(&self.imports);
376381
js.push_str("\n");
@@ -382,7 +387,7 @@ impl<'a> Context<'a> {
382387
js.push_str("\n");
383388

384389
// Generate the initialization glue, if there was any
385-
js.push_str(&init);
390+
js.push_str(&init_js);
386391
js.push_str("\n");
387392
js.push_str(&self.footer);
388393
js.push_str("\n");
@@ -394,7 +399,7 @@ impl<'a> Context<'a> {
394399
js = js.replace("\n\n\n", "\n\n");
395400
}
396401

397-
(js, self.typescript.clone())
402+
(js, ts)
398403
}
399404

400405
fn wire_up_initial_intrinsics(&mut self) -> Result<(), Error> {
@@ -842,7 +847,34 @@ impl<'a> Context<'a> {
842847
Ok(())
843848
}
844849

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) {
846878
let mem = self.module.memories.get(self.memory);
847879
let (init_memory1, init_memory2) = if mem.import.is_some() {
848880
let mut memory = String::from("new WebAssembly.Memory({");
@@ -862,10 +894,16 @@ impl<'a> Context<'a> {
862894
} else {
863895
(String::new(), String::new())
864896
};
897+
let init_memory_arg = if mem.import.is_some() {
898+
", maybe_memory"
899+
} else {
900+
""
901+
};
865902

866-
format!(
903+
let ts = Self::ts_for_init_fn(mem.import.is_some());
904+
let js = format!(
867905
"\
868-
function init(module_or_path, maybe_memory) {{
906+
function init(module_or_path{init_memory_arg}) {{
869907
let result;
870908
const imports = {{ './{module}': __exports }};
871909
if (module_or_path instanceof URL || typeof module_or_path === 'string' || module_or_path instanceof Request) {{
@@ -903,6 +941,7 @@ impl<'a> Context<'a> {
903941
}});
904942
}}
905943
",
944+
init_memory_arg = init_memory_arg,
906945
module = module_name,
907946
init_memory1 = init_memory1,
908947
init_memory2 = init_memory2,
@@ -911,7 +950,9 @@ impl<'a> Context<'a> {
911950
} else {
912951
""
913952
},
914-
)
953+
);
954+
955+
(js, ts)
915956
}
916957

917958
fn bind(

crates/typescript-tests/run.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ cargo run -p wasm-bindgen-cli --bin wasm-bindgen -- \
1111
--out-dir pkg \
1212
--typescript
1313

14+
mkdir pkg/web
15+
cargo run -p wasm-bindgen-cli --bin wasm-bindgen -- \
16+
../../target/wasm32-unknown-unknown/debug/typescript_tests.wasm \
17+
--out-dir pkg/web \
18+
--target web \
19+
--typescript
20+
1421
if [ ! -d node_modules ]; then
1522
npm install
1623
fi
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import * as wbg from '../../pkg/web/typescript_tests';
2+
3+
const init: Promise<any> = wbg.init('.');

crates/typescript-tests/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
"baseUrl": "."
1010
},
1111
"include": [
12-
"src/*.ts"
12+
"src/**/*.ts"
1313
]
1414
}

0 commit comments

Comments
 (0)