mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-04-02 02:11:06 +00:00
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:
parent
4a70198143
commit
047c41c1ec
@ -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(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -48,82 +48,77 @@ impl Config {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn typescript(module: &Module) -> String {
|
||||||
|
let mut exports = format!("/* tslint:disable */\n");
|
||||||
|
|
||||||
|
if let Some(i) = module.export_section() {
|
||||||
|
let imported_functions = module
|
||||||
|
.import_section()
|
||||||
|
.map(|m| m.functions() as u32)
|
||||||
|
.unwrap_or(0);
|
||||||
|
for entry in i.entries() {
|
||||||
|
let idx = match *entry.internal() {
|
||||||
|
Internal::Function(i) => i - imported_functions,
|
||||||
|
Internal::Memory(_) => {
|
||||||
|
exports.push_str(&format!(
|
||||||
|
"export const {}: WebAssembly.Memory;\n",
|
||||||
|
entry.field()
|
||||||
|
));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
Internal::Table(_) => {
|
||||||
|
exports.push_str(&format!(
|
||||||
|
"export const {}: WebAssembly.Table;\n",
|
||||||
|
entry.field()
|
||||||
|
));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
Internal::Global(_) => continue,
|
||||||
|
};
|
||||||
|
|
||||||
|
let functions = module
|
||||||
|
.function_section()
|
||||||
|
.expect("failed to find function section");
|
||||||
|
let idx = functions.entries()[idx as usize].type_ref();
|
||||||
|
|
||||||
|
let types = module
|
||||||
|
.type_section()
|
||||||
|
.expect("failed to find type section");
|
||||||
|
let ty = match types.types()[idx as usize] {
|
||||||
|
Type::Function(ref f) => f,
|
||||||
|
};
|
||||||
|
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!(
|
||||||
|
"export function {name}({args}): {ret};\n",
|
||||||
|
name = entry.field(),
|
||||||
|
args = args,
|
||||||
|
ret = if ty.return_type().is_some() {
|
||||||
|
"number"
|
||||||
|
} else {
|
||||||
|
"void"
|
||||||
|
},
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return exports;
|
||||||
|
}
|
||||||
|
|
||||||
impl Output {
|
impl Output {
|
||||||
pub fn typescript(&self) -> String {
|
pub fn typescript(&self) -> String {
|
||||||
let mut exports = format!("/* tslint:disable */\n");
|
let mut ts = typescript(&self.module);
|
||||||
|
|
||||||
if let Some(i) = self.module.export_section() {
|
|
||||||
let imported_functions = self
|
|
||||||
.module
|
|
||||||
.import_section()
|
|
||||||
.map(|m| m.functions() as u32)
|
|
||||||
.unwrap_or(0);
|
|
||||||
for entry in i.entries() {
|
|
||||||
let idx = match *entry.internal() {
|
|
||||||
Internal::Function(i) => i - imported_functions,
|
|
||||||
Internal::Memory(_) => {
|
|
||||||
exports.push_str(&format!(
|
|
||||||
"
|
|
||||||
export const {}: WebAssembly.Memory;
|
|
||||||
",
|
|
||||||
entry.field()
|
|
||||||
));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
Internal::Table(_) => {
|
|
||||||
exports.push_str(&format!(
|
|
||||||
"
|
|
||||||
export const {}: WebAssembly.Table;
|
|
||||||
",
|
|
||||||
entry.field()
|
|
||||||
));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
Internal::Global(_) => continue,
|
|
||||||
};
|
|
||||||
|
|
||||||
let functions = self
|
|
||||||
.module
|
|
||||||
.function_section()
|
|
||||||
.expect("failed to find function section");
|
|
||||||
let idx = functions.entries()[idx as usize].type_ref();
|
|
||||||
|
|
||||||
let types = self
|
|
||||||
.module
|
|
||||||
.type_section()
|
|
||||||
.expect("failed to find type section");
|
|
||||||
let ty = match types.types()[idx as usize] {
|
|
||||||
Type::Function(ref f) => f,
|
|
||||||
};
|
|
||||||
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!(
|
|
||||||
"
|
|
||||||
export function {name}({args}): {ret};
|
|
||||||
",
|
|
||||||
name = entry.field(),
|
|
||||||
args = args,
|
|
||||||
ret = if ty.return_type().is_some() {
|
|
||||||
"number"
|
|
||||||
} else {
|
|
||||||
"void"
|
|
||||||
},
|
|
||||||
));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if self.base64 {
|
if self.base64 {
|
||||||
exports.push_str("export const booted: Promise<boolean>;");
|
ts.push_str("export const booted: Promise<boolean>;\n");
|
||||||
}
|
}
|
||||||
|
return ts
|
||||||
return exports;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn js(self) -> Result<String, Error> {
|
pub fn js(self) -> Result<String, Error> {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user