Skip to content

Commit

Permalink
Add TS type for init fn
Browse files Browse the repository at this point in the history
  • Loading branch information
c410-f3r committed Mar 31, 2019
1 parent c5f18b6 commit 4c10485
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
17 changes: 15 additions & 2 deletions crates/cli-support/src/js/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -327,6 +328,7 @@ impl<'a> Context<'a> {
js.push_str("const __exports = {};\n");
js.push_str("let wasm;\n");
init = self.gen_init(&module_name, needs_manual_start);
ts.push_str(Self::ts_for_init_fn());
self.footer.push_str(&format!(
"self.{} = Object.assign(init, __exports);\n",
global
Expand Down Expand Up @@ -367,6 +369,7 @@ impl<'a> Context<'a> {
js.push_str("const __exports = {};\n");
self.imports_post.push_str("let wasm;\n");
init = self.gen_init(&module_name, needs_manual_start);
ts.push_str(Self::ts_for_init_fn());
self.footer.push_str("export default init;\n");
}
}
Expand Down Expand Up @@ -394,7 +397,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> {
Expand Down Expand Up @@ -842,6 +845,16 @@ impl<'a> Context<'a> {
Ok(())
}

fn ts_for_init_fn() -> &'static str {
"\n\
/**\n\
* @param {string} module_or_path\n\
* @returns {Promise<any>}\n\
*/\n\
export function init(module_or_path: string): Promise<any>;
"
}

fn gen_init(&mut self, module_name: &str, needs_manual_start: bool) -> String {
let mem = self.module.memories.get(self.memory);
let (init_memory1, init_memory2) = if mem.import.is_some() {
Expand All @@ -865,7 +878,7 @@ impl<'a> Context<'a> {

format!(
"\
function init(module_or_path, maybe_memory) {{
function init(module_or_path) {{
let result;
const imports = {{ './{module}': __exports }};
if (module_or_path instanceof URL || typeof module_or_path === 'string' || module_or_path instanceof Request) {{
Expand Down
7 changes: 7 additions & 0 deletions crates/typescript-tests/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions crates/typescript-tests/src/web/init.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import * as wbg from '../../pkg/web/typescript_tests';

const height: Promise<any> = wbg.init('.');
2 changes: 1 addition & 1 deletion crates/typescript-tests/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
"baseUrl": "."
},
"include": [
"src/*.ts"
"src/**/*.ts"
]
}

0 comments on commit 4c10485

Please sign in to comment.