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 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(
// Arc::clone(&module),

View File

@ -9,11 +9,12 @@ use std::process::exit;
use structopt::StructOpt;
use wasmer::webassembly::InstanceABI;
use wasmer::*;
use wasmer_emscripten;
#[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
enum CLIOptions {
/// 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)
}
/// Execute a WASM/WAT file
/// Execute a wasm/wat file
fn execute_wasm(options: &Run) -> Result<(), String> {
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))?;
}
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
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),
)
} else {
webassembly::InstanceABI::None
(InstanceABI::None, wasmer_runtime::import::Imports::new())
};
let emscripten_globals = wasmer_emscripten::EmscriptenGlobals::new();
let import_object = if abi == webassembly::InstanceABI::Emscripten {
wasmer_emscripten::generate_emscripten_env(&emscripten_globals)
} else {
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
.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,
&mut instance,
options.path.to_str().unwrap(),
options.args.iter().map(|arg| arg.as_str()).collect(),
)
.map_err(|e| format!("{:?}", e))?)
.map_err(|e| format!("{:?}", e))?;
Ok(())
}
fn run(options: Run) {
match execute_wasm(&options) {
Ok(()) => {}
Err(message) => {
// let name = options.path.as_os_str().to_string_lossy();
println!("{:?}", message);
exit(1);
}

View File

@ -8,22 +8,11 @@ use wasmer_runtime::{
error::{CallResult, Result},
import::Imports,
instance::Instance,
module::{Module, ModuleInner},
module::Module,
};
use cranelift_codegen::{
isa,
settings::{self, Configurable},
};
use std::panic;
use std::rc::Rc;
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};
use wasmer_emscripten::is_emscripten_module;
pub struct ResultObject {
/// A webassembly::Module object representing the compiled WebAssembly module.
@ -34,16 +23,6 @@ pub struct ResultObject {
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)]
pub enum InstanceABI {
Emscripten,
@ -64,39 +43,18 @@ pub enum InstanceABI {
/// If the operation fails, the Result rejects with a
/// webassembly::CompileError, webassembly::LinkError, or
/// webassembly::RuntimeError, depending on the cause of the failure.
pub fn instantiate(
buffer_source: &[u8],
import_object: &Imports,
options: Option<InstanceOptions>,
) -> Result<ResultObject> {
debug!("webassembly - creating instance");
pub fn instantiate(buffer_source: &[u8], import_object: Imports) -> Result<ResultObject> {
debug!("webassembly - compiling module");
let module = compile(&buffer_source[..])?;
//let instance = Instance::new(&module, import_object, options)?;
unimplemented!()
// 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 - instantiating");
let instance = module.instantiate(import_object)?;
// debug!("webassembly - instance created");
// Ok(ResultObject {
// module: Arc::clone(&instance.module),
// instance,
// })
debug!("webassembly - instance created");
Ok(ResultObject {
module,
instance: Box::new(instance),
})
}
/// The webassembly::instantiate_streaming() function compiles and instantiates
@ -122,31 +80,16 @@ pub fn instantiate_streaming(
pub fn compile(buffer_source: &[u8]) -> Result<Module> {
let compiler = CraneliftCompiler::new();
let module = runtime::compile(buffer_source, &compiler)?;
Ok(module)
}
pub fn get_isa() -> Box<isa::TargetIsa> {
let flags = {
let mut builder = settings::builder();
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(
/// Performs common instance operations needed when an instance is first run
/// including data setup, handling arguments and calling a main function
pub fn run_instance(
module: &Module,
instance: &mut Instance,
path: &str,
args: Vec<&str>,
_path: &str,
_args: Vec<&str>,
) -> CallResult<()> {
let main_name = if is_emscripten_module(module) {
"_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
pub fn is_wasm_binary(binary: &Vec<u8>) -> bool {
/// Detect if a provided binary is a Wasm file
pub fn is_wasm_binary(binary: &[u8]) -> bool {
binary.starts_with(&[b'\0', b'a', b's', b'm'])
}