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-07 03:40:44 +00:00
|
|
|
use apis::emscripten::{allocate_on_stack, allocate_cstr_on_stack};
|
2018-10-11 19:29:36 +00:00
|
|
|
use structopt::StructOpt;
|
|
|
|
|
2018-11-28 05:15:33 +00:00
|
|
|
use wasmer::*;
|
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
|
|
|
|
#[structopt(name = "--", raw(multiple="true"))]
|
|
|
|
args: Vec<String>,
|
2018-10-11 19:29:36 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 11:32:53 +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)?;
|
|
|
|
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-11-21 04:51:11 +00:00
|
|
|
// TODO: We should instantiate after compilation, so we provide the
|
|
|
|
// emscripten environment conditionally based on the module
|
2018-11-20 19:11:58 +00:00
|
|
|
let import_object = apis::generate_emscripten_env();
|
2018-12-06 20:21:58 +00:00
|
|
|
let webassembly::ResultObject { module, mut instance } =
|
2018-11-06 14:51:01 +00:00
|
|
|
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-12-01 19:32:24 +00:00
|
|
|
if apis::emscripten::is_emscripten_module(&module) {
|
2018-12-06 11:32:53 +00:00
|
|
|
|
2018-11-30 06:16:36 +00:00
|
|
|
// Emscripten __ATINIT__
|
|
|
|
if let Some(&webassembly::Export::Function(environ_constructor_index)) = module.info.exports.get("___emscripten_environ_constructor") {
|
|
|
|
debug!("emscripten::___emscripten_environ_constructor");
|
|
|
|
let ___emscripten_environ_constructor: extern "C" fn(&webassembly::Instance) =
|
|
|
|
get_instance_function!(instance, environ_constructor_index);
|
|
|
|
call_protected!(___emscripten_environ_constructor(&instance)).map_err(|err| format!("{}", err))?;
|
|
|
|
};
|
2018-12-06 11:32:53 +00:00
|
|
|
|
2018-11-30 06:16:36 +00:00
|
|
|
// TODO: We also need to handle TTY.init() and SOCKFS.root = FS.mount(SOCKFS, {}, null)
|
2018-11-21 22:31:55 +00:00
|
|
|
let func_index = match module.info.exports.get("_main") {
|
2018-10-17 14:45:24 +00:00
|
|
|
Some(&webassembly::Export::Function(index)) => index,
|
2018-11-21 04:51:11 +00:00
|
|
|
_ => panic!("_main emscripten function not found"),
|
|
|
|
};
|
2018-12-06 11:32:53 +00:00
|
|
|
|
2018-12-07 03:08:17 +00:00
|
|
|
let main: extern "C" fn(u32, u32, &webassembly::Instance) =
|
2018-11-22 04:59:23 +00:00
|
|
|
get_instance_function!(instance, func_index);
|
2018-12-06 11:32:53 +00:00
|
|
|
|
2018-12-07 03:40:44 +00:00
|
|
|
let (argc, argv) = store_module_arguments(options, &mut instance);
|
2018-12-06 11:32:53 +00:00
|
|
|
|
2018-12-07 03:08:17 +00:00
|
|
|
return call_protected!(main(argc, argv, &instance)).map_err(|err| format!("{}", err));
|
2018-11-30 06:16:36 +00:00
|
|
|
// TODO: We should implement emscripten __ATEXIT__
|
2018-11-22 04:59:23 +00:00
|
|
|
} else {
|
|
|
|
let func_index =
|
|
|
|
instance
|
|
|
|
.start_func
|
|
|
|
.unwrap_or_else(|| match module.info.exports.get("main") {
|
|
|
|
Some(&webassembly::Export::Function(index)) => index,
|
|
|
|
_ => panic!("Main function not found"),
|
|
|
|
});
|
2018-11-24 18:50:48 +00:00
|
|
|
let main: extern "C" fn(&webassembly::Instance) =
|
|
|
|
get_instance_function!(instance, func_index);
|
|
|
|
return call_protected!(main(&instance)).map_err(|err| format!("{}", err));
|
2018-11-21 04:51:11 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
2018-12-06 15:26:27 +00:00
|
|
|
|
2018-12-07 03:40:44 +00:00
|
|
|
fn store_module_arguments(options: &Run, instance: &mut webassembly::Instance) -> (u32, u32) {
|
|
|
|
let argc = options.args.len() + 1;
|
2018-12-06 15:26:27 +00:00
|
|
|
|
2018-12-07 03:40:44 +00:00
|
|
|
let (argv_offset, argv_slice): (_, &mut [u32]) = unsafe { allocate_on_stack(((argc + 1) * 4) as u32, instance) };
|
|
|
|
assert!(argv_slice.len() >= 1);
|
2018-12-06 15:26:27 +00:00
|
|
|
|
2018-12-07 03:40:44 +00:00
|
|
|
argv_slice[0] = unsafe { allocate_cstr_on_stack(options.path.to_str().unwrap(), instance).0 };
|
2018-12-06 15:26:27 +00:00
|
|
|
|
2018-12-07 03:40:44 +00:00
|
|
|
for (slot, arg) in argv_slice[1..argc].iter_mut().zip(options.args.iter()) {
|
|
|
|
*slot = unsafe { allocate_cstr_on_stack(&arg, instance).0 };
|
2018-12-06 15:26:27 +00:00
|
|
|
}
|
|
|
|
|
2018-12-07 03:40:44 +00:00
|
|
|
argv_slice[argc] = 0;
|
2018-12-06 15:26:27 +00:00
|
|
|
|
2018-12-07 03:40:44 +00:00
|
|
|
(argc as u32, argv_offset)
|
|
|
|
}
|
2018-12-06 15:26:27 +00:00
|
|
|
|
2018-12-07 03:40:44 +00:00
|
|
|
// fn get_module_arguments(options: &Run, instance: &mut webassembly::Instance) -> (u32, u32) {
|
|
|
|
// // Application Arguments
|
|
|
|
// let mut arg_values: Vec<String> = Vec::new();
|
|
|
|
// let mut arg_addrs: Vec<*const u8> = Vec::new();
|
|
|
|
// let arg_length = options.args.len() + 1;
|
2018-12-06 15:26:27 +00:00
|
|
|
|
2018-12-07 03:40:44 +00:00
|
|
|
// arg_values.reserve_exact(arg_length);
|
|
|
|
// arg_addrs.reserve_exact(arg_length);
|
|
|
|
|
|
|
|
// // Push name of wasm file
|
|
|
|
// arg_values.push(format!("{}\0", options.path.to_str().unwrap()));
|
|
|
|
// arg_addrs.push(arg_values[0].as_ptr());
|
|
|
|
|
|
|
|
// // Push additional arguments
|
|
|
|
// for (i, arg) in options.args.iter().enumerate() {
|
|
|
|
// arg_values.push(format!("{}\0", arg));
|
|
|
|
// arg_addrs.push(arg_values[i + 1].as_ptr());
|
|
|
|
// }
|
|
|
|
|
|
|
|
// // Get argument count and pointer to addresses
|
|
|
|
// let argv = arg_addrs.as_ptr() as *mut *mut i8;
|
|
|
|
// let argc = arg_length as u32;
|
|
|
|
|
|
|
|
// // Copy the the arguments into the wasm memory and get offset
|
|
|
|
// let argv_offset = unsafe {
|
|
|
|
// copy_cstr_array_into_wasm(argc, argv, instance)
|
|
|
|
// };
|
|
|
|
|
|
|
|
// debug!("argc = {:?}", argc);
|
|
|
|
// debug!("argv = {:?}", arg_addrs);
|
|
|
|
|
|
|
|
// (argc, argv_offset)
|
|
|
|
// }
|