wasmer/src/webassembly.rs

73 lines
2.6 KiB
Rust
Raw Normal View History

2019-01-24 06:00:38 +00:00
use std::panic;
2019-04-11 21:34:54 +00:00
pub use wasmer_runtime::compile_with_config_with;
2019-04-30 04:40:16 +00:00
use wasmer_runtime::{self as runtime, error::Result, ImportObject, Instance, Module};
pub struct ResultObject {
/// A webassembly::Module object representing the compiled WebAssembly module.
/// This Module can be instantiated again
pub module: Module,
/// A webassembly::Instance object that contains all the Exported WebAssembly
/// functions.
2018-12-30 10:23:16 +00:00
pub instance: Box<Instance>,
}
2019-01-10 01:45:48 +00:00
#[derive(PartialEq)]
pub enum InstanceABI {
Emscripten,
2019-03-28 19:19:23 +00:00
WASI,
2019-01-10 01:45:48 +00:00
None,
}
/// The webassembly::instantiate() function allows you to compile and
/// instantiate WebAssembly code
2018-10-14 21:48:59 +00:00
/// Params:
/// * `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
/// webassembly::Memory objects. There must be one matching property
/// for each declared import of the compiled module or else a
/// webassembly::LinkError is thrown.
/// Errors:
2018-10-14 21:48:59 +00:00
/// 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: ImportObject) -> Result<ResultObject> {
2019-01-19 06:28:41 +00:00
debug!("webassembly - compiling module");
let module = compile(&buffer_source[..])?;
debug!("webassembly - instantiating");
2019-03-27 21:01:27 +00:00
let instance = module.instantiate(&import_object)?;
2019-01-19 06:28:41 +00:00
debug!("webassembly - instance created");
Ok(ResultObject {
module,
instance: Box::new(instance),
})
}
2018-10-24 10:36:43 +00:00
/// The webassembly::instantiate_streaming() function compiles and instantiates
/// 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>,
_import_object: ImportObject,
) -> Result<ResultObject> {
unimplemented!();
}
/// 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:
/// * `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
/// webassembly::CompileError.
pub fn compile(buffer_source: &[u8]) -> Result<Module> {
let module = runtime::compile(buffer_source)?;
Ok(module)
}