mirror of
https://github.com/fluencelabs/wasmer
synced 2025-07-01 17:41:32 +00:00
Improved errors formatting
This commit is contained in:
14
src/main.rs
14
src/main.rs
@ -24,7 +24,6 @@ extern crate spin;
|
|||||||
extern crate log;
|
extern crate log;
|
||||||
|
|
||||||
// use libc;
|
// use libc;
|
||||||
use std::error::Error;
|
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
@ -32,7 +31,6 @@ use std::path::PathBuf;
|
|||||||
use std::process::exit;
|
use std::process::exit;
|
||||||
|
|
||||||
use structopt::StructOpt;
|
use structopt::StructOpt;
|
||||||
use wabt::wat2wasm;
|
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
mod macros;
|
mod macros;
|
||||||
@ -62,7 +60,7 @@ struct Run {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Read the contents of a file
|
/// Read the contents of a file
|
||||||
fn read_file_contents(path: PathBuf) -> Result<Vec<u8>, io::Error> {
|
fn read_file_contents(path: &PathBuf) -> Result<Vec<u8>, io::Error> {
|
||||||
let mut buffer: Vec<u8> = Vec::new();
|
let mut buffer: Vec<u8> = Vec::new();
|
||||||
let mut file = File::open(path)?;
|
let mut file = File::open(path)?;
|
||||||
file.read_to_end(&mut buffer)?;
|
file.read_to_end(&mut buffer)?;
|
||||||
@ -72,15 +70,15 @@ fn read_file_contents(path: PathBuf) -> Result<Vec<u8>, io::Error> {
|
|||||||
/// Execute a WASM/WAT file
|
/// Execute a WASM/WAT file
|
||||||
fn execute_wasm(wasm_path: PathBuf) -> Result<(), String> {
|
fn execute_wasm(wasm_path: PathBuf) -> Result<(), String> {
|
||||||
let mut wasm_binary: Vec<u8> =
|
let mut wasm_binary: Vec<u8> =
|
||||||
read_file_contents(wasm_path).map_err(|err| String::from(err.description()))?;
|
read_file_contents(&wasm_path).map_err(|err| format!("Can't read the file {}: {}", wasm_path.as_os_str().to_string_lossy(), err))?;
|
||||||
if !webassembly::utils::is_wasm_binary(&wasm_binary) {
|
if !webassembly::utils::is_wasm_binary(&wasm_binary) {
|
||||||
wasm_binary = wat2wasm(wasm_binary).map_err(|err| String::from(err.description()))?;
|
wasm_binary = wabt::wat2wasm(wasm_binary).map_err(|err| format!("Can't convert from wast to wasm: {:?}", err))?;
|
||||||
}
|
}
|
||||||
|
|
||||||
let import_object = linkers::generate_emscripten_env();
|
let import_object = linkers::generate_emscripten_env();
|
||||||
let webassembly::ResultObject { module, instance } =
|
let webassembly::ResultObject { module, instance } =
|
||||||
webassembly::instantiate(wasm_binary, import_object)
|
webassembly::instantiate(wasm_binary, import_object)
|
||||||
.map_err(|err| format!("{}", err))?;
|
.map_err(|err| format!("Can't instantiate the WebAssembly module: {}", err))?;
|
||||||
|
|
||||||
// webassembly::utils::print_instance_offsets(&instance);
|
// webassembly::utils::print_instance_offsets(&instance);
|
||||||
|
|
||||||
@ -99,8 +97,8 @@ fn run(options: Run) {
|
|||||||
match execute_wasm(options.path.clone()) {
|
match execute_wasm(options.path.clone()) {
|
||||||
Ok(()) => {}
|
Ok(()) => {}
|
||||||
Err(message) => {
|
Err(message) => {
|
||||||
let name = options.path.as_os_str().to_string_lossy();
|
// let name = options.path.as_os_str().to_string_lossy();
|
||||||
println!("error while executing {}: {}", name, message);
|
println!("{}", message);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,6 +10,7 @@ use std::panic;
|
|||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use target_lexicon;
|
use target_lexicon;
|
||||||
use wasmparser;
|
use wasmparser;
|
||||||
|
use wasmparser::WasmDecoder;
|
||||||
|
|
||||||
pub use self::errors::{Error, ErrorKind};
|
pub use self::errors::{Error, ErrorKind};
|
||||||
pub use self::import_object::ImportObject;
|
pub use self::import_object::ImportObject;
|
||||||
@ -73,11 +74,8 @@ pub fn instantiate_streaming(
|
|||||||
/// webassembly::CompileError.
|
/// webassembly::CompileError.
|
||||||
pub fn compile(buffer_source: Vec<u8>) -> Result<Module, ErrorKind> {
|
pub fn compile(buffer_source: Vec<u8>) -> Result<Module, ErrorKind> {
|
||||||
// TODO: This should be automatically validated when creating the Module
|
// TODO: This should be automatically validated when creating the Module
|
||||||
let valid = validate(&buffer_source);
|
debug!("webassembly - validating module");
|
||||||
debug!("webassembly - valid {:?}", valid);
|
validate_or_error(&buffer_source)?;
|
||||||
if !valid {
|
|
||||||
return Err(ErrorKind::CompileError("Module not valid".to_string()));
|
|
||||||
}
|
|
||||||
|
|
||||||
debug!("webassembly - creating module");
|
debug!("webassembly - creating module");
|
||||||
let module = Module::from_bytes(buffer_source, triple!("x86_64"), None)?;
|
let module = Module::from_bytes(buffer_source, triple!("x86_64"), None)?;
|
||||||
@ -90,8 +88,20 @@ pub fn compile(buffer_source: Vec<u8>) -> Result<Module, ErrorKind> {
|
|||||||
/// array of WebAssembly binary code, returning whether the bytes
|
/// array of WebAssembly binary code, returning whether the bytes
|
||||||
/// form a valid wasm module (true) or not (false).
|
/// form a valid wasm module (true) or not (false).
|
||||||
/// Params:
|
/// Params:
|
||||||
/// * `buffer_source`: A `Vec<u8>` containing the
|
/// * `buffer_source`: A `&[u8]` containing the
|
||||||
/// binary code of the .wasm module you want to compile.
|
/// binary code of the .wasm module you want to compile.
|
||||||
pub fn validate(buffer_source: &Vec<u8>) -> bool {
|
pub fn validate(buffer_source: &[u8]) -> bool {
|
||||||
wasmparser::validate(buffer_source, None)
|
validate_or_error(buffer_source).is_ok()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn validate_or_error(bytes: &[u8]) -> Result<(), ErrorKind> {
|
||||||
|
let mut parser = wasmparser::ValidatingParser::new(bytes, None);
|
||||||
|
loop {
|
||||||
|
let state = parser.read();
|
||||||
|
match *state {
|
||||||
|
wasmparser::ParserState::EndWasm => return Ok(()),
|
||||||
|
wasmparser::ParserState::Error(err) => return Err(ErrorKind::CompileError(format!("Validation error: {}", err.message))),
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user