Handle errors cleanly in the wast runner.

This commit is contained in:
Dan Gohman
2019-01-03 14:16:28 -08:00
parent 40acc1f340
commit 35d7f78a11
2 changed files with 22 additions and 2 deletions

View File

@@ -47,6 +47,10 @@ pub enum WastError {
Trap(String),
/// There was a type error in inputs or outputs of an action.
Type(String),
/// The was a syntax error while parsing the wast script.
Syntax(wabt::script::Error),
/// The was a character encoding error while parsing the wast script.
Utf8(str::Utf8Error),
/// The was an I/O error while reading the wast file.
IO(io::Error),
}
@@ -59,6 +63,8 @@ impl fmt::Display for WastError {
WastError::Action(ref error) => error.fmt(f),
WastError::Trap(ref message) => write!(f, "trap: {}", message),
WastError::Type(ref message) => write!(f, "type error: {}", message),
WastError::Syntax(ref message) => write!(f, "syntax error: {}", message),
WastError::Utf8(ref message) => write!(f, "UTF-8 decoding error: {}", message),
WastError::IO(ref error) => write!(f, "I/O error: {}", error),
}
}
@@ -223,7 +229,17 @@ impl WastContext {
/// Run a wast script from a byte buffer.
pub fn run_buffer(&mut self, filename: &str, wast: &[u8]) -> Result<(), WastFileError> {
let mut parser = ScriptParser::from_str(str::from_utf8(wast).unwrap()).unwrap();
let mut parser =
ScriptParser::from_str(str::from_utf8(wast).map_err(|error| WastFileError {
filename: filename.to_string(),
line: 0,
error: WastError::Utf8(error),
})?)
.map_err(|error| WastFileError {
filename: filename.to_string(),
line: 0,
error: WastError::Syntax(error),
})?;
while let Some(Command { kind, line }) = parser.next().expect("parser") {
match kind {

View File

@@ -35,6 +35,7 @@ use docopt::Docopt;
use file_per_thread_logger;
use pretty_env_logger;
use std::path::Path;
use std::process;
use wasmtime_jit::Compiler;
use wasmtime_wast::WastContext;
@@ -102,6 +103,9 @@ fn main() {
for filename in &args.arg_file {
wast_context
.run_file(Path::new(&filename))
.unwrap_or_else(|e| panic!("{}", e));
.unwrap_or_else(|e| {
eprintln!("{}", e);
process::exit(1)
});
}
}