wasmer/src/bin/wasmer.rs

113 lines
2.9 KiB
Rust
Raw Normal View History

2018-11-06 15:51:01 +01:00
extern crate structopt;
use std::fs::File;
use std::io;
use std::io::Read;
2018-10-14 23:48:59 +02:00
use std::path::PathBuf;
use std::process::exit;
use structopt::StructOpt;
2019-01-19 00:28:41 -06:00
use wasmer::webassembly::InstanceABI;
2018-11-28 13:15:33 +08:00
use wasmer::*;
2019-01-10 21:37:59 -08:00
use wasmer_emscripten;
#[derive(Debug, StructOpt)]
2019-01-19 00:28:41 -06:00
#[structopt(name = "wasmer", about = "Wasm execution runtime.")]
2018-10-14 23:47:35 +02: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 23:48:59 +02:00
Run(Run),
2018-11-25 21:31:32 -08:00
/// Update wasmer to the latest version
#[structopt(name = "self-update")]
SelfUpdate,
2018-10-14 23:47:35 +02:00
}
#[derive(Debug, StructOpt)]
struct Run {
#[structopt(short = "d", long = "debug")]
debug: bool,
2018-12-06 12:32:53 +01:00
/// Input file
#[structopt(parse(from_os_str))]
path: PathBuf,
2018-12-06 12:32:53 +01:00
/// Application arguments
2018-12-15 00:46:11 -06:00
#[structopt(name = "--", raw(multiple = "true"))]
2018-12-06 12:32:53 +01:00
args: Vec<String>,
}
2018-10-14 23:48:59 +02:00
/// Read the contents of a file
2018-11-15 00:50:54 -08: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)?;
// We force to close the file
drop(file);
Ok(buffer)
}
2019-01-19 00:28:41 -06:00
/// Execute a wasm/wat file
2018-12-06 12:32:53 +01:00
fn execute_wasm(options: &Run) -> Result<(), String> {
let wasm_path = &options.path;
let mut wasm_binary: Vec<u8> = read_file_contents(wasm_path).map_err(|err| {
2018-11-15 13:31:37 -08:00
format!(
"Can't read the file {}: {}",
wasm_path.as_os_str().to_string_lossy(),
err
)
})?;
2018-12-06 12:32:53 +01:00
if !webassembly::utils::is_wasm_binary(&wasm_binary) {
2018-11-15 13:31:37 -08:00
wasm_binary = wabt::wat2wasm(wasm_binary)
.map_err(|e| format!("Can't convert from wast to wasm: {:?}", e))?;
}
2018-12-06 12:32:53 +01:00
2019-01-19 00:28:41 -06:00
let module = webassembly::compile(&wasm_binary[..])
.map_err(|e| format!("Can't compile module: {:?}", e))?;
2019-01-19 00:28:41 -06:00
let (_abi, import_object) = if wasmer_emscripten::is_emscripten_module(&module) {
let emscripten_globals = wasmer_emscripten::EmscriptenGlobals::new();
(
InstanceABI::Emscripten,
wasmer_emscripten::generate_emscripten_env(&emscripten_globals),
)
2018-12-15 00:46:11 -06:00
} else {
(InstanceABI::None, wasmer_runtime::import::ImportObject::new())
};
2019-01-18 00:18:13 -06:00
let mut instance = module
.instantiate(import_object)
2019-01-19 00:28:41 -06:00
.map_err(|e| format!("Can't instantiate module: {:?}", e))?;
2018-11-07 11:47:06 +01:00
2019-01-19 00:28:41 -06:00
webassembly::run_instance(
&module,
2018-12-15 00:46:11 -06:00
&mut instance,
options.path.to_str().unwrap(),
options.args.iter().map(|arg| arg.as_str()).collect(),
2019-01-18 11:15:13 -08:00
)
2019-01-19 00:28:41 -06:00
.map_err(|e| format!("{:?}", e))?;
Ok(())
}
2018-10-14 23:47:35 +02:00
fn run(options: Run) {
2018-12-06 12:32:53 +01:00
match execute_wasm(&options) {
Ok(()) => {}
Err(message) => {
eprintln!("{:?}", message);
exit(1);
}
}
}
2018-10-14 23:47:35 +02:00
fn main() {
let options = CLIOptions::from_args();
match options {
CLIOptions::Run(options) => run(options),
2018-11-25 21:31:32 -08:00
CLIOptions::SelfUpdate => update::self_update(),
2018-10-14 23:47:35 +02:00
}
}