2018-10-15 01:03:00 +00:00
|
|
|
pub mod utils;
|
2018-10-11 19:29:36 +00:00
|
|
|
|
2019-01-09 06:49:11 +00:00
|
|
|
use wasmer_clif_backend::CraneliftCompiler;
|
2019-01-11 19:47:41 +00:00
|
|
|
use wasmer_runtime::{
|
2019-01-18 18:54:16 +00:00
|
|
|
self as runtime,
|
2019-01-18 19:15:13 +00:00
|
|
|
error::{CallResult, Result},
|
2019-01-21 22:43:04 +00:00
|
|
|
import::ImportObject,
|
2019-01-11 19:47:41 +00:00
|
|
|
instance::Instance,
|
2019-01-19 06:28:41 +00:00
|
|
|
module::Module,
|
2019-01-11 19:47:41 +00:00
|
|
|
};
|
2019-01-18 01:43:58 +00:00
|
|
|
|
2018-10-13 13:31:56 +00:00
|
|
|
use std::panic;
|
2019-01-19 06:28:41 +00:00
|
|
|
use wasmer_emscripten::is_emscripten_module;
|
2018-12-11 03:19:46 +00:00
|
|
|
|
2018-10-14 20:23:48 +00:00
|
|
|
pub struct ResultObject {
|
|
|
|
/// A webassembly::Module object representing the compiled WebAssembly module.
|
|
|
|
/// This Module can be instantiated again
|
2019-01-18 18:54:16 +00:00
|
|
|
pub module: Module,
|
2018-10-14 20:23:48 +00:00
|
|
|
/// A webassembly::Instance object that contains all the Exported WebAssembly
|
|
|
|
/// functions.
|
2018-12-30 10:23:16 +00:00
|
|
|
pub instance: Box<Instance>,
|
2018-10-14 20:23:48 +00:00
|
|
|
}
|
2018-10-11 19:29:36 +00:00
|
|
|
|
2019-01-10 01:45:48 +00:00
|
|
|
#[derive(PartialEq)]
|
|
|
|
pub enum InstanceABI {
|
|
|
|
Emscripten,
|
|
|
|
None,
|
|
|
|
}
|
|
|
|
|
2018-10-14 20:10:53 +00:00
|
|
|
/// The webassembly::instantiate() function allows you to compile and
|
2018-10-11 19:29:36 +00:00
|
|
|
/// instantiate WebAssembly code
|
2018-10-14 21:48:59 +00:00
|
|
|
/// Params:
|
2018-10-11 19:29:36 +00: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 20:10:53 +00:00
|
|
|
/// webassembly::Memory objects. There must be one matching property
|
2018-10-11 19:29:36 +00:00
|
|
|
/// for each declared import of the compiled module or else a
|
2018-10-14 20:10:53 +00:00
|
|
|
/// webassembly::LinkError is thrown.
|
2018-10-11 19:29:36 +00:00
|
|
|
/// Errors:
|
2018-10-14 21:48:59 +00:00
|
|
|
/// If the operation fails, the Result rejects with a
|
2018-10-14 20:10:53 +00:00
|
|
|
/// webassembly::CompileError, webassembly::LinkError, or
|
|
|
|
/// webassembly::RuntimeError, depending on the cause of the failure.
|
2019-01-21 22:43:04 +00:00
|
|
|
pub fn instantiate(buffer_source: &[u8], import_object: ImportObject) -> Result<ResultObject> {
|
2019-01-19 06:28:41 +00:00
|
|
|
debug!("webassembly - compiling module");
|
|
|
|
let module = compile(&buffer_source[..])?;
|
|
|
|
|
|
|
|
debug!("webassembly - instantiating");
|
|
|
|
let instance = module.instantiate(import_object)?;
|
|
|
|
|
|
|
|
debug!("webassembly - instance created");
|
|
|
|
Ok(ResultObject {
|
|
|
|
module,
|
|
|
|
instance: Box::new(instance),
|
|
|
|
})
|
2018-10-14 20:23:48 +00:00
|
|
|
}
|
|
|
|
|
2018-10-24 10:36:43 +00:00
|
|
|
/// The webassembly::instantiate_streaming() function compiles and instantiates
|
2018-10-24 09:56:42 +00:00
|
|
|
/// a WebAssembly module directly from a streamed underlying source.
|
|
|
|
/// This is the most efficient, optimized way to load wasm code.
|
2018-10-24 10:36:43 +00:00
|
|
|
pub fn instantiate_streaming(
|
2018-11-06 14:51:01 +00:00
|
|
|
_buffer_source: Vec<u8>,
|
2019-01-21 22:43:04 +00:00
|
|
|
_import_object: ImportObject,
|
2019-01-18 18:54:16 +00:00
|
|
|
) -> Result<ResultObject> {
|
2018-10-24 09:56:42 +00:00
|
|
|
unimplemented!();
|
|
|
|
}
|
|
|
|
|
2018-10-14 20:23:48 +00: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 21:48:59 +00:00
|
|
|
/// Params:
|
2018-10-14 20:23:48 +00:00
|
|
|
/// * `buffer_source`: A `Vec<u8>` containing the
|
|
|
|
/// binary code of the .wasm module you want to compile.
|
|
|
|
/// Errors:
|
2018-10-14 21:48:59 +00:00
|
|
|
/// If the operation fails, the Result rejects with a
|
2018-10-14 20:23:48 +00:00
|
|
|
/// webassembly::CompileError.
|
2019-01-18 18:54:16 +00:00
|
|
|
pub fn compile(buffer_source: &[u8]) -> Result<Module> {
|
|
|
|
let compiler = CraneliftCompiler::new();
|
|
|
|
let module = runtime::compile(buffer_source, &compiler)?;
|
|
|
|
Ok(module)
|
2018-10-11 19:29:36 +00:00
|
|
|
}
|
2018-12-11 00:23:14 +00:00
|
|
|
|
2019-01-19 06:28:41 +00:00
|
|
|
/// 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(
|
2019-01-18 18:54:16 +00:00
|
|
|
module: &Module,
|
2018-12-15 06:46:11 +00:00
|
|
|
instance: &mut Instance,
|
2019-01-19 06:28:41 +00:00
|
|
|
_path: &str,
|
|
|
|
_args: Vec<&str>,
|
2019-01-18 18:54:16 +00:00
|
|
|
) -> CallResult<()> {
|
|
|
|
let main_name = if is_emscripten_module(module) {
|
2018-12-30 10:23:16 +00:00
|
|
|
"_main"
|
2018-12-11 00:23:14 +00:00
|
|
|
} else {
|
2018-12-30 10:23:16 +00:00
|
|
|
"main"
|
|
|
|
};
|
|
|
|
|
|
|
|
// TODO handle args
|
2019-01-18 18:54:16 +00:00
|
|
|
instance.call(main_name, &[])?;
|
2018-12-30 10:23:16 +00:00
|
|
|
// TODO atinit and atexit for emscripten
|
|
|
|
|
2019-01-18 18:54:16 +00:00
|
|
|
Ok(())
|
2018-12-11 00:23:14 +00:00
|
|
|
}
|