Skip to content

Commit 7a7b412

Browse files
authored
Merge pull request #2029 from Pauan/fixing-typescript-emit
Fixing the TypeScript type for the init function
2 parents 57f8ed2 + b99ab10 commit 7a7b412

File tree

2 files changed

+64
-9
lines changed

2 files changed

+64
-9
lines changed

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

+21-9
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,13 @@ impl<'a> Context<'a> {
461461
Ok(imports)
462462
}
463463

464-
fn ts_for_init_fn(has_memory: bool, has_module_or_path_optional: bool) -> String {
464+
fn ts_for_init_fn(
465+
&self,
466+
has_memory: bool,
467+
has_module_or_path_optional: bool,
468+
) -> Result<String, Error> {
469+
let output = crate::wasm2es6js::interface(&self.module)?;
470+
465471
let (memory_doc, memory_param) = if has_memory {
466472
(
467473
"* @param {WebAssembly.Memory} maybe_memory\n",
@@ -471,22 +477,28 @@ impl<'a> Context<'a> {
471477
("", "")
472478
};
473479
let arg_optional = if has_module_or_path_optional { "?" } else { "" };
474-
format!(
480+
Ok(format!(
475481
"\n\
482+
export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;\n\
483+
\n\
484+
export interface InitOutput {{\n\
485+
{output}}}\n\
486+
\n\
476487
/**\n\
477-
* If `module_or_path` is {{RequestInfo}}, makes a request and\n\
488+
* If `module_or_path` is {{RequestInfo}} or {{URL}}, makes a request and\n\
478489
* for everything else, calls `WebAssembly.instantiate` directly.\n\
479490
*\n\
480-
* @param {{RequestInfo | BufferSource | WebAssembly.Module}} module_or_path\n\
491+
* @param {{InitInput | Promise<InitInput>}} module_or_path\n\
481492
{}\
482493
*\n\
483-
* @returns {{Promise<any>}}\n\
494+
* @returns {{Promise<InitOutput>}}\n\
484495
*/\n\
485496
export default function init \
486-
(module_or_path{}: RequestInfo | BufferSource | WebAssembly.Module{}): Promise<any>;
497+
(module_or_path{}: InitInput | Promise<InitInput>{}): Promise<InitOutput>;
487498
",
488-
memory_doc, arg_optional, memory_param
489-
)
499+
memory_doc, arg_optional, memory_param,
500+
output = output,
501+
))
490502
}
491503

492504
fn gen_init(
@@ -541,7 +553,7 @@ impl<'a> Context<'a> {
541553
_ => "",
542554
};
543555

544-
let ts = Self::ts_for_init_fn(has_memory, !default_module_path.is_empty());
556+
let ts = self.ts_for_init_fn(has_memory, !default_module_path.is_empty())?;
545557

546558
// Initialize the `imports` object for all import definitions that we're
547559
// directed to wire up.

crates/cli-support/src/wasm2es6js.rs

+43
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,49 @@ impl Config {
4444
}
4545
}
4646

47+
pub fn interface(module: &Module) -> Result<String, Error> {
48+
let mut exports = String::new();
49+
50+
for entry in module.exports.iter() {
51+
let id = match entry.item {
52+
walrus::ExportItem::Function(i) => i,
53+
walrus::ExportItem::Memory(_) => {
54+
exports.push_str(&format!(" readonly {}: WebAssembly.Memory;\n", entry.name,));
55+
continue;
56+
}
57+
walrus::ExportItem::Table(_) => {
58+
exports.push_str(&format!(" readonly {}: WebAssembly.Table;\n", entry.name,));
59+
continue;
60+
}
61+
walrus::ExportItem::Global(_) => continue,
62+
};
63+
64+
let func = module.funcs.get(id);
65+
let ty = module.types.get(func.ty());
66+
let mut args = String::new();
67+
for (i, _) in ty.params().iter().enumerate() {
68+
if i > 0 {
69+
args.push_str(", ");
70+
}
71+
args.push((b'a' + (i as u8)) as char);
72+
args.push_str(": number");
73+
}
74+
75+
exports.push_str(&format!(
76+
" readonly {name}: ({args}) => {ret};\n",
77+
name = entry.name,
78+
args = args,
79+
ret = match ty.results().len() {
80+
0 => "void",
81+
1 => "number",
82+
_ => "Array",
83+
},
84+
));
85+
}
86+
87+
Ok(exports)
88+
}
89+
4790
pub fn typescript(module: &Module) -> Result<String, Error> {
4891
let mut exports = format!("/* tslint:disable */\n/* eslint-disable */\n");
4992

0 commit comments

Comments
 (0)