-
Notifications
You must be signed in to change notification settings - Fork 42
/
wasm.rs
230 lines (216 loc) · 6.56 KB
/
wasm.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// Copyright 2018-2024 the Deno authors. MIT license.
use std::borrow::Cow;
use std::collections::HashSet;
use wasm_dep_analyzer::ValueType;
pub fn wasm_module_to_dts(
bytes: &[u8],
) -> Result<String, wasm_dep_analyzer::ParseError> {
let wasm_deps = wasm_dep_analyzer::WasmDeps::parse(
bytes,
wasm_dep_analyzer::ParseOptions { skip_types: false },
)?;
Ok(wasm_module_deps_to_dts(&wasm_deps))
}
fn wasm_module_deps_to_dts(wasm_deps: &wasm_dep_analyzer::WasmDeps) -> String {
#[derive(PartialEq, Eq)]
enum TypePosition {
Input,
Output,
}
fn value_type_to_ts_type(
value_type: ValueType,
position: TypePosition,
) -> &'static str {
match value_type {
ValueType::I32 => "number",
ValueType::I64 if position == TypePosition::Input => "bigint | number",
ValueType::I64 => "bigint",
ValueType::F32 => "number",
ValueType::F64 => "number",
ValueType::Unknown => "unknown",
}
}
fn is_valid_ident(export_name: &str) -> bool {
!export_name.is_empty()
&& deno_ast::swc::ast::Ident::verify_symbol(export_name).is_ok()
}
let mut text = String::new();
let mut internal_names_count = 0;
let mut seen_modules = HashSet::with_capacity(wasm_deps.imports.len());
for import in &wasm_deps.imports {
if seen_modules.insert(&import.module) {
text.push_str(&format!("import \"{}\";\n", import.module));
}
}
for export in &wasm_deps.exports {
let has_valid_export_ident = is_valid_ident(export.name);
let export_name = if has_valid_export_ident {
Cow::Borrowed(export.name)
} else {
let export_name =
format!("__deno_wasm_export_{}__", internal_names_count);
internal_names_count += 1;
Cow::Owned(export_name)
};
if has_valid_export_ident {
text.push_str("export ");
}
let mut add_var = |type_text: &str| {
text.push_str("declare const ");
text.push_str(&export_name);
text.push_str(": ");
text.push_str(type_text);
text.push_str(";\n");
};
match &export.export_type {
wasm_dep_analyzer::ExportType::Function(function_signature) => {
match function_signature {
Ok(signature) => {
text.push_str("declare function ");
text.push_str(&export_name);
text.push('(');
for (i, param) in signature.params.iter().enumerate() {
if i > 0 {
text.push_str(", ");
}
text.push_str("arg");
text.push_str(i.to_string().as_str());
text.push_str(": ");
text.push_str(value_type_to_ts_type(*param, TypePosition::Input));
}
text.push_str("): ");
text.push_str(
signature
.returns
.first()
.map(|t| value_type_to_ts_type(*t, TypePosition::Output))
.unwrap_or("void"),
);
text.push_str(";\n");
}
Err(_) => add_var("unknown"),
}
}
wasm_dep_analyzer::ExportType::Table => add_var("WebAssembly.Table"),
wasm_dep_analyzer::ExportType::Memory => add_var("WebAssembly.Memory"),
wasm_dep_analyzer::ExportType::Global(global_type) => match global_type {
Ok(global_type) => add_var(value_type_to_ts_type(
global_type.value_type,
TypePosition::Output,
)),
Err(_) => add_var("unknown"),
},
wasm_dep_analyzer::ExportType::Tag
| wasm_dep_analyzer::ExportType::Unknown => add_var("unknown"),
}
if !has_valid_export_ident {
text.push_str(&format!(
"export {{ {} as \"{}\" }};\n",
export_name, export.name
));
}
}
text
}
#[cfg(test)]
mod test {
use pretty_assertions::assert_eq;
use wasm_dep_analyzer::Export;
use wasm_dep_analyzer::FunctionSignature;
use wasm_dep_analyzer::WasmDeps;
use super::*;
#[test]
fn test_output() {
let text = wasm_module_deps_to_dts(&WasmDeps {
imports: vec![],
exports: vec![
Export {
name: "name--1",
index: 0,
export_type: wasm_dep_analyzer::ExportType::Function(Ok(
FunctionSignature {
params: vec![],
returns: vec![],
},
)),
},
Export {
name: "name2",
index: 1,
export_type: wasm_dep_analyzer::ExportType::Function(Ok(
FunctionSignature {
params: vec![ValueType::I32, ValueType::I64],
returns: vec![ValueType::I64],
},
)),
},
Export {
name: "name3",
index: 2,
export_type: wasm_dep_analyzer::ExportType::Function(Err(
wasm_dep_analyzer::ParseError::IntegerOverflow,
)),
},
Export {
name: "name4",
index: 3,
export_type: wasm_dep_analyzer::ExportType::Table,
},
Export {
name: "name5",
index: 4,
export_type: wasm_dep_analyzer::ExportType::Memory,
},
Export {
name: "name6",
index: 5,
export_type: wasm_dep_analyzer::ExportType::Global(Ok(
wasm_dep_analyzer::GlobalType {
value_type: ValueType::I32,
mutability: false,
},
)),
},
Export {
name: "name7",
index: 6,
export_type: wasm_dep_analyzer::ExportType::Global(Err(
wasm_dep_analyzer::ParseError::NotWasm,
)),
},
Export {
name: "name8",
index: 7,
export_type: wasm_dep_analyzer::ExportType::Unknown,
},
Export {
name: "name9--",
index: 8,
export_type: wasm_dep_analyzer::ExportType::Unknown,
},
Export {
name: "default",
index: 9,
export_type: wasm_dep_analyzer::ExportType::Unknown,
},
],
});
assert_eq!(
text,
"declare function __deno_wasm_export_0__(): void;
export { __deno_wasm_export_0__ as \"name--1\" };
export declare function name2(arg0: number, arg1: bigint | number): bigint;
export declare const name3: unknown;
export declare const name4: WebAssembly.Table;
export declare const name5: WebAssembly.Memory;
export declare const name6: number;
export declare const name7: unknown;
export declare const name8: unknown;
declare const __deno_wasm_export_1__: unknown;
export { __deno_wasm_export_1__ as \"name9--\" };
declare const __deno_wasm_export_2__: unknown;
export { __deno_wasm_export_2__ as \"default\" };
"
);
}
}