Generate a *.d.ts file for wasm files

This generates a `*.d.ts` file for the wasm file that wasm-bindgen emits
whenever typescript is enable *in addition* to the `*.d.ts` file that
already exists for the JS shim.

Closes #1040
This commit is contained in:
Alex Crichton 2018-11-27 12:36:09 -08:00
parent 4a70198143
commit 047c41c1ec
2 changed files with 76 additions and 73 deletions

View File

@ -216,7 +216,7 @@ impl Bindgen {
.with_context(|_| format!("failed to write `{}`", js_path.display()))?; .with_context(|_| format!("failed to write `{}`", js_path.display()))?;
if self.typescript { if self.typescript {
let ts_path = out_dir.join(stem).with_extension("d.ts"); let ts_path = js_path.with_extension("d.ts");
fs::write(&ts_path, ts) fs::write(&ts_path, ts)
.with_context(|_| format!("failed to write `{}`", ts_path.display()))?; .with_context(|_| format!("failed to write `{}`", ts_path.display()))?;
} }
@ -230,9 +230,17 @@ impl Bindgen {
.with_context(|_| format!("failed to write `{}`", js_path.display()))?; .with_context(|_| format!("failed to write `{}`", js_path.display()))?;
} }
if self.typescript {
let ts_path = wasm_path.with_extension("d.ts");
let ts = wasm2es6js::typescript(&module);
fs::write(&ts_path, ts)
.with_context(|_| format!("failed to write `{}`", ts_path.display()))?;
}
let wasm_bytes = parity_wasm::serialize(module)?; let wasm_bytes = parity_wasm::serialize(module)?;
fs::write(&wasm_path, wasm_bytes) fs::write(&wasm_path, wasm_bytes)
.with_context(|_| format!("failed to write `{}`", wasm_path.display()))?; .with_context(|_| format!("failed to write `{}`", wasm_path.display()))?;
Ok(()) Ok(())
} }

View File

@ -48,13 +48,11 @@ impl Config {
} }
} }
impl Output { pub fn typescript(module: &Module) -> String {
pub fn typescript(&self) -> String {
let mut exports = format!("/* tslint:disable */\n"); let mut exports = format!("/* tslint:disable */\n");
if let Some(i) = self.module.export_section() { if let Some(i) = module.export_section() {
let imported_functions = self let imported_functions = module
.module
.import_section() .import_section()
.map(|m| m.functions() as u32) .map(|m| m.functions() as u32)
.unwrap_or(0); .unwrap_or(0);
@ -63,18 +61,14 @@ impl Output {
Internal::Function(i) => i - imported_functions, Internal::Function(i) => i - imported_functions,
Internal::Memory(_) => { Internal::Memory(_) => {
exports.push_str(&format!( exports.push_str(&format!(
" "export const {}: WebAssembly.Memory;\n",
export const {}: WebAssembly.Memory;
",
entry.field() entry.field()
)); ));
continue; continue;
} }
Internal::Table(_) => { Internal::Table(_) => {
exports.push_str(&format!( exports.push_str(&format!(
" "export const {}: WebAssembly.Table;\n",
export const {}: WebAssembly.Table;
",
entry.field() entry.field()
)); ));
continue; continue;
@ -82,14 +76,12 @@ impl Output {
Internal::Global(_) => continue, Internal::Global(_) => continue,
}; };
let functions = self let functions = module
.module
.function_section() .function_section()
.expect("failed to find function section"); .expect("failed to find function section");
let idx = functions.entries()[idx as usize].type_ref(); let idx = functions.entries()[idx as usize].type_ref();
let types = self let types = module
.module
.type_section() .type_section()
.expect("failed to find type section"); .expect("failed to find type section");
let ty = match types.types()[idx as usize] { let ty = match types.types()[idx as usize] {
@ -105,9 +97,7 @@ impl Output {
} }
exports.push_str(&format!( exports.push_str(&format!(
" "export function {name}({args}): {ret};\n",
export function {name}({args}): {ret};
",
name = entry.field(), name = entry.field(),
args = args, args = args,
ret = if ty.return_type().is_some() { ret = if ty.return_type().is_some() {
@ -119,11 +109,16 @@ impl Output {
} }
} }
if self.base64 {
exports.push_str("export const booted: Promise<boolean>;");
}
return exports; return exports;
}
impl Output {
pub fn typescript(&self) -> String {
let mut ts = typescript(&self.module);
if self.base64 {
ts.push_str("export const booted: Promise<boolean>;\n");
}
return ts
} }
pub fn js(self) -> Result<String, Error> { pub fn js(self) -> Result<String, Error> {