2018-12-13 12:49:30 -08:00
|
|
|
pub mod libcalls;
|
2018-10-15 02:48:59 +02:00
|
|
|
pub mod relocation;
|
2018-10-15 03:03:00 +02:00
|
|
|
pub mod utils;
|
2018-10-11 21:29:36 +02:00
|
|
|
|
2019-01-09 00:49:11 -06:00
|
|
|
use wasmer_clif_backend::CraneliftCompiler;
|
2019-01-11 20:47:41 +01:00
|
|
|
use wasmer_runtime::{
|
2019-01-18 10:54:16 -08:00
|
|
|
self as runtime,
|
2019-01-18 11:15:13 -08:00
|
|
|
error::{CallResult, Result},
|
2019-01-11 20:47:41 +01:00
|
|
|
import::Imports,
|
|
|
|
instance::Instance,
|
2019-01-18 00:18:13 -06:00
|
|
|
module::{Module, ModuleInner},
|
2019-01-11 20:47:41 +01:00
|
|
|
};
|
2019-01-17 17:43:58 -08:00
|
|
|
|
2018-11-26 20:29:26 -08:00
|
|
|
use cranelift_codegen::{
|
|
|
|
isa,
|
|
|
|
settings::{self, Configurable},
|
|
|
|
};
|
2018-10-13 15:31:56 +02:00
|
|
|
use std::panic;
|
2019-01-18 00:18:13 -06:00
|
|
|
use std::rc::Rc;
|
2018-10-14 23:48:59 +02:00
|
|
|
use std::str::FromStr;
|
2018-12-30 11:23:16 +01:00
|
|
|
use std::sync::Arc;
|
2018-11-06 15:51:01 +01:00
|
|
|
use target_lexicon;
|
2018-10-14 22:23:48 +02:00
|
|
|
use wasmparser;
|
2018-11-15 00:50:54 -08:00
|
|
|
use wasmparser::WasmDecoder;
|
2018-10-13 19:22:57 +02:00
|
|
|
|
2019-01-10 21:37:59 -08:00
|
|
|
use wasmer_emscripten::{allocate_cstr_on_stack, allocate_on_stack, is_emscripten_module};
|
2018-12-10 19:19:46 -08:00
|
|
|
|
2018-10-14 22:23:48 +02:00
|
|
|
pub struct ResultObject {
|
|
|
|
/// A webassembly::Module object representing the compiled WebAssembly module.
|
|
|
|
/// This Module can be instantiated again
|
2019-01-18 10:54:16 -08:00
|
|
|
pub module: Module,
|
2018-10-14 22:23:48 +02:00
|
|
|
/// A webassembly::Instance object that contains all the Exported WebAssembly
|
|
|
|
/// functions.
|
2018-12-30 11:23:16 +01:00
|
|
|
pub instance: Box<Instance>,
|
2018-10-14 22:23:48 +02:00
|
|
|
}
|
2018-10-11 21:29:36 +02:00
|
|
|
|
2019-01-09 19:45:48 -06:00
|
|
|
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,
|
2019-01-17 17:43:58 -08:00
|
|
|
// pub isa: Box<isa::TargetIsa>, TODO isa
|
2019-01-09 19:45:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(PartialEq)]
|
|
|
|
pub enum InstanceABI {
|
|
|
|
Emscripten,
|
|
|
|
None,
|
|
|
|
}
|
|
|
|
|
2018-10-14 22:10:53 +02:00
|
|
|
/// The webassembly::instantiate() function allows you to compile and
|
2018-10-11 21:29:36 +02:00
|
|
|
/// instantiate WebAssembly code
|
2018-10-14 23:48:59 +02:00
|
|
|
/// Params:
|
2018-10-11 21:29:36 +02:00
|
|
|
/// * `buffer_source`: A `Vec<u8>` containing the
|
|
|
|
/// binary code of the .wasm module you want to compile.
|
|
|
|
/// * `import_object`: An object containing the values to be imported
|
|
|
|
/// into the newly-created Instance, such as functions or
|
2018-10-14 22:10:53 +02:00
|
|
|
/// webassembly::Memory objects. There must be one matching property
|
2018-10-11 21:29:36 +02:00
|
|
|
/// for each declared import of the compiled module or else a
|
2018-10-14 22:10:53 +02:00
|
|
|
/// webassembly::LinkError is thrown.
|
2018-10-11 21:29:36 +02:00
|
|
|
/// Errors:
|
2018-10-14 23:48:59 +02:00
|
|
|
/// If the operation fails, the Result rejects with a
|
2018-10-14 22:10:53 +02:00
|
|
|
/// webassembly::CompileError, webassembly::LinkError, or
|
|
|
|
/// webassembly::RuntimeError, depending on the cause of the failure.
|
2018-10-14 23:48:59 +02:00
|
|
|
pub fn instantiate(
|
2018-12-30 11:23:16 +01:00
|
|
|
buffer_source: &[u8],
|
|
|
|
import_object: &Imports,
|
2018-12-10 19:19:46 -08:00
|
|
|
options: Option<InstanceOptions>,
|
2019-01-18 10:54:16 -08:00
|
|
|
) -> Result<ResultObject> {
|
2018-12-30 11:23:16 +01:00
|
|
|
debug!("webassembly - creating instance");
|
2018-11-25 13:51:21 -05:00
|
|
|
|
2018-12-30 11:23:16 +01:00
|
|
|
//let instance = Instance::new(&module, import_object, options)?;
|
2019-01-09 00:49:11 -06:00
|
|
|
unimplemented!()
|
2019-01-17 17:43:58 -08:00
|
|
|
// let instance = wasmer_runtime::instantiate(buffer_source, &CraneliftCompiler::new(), import_object)
|
|
|
|
// .map_err(|e| ErrorKind::CompileError(e))?;
|
|
|
|
//
|
|
|
|
// let isa = get_isa();
|
2019-01-06 15:21:06 -06:00
|
|
|
// 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,
|
|
|
|
// });
|
2018-11-14 23:10:35 -08:00
|
|
|
|
2019-01-17 17:43:58 -08:00
|
|
|
// debug!("webassembly - instance created");
|
|
|
|
// Ok(ResultObject {
|
|
|
|
// module: Arc::clone(&instance.module),
|
|
|
|
// instance,
|
|
|
|
// })
|
2018-10-14 22:23:48 +02:00
|
|
|
}
|
|
|
|
|
2018-10-24 12:36:43 +02:00
|
|
|
/// The webassembly::instantiate_streaming() function compiles and instantiates
|
2018-10-24 11:56:42 +02:00
|
|
|
/// a WebAssembly module directly from a streamed underlying source.
|
|
|
|
/// This is the most efficient, optimized way to load wasm code.
|
2018-10-24 12:36:43 +02:00
|
|
|
pub fn instantiate_streaming(
|
2018-11-06 15:51:01 +01:00
|
|
|
_buffer_source: Vec<u8>,
|
2019-01-06 15:21:06 -06:00
|
|
|
_import_object: Imports,
|
2019-01-18 10:54:16 -08:00
|
|
|
) -> Result<ResultObject> {
|
2018-10-24 11:56:42 +02:00
|
|
|
unimplemented!();
|
|
|
|
}
|
|
|
|
|
2018-10-14 22:23:48 +02:00
|
|
|
/// The webassembly::compile() function compiles a webassembly::Module
|
|
|
|
/// from WebAssembly binary code. This function is useful if it
|
|
|
|
/// is necessary to a compile a module before it can be instantiated
|
|
|
|
/// (otherwise, the webassembly::instantiate() function should be used).
|
2018-10-14 23:48:59 +02:00
|
|
|
/// Params:
|
2018-10-14 22:23:48 +02:00
|
|
|
/// * `buffer_source`: A `Vec<u8>` containing the
|
|
|
|
/// binary code of the .wasm module you want to compile.
|
|
|
|
/// Errors:
|
2018-10-14 23:48:59 +02:00
|
|
|
/// If the operation fails, the Result rejects with a
|
2018-10-14 22:23:48 +02:00
|
|
|
/// webassembly::CompileError.
|
2019-01-18 10:54:16 -08:00
|
|
|
pub fn compile(buffer_source: &[u8]) -> Result<Module> {
|
|
|
|
let compiler = CraneliftCompiler::new();
|
|
|
|
let module = runtime::compile(buffer_source, &compiler)?;
|
2018-11-15 00:50:54 -08:00
|
|
|
|
2019-01-18 10:54:16 -08:00
|
|
|
Ok(module)
|
2018-10-11 21:29:36 +02:00
|
|
|
}
|
2018-12-10 16:23:14 -08:00
|
|
|
|
2018-12-10 19:19:46 -08:00
|
|
|
pub fn get_isa() -> Box<isa::TargetIsa> {
|
|
|
|
let flags = {
|
|
|
|
let mut builder = settings::builder();
|
|
|
|
builder.set("opt_level", "best").unwrap();
|
2018-12-10 16:23:14 -08:00
|
|
|
|
2018-12-19 19:47:51 -06:00
|
|
|
if cfg!(not(test)) {
|
|
|
|
builder.set("enable_verifier", "false").unwrap();
|
|
|
|
}
|
|
|
|
|
2018-12-10 19:19:46 -08:00
|
|
|
let flags = settings::Flags::new(builder);
|
|
|
|
debug_assert_eq!(flags.opt_level(), settings::OptLevel::Best);
|
|
|
|
flags
|
|
|
|
};
|
|
|
|
isa::lookup(triple!("x86_64")).unwrap().finish(flags)
|
|
|
|
}
|
2018-12-10 16:23:14 -08:00
|
|
|
|
2018-12-15 00:46:11 -06:00
|
|
|
pub fn start_instance(
|
2019-01-18 10:54:16 -08:00
|
|
|
module: &Module,
|
2018-12-15 00:46:11 -06:00
|
|
|
instance: &mut Instance,
|
|
|
|
path: &str,
|
|
|
|
args: Vec<&str>,
|
2019-01-18 10:54:16 -08:00
|
|
|
) -> CallResult<()> {
|
|
|
|
let main_name = if is_emscripten_module(module) {
|
2018-12-30 11:23:16 +01:00
|
|
|
"_main"
|
2018-12-10 16:23:14 -08:00
|
|
|
} else {
|
2018-12-30 11:23:16 +01:00
|
|
|
"main"
|
|
|
|
};
|
|
|
|
|
|
|
|
// TODO handle args
|
2019-01-18 10:54:16 -08:00
|
|
|
instance.call(main_name, &[])?;
|
2018-12-30 11:23:16 +01:00
|
|
|
// TODO atinit and atexit for emscripten
|
|
|
|
|
2019-01-18 10:54:16 -08:00
|
|
|
Ok(())
|
2018-12-10 16:23:14 -08:00
|
|
|
}
|