2018-11-06 14:51:01 +00:00
|
|
|
extern crate structopt;
|
2018-11-28 05:15:33 +00:00
|
|
|
extern crate wasmer;
|
2018-10-11 19:29:36 +00:00
|
|
|
|
|
|
|
use std::fs::File;
|
|
|
|
use std::io;
|
|
|
|
use std::io::Read;
|
2018-10-14 21:48:59 +00:00
|
|
|
use std::path::PathBuf;
|
2018-10-11 19:29:36 +00:00
|
|
|
use std::process::exit;
|
2018-12-30 10:23:16 +00:00
|
|
|
use std::sync::Arc;
|
2018-10-11 19:29:36 +00:00
|
|
|
|
|
|
|
use structopt::StructOpt;
|
|
|
|
|
2018-11-28 05:15:33 +00:00
|
|
|
use wasmer::*;
|
2019-01-09 06:49:11 +00:00
|
|
|
use wasmer_runtime;
|
2018-10-11 19:29:36 +00:00
|
|
|
|
|
|
|
#[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-11-26 05:31:32 +00:00
|
|
|
|
|
|
|
/// Update wasmer to the latest version
|
|
|
|
#[structopt(name = "self-update")]
|
|
|
|
SelfUpdate,
|
2018-10-14 21:47:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, StructOpt)]
|
|
|
|
struct Run {
|
2018-10-11 19:29:36 +00:00
|
|
|
#[structopt(short = "d", long = "debug")]
|
|
|
|
debug: bool,
|
2018-12-06 11:32:53 +00:00
|
|
|
|
2018-10-11 19:29:36 +00:00
|
|
|
/// Input file
|
|
|
|
#[structopt(parse(from_os_str))]
|
|
|
|
path: PathBuf,
|
2018-12-06 11:32:53 +00:00
|
|
|
|
|
|
|
/// Application arguments
|
2018-12-15 06:46:11 +00:00
|
|
|
#[structopt(name = "--", raw(multiple = "true"))]
|
2018-12-06 11:32:53 +00:00
|
|
|
args: Vec<String>,
|
2018-10-11 19:29:36 +00:00
|
|
|
}
|
|
|
|
|
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> {
|
2018-10-11 19:29:36 +00:00
|
|
|
let mut buffer: Vec<u8> = Vec::new();
|
|
|
|
let mut file = File::open(path)?;
|
|
|
|
file.read_to_end(&mut buffer)?;
|
2018-12-28 12:59:55 +00:00
|
|
|
// We force to close the file
|
|
|
|
drop(file);
|
2018-10-11 19:29:36 +00:00
|
|
|
Ok(buffer)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Execute a WASM/WAT file
|
2018-12-06 11:32:53 +00: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 21:31:37 +00:00
|
|
|
format!(
|
|
|
|
"Can't read the file {}: {}",
|
|
|
|
wasm_path.as_os_str().to_string_lossy(),
|
|
|
|
err
|
|
|
|
)
|
|
|
|
})?;
|
2018-12-06 11:32:53 +00:00
|
|
|
|
2018-10-11 19:29:36 +00:00
|
|
|
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-10-11 19:29:36 +00:00
|
|
|
}
|
2018-12-06 11:32:53 +00:00
|
|
|
|
2018-12-11 04:15:41 +00:00
|
|
|
let isa = webassembly::get_isa();
|
|
|
|
|
|
|
|
debug!("webassembly - creating module");
|
2018-12-30 10:23:16 +00:00
|
|
|
let module = webassembly::compile(&wasm_binary[..])
|
2018-12-15 06:46:11 +00:00
|
|
|
.map_err(|err| format!("Can't create the WebAssembly module: {}", err))?;
|
2018-12-11 04:15:41 +00:00
|
|
|
|
2018-12-11 05:19:39 +00:00
|
|
|
let abi = if apis::is_emscripten_module(&module) {
|
2019-01-09 06:49:11 +00:00
|
|
|
wasmer_runtime::InstanceABI::Emscripten
|
2018-12-11 05:19:39 +00:00
|
|
|
} else {
|
2019-01-09 06:49:11 +00:00
|
|
|
wasmer_runtime::InstanceABI::None
|
2018-12-11 05:19:39 +00:00
|
|
|
};
|
|
|
|
|
2019-01-09 06:49:11 +00:00
|
|
|
let import_object = if abi == wasmer_runtime::InstanceABI::Emscripten {
|
2018-12-11 05:19:39 +00:00
|
|
|
apis::generate_emscripten_env()
|
2018-12-15 06:46:11 +00:00
|
|
|
} else {
|
2019-01-09 06:49:11 +00:00
|
|
|
wasmer_runtime::Imports::new()
|
2018-12-11 05:19:39 +00:00
|
|
|
};
|
2018-12-11 05:07:41 +00:00
|
|
|
|
2019-01-09 06:49:11 +00:00
|
|
|
let instance_options = wasmer_runtime::InstanceOptions {
|
2018-12-11 04:15:41 +00:00
|
|
|
mock_missing_imports: true,
|
|
|
|
mock_missing_globals: true,
|
|
|
|
mock_missing_tables: true,
|
2018-12-11 05:19:39 +00:00
|
|
|
abi: abi,
|
2018-12-11 04:15:41 +00:00
|
|
|
show_progressbar: true,
|
2019-01-09 06:49:11 +00:00
|
|
|
// isa: isa,
|
2018-12-11 04:15:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
debug!("webassembly - creating instance");
|
2018-12-30 10:23:16 +00:00
|
|
|
|
2019-01-09 06:49:11 +00:00
|
|
|
let mut instance = module.instantiate(&import_object)
|
2018-12-15 06:46:11 +00:00
|
|
|
.map_err(|err| format!("Can't instantiate the WebAssembly module: {}", err))?;
|
2018-11-07 10:47:06 +00:00
|
|
|
|
2018-12-15 06:46:11 +00:00
|
|
|
webassembly::start_instance(
|
2018-12-30 10:23:16 +00:00
|
|
|
Arc::clone(&module),
|
2018-12-15 06:46:11 +00:00
|
|
|
&mut instance,
|
|
|
|
options.path.to_str().unwrap(),
|
|
|
|
options.args.iter().map(|arg| arg.as_str()).collect(),
|
|
|
|
)
|
2018-10-11 19:29:36 +00:00
|
|
|
}
|
|
|
|
|
2018-10-14 21:47:35 +00:00
|
|
|
fn run(options: Run) {
|
2018-12-06 11:32:53 +00:00
|
|
|
match execute_wasm(&options) {
|
2018-10-11 19:29:36 +00:00
|
|
|
Ok(()) => {}
|
|
|
|
Err(message) => {
|
2018-11-15 08:50:54 +00:00
|
|
|
// let name = options.path.as_os_str().to_string_lossy();
|
|
|
|
println!("{}", message);
|
2018-10-11 19:29:36 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-14 21:47:35 +00:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let options = CLIOptions::from_args();
|
|
|
|
match options {
|
|
|
|
CLIOptions::Run(options) => run(options),
|
2018-11-26 05:31:32 +00:00
|
|
|
CLIOptions::SelfUpdate => update::self_update(),
|
2018-10-14 21:47:35 +00:00
|
|
|
}
|
|
|
|
}
|