-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathmain.rs
274 lines (216 loc) · 9.14 KB
/
main.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
extern crate wast;
mod handle_instructions;
mod handle_module;
mod source_file;
mod stats;
mod utils;
mod wat_to_dts;
use std::{fs, path::Path};
use wat_to_dts::wat_to_dts;
fn main() {
let current_dir = std::env::current_dir().unwrap();
let going_to = Path::new("packages/playground/doom/doom.wat");
let wat_path = current_dir.join(going_to);
let wat = fs::read_to_string(wat_path).unwrap();
let output = wat_to_dts(wat, "packages/playground/doom/doom.dump").to_string();
fs::write("packages/playground/doom/doom.actual.ts", output).unwrap();
}
#[cfg(test)]
mod tests {
use std::{
ffi::OsStr,
fs::{self, DirEntry},
process::Command,
};
use pretty_assertions::assert_eq;
/// SETTING THIS TO TRUE WILL FORCIBLY OVERWRITE ALL TESTS
const OVERWRITE: bool = true;
use self::source_file::SourceFile;
use super::*;
fn skip_list() -> Vec<&'static str> {
vec![
// "conway", //
]
}
fn focus_list() -> Vec<&'static str> {
vec![
// "add-middle", //
// "if-else-nested",
// "if-else",
]
}
/// skip the file if anything in the skip list matches the given file name
fn should_skip(file_name: &str) -> bool {
skip_list().iter().any(|&skip| file_name == skip)
}
/// the file is focused if anything in the focus list matches the given file name
fn is_focused(file_name: &str) -> bool {
focus_list().iter().any(|&focus| file_name == focus)
}
/// this function consults skip_list and focus_list to determine if a test should be run
fn should_run(dir_entry: &DirEntry) -> bool {
let path = dir_entry.path().with_extension("");
let file_name = path.file_name().and_then(OsStr::to_str).expect("invalid file name");
// focusing takes precedence over skipping
if is_focused(file_name) {
return true;
}
if !focus_list().is_empty() {
return false;
}
if should_skip(file_name) {
return false;
}
true
}
fn get_wat_files() -> Vec<DirEntry> {
let from_wat = fs::read_dir("./packages/conformance-tests/from-wat/").unwrap().flatten();
let single_wat = fs::read_dir("./packages/conformance-tests/from-wat-single/").unwrap().flatten();
let files = from_wat.chain(single_wat);
files
.filter_map(|dir_entry| {
let path = dir_entry.path();
// dbg!(&path);
if !path.is_file() {
return None;
}
if path.extension().and_then(OsStr::to_str) != Some("wat") {
return None;
}
if !should_run(&dir_entry) {
// println!("skipping: {:?}", dir_entry.path());
return None;
}
Some(dir_entry)
})
.collect()
}
fn get_c_files() -> Vec<DirEntry> {
fs::read_dir("./packages/conformance-tests/from-c/")
.unwrap()
.flatten()
.filter_map(|dir_entry| {
let path = dir_entry.path();
// dbg!(&path);
if !path.is_file() {
return None;
}
if path.extension().and_then(OsStr::to_str) != Some("c") {
return None;
}
if !should_run(&dir_entry) {
// println!("skipping: {:?}", dir_entry.path());
return None;
}
Some(dir_entry)
})
.collect()
}
fn generate_wasm_from_wat(wat_input: &DirEntry) {
// println!("generating wasm from wat: {:?}", &wat_input.path());
// convert the .wat file to a .wasm file (also validates the .wat)
let output = Command::new("wat2wasm")
.arg(&wat_input.path())
.args(["--output", &wat_input.path().with_extension("wasm").to_string_lossy()])
.output()
.expect("failed to execute wat2wasm");
if !output.status.success() {
// Print the standard error output
let stderr = std::str::from_utf8(&output.stderr).unwrap_or("Error decoding stderr");
println!("wat2wasm failed for {:?}: {}", wat_input.path().file_name(), stderr);
panic!("wat2wasm failed");
}
}
fn parse_wat_and_dump(dir_entry: &DirEntry) -> SourceFile {
let wat_file = dir_entry.path().with_extension("wat");
// println!("parsing wat and dumping: {:?}", &wat_file);
let wat = fs::read_to_string(wat_file).unwrap();
let dump_path = dir_entry.path().with_extension("dump").to_str().unwrap().to_owned();
wat_to_dts(wat, &dump_path)
}
fn create_expected_d_ts(source_file: &SourceFile, dir_entry: &DirEntry) -> String {
let actual = source_file.to_string();
let path = dir_entry.path().with_extension("actual.ts");
fs::write(path, &actual).unwrap();
actual
}
fn get_expected(dir_entry: &DirEntry) -> String {
let path = dir_entry.path().with_extension("expected.ts");
fs::read_to_string(path).unwrap()
}
fn generate_wasm_from_c(c_input: &DirEntry) {
// println!("generating wasm from c: {:?}", &c_input.path());
// convert the .wat file to a .wasm file (also validates the .wat)
let output = Command::new("emcc")
.arg(&c_input.path())
.args(["-o", &c_input.path().with_extension("wasm").to_string_lossy()]) // output target
.arg("-g") // preserve debug information
.arg("-O0") // no optimizations
.args(["-s", "STANDALONE_WASM"]) // setting
.arg("--no-entry") // no entry point (we disregard the main function and use the `entry` function instead)
.output()
.expect("failed to execute emcc");
if !output.status.success() {
// Print the standard error output
let stderr = std::str::from_utf8(&output.stderr).unwrap_or("Error decoding stderr");
println!("emcc failed for {:?}: {}", c_input.path().file_name(), stderr);
panic!("emcc failed");
}
}
fn generate_wat_from_wasm(c_input: &DirEntry) {
let wasm_input = c_input.path().with_extension("wasm");
// println!("generating wat from wasm: {:?}", &wasm_input);
// convert the .wat file to a .wasm file (also validates the .wat)
let output = Command::new("wasm2wat")
.arg(&wasm_input)
.arg("--enable-code-metadata")
.arg("--inline-exports")
.arg("--inline-imports")
.arg("--disable-reference-types")
.arg("--generate-names")
.arg("--fold-exprs")
.args(["--output", &wasm_input.with_extension("wat").to_string_lossy()]) // output target
.output()
.expect("failed to execute wat2wasm");
if !output.status.success() {
// Print the standard error output
let stderr = std::str::from_utf8(&output.stderr).unwrap_or("Error decoding stderr");
println!("emcc failed for {:?}: {}", wasm_input.file_name(), stderr);
panic!("emcc failed");
}
}
#[test]
fn run_conformance_tests() {
/*
Here's how this works. We can take two inputs. One is a .wat file from the `test/from-wat` directory, and the other is a .c file from the `test/from-c` directory. If it starts as a C file, we generate a .wat from that. In both cases we generate `.wasm` files. The `.dump` file is a debug representation of the `.wat` file's parsed contents. We then generate an `.actual.ts` file from our program. We then compare the `.d.ts` file to the `.expected.ts` file.
## from-wat
1. read .wat
2. generate and write .wasm from .wat with wat2wasm
## from-c
1. read .c
2. generate and write .wasm from the .c with emcc
3. generate and write .wat with wasm2wat
## point of convergence
- parse .wat and write .dump
- generate and write actual"
- compare actual.ts to expected"
## runtime tests
runtime tests are done from javascript, so they need to be run with `pnpm test` in a separate step
*/
let from_wat = get_wat_files();
from_wat.iter().for_each(generate_wasm_from_wat);
let from_c = get_c_files();
from_c.iter().for_each(generate_wasm_from_c);
from_c.iter().for_each(generate_wat_from_wasm);
let all_files: Vec<_> = from_wat.iter().chain(from_c.iter()).collect();
for dir_entry in all_files {
let source_file = parse_wat_and_dump(dir_entry);
let actual = create_expected_d_ts(&source_file, dir_entry);
if OVERWRITE {
fs::write(dir_entry.path().with_extension("expected.ts"), &actual).unwrap();
}
let expected = get_expected(dir_entry);
assert_eq!(actual, expected, "{:?}", dir_entry.path().file_name());
}
}
}