Skip to content

Commit

Permalink
Merge pull request #2029 from Pauan/fixing-typescript-emit
Browse files Browse the repository at this point in the history
Fixing the TypeScript type for the init function
  • Loading branch information
Pauan committed Mar 4, 2020
2 parents 57f8ed2 + b99ab10 commit 7a7b412
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 9 deletions.
30 changes: 21 additions & 9 deletions crates/cli-support/src/js/mod.rs
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
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

0 comments on commit 7a7b412

Please sign in to comment.