Skip to content

Commit e3b6286

Browse files
authored
Merge pull request #1053 from alexcrichton/dts-wasm
Generate a `*.d.ts` file for wasm files
2 parents d54340e + 047c41c commit e3b6286

File tree

2 files changed

+74
-71
lines changed

2 files changed

+74
-71
lines changed

crates/cli-support/src/lib.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ impl Bindgen {
217217
.with_context(|_| format!("failed to write `{}`", js_path.display()))?;
218218

219219
if self.typescript {
220-
let ts_path = out_dir.join(stem).with_extension("d.ts");
220+
let ts_path = js_path.with_extension("d.ts");
221221
fs::write(&ts_path, ts)
222222
.with_context(|_| format!("failed to write `{}`", ts_path.display()))?;
223223
}
@@ -231,9 +231,17 @@ impl Bindgen {
231231
.with_context(|_| format!("failed to write `{}`", js_path.display()))?;
232232
}
233233

234+
if self.typescript {
235+
let ts_path = wasm_path.with_extension("d.ts");
236+
let ts = wasm2es6js::typescript(&module);
237+
fs::write(&ts_path, ts)
238+
.with_context(|_| format!("failed to write `{}`", ts_path.display()))?;
239+
}
240+
234241
let wasm_bytes = parity_wasm::serialize(module)?;
235242
fs::write(&wasm_path, wasm_bytes)
236243
.with_context(|_| format!("failed to write `{}`", wasm_path.display()))?;
244+
237245
Ok(())
238246
}
239247

crates/cli-support/src/wasm2es6js.rs

Lines changed: 65 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -48,82 +48,77 @@ impl Config {
4848
}
4949
}
5050

51-
impl Output {
52-
pub fn typescript(&self) -> String {
53-
let mut exports = format!("/* tslint:disable */\n");
54-
55-
if let Some(i) = self.module.export_section() {
56-
let imported_functions = self
57-
.module
58-
.import_section()
59-
.map(|m| m.functions() as u32)
60-
.unwrap_or(0);
61-
for entry in i.entries() {
62-
let idx = match *entry.internal() {
63-
Internal::Function(i) => i - imported_functions,
64-
Internal::Memory(_) => {
65-
exports.push_str(&format!(
66-
"
67-
export const {}: WebAssembly.Memory;
68-
",
69-
entry.field()
70-
));
71-
continue;
72-
}
73-
Internal::Table(_) => {
74-
exports.push_str(&format!(
75-
"
76-
export const {}: WebAssembly.Table;
77-
",
78-
entry.field()
79-
));
80-
continue;
81-
}
82-
Internal::Global(_) => continue,
83-
};
84-
85-
let functions = self
86-
.module
87-
.function_section()
88-
.expect("failed to find function section");
89-
let idx = functions.entries()[idx as usize].type_ref();
90-
91-
let types = self
92-
.module
93-
.type_section()
94-
.expect("failed to find type section");
95-
let ty = match types.types()[idx as usize] {
96-
Type::Function(ref f) => f,
97-
};
98-
let mut args = String::new();
99-
for (i, _) in ty.params().iter().enumerate() {
100-
if i > 0 {
101-
args.push_str(", ");
102-
}
103-
args.push((b'a' + (i as u8)) as char);
104-
args.push_str(": number");
51+
pub fn typescript(module: &Module) -> String {
52+
let mut exports = format!("/* tslint:disable */\n");
53+
54+
if let Some(i) = module.export_section() {
55+
let imported_functions = module
56+
.import_section()
57+
.map(|m| m.functions() as u32)
58+
.unwrap_or(0);
59+
for entry in i.entries() {
60+
let idx = match *entry.internal() {
61+
Internal::Function(i) => i - imported_functions,
62+
Internal::Memory(_) => {
63+
exports.push_str(&format!(
64+
"export const {}: WebAssembly.Memory;\n",
65+
entry.field()
66+
));
67+
continue;
10568
}
106-
107-
exports.push_str(&format!(
108-
"
109-
export function {name}({args}): {ret};
110-
",
111-
name = entry.field(),
112-
args = args,
113-
ret = if ty.return_type().is_some() {
114-
"number"
115-
} else {
116-
"void"
117-
},
118-
));
69+
Internal::Table(_) => {
70+
exports.push_str(&format!(
71+
"export const {}: WebAssembly.Table;\n",
72+
entry.field()
73+
));
74+
continue;
75+
}
76+
Internal::Global(_) => continue,
77+
};
78+
79+
let functions = module
80+
.function_section()
81+
.expect("failed to find function section");
82+
let idx = functions.entries()[idx as usize].type_ref();
83+
84+
let types = module
85+
.type_section()
86+
.expect("failed to find type section");
87+
let ty = match types.types()[idx as usize] {
88+
Type::Function(ref f) => f,
89+
};
90+
let mut args = String::new();
91+
for (i, _) in ty.params().iter().enumerate() {
92+
if i > 0 {
93+
args.push_str(", ");
94+
}
95+
args.push((b'a' + (i as u8)) as char);
96+
args.push_str(": number");
11997
}
98+
99+
exports.push_str(&format!(
100+
"export function {name}({args}): {ret};\n",
101+
name = entry.field(),
102+
args = args,
103+
ret = if ty.return_type().is_some() {
104+
"number"
105+
} else {
106+
"void"
107+
},
108+
));
120109
}
110+
}
121111

112+
return exports;
113+
}
114+
115+
impl Output {
116+
pub fn typescript(&self) -> String {
117+
let mut ts = typescript(&self.module);
122118
if self.base64 {
123-
exports.push_str("export const booted: Promise<boolean>;");
119+
ts.push_str("export const booted: Promise<boolean>;\n");
124120
}
125-
126-
return exports;
121+
return ts
127122
}
128123

129124
pub fn js(self) -> Result<String, Error> {

0 commit comments

Comments
 (0)