wasmer/src/main.rs

110 lines
2.9 KiB
Rust
Raw Normal View History

#[macro_use]
extern crate error_chain;
extern crate cranelift_codegen;
2018-10-14 21:48:59 +00:00
extern crate cranelift_entity;
extern crate cranelift_native;
extern crate cranelift_wasm;
2018-11-15 21:31:37 +00:00
extern crate libc;
extern crate memmap;
extern crate region;
2018-11-06 14:51:01 +00:00
extern crate structopt;
2018-10-14 21:48:59 +00:00
extern crate wabt;
2018-11-15 21:30:00 +00:00
extern crate wasmparser;
2018-10-13 17:22:57 +00:00
#[macro_use]
extern crate target_lexicon;
extern crate nix;
extern crate rayon;
use std::fs::File;
use std::io;
use std::io::Read;
2018-10-14 21:48:59 +00:00
use std::path::PathBuf;
use std::process::exit;
use structopt::StructOpt;
2018-10-16 15:01:47 +00:00
#[macro_use]
mod macros;
2018-10-14 11:59:11 +00:00
pub mod common;
2018-11-20 19:11:58 +00:00
pub mod apis;
pub mod sighandler;
#[cfg(test)]
mod spectests;
2018-10-24 00:01:46 +00:00
pub mod webassembly;
#[derive(Debug, StructOpt)]
#[structopt(name = "wasmer", about = "WASM execution runtime.")]
2018-10-14 21:47:35 +00:00
/// The options for the wasmer Command Line Interface
enum CLIOptions {
/// Run a WebAssembly file. Formats accepted: wasm, wast
#[structopt(name = "run")]
2018-10-14 21:48:59 +00:00
Run(Run),
2018-10-14 21:47:35 +00:00
}
#[derive(Debug, StructOpt)]
struct Run {
#[structopt(short = "d", long = "debug")]
debug: bool,
/// Input file
#[structopt(parse(from_os_str))]
path: PathBuf,
}
2018-10-14 21:48:59 +00:00
/// Read the contents of a file
2018-11-15 08:50:54 +00:00
fn read_file_contents(path: &PathBuf) -> Result<Vec<u8>, io::Error> {
let mut buffer: Vec<u8> = Vec::new();
let mut file = File::open(path)?;
file.read_to_end(&mut buffer)?;
Ok(buffer)
}
/// Execute a WASM/WAT file
2018-10-14 21:48:59 +00:00
fn execute_wasm(wasm_path: PathBuf) -> Result<(), String> {
2018-11-15 21:31:37 +00:00
let mut wasm_binary: Vec<u8> = 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) {
2018-11-15 21:31:37 +00:00
wasm_binary = wabt::wat2wasm(wasm_binary)
.map_err(|err| format!("Can't convert from wast to wasm: {:?}", err))?;
}
2018-11-20 19:11:58 +00:00
let import_object = apis::generate_emscripten_env();
2018-11-06 14:51:01 +00:00
let webassembly::ResultObject { module, instance } =
webassembly::instantiate(wasm_binary, import_object)
2018-11-15 08:50:54 +00:00
.map_err(|err| format!("Can't instantiate the WebAssembly module: {}", err))?;
2018-11-07 10:47:06 +00:00
2018-11-20 19:11:58 +00:00
// webassembly::utils::print_instance_offsets(&instance);
2018-11-07 10:47:06 +00:00
let func_index = instance
.start_func
2018-11-19 21:21:11 +00:00
.unwrap_or_else(|| match module.info.exports.get("main").or(module.info.exports.get("_main")) {
2018-10-17 14:45:24 +00:00
Some(&webassembly::Export::Function(index)) => index,
_ => panic!("Main function not found"),
});
2018-11-21 03:24:23 +00:00
let main: extern fn(u32, u32, &webassembly::Instance) = get_instance_function!(instance, func_index);
main(0, 0, &instance);
Ok(())
}
2018-10-14 21:47:35 +00:00
fn run(options: Run) {
match execute_wasm(options.path.clone()) {
Ok(()) => {}
Err(message) => {
2018-11-15 08:50:54 +00:00
// let name = options.path.as_os_str().to_string_lossy();
println!("{}", message);
exit(1);
}
}
}
2018-10-14 21:47:35 +00:00
fn main() {
let options = CLIOptions::from_args();
match options {
CLIOptions::Run(options) => run(options),
}
}