Skip to content

Fixing the TypeScript type for the init function #2029

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions crates/cli-support/src/js/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,13 @@ impl<'a> Context<'a> {
Ok(imports)
}

fn ts_for_init_fn(has_memory: bool, has_module_or_path_optional: bool) -> String {
fn ts_for_init_fn(
&self,
has_memory: bool,
has_module_or_path_optional: bool,
) -> Result<String, Error> {
let output = crate::wasm2es6js::interface(&self.module)?;

let (memory_doc, memory_param) = if has_memory {
(
"* @param {WebAssembly.Memory} maybe_memory\n",
Expand All @@ -471,22 +477,28 @@ impl<'a> Context<'a> {
("", "")
};
let arg_optional = if has_module_or_path_optional { "?" } else { "" };
format!(
Ok(format!(
"\n\
export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;\n\
\n\
export interface InitOutput {{\n\
{output}}}\n\
\n\
/**\n\
* If `module_or_path` is {{RequestInfo}}, makes a request and\n\
* If `module_or_path` is {{RequestInfo}} or {{URL}}, makes a request and\n\
* for everything else, calls `WebAssembly.instantiate` directly.\n\
*\n\
* @param {{RequestInfo | BufferSource | WebAssembly.Module}} module_or_path\n\
* @param {{InitInput | Promise<InitInput>}} module_or_path\n\
{}\
*\n\
* @returns {{Promise<any>}}\n\
* @returns {{Promise<InitOutput>}}\n\
*/\n\
export default function init \
(module_or_path{}: RequestInfo | BufferSource | WebAssembly.Module{}): Promise<any>;
(module_or_path{}: InitInput | Promise<InitInput>{}): Promise<InitOutput>;
",
memory_doc, arg_optional, memory_param
)
memory_doc, arg_optional, memory_param,
output = output,
))
}

fn gen_init(
Expand Down Expand Up @@ -541,7 +553,7 @@ impl<'a> Context<'a> {
_ => "",
};

let ts = Self::ts_for_init_fn(has_memory, !default_module_path.is_empty());
let ts = self.ts_for_init_fn(has_memory, !default_module_path.is_empty())?;

// Initialize the `imports` object for all import definitions that we're
// directed to wire up.
Expand Down
43 changes: 43 additions & 0 deletions crates/cli-support/src/wasm2es6js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,49 @@ impl Config {
}
}

pub fn interface(module: &Module) -> Result<String, Error> {
let mut exports = String::new();

for entry in module.exports.iter() {
let id = match entry.item {
walrus::ExportItem::Function(i) => i,
walrus::ExportItem::Memory(_) => {
exports.push_str(&format!(" readonly {}: WebAssembly.Memory;\n", entry.name,));
continue;
}
walrus::ExportItem::Table(_) => {
exports.push_str(&format!(" readonly {}: WebAssembly.Table;\n", entry.name,));
continue;
}
walrus::ExportItem::Global(_) => continue,
};

let func = module.funcs.get(id);
let ty = module.types.get(func.ty());
let mut args = String::new();
for (i, _) in ty.params().iter().enumerate() {
if i > 0 {
args.push_str(", ");
}
args.push((b'a' + (i as u8)) as char);
args.push_str(": number");
}

exports.push_str(&format!(
" readonly {name}: ({args}) => {ret};\n",
name = entry.name,
args = args,
ret = match ty.results().len() {
0 => "void",
1 => "number",
_ => "Array",
},
));
}

Ok(exports)
}

pub fn typescript(module: &Module) -> Result<String, Error> {
let mut exports = format!("/* tslint:disable */\n/* eslint-disable */\n");

Expand Down