-
Notifications
You must be signed in to change notification settings - Fork 58
/
lib.rs
184 lines (157 loc) Β· 5.01 KB
/
lib.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
use anyhow::{bail, Context};
use std::ffi::OsStr;
use std::fs;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use std::sync::Once;
pub type Result<T> = std::result::Result<T, anyhow::Error>;
pub const FEATURES: &[&str] = &[
"--enable-threads",
"--enable-bulk-memory",
"--enable-reference-types",
"--enable-simd",
"--enable-multi-value",
// TODO
// "--enable-saturating-float-to-int",
// "--enable-sign-extension",
// "--enable-tail-call",
// "--enable-annotations",
];
fn require_tool(tool: &str, repo: &str) {
let diagnostic = format!("Could not spawn {}; do you have {} installed?", tool, repo);
let status = Command::new("wat2wasm")
.arg("--help")
.stdout(Stdio::null())
.stderr(Stdio::null())
.status()
.expect(&diagnostic);
assert!(status.success(), "{}", diagnostic)
}
fn require_wat2wasm() {
require_tool("wat2wasm", "https://github.com/WebAssembly/wabt");
}
/// Compile the `.wat` file at the given path into a `.wasm`.
pub fn wat2wasm(path: &Path, extra_args: &[&str]) -> Result<Vec<u8>> {
static CHECK: Once = Once::new();
CHECK.call_once(require_wat2wasm);
let file = tempfile::NamedTempFile::new().context("could not create named temp file")?;
let mut wasm = PathBuf::from(path);
wasm.set_extension("wasm");
let mut cmd = Command::new("wat2wasm");
cmd.arg(path)
.args(FEATURES)
.arg("--debug-names")
.arg("-o")
.arg(file.path())
.args(extra_args);
println!("running: {:?}", cmd);
let output = cmd.output().context("could not spawn wat2wasm")?;
if !output.status.success() {
bail!(
"wat2wasm exited with status {:?}\n\nstderr = '''\n{}\n'''",
output.status,
String::from_utf8_lossy(&output.stderr)
);
}
let buf = fs::read(file.path())?;
Ok(buf)
}
fn require_wasm2wat() {
require_tool("wasm2wat", "https://github.com/WebAssembly/wabt");
}
/// Disassemble the `.wasm` file at the given path into a `.wat`.
pub fn wasm2wat(path: &Path) -> Result<String> {
static CHECK: Once = Once::new();
CHECK.call_once(require_wasm2wat);
let mut cmd = Command::new("wasm2wat");
cmd.arg(path);
cmd.args(FEATURES);
println!("running: {:?}", cmd);
let output = cmd.output().context("could not run wasm2wat")?;
if !output.status.success() {
bail!(
"wasm2wat exited with status {:?}\n\nstderr = '''\n{}\n'''",
output.status,
String::from_utf8_lossy(&output.stderr)
);
}
Ok(String::from_utf8_lossy(&output.stdout).into_owned())
}
fn require_wasm_interp() {
require_tool("wasm-insterp", "https://github.com/WebAssembly/wabt");
}
/// Run `wasm-interp` on the given wat file.
pub fn wasm_interp(path: &Path) -> Result<String> {
static CHECK: Once = Once::new();
CHECK.call_once(require_wasm_interp);
let mut cmd = Command::new("wasm-interp");
cmd.arg(path);
cmd.arg("--run-all-exports");
// This requires a build of WABT at least as new as `41adcbfb` to get
// `wasm-interp`'s `--dummy-import-func`.
cmd.arg("--dummy-import-func");
cmd.args(FEATURES);
println!("running: {:?}", cmd);
let output = cmd.output().context("could notrun wasm-interp")?;
if !output.status.success() {
bail!(
"wasm-interp exited with status {:?}\n\nstderr = '''\n{}\n'''",
output.status,
String::from_utf8_lossy(&output.stderr)
);
}
Ok(String::from_utf8_lossy(&output.stdout).into_owned())
}
fn require_wasm_opt() {
require_tool("wasm-opt", "https://github.com/WebAssembly/binaryen");
}
/// Run `wasm-opt` on the given input file with optional extra arguments, and
/// return the resulting wasm binary as an in-memory buffer.
pub fn wasm_opt<A, S>(input: &Path, args: A) -> Result<Vec<u8>>
where
A: IntoIterator<Item = S>,
S: AsRef<OsStr>,
{
static CHECK: Once = Once::new();
CHECK.call_once(require_wasm_opt);
let tmp = tempfile::NamedTempFile::new().unwrap();
let mut cmd = Command::new("wasm-opt");
cmd.arg(input);
cmd.arg("-o");
cmd.arg(tmp.path());
cmd.args(&[
"--enable-threads",
"--enable-bulk-memory",
// "--enable-reference-types",
"--enable-simd",
]);
cmd.args(args);
println!("running: {:?}", cmd);
let output = cmd.output().context("could not run wasm-opt")?;
if !output.status.success() {
bail!(
"wasm-opt exited with status {:?}\n\nstderr = '''\n{}\n'''",
output.status,
String::from_utf8_lossy(&output.stderr)
);
}
let buf = fs::read(tmp.path())?;
Ok(buf)
}
pub fn handle<T: TestResult>(result: T) {
result.handle();
}
pub trait TestResult {
fn handle(self);
}
impl TestResult for () {
fn handle(self) {}
}
impl TestResult for Result<()> {
fn handle(self) {
match self {
Ok(()) => {}
Err(e) => panic!("got an error: {:?}", e),
}
}
}