-
Notifications
You must be signed in to change notification settings - Fork 58
/
lib.rs
123 lines (107 loc) Β· 3.23 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
use anyhow::{bail, Context};
use std::ffi::OsStr;
use std::fs;
use std::path::Path;
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(tool)
.arg("--help")
.stdout(Stdio::null())
.stderr(Stdio::null())
.status()
.expect(&diagnostic);
assert!(status.success(), "{}", diagnostic)
}
fn require_wasm_interp() {
require_tool("wasm-interp", "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),
}
}
}