Fix idents
This commit is contained in:
parent
bc89a20b96
commit
cc24d8a77a
119
src/host.rs
119
src/host.rs
|
@ -45,31 +45,30 @@ impl<'a> RuntimeArgs<'a> {
|
||||||
///
|
///
|
||||||
/// #[derive(Debug)]
|
/// #[derive(Debug)]
|
||||||
/// struct MyError {
|
/// struct MyError {
|
||||||
/// code: u32,
|
/// code: u32,
|
||||||
/// }
|
/// }
|
||||||
///
|
///
|
||||||
/// impl fmt::Display for MyError {
|
/// impl fmt::Display for MyError {
|
||||||
/// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
/// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
/// write!(f, "MyError, code={}", self.code)
|
/// write!(f, "MyError, code={}", self.code)
|
||||||
/// }
|
/// }
|
||||||
/// }
|
/// }
|
||||||
///
|
///
|
||||||
/// impl HostError for MyError { }
|
/// impl HostError for MyError { }
|
||||||
///
|
///
|
||||||
/// fn failable_fn() -> Result<(), Error> {
|
/// fn failable_fn() -> Result<(), Error> {
|
||||||
/// let my_error = MyError { code: 1312 };
|
/// let my_error = MyError { code: 1312 };
|
||||||
/// Err(Error::Host(Box::new(my_error)))
|
/// Err(Error::Host(Box::new(my_error)))
|
||||||
/// }
|
/// }
|
||||||
///
|
///
|
||||||
/// match failable_fn() {
|
/// match failable_fn() {
|
||||||
/// Err(Error::Host(host_error)) => {
|
/// Err(Error::Host(host_error)) => {
|
||||||
/// let my_error = host_error.downcast_ref::<MyError>().unwrap();
|
/// let my_error = host_error.downcast_ref::<MyError>().unwrap();
|
||||||
/// assert_eq!(my_error.code, 1312);
|
/// assert_eq!(my_error.code, 1312);
|
||||||
/// }
|
/// }
|
||||||
/// _ => panic!(),
|
/// _ => panic!(),
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
///
|
|
||||||
pub trait HostError: 'static + ::std::fmt::Display + ::std::fmt::Debug + Send + Sync {
|
pub trait HostError: 'static + ::std::fmt::Display + ::std::fmt::Debug + Send + Sync {
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
fn __private_get_type_id__(&self) -> TypeId {
|
fn __private_get_type_id__(&self) -> TypeId {
|
||||||
|
@ -104,69 +103,69 @@ impl HostError {
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// use wasmi::{
|
/// use wasmi::{
|
||||||
/// Externals, RuntimeValue, RuntimeArgs, Error, ModuleImportResolver,
|
/// Externals, RuntimeValue, RuntimeArgs, Error, ModuleImportResolver,
|
||||||
/// FuncRef, ValueType, Signature, FuncInstance
|
/// FuncRef, ValueType, Signature, FuncInstance
|
||||||
/// };
|
/// };
|
||||||
///
|
///
|
||||||
/// struct HostExternals {
|
/// struct HostExternals {
|
||||||
/// counter: usize,
|
/// counter: usize,
|
||||||
/// }
|
/// }
|
||||||
///
|
///
|
||||||
/// const ADD_FUNC_INDEX: usize = 0;
|
/// const ADD_FUNC_INDEX: usize = 0;
|
||||||
///
|
///
|
||||||
/// impl Externals for HostExternals {
|
/// impl Externals for HostExternals {
|
||||||
/// fn invoke_index(
|
/// fn invoke_index(
|
||||||
/// &mut self,
|
/// &mut self,
|
||||||
/// index: usize,
|
/// index: usize,
|
||||||
/// args: RuntimeArgs,
|
/// args: RuntimeArgs,
|
||||||
/// ) -> Result<Option<RuntimeValue>, Error> {
|
/// ) -> Result<Option<RuntimeValue>, Error> {
|
||||||
/// match index {
|
/// match index {
|
||||||
/// ADD_FUNC_INDEX => {
|
/// ADD_FUNC_INDEX => {
|
||||||
/// let a: u32 = args.nth(0).unwrap();
|
/// let a: u32 = args.nth(0).unwrap();
|
||||||
/// let b: u32 = args.nth(1).unwrap();
|
/// let b: u32 = args.nth(1).unwrap();
|
||||||
/// let result = a + b;
|
/// let result = a + b;
|
||||||
///
|
///
|
||||||
/// Ok(Some(RuntimeValue::I32(result as i32)))
|
/// Ok(Some(RuntimeValue::I32(result as i32)))
|
||||||
/// }
|
/// }
|
||||||
/// _ => panic!("Unimplemented function at {}", index),
|
/// _ => panic!("Unimplemented function at {}", index),
|
||||||
/// }
|
/// }
|
||||||
/// }
|
/// }
|
||||||
/// }
|
/// }
|
||||||
///
|
///
|
||||||
/// impl HostExternals {
|
/// impl HostExternals {
|
||||||
/// fn check_signature(
|
/// fn check_signature(
|
||||||
/// &self,
|
/// &self,
|
||||||
/// index: usize,
|
/// index: usize,
|
||||||
/// signature: &Signature
|
/// signature: &Signature
|
||||||
/// ) -> bool {
|
/// ) -> bool {
|
||||||
/// let (params, ret_ty): (&[ValueType], Option<ValueType>) = match index {
|
/// let (params, ret_ty): (&[ValueType], Option<ValueType>) = match index {
|
||||||
/// ADD_FUNC_INDEX => (&[ValueType::I32, ValueType::I32], Some(ValueType::I32)),
|
/// ADD_FUNC_INDEX => (&[ValueType::I32, ValueType::I32], Some(ValueType::I32)),
|
||||||
/// _ => return false,
|
/// _ => return false,
|
||||||
/// };
|
/// };
|
||||||
/// signature.params() == params && signature.return_type() == ret_ty
|
/// signature.params() == params && signature.return_type() == ret_ty
|
||||||
/// }
|
/// }
|
||||||
/// }
|
/// }
|
||||||
///
|
///
|
||||||
/// impl ModuleImportResolver for HostExternals {
|
/// impl ModuleImportResolver for HostExternals {
|
||||||
/// fn resolve_func(
|
/// fn resolve_func(
|
||||||
/// &self,
|
/// &self,
|
||||||
/// field_name: &str,
|
/// field_name: &str,
|
||||||
/// signature: &Signature
|
/// signature: &Signature
|
||||||
/// ) -> Result<FuncRef, Error> {
|
/// ) -> Result<FuncRef, Error> {
|
||||||
/// let index = match field_name {
|
/// let index = match field_name {
|
||||||
/// "add" => ADD_FUNC_INDEX,
|
/// "add" => ADD_FUNC_INDEX,
|
||||||
/// _ => {
|
/// _ => {
|
||||||
/// return Err(Error::Instantiation(
|
/// return Err(Error::Instantiation(
|
||||||
/// format!("Export {} not found", field_name),
|
/// format!("Export {} not found", field_name),
|
||||||
/// ))
|
/// ))
|
||||||
/// }
|
/// }
|
||||||
/// };
|
/// };
|
||||||
///
|
///
|
||||||
/// Ok(FuncInstance::alloc_host(
|
/// Ok(FuncInstance::alloc_host(
|
||||||
/// Signature::new(&[ValueType::I32, ValueType::I32][..], Some(ValueType::I32)),
|
/// Signature::new(&[ValueType::I32, ValueType::I32][..], Some(ValueType::I32)),
|
||||||
/// ADD_FUNC_INDEX,
|
/// ADD_FUNC_INDEX,
|
||||||
/// ))
|
/// ))
|
||||||
/// }
|
/// }
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
pub trait Externals {
|
pub trait Externals {
|
||||||
|
|
|
@ -89,9 +89,9 @@ pub trait ImportResolver {
|
||||||
/// # let other_instance = ModuleInstance::new(&module, &ImportsBuilder::default())?.assert_no_start();
|
/// # let other_instance = ModuleInstance::new(&module, &ImportsBuilder::default())?.assert_no_start();
|
||||||
///
|
///
|
||||||
/// let imports = ImportsBuilder::new()
|
/// let imports = ImportsBuilder::new()
|
||||||
/// .with_resolver("env", &EnvModuleResolver)
|
/// .with_resolver("env", &EnvModuleResolver)
|
||||||
/// // Note, that ModuleInstance can be a resolver too.
|
/// // Note, that ModuleInstance can be a resolver too.
|
||||||
/// .with_resolver("other_instance", &other_instance);
|
/// .with_resolver("other_instance", &other_instance);
|
||||||
/// let instance = ModuleInstance::new(&module, &imports)?.assert_no_start();
|
/// let instance = ModuleInstance::new(&module, &imports)?.assert_no_start();
|
||||||
///
|
///
|
||||||
/// # Ok(())
|
/// # Ok(())
|
||||||
|
|
69
src/lib.rs
69
src/lib.rs
|
@ -54,46 +54,45 @@
|
||||||
//! use wasmi::{ModuleInstance, ImportsBuilder, NopExternals, RuntimeValue};
|
//! use wasmi::{ModuleInstance, ImportsBuilder, NopExternals, RuntimeValue};
|
||||||
//!
|
//!
|
||||||
//! fn main() {
|
//! fn main() {
|
||||||
//! // Parse WAT (WebAssembly Text format) into wasm bytecode.
|
//! // Parse WAT (WebAssembly Text format) into wasm bytecode.
|
||||||
//! let wasm_binary: Vec<u8> =
|
//! let wasm_binary: Vec<u8> =
|
||||||
//! wabt::wat2wasm(
|
//! wabt::wat2wasm(
|
||||||
//! r#"
|
//! r#"
|
||||||
//! (module
|
//! (module
|
||||||
//! (func (export "test") (result i32)
|
//! (func (export "test") (result i32)
|
||||||
//! i32.const 1337
|
//! i32.const 1337
|
||||||
//! )
|
//! )
|
||||||
//! )
|
//! )
|
||||||
//! "#,
|
//! "#,
|
||||||
//! )
|
//! )
|
||||||
//! .expect("failed to parse wat");
|
//! .expect("failed to parse wat");
|
||||||
//!
|
//!
|
||||||
//! // Load wasm binary and prepare it for instantiation.
|
//! // Load wasm binary and prepare it for instantiation.
|
||||||
//! let module = wasmi::load_from_buffer(&wasm_binary)
|
//! let module = wasmi::load_from_buffer(&wasm_binary)
|
||||||
//! .expect("failed to load wasm");
|
//! .expect("failed to load wasm");
|
||||||
//!
|
//!
|
||||||
//! // Instantiate a module with empty imports and
|
//! // Instantiate a module with empty imports and
|
||||||
//! // asserting that there is no `start` function.
|
//! // asserting that there is no `start` function.
|
||||||
//! let instance =
|
//! let instance =
|
||||||
//! ModuleInstance::new(
|
//! ModuleInstance::new(
|
||||||
//! &module,
|
//! &module,
|
||||||
//! &ImportsBuilder::default()
|
//! &ImportsBuilder::default()
|
||||||
//! )
|
//! )
|
||||||
//! .expect("failed to instantiate wasm module")
|
//! .expect("failed to instantiate wasm module")
|
||||||
//! .assert_no_start();
|
//! .assert_no_start();
|
||||||
//!
|
//!
|
||||||
//! // Finally, invoke exported function "test" with no parameters
|
//! // Finally, invoke exported function "test" with no parameters
|
||||||
//! // and empty external function executor.
|
//! // and empty external function executor.
|
||||||
//! assert_eq!(
|
//! assert_eq!(
|
||||||
//! instance.invoke_export(
|
//! instance.invoke_export(
|
||||||
//! "test",
|
//! "test",
|
||||||
//! &[],
|
//! &[],
|
||||||
//! &mut NopExternals,
|
//! &mut NopExternals,
|
||||||
//! ).expect("failed to execute export"),
|
//! ).expect("failed to execute export"),
|
||||||
//! Some(RuntimeValue::I32(1337)),
|
//! Some(RuntimeValue::I32(1337)),
|
||||||
//! );
|
//! );
|
||||||
//! }
|
//! }
|
||||||
//! ```
|
//! ```
|
||||||
//!
|
|
||||||
|
|
||||||
// TODO(pepyakin): Fix this asap https://github.com/pepyakin/wasmi/issues/3
|
// TODO(pepyakin): Fix this asap https://github.com/pepyakin/wasmi/issues/3
|
||||||
#![allow(missing_docs)]
|
#![allow(missing_docs)]
|
||||||
|
|
|
@ -13,6 +13,11 @@ use host::Externals;
|
||||||
use common::{DEFAULT_MEMORY_INDEX, DEFAULT_TABLE_INDEX};
|
use common::{DEFAULT_MEMORY_INDEX, DEFAULT_TABLE_INDEX};
|
||||||
use types::{GlobalDescriptor, TableDescriptor, MemoryDescriptor};
|
use types::{GlobalDescriptor, TableDescriptor, MemoryDescriptor};
|
||||||
|
|
||||||
|
/// Reference to a [`ModuleInstance`].
|
||||||
|
///
|
||||||
|
/// This reference has a reference-counting semantics.
|
||||||
|
///
|
||||||
|
/// [`ModuleInstance`]: struct.ModuleInstance.html
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct ModuleRef(pub(crate) Rc<ModuleInstance>);
|
pub struct ModuleRef(pub(crate) Rc<ModuleInstance>);
|
||||||
|
|
||||||
|
@ -421,9 +426,9 @@ impl ModuleInstance {
|
||||||
///
|
///
|
||||||
/// // ModuleInstance::new returns instance which `start` function isn't called.
|
/// // ModuleInstance::new returns instance which `start` function isn't called.
|
||||||
/// let not_started = ModuleInstance::new(
|
/// let not_started = ModuleInstance::new(
|
||||||
/// &module,
|
/// &module,
|
||||||
/// &ImportsBuilder::default()
|
/// &ImportsBuilder::default()
|
||||||
/// )?;
|
/// )?;
|
||||||
/// // Call `start` function if any.
|
/// // Call `start` function if any.
|
||||||
/// let instance = not_started.run_start(&mut NopExternals)?;
|
/// let instance = not_started.run_start(&mut NopExternals)?;
|
||||||
///
|
///
|
||||||
|
@ -441,9 +446,9 @@ impl ModuleInstance {
|
||||||
///
|
///
|
||||||
/// // This will panic if the module actually contain `start` function.
|
/// // This will panic if the module actually contain `start` function.
|
||||||
/// let not_started = ModuleInstance::new(
|
/// let not_started = ModuleInstance::new(
|
||||||
/// &module,
|
/// &module,
|
||||||
/// &ImportsBuilder::default()
|
/// &ImportsBuilder::default()
|
||||||
/// )?.assert_no_start();
|
/// )?.assert_no_start();
|
||||||
///
|
///
|
||||||
/// # Ok(())
|
/// # Ok(())
|
||||||
/// # }
|
/// # }
|
||||||
|
@ -520,30 +525,30 @@ impl ModuleInstance {
|
||||||
/// # extern crate wabt;
|
/// # extern crate wabt;
|
||||||
/// # use wasmi::{ModuleInstance, ImportsBuilder, NopExternals, RuntimeValue};
|
/// # use wasmi::{ModuleInstance, ImportsBuilder, NopExternals, RuntimeValue};
|
||||||
/// # fn main() {
|
/// # fn main() {
|
||||||
/// # let wasm_binary: Vec<u8> = wabt::wat2wasm(
|
/// # let wasm_binary: Vec<u8> = wabt::wat2wasm(
|
||||||
/// # r#"
|
/// # r#"
|
||||||
/// # (module
|
/// # (module
|
||||||
/// # (func (export "add") (param i32 i32) (result i32)
|
/// # (func (export "add") (param i32 i32) (result i32)
|
||||||
/// # get_local 0
|
/// # get_local 0
|
||||||
/// # get_local 1
|
/// # get_local 1
|
||||||
/// # i32.add
|
/// # i32.add
|
||||||
/// # )
|
/// # )
|
||||||
/// # )
|
/// # )
|
||||||
/// # "#,
|
/// # "#,
|
||||||
/// # ).expect("failed to parse wat");
|
/// # ).expect("failed to parse wat");
|
||||||
/// # let module = wasmi::load_from_buffer(&wasm_binary).expect("failed to load wasm");
|
/// # let module = wasmi::load_from_buffer(&wasm_binary).expect("failed to load wasm");
|
||||||
/// # let instance = ModuleInstance::new(
|
/// # let instance = ModuleInstance::new(
|
||||||
/// # &module,
|
/// # &module,
|
||||||
/// # &ImportsBuilder::default()
|
/// # &ImportsBuilder::default()
|
||||||
/// # ).expect("failed to instantiate wasm module").assert_no_start();
|
/// # ).expect("failed to instantiate wasm module").assert_no_start();
|
||||||
/// assert_eq!(
|
/// assert_eq!(
|
||||||
/// instance.invoke_export(
|
/// instance.invoke_export(
|
||||||
/// "add",
|
/// "add",
|
||||||
/// &[RuntimeValue::I32(5), RuntimeValue::I32(3)],
|
/// &[RuntimeValue::I32(5), RuntimeValue::I32(3)],
|
||||||
/// &mut NopExternals,
|
/// &mut NopExternals,
|
||||||
/// ).expect("failed to execute export"),
|
/// ).expect("failed to execute export"),
|
||||||
/// Some(RuntimeValue::I32(8)),
|
/// Some(RuntimeValue::I32(8)),
|
||||||
/// );
|
/// );
|
||||||
/// # }
|
/// # }
|
||||||
/// ```
|
/// ```
|
||||||
pub fn invoke_export<E: Externals>(
|
pub fn invoke_export<E: Externals>(
|
||||||
|
|
Loading…
Reference in New Issue