-
-
Notifications
You must be signed in to change notification settings - Fork 46
/
wasm_bindings.rs
279 lines (246 loc) · 8.69 KB
/
wasm_bindings.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
use std::path::Path;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_namespace = console)]
pub(crate) fn log(s: &str);
}
/// Wrapper the abstracts some of the properties
#[wasm_bindgen(typescript_custom_section)]
const TYPES_WASM_CHECK_OUTPUT: &str = r###"
interface WASMCheckOutput {
readonly diagnostics: DiagnosticsContainer,
get_type_at_position(path: string, pos: number): string;
get_type_at_position_debug(path: string, pos: number): string;
}
"###;
#[wasm_bindgen]
pub struct WASMCheckOutput(checker::CheckOutput<checker::synthesis::EznoParser>);
#[wasm_bindgen]
impl WASMCheckOutput {
#[wasm_bindgen(js_name = diagnostics, getter, skip_typescript)]
pub fn get_diagnostics(&self) -> JsValue {
serde_wasm_bindgen::to_value(&self.0.diagnostics).unwrap()
}
pub fn get_type_at_position(&self, path: &str, pos: u32) -> Option<String> {
self.0.get_type_at_position(path, pos, false)
}
pub fn get_type_at_position_debug(&self, path: &str, pos: u32) -> Option<String> {
self.0.get_type_at_position(path, pos, true)
}
pub fn get_module_ast(&self, path: &str) -> JsValue {
self.0.get_module(path).map_or(JsValue::NULL, |m| {
serde_wasm_bindgen::to_value(m).expect("cannot turn Module into `JsValue`")
})
}
}
#[wasm_bindgen(typescript_custom_section)]
const TYPES_CHECK_WITH_OPTIONS: &str = r#"
export function check(
entry_path: string,
fs_resolver_js: (path: string) => string | undefined,
options: TypeCheckOptions | undefined
): WASMCheckOutput"#;
#[wasm_bindgen(js_name = check, skip_typescript)]
pub fn check_wasm(
entry_path: String,
fs_resolver_js: &js_sys::Function,
options: JsValue,
) -> WASMCheckOutput {
std::panic::set_hook(Box::new(console_error_panic_hook::hook));
let options: checker::TypeCheckOptions = if options.is_undefined() || options.is_null() {
Default::default()
} else {
serde_wasm_bindgen::from_value(options).expect("invalid TypeCheckOptions")
};
let fs_resolver = |path: &std::path::Path| {
let res =
fs_resolver_js.call1(&JsValue::null(), &JsValue::from(path.display().to_string()));
res.ok().and_then(|res| res.as_string())
};
WASMCheckOutput(crate::check::check(vec![entry_path.into()], &fs_resolver, None, options))
}
/// Wrapper the abstracts some of the properties
#[wasm_bindgen(typescript_custom_section)]
const TYPES_WASM_CHECK_OUTPUT: &str = r###"
interface WASMBuildOutput {
readonly artifacts: Array<Output>,
readonly diagnostics: DiagnosticsContainer,
get_type_at_position(path: string, pos: number): string;
get_type_at_position_debug(path: string, pos: number): string;
}
"###;
#[wasm_bindgen]
pub struct WASMBuildOutput {
artifacts: Vec<crate::build::Output>,
check_output: WASMCheckOutput,
}
#[wasm_bindgen]
impl WASMBuildOutput {
#[wasm_bindgen(js_name = artifacts, getter, skip_typescript)]
pub fn get_artifacts(&self) -> JsValue {
serde_wasm_bindgen::to_value(&self.artifacts).unwrap()
}
#[wasm_bindgen(js_name = diagnostics, getter, skip_typescript)]
pub fn get_diagnostics(&self) -> JsValue {
self.check_output.get_diagnostics()
}
pub fn get_type_at_position(&self, path: &str, pos: u32) -> Option<String> {
self.check_output.get_type_at_position(path, pos)
}
pub fn get_type_at_position_debug(&self, path: &str, pos: u32) -> Option<String> {
self.check_output.get_type_at_position_debug(path, pos)
}
}
#[wasm_bindgen(typescript_custom_section)]
const TYPES_EXPERIMENTAL_BUILD: &str = r###"
export function experimental_build(
entry_path: string,
fs_resolve_js: (path: string) => string | undefined,
config: BuildConfig | undefined
): WASMBuildOutput
"###;
#[wasm_bindgen(js_name = experimental_build, skip_typescript)]
pub fn experimental_build_wasm(
entry_path: String,
fs_resolver_js: &js_sys::Function,
config: JsValue,
) -> WASMBuildOutput {
std::panic::set_hook(Box::new(console_error_panic_hook::hook));
let fs_resolver = |path: &std::path::Path| {
let res =
fs_resolver_js.call1(&JsValue::null(), &JsValue::from(path.display().to_string()));
res.ok().and_then(|res| res.as_string())
};
let config: crate::build::BuildConfig = if config.is_undefined() || config.is_null() {
Default::default()
} else {
serde_wasm_bindgen::from_value(config).expect("invalid BuildConfig")
};
let result = crate::build::build(vec![entry_path.into()], &fs_resolver, config);
match result {
Ok(crate::build::BuildOutput { artifacts, check_output }) => {
WASMBuildOutput { artifacts, check_output: WASMCheckOutput(check_output) }
}
Err(crate::build::FailedBuildOutput(check_output)) => WASMBuildOutput {
artifacts: Default::default(),
check_output: WASMCheckOutput(check_output),
},
}
}
#[wasm_bindgen(typescript_custom_section)]
const TYPES_RUN_CLI: &str = r#"
export function run_cli(
cli_arguments: string[],
read_from_file: (path: string) => string | undefined,
write_to_file: (path: string, content: string) => void,
): void
"#;
#[wasm_bindgen(js_name = run_cli, skip_typescript)]
pub fn run_cli_wasm(
cli_arguments: Box<[JsValue]>,
read_from_file: &js_sys::Function,
write_to_file: &js_sys::Function,
) {
std::panic::set_hook(Box::new(console_error_panic_hook::hook));
let arguments = cli_arguments.iter().flat_map(JsValue::as_string).collect::<Vec<_>>();
let arguments = arguments.iter().map(String::as_str).collect::<Vec<_>>();
let read_from_file = |path: &std::path::Path| {
let res =
read_from_file.call1(&JsValue::null(), &JsValue::from(path.display().to_string()));
res.ok().and_then(|res| res.as_string())
};
let write_to_file = |path: &std::path::Path, content: String| {
write_to_file
.call2(
&JsValue::null(),
&JsValue::from(path.display().to_string()),
&JsValue::from(content),
)
.unwrap();
};
crate::run_cli(&arguments, &read_from_file, write_to_file);
}
#[wasm_bindgen(typescript_custom_section)]
const TYPES_PARSE_EXPRESSION: &str =
"export function parse_expression(input: string): Expression | [string, Span]";
#[wasm_bindgen(js_name = parse_expression, skip_typescript)]
pub fn parse_expression_to_json(input: String) -> JsValue {
use parser::{ASTNode, Expression};
std::panic::set_hook(Box::new(console_error_panic_hook::hook));
let item = Expression::from_string(input, Default::default());
match item {
Ok(item) => serde_wasm_bindgen::to_value(&Ok::<_, ()>(item)).unwrap(),
Err(parse_error) => {
serde_wasm_bindgen::to_value(&(parse_error.reason, parse_error.position)).unwrap()
}
}
}
#[wasm_bindgen(typescript_custom_section)]
const TYPES_PARSE_MODULE: &str =
"export function parse_module(input: string): Module | [string, Span]";
#[wasm_bindgen(js_name = parse_module, skip_typescript)]
pub fn parse_module_to_json(input: String) -> JsValue {
use parser::{ASTNode, Module};
std::panic::set_hook(Box::new(console_error_panic_hook::hook));
let item = Module::from_string(input, Default::default());
match item {
Ok(item) => serde_wasm_bindgen::to_value(&Ok::<_, ()>(item)).unwrap(),
Err(parse_error) => {
serde_wasm_bindgen::to_value(&(parse_error.reason, parse_error.position)).unwrap()
}
}
}
#[wasm_bindgen(typescript_custom_section)]
const TYPES_PARSE_MODULE_AND_INTO_STRING: &str = r#"
export function parse_module_and_into_string(
input: string,
parse_options: ParseOptions,
to_string_options: ToStringOptions
): string | [string, Span]
"#;
#[wasm_bindgen(js_name = parse_module_and_into_string, skip_typescript)]
pub fn parse_module_and_into_string(
input: String,
parse_options: JsValue,
to_string_options: JsValue,
) -> JsValue {
use parser::{ASTNode, Module};
std::panic::set_hook(Box::new(console_error_panic_hook::hook));
let item = Module::from_string(
input,
serde_wasm_bindgen::from_value(parse_options).expect("invalid ParseOptions"),
);
match item {
Ok(item) => serde_wasm_bindgen::to_value(&item.to_string(
&serde_wasm_bindgen::from_value(to_string_options).expect("invalid ToStringOptions"),
))
.unwrap(),
Err(parse_error) => {
serde_wasm_bindgen::to_value(&(parse_error.reason, parse_error.position)).unwrap()
}
}
}
#[wasm_bindgen(typescript_custom_section)]
const TYPES_JUST_IMPORTS: &str =
"export function just_imports(input: string): string | [string, Span]";
#[wasm_bindgen(skip_typescript)]
pub fn just_imports(input: String) -> JsValue {
use parser::{ASTNode, Module};
std::panic::set_hook(Box::new(console_error_panic_hook::hook));
let item = Module::from_string(input, Default::default());
match item {
Ok(mut item) => {
crate::transformers::filter_imports(&mut item);
serde_wasm_bindgen::to_value(&item.to_string(&parser::ToStringOptions::minified()))
.unwrap()
}
Err(parse_error) => {
serde_wasm_bindgen::to_value(&(parse_error.reason, parse_error.position)).unwrap()
}
}
}
#[wasm_bindgen]
pub fn get_version() -> JsValue {
serde_wasm_bindgen::to_value(&env!("CARGO_PKG_VERSION")).unwrap()
}