Cleanup wasmer bin and webassembly

This commit is contained in:
Brandon Fish 2019-01-19 00:28:41 -06:00
parent 89c5984d47
commit 2f6746655d
4 changed files with 37 additions and 109 deletions

View File

@ -20,7 +20,7 @@ macro_rules! assert_emscripten_output {
let import_object = generate_emscripten_env(&emscripten_globals); let import_object = generate_emscripten_env(&emscripten_globals);
let mut instance = module.instantiate(import_object) let mut instance = module.instantiate(import_object)
.map_err(|err| format!("Can't instantiate the WebAssembly module: {}", err)).unwrap(); // NOTE: Need to figure what the unwrap is for ?? .map_err(|err| format!("Can't instantiate the WebAssembly module: {:?}", err)).unwrap(); // NOTE: Need to figure what the unwrap is for ??
// start_instance( // start_instance(
// Arc::clone(&module), // Arc::clone(&module),

View File

@ -9,11 +9,12 @@ use std::process::exit;
use structopt::StructOpt; use structopt::StructOpt;
use wasmer::webassembly::InstanceABI;
use wasmer::*; use wasmer::*;
use wasmer_emscripten; use wasmer_emscripten;
#[derive(Debug, StructOpt)] #[derive(Debug, StructOpt)]
#[structopt(name = "wasmer", about = "WASM execution runtime.")] #[structopt(name = "wasmer", about = "Wasm execution runtime.")]
/// The options for the wasmer Command Line Interface /// The options for the wasmer Command Line Interface
enum CLIOptions { enum CLIOptions {
/// Run a WebAssembly file. Formats accepted: wasm, wast /// Run a WebAssembly file. Formats accepted: wasm, wast
@ -49,7 +50,7 @@ fn read_file_contents(path: &PathBuf) -> Result<Vec<u8>, io::Error> {
Ok(buffer) Ok(buffer)
} }
/// Execute a WASM/WAT file /// Execute a wasm/wat file
fn execute_wasm(options: &Run) -> Result<(), String> { fn execute_wasm(options: &Run) -> Result<(), String> {
let wasm_path = &options.path; let wasm_path = &options.path;
@ -66,53 +67,37 @@ fn execute_wasm(options: &Run) -> Result<(), String> {
.map_err(|e| format!("Can't convert from wast to wasm: {:?}", e))?; .map_err(|e| format!("Can't convert from wast to wasm: {:?}", e))?;
} }
let isa = webassembly::get_isa(); let module = webassembly::compile(&wasm_binary[..])
.map_err(|e| format!("Can't compile module: {:?}", e))?;
debug!("webassembly - creating module");
let module = webassembly::compile(&wasm_binary[..]).map_err(|e| format!("{:?}", e))?;
let abi = if wasmer_emscripten::is_emscripten_module(&module) {
webassembly::InstanceABI::Emscripten
} else {
webassembly::InstanceABI::None
};
let (_abi, import_object) = if wasmer_emscripten::is_emscripten_module(&module) {
let emscripten_globals = wasmer_emscripten::EmscriptenGlobals::new(); let emscripten_globals = wasmer_emscripten::EmscriptenGlobals::new();
(
let import_object = if abi == webassembly::InstanceABI::Emscripten { InstanceABI::Emscripten,
wasmer_emscripten::generate_emscripten_env(&emscripten_globals) wasmer_emscripten::generate_emscripten_env(&emscripten_globals),
)
} else { } else {
wasmer_runtime::import::Imports::new() (InstanceABI::None, wasmer_runtime::import::Imports::new())
}; };
let instance_options = webassembly::InstanceOptions {
mock_missing_imports: true,
mock_missing_globals: true,
mock_missing_tables: true,
abi: abi,
show_progressbar: true,
};
debug!("webassembly - creating instance");
let mut instance = module let mut instance = module
.instantiate(import_object) .instantiate(import_object)
.map_err(|e| format!("{:?}", e))?; .map_err(|e| format!("Can't instantiate module: {:?}", e))?;
Ok(webassembly::start_instance( webassembly::run_instance(
&module, &module,
&mut instance, &mut instance,
options.path.to_str().unwrap(), options.path.to_str().unwrap(),
options.args.iter().map(|arg| arg.as_str()).collect(), options.args.iter().map(|arg| arg.as_str()).collect(),
) )
.map_err(|e| format!("{:?}", e))?) .map_err(|e| format!("{:?}", e))?;
Ok(())
} }
fn run(options: Run) { fn run(options: Run) {
match execute_wasm(&options) { match execute_wasm(&options) {
Ok(()) => {} Ok(()) => {}
Err(message) => { Err(message) => {
// let name = options.path.as_os_str().to_string_lossy();
println!("{:?}", message); println!("{:?}", message);
exit(1); exit(1);
} }

View File

@ -8,22 +8,11 @@ use wasmer_runtime::{
error::{CallResult, Result}, error::{CallResult, Result},
import::Imports, import::Imports,
instance::Instance, instance::Instance,
module::{Module, ModuleInner}, module::Module,
}; };
use cranelift_codegen::{
isa,
settings::{self, Configurable},
};
use std::panic; use std::panic;
use std::rc::Rc; use wasmer_emscripten::is_emscripten_module;
use std::str::FromStr;
use std::sync::Arc;
use target_lexicon;
use wasmparser;
use wasmparser::WasmDecoder;
use wasmer_emscripten::{allocate_cstr_on_stack, allocate_on_stack, is_emscripten_module};
pub struct ResultObject { pub struct ResultObject {
/// A webassembly::Module object representing the compiled WebAssembly module. /// A webassembly::Module object representing the compiled WebAssembly module.
@ -34,16 +23,6 @@ pub struct ResultObject {
pub instance: Box<Instance>, pub instance: Box<Instance>,
} }
pub struct InstanceOptions {
// Shall we mock automatically the imported functions if they don't exist?
pub mock_missing_imports: bool,
pub mock_missing_globals: bool,
pub mock_missing_tables: bool,
pub abi: InstanceABI,
pub show_progressbar: bool,
// pub isa: Box<isa::TargetIsa>, TODO isa
}
#[derive(PartialEq)] #[derive(PartialEq)]
pub enum InstanceABI { pub enum InstanceABI {
Emscripten, Emscripten,
@ -64,39 +43,18 @@ pub enum InstanceABI {
/// If the operation fails, the Result rejects with a /// If the operation fails, the Result rejects with a
/// webassembly::CompileError, webassembly::LinkError, or /// webassembly::CompileError, webassembly::LinkError, or
/// webassembly::RuntimeError, depending on the cause of the failure. /// webassembly::RuntimeError, depending on the cause of the failure.
pub fn instantiate( pub fn instantiate(buffer_source: &[u8], import_object: Imports) -> Result<ResultObject> {
buffer_source: &[u8], debug!("webassembly - compiling module");
import_object: &Imports, let module = compile(&buffer_source[..])?;
options: Option<InstanceOptions>,
) -> Result<ResultObject> {
debug!("webassembly - creating instance");
//let instance = Instance::new(&module, import_object, options)?; debug!("webassembly - instantiating");
unimplemented!() let instance = module.instantiate(import_object)?;
// let instance = wasmer_runtime::instantiate(buffer_source, &CraneliftCompiler::new(), import_object)
// .map_err(|e| ErrorKind::CompileError(e))?;
//
// let isa = get_isa();
// let abi = if is_emscripten_module(&instance.module) {
// InstanceABI::Emscripten
// } else {
// InstanceABI::None
// };
//
// let options = options.unwrap_or_else(|| InstanceOptions {
// mock_missing_imports: false,
// mock_missing_globals: false,
// mock_missing_tables: false,
// abi,
// show_progressbar: false,
// isa,
// });
// debug!("webassembly - instance created"); debug!("webassembly - instance created");
// Ok(ResultObject { Ok(ResultObject {
// module: Arc::clone(&instance.module), module,
// instance, instance: Box::new(instance),
// }) })
} }
/// The webassembly::instantiate_streaming() function compiles and instantiates /// The webassembly::instantiate_streaming() function compiles and instantiates
@ -122,31 +80,16 @@ pub fn instantiate_streaming(
pub fn compile(buffer_source: &[u8]) -> Result<Module> { pub fn compile(buffer_source: &[u8]) -> Result<Module> {
let compiler = CraneliftCompiler::new(); let compiler = CraneliftCompiler::new();
let module = runtime::compile(buffer_source, &compiler)?; let module = runtime::compile(buffer_source, &compiler)?;
Ok(module) Ok(module)
} }
pub fn get_isa() -> Box<isa::TargetIsa> { /// Performs common instance operations needed when an instance is first run
let flags = { /// including data setup, handling arguments and calling a main function
let mut builder = settings::builder(); pub fn run_instance(
builder.set("opt_level", "best").unwrap();
if cfg!(not(test)) {
builder.set("enable_verifier", "false").unwrap();
}
let flags = settings::Flags::new(builder);
debug_assert_eq!(flags.opt_level(), settings::OptLevel::Best);
flags
};
isa::lookup(triple!("x86_64")).unwrap().finish(flags)
}
pub fn start_instance(
module: &Module, module: &Module,
instance: &mut Instance, instance: &mut Instance,
path: &str, _path: &str,
args: Vec<&str>, _args: Vec<&str>,
) -> CallResult<()> { ) -> CallResult<()> {
let main_name = if is_emscripten_module(module) { let main_name = if is_emscripten_module(module) {
"_main" "_main"

View File

@ -1,6 +1,6 @@
//! Utility functions for the webassembly library //! Utility functions for the WebAssembly module
/// Detect if a provided binary is a WASM file /// Detect if a provided binary is a Wasm file
pub fn is_wasm_binary(binary: &Vec<u8>) -> bool { pub fn is_wasm_binary(binary: &[u8]) -> bool {
binary.starts_with(&[b'\0', b'a', b's', b'm']) binary.starts_with(&[b'\0', b'a', b's', b'm'])
} }