From 95ec0949af3577ff8a75625300a5322f0bea5b7a Mon Sep 17 00:00:00 2001 From: Caio Date: Mon, 1 Apr 2019 11:59:01 -0300 Subject: [PATCH] Add TS type for init fn --- crates/cli-support/src/js/mod.rs | 60 ++++++++++++++++++++++--- crates/typescript-tests/run.sh | 7 +++ crates/typescript-tests/src/web/init.ts | 3 ++ crates/typescript-tests/tsconfig.json | 2 +- 4 files changed, 64 insertions(+), 8 deletions(-) create mode 100644 crates/typescript-tests/src/web/init.ts diff --git a/crates/cli-support/src/js/mod.rs b/crates/cli-support/src/js/mod.rs index 4a9fabcd41f1..5859139d1a31 100644 --- a/crates/cli-support/src/js/mod.rs +++ b/crates/cli-support/src/js/mod.rs @@ -311,6 +311,7 @@ impl<'a> Context<'a> { /// `--target no-modules`, `--target web`, or for bundlers. This is the very /// last step performed in `finalize`. fn finalize_js(&mut self, module_name: &str, needs_manual_start: bool) -> (String, String) { + let mut ts = self.typescript.clone(); let mut js = String::new(); if self.config.mode.no_modules() { js.push_str("(function() {\n"); @@ -318,7 +319,7 @@ impl<'a> Context<'a> { // Depending on the output mode, generate necessary glue to actually // import the wasm file in one way or another. - let mut init = String::new(); + let mut init = (String::new(), String::new()); match &self.config.mode { // In `--target no-modules` mode we need to both expose a name on // the global object as well as generate our own custom start @@ -371,6 +372,10 @@ impl<'a> Context<'a> { } } + let (init_js, init_ts) = init; + + ts.push_str(&init_ts); + // Emit all the JS for importing all our functionality js.push_str(&self.imports); js.push_str("\n"); @@ -382,7 +387,7 @@ impl<'a> Context<'a> { js.push_str("\n"); // Generate the initialization glue, if there was any - js.push_str(&init); + js.push_str(&init_js); js.push_str("\n"); js.push_str(&self.footer); js.push_str("\n"); @@ -394,7 +399,7 @@ impl<'a> Context<'a> { js = js.replace("\n\n\n", "\n\n"); } - (js, self.typescript.clone()) + (js, ts) } fn wire_up_initial_intrinsics(&mut self) -> Result<(), Error> { @@ -842,7 +847,39 @@ impl<'a> Context<'a> { Ok(()) } - fn gen_init(&mut self, module_name: &str, needs_manual_start: bool) -> String { + fn ts_for_init_fn(has_memory: bool) -> &'static str { + macro_rules! initial_ts { + () => (" + \n\ + /**\n\ + * If `module_or_path` is {RequestInfo}, makes a request and\n\ + * for everything else, calls `WebAssembly.instantiate` directly.\n\ + *\n\ + * @param {RequestInfo | any} module_or_path\n\ + "); + } + if has_memory { + concat!(initial_ts!(), "\ + * @param {any} maybe_memory\n\ + *\n\ + * @returns {Promise}\n\ + */\n\ + export function init (module_or_path: RequestInfo | any, maybe_memory: any): Promise; + ") + } else { + concat!( + initial_ts!(), + "\ + *\n\ + * @returns {Promise}\n\ + */\n\ + export function init (module_or_path: RequestInfo | any): Promise; + " + ) + } + } + + fn gen_init(&mut self, module_name: &str, needs_manual_start: bool) -> (String, String) { let mem = self.module.memories.get(self.memory); let (init_memory1, init_memory2) = if mem.import.is_some() { let mut memory = String::from("new WebAssembly.Memory({"); @@ -862,10 +899,16 @@ impl<'a> Context<'a> { } else { (String::new(), String::new()) }; + let init_memory_arg = if mem.import.is_some() { + ", maybe_memory" + } else { + "" + }; - format!( + let ts = Self::ts_for_init_fn(mem.import.is_some()); + let js = format!( "\ - function init(module_or_path, maybe_memory) {{ + function init(module_or_path{init_memory_arg}) {{ let result; const imports = {{ './{module}': __exports }}; if (module_or_path instanceof URL || typeof module_or_path === 'string' || module_or_path instanceof Request) {{ @@ -903,6 +946,7 @@ impl<'a> Context<'a> { }}); }} ", + init_memory_arg = init_memory_arg, module = module_name, init_memory1 = init_memory1, init_memory2 = init_memory2, @@ -911,7 +955,9 @@ impl<'a> Context<'a> { } else { "" }, - ) + ); + + (js, ts.into()) } fn bind( diff --git a/crates/typescript-tests/run.sh b/crates/typescript-tests/run.sh index d6b033988a10..95a868a58e61 100755 --- a/crates/typescript-tests/run.sh +++ b/crates/typescript-tests/run.sh @@ -11,6 +11,13 @@ cargo run -p wasm-bindgen-cli --bin wasm-bindgen -- \ --out-dir pkg \ --typescript +mkdir pkg/web +cargo run -p wasm-bindgen-cli --bin wasm-bindgen -- \ + ../../target/wasm32-unknown-unknown/debug/typescript_tests.wasm \ + --out-dir pkg/web \ + --target web \ + --typescript + if [ ! -d node_modules ]; then npm install fi diff --git a/crates/typescript-tests/src/web/init.ts b/crates/typescript-tests/src/web/init.ts new file mode 100644 index 000000000000..623efe72704e --- /dev/null +++ b/crates/typescript-tests/src/web/init.ts @@ -0,0 +1,3 @@ +import * as wbg from '../../pkg/web/typescript_tests'; + +const init: Promise = wbg.init('.'); \ No newline at end of file diff --git a/crates/typescript-tests/tsconfig.json b/crates/typescript-tests/tsconfig.json index 9089297990a3..3032c028f840 100644 --- a/crates/typescript-tests/tsconfig.json +++ b/crates/typescript-tests/tsconfig.json @@ -9,6 +9,6 @@ "baseUrl": "." }, "include": [ - "src/*.ts" + "src/**/*.ts" ] }