Fix idents

This commit is contained in:
Sergey Pepyakin 2018-01-25 19:56:52 +03:00
parent bc89a20b96
commit cc24d8a77a
4 changed files with 128 additions and 125 deletions

View File

@ -45,31 +45,30 @@ impl<'a> RuntimeArgs<'a> {
///
/// #[derive(Debug)]
/// struct MyError {
/// code: u32,
/// code: u32,
/// }
///
/// impl fmt::Display for MyError {
/// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
/// write!(f, "MyError, code={}", self.code)
/// }
/// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
/// write!(f, "MyError, code={}", self.code)
/// }
/// }
///
/// impl HostError for MyError { }
///
/// fn failable_fn() -> Result<(), Error> {
/// let my_error = MyError { code: 1312 };
/// Err(Error::Host(Box::new(my_error)))
/// let my_error = MyError { code: 1312 };
/// Err(Error::Host(Box::new(my_error)))
/// }
///
/// match failable_fn() {
/// Err(Error::Host(host_error)) => {
/// let my_error = host_error.downcast_ref::<MyError>().unwrap();
/// assert_eq!(my_error.code, 1312);
/// }
/// _ => panic!(),
/// Err(Error::Host(host_error)) => {
/// let my_error = host_error.downcast_ref::<MyError>().unwrap();
/// assert_eq!(my_error.code, 1312);
/// }
/// _ => panic!(),
/// }
/// ```
///
pub trait HostError: 'static + ::std::fmt::Display + ::std::fmt::Debug + Send + Sync {
#[doc(hidden)]
fn __private_get_type_id__(&self) -> TypeId {
@ -104,69 +103,69 @@ impl HostError {
///
/// ```rust
/// use wasmi::{
/// Externals, RuntimeValue, RuntimeArgs, Error, ModuleImportResolver,
/// FuncRef, ValueType, Signature, FuncInstance
/// };
/// Externals, RuntimeValue, RuntimeArgs, Error, ModuleImportResolver,
/// FuncRef, ValueType, Signature, FuncInstance
/// };
///
/// struct HostExternals {
/// counter: usize,
/// counter: usize,
/// }
///
/// const ADD_FUNC_INDEX: usize = 0;
///
/// impl Externals for HostExternals {
/// fn invoke_index(
/// &mut self,
/// index: usize,
/// args: RuntimeArgs,
/// ) -> Result<Option<RuntimeValue>, Error> {
/// match index {
/// ADD_FUNC_INDEX => {
/// let a: u32 = args.nth(0).unwrap();
/// let b: u32 = args.nth(1).unwrap();
/// let result = a + b;
/// fn invoke_index(
/// &mut self,
/// index: usize,
/// args: RuntimeArgs,
/// ) -> Result<Option<RuntimeValue>, Error> {
/// match index {
/// ADD_FUNC_INDEX => {
/// let a: u32 = args.nth(0).unwrap();
/// let b: u32 = args.nth(1).unwrap();
/// let result = a + b;
///
/// Ok(Some(RuntimeValue::I32(result as i32)))
/// }
/// _ => panic!("Unimplemented function at {}", index),
/// }
/// }
/// Ok(Some(RuntimeValue::I32(result as i32)))
/// }
/// _ => panic!("Unimplemented function at {}", index),
/// }
/// }
/// }
///
/// impl HostExternals {
/// fn check_signature(
/// &self,
/// index: usize,
/// signature: &Signature
/// ) -> bool {
/// let (params, ret_ty): (&[ValueType], Option<ValueType>) = match index {
/// ADD_FUNC_INDEX => (&[ValueType::I32, ValueType::I32], Some(ValueType::I32)),
/// _ => return false,
/// };
/// signature.params() == params && signature.return_type() == ret_ty
/// }
/// fn check_signature(
/// &self,
/// index: usize,
/// signature: &Signature
/// ) -> bool {
/// let (params, ret_ty): (&[ValueType], Option<ValueType>) = match index {
/// ADD_FUNC_INDEX => (&[ValueType::I32, ValueType::I32], Some(ValueType::I32)),
/// _ => return false,
/// };
/// signature.params() == params && signature.return_type() == ret_ty
/// }
/// }
///
/// impl ModuleImportResolver for HostExternals {
/// fn resolve_func(
/// &self,
/// field_name: &str,
/// signature: &Signature
/// ) -> Result<FuncRef, Error> {
/// let index = match field_name {
/// "add" => ADD_FUNC_INDEX,
/// _ => {
/// return Err(Error::Instantiation(
/// format!("Export {} not found", field_name),
/// ))
/// }
/// };
/// fn resolve_func(
/// &self,
/// field_name: &str,
/// signature: &Signature
/// ) -> Result<FuncRef, Error> {
/// let index = match field_name {
/// "add" => ADD_FUNC_INDEX,
/// _ => {
/// return Err(Error::Instantiation(
/// format!("Export {} not found", field_name),
/// ))
/// }
/// };
///
/// Ok(FuncInstance::alloc_host(
/// Signature::new(&[ValueType::I32, ValueType::I32][..], Some(ValueType::I32)),
/// ADD_FUNC_INDEX,
/// ))
/// }
/// Ok(FuncInstance::alloc_host(
/// Signature::new(&[ValueType::I32, ValueType::I32][..], Some(ValueType::I32)),
/// ADD_FUNC_INDEX,
/// ))
/// }
/// }
/// ```
pub trait Externals {

View File

@ -89,9 +89,9 @@ pub trait ImportResolver {
/// # let other_instance = ModuleInstance::new(&module, &ImportsBuilder::default())?.assert_no_start();
///
/// let imports = ImportsBuilder::new()
/// .with_resolver("env", &EnvModuleResolver)
/// // Note, that ModuleInstance can be a resolver too.
/// .with_resolver("other_instance", &other_instance);
/// .with_resolver("env", &EnvModuleResolver)
/// // Note, that ModuleInstance can be a resolver too.
/// .with_resolver("other_instance", &other_instance);
/// let instance = ModuleInstance::new(&module, &imports)?.assert_no_start();
///
/// # Ok(())

View File

@ -54,46 +54,45 @@
//! use wasmi::{ModuleInstance, ImportsBuilder, NopExternals, RuntimeValue};
//!
//! fn main() {
//! // Parse WAT (WebAssembly Text format) into wasm bytecode.
//! let wasm_binary: Vec<u8> =
//! wabt::wat2wasm(
//! r#"
//! (module
//! (func (export "test") (result i32)
//! i32.const 1337
//! )
//! )
//! "#,
//! )
//! .expect("failed to parse wat");
//! // Parse WAT (WebAssembly Text format) into wasm bytecode.
//! let wasm_binary: Vec<u8> =
//! wabt::wat2wasm(
//! r#"
//! (module
//! (func (export "test") (result i32)
//! i32.const 1337
//! )
//! )
//! "#,
//! )
//! .expect("failed to parse wat");
//!
//! // Load wasm binary and prepare it for instantiation.
//! let module = wasmi::load_from_buffer(&wasm_binary)
//! .expect("failed to load wasm");
//! // Load wasm binary and prepare it for instantiation.
//! let module = wasmi::load_from_buffer(&wasm_binary)
//! .expect("failed to load wasm");
//!
//! // Instantiate a module with empty imports and
//! // asserting that there is no `start` function.
//! let instance =
//! ModuleInstance::new(
//! &module,
//! &ImportsBuilder::default()
//! )
//! .expect("failed to instantiate wasm module")
//! .assert_no_start();
//! // Instantiate a module with empty imports and
//! // asserting that there is no `start` function.
//! let instance =
//! ModuleInstance::new(
//! &module,
//! &ImportsBuilder::default()
//! )
//! .expect("failed to instantiate wasm module")
//! .assert_no_start();
//!
//! // Finally, invoke exported function "test" with no parameters
//! // and empty external function executor.
//! assert_eq!(
//! instance.invoke_export(
//! "test",
//! &[],
//! &mut NopExternals,
//! ).expect("failed to execute export"),
//! Some(RuntimeValue::I32(1337)),
//! );
//! // Finally, invoke exported function "test" with no parameters
//! // and empty external function executor.
//! assert_eq!(
//! instance.invoke_export(
//! "test",
//! &[],
//! &mut NopExternals,
//! ).expect("failed to execute export"),
//! Some(RuntimeValue::I32(1337)),
//! );
//! }
//! ```
//!
// TODO(pepyakin): Fix this asap https://github.com/pepyakin/wasmi/issues/3
#![allow(missing_docs)]

View File

@ -13,6 +13,11 @@ use host::Externals;
use common::{DEFAULT_MEMORY_INDEX, DEFAULT_TABLE_INDEX};
use types::{GlobalDescriptor, TableDescriptor, MemoryDescriptor};
/// Reference to a [`ModuleInstance`].
///
/// This reference has a reference-counting semantics.
///
/// [`ModuleInstance`]: struct.ModuleInstance.html
#[derive(Clone, Debug)]
pub struct ModuleRef(pub(crate) Rc<ModuleInstance>);
@ -421,9 +426,9 @@ impl ModuleInstance {
///
/// // ModuleInstance::new returns instance which `start` function isn't called.
/// let not_started = ModuleInstance::new(
/// &module,
/// &ImportsBuilder::default()
/// )?;
/// &module,
/// &ImportsBuilder::default()
/// )?;
/// // Call `start` function if any.
/// let instance = not_started.run_start(&mut NopExternals)?;
///
@ -441,9 +446,9 @@ impl ModuleInstance {
///
/// // This will panic if the module actually contain `start` function.
/// let not_started = ModuleInstance::new(
/// &module,
/// &ImportsBuilder::default()
/// )?.assert_no_start();
/// &module,
/// &ImportsBuilder::default()
/// )?.assert_no_start();
///
/// # Ok(())
/// # }
@ -520,30 +525,30 @@ impl ModuleInstance {
/// # extern crate wabt;
/// # use wasmi::{ModuleInstance, ImportsBuilder, NopExternals, RuntimeValue};
/// # fn main() {
/// # let wasm_binary: Vec<u8> = wabt::wat2wasm(
/// # r#"
/// # (module
/// # (func (export "add") (param i32 i32) (result i32)
/// # get_local 0
/// # get_local 1
/// # i32.add
/// # )
/// # )
/// # "#,
/// # ).expect("failed to parse wat");
/// # let wasm_binary: Vec<u8> = wabt::wat2wasm(
/// # r#"
/// # (module
/// # (func (export "add") (param i32 i32) (result i32)
/// # get_local 0
/// # get_local 1
/// # i32.add
/// # )
/// # )
/// # "#,
/// # ).expect("failed to parse wat");
/// # let module = wasmi::load_from_buffer(&wasm_binary).expect("failed to load wasm");
/// # let instance = ModuleInstance::new(
/// # &module,
/// # &ImportsBuilder::default()
/// # ).expect("failed to instantiate wasm module").assert_no_start();
/// # &module,
/// # &ImportsBuilder::default()
/// # ).expect("failed to instantiate wasm module").assert_no_start();
/// assert_eq!(
/// instance.invoke_export(
/// "add",
/// &[RuntimeValue::I32(5), RuntimeValue::I32(3)],
/// &mut NopExternals,
/// ).expect("failed to execute export"),
/// Some(RuntimeValue::I32(8)),
/// );
/// instance.invoke_export(
/// "add",
/// &[RuntimeValue::I32(5), RuntimeValue::I32(3)],
/// &mut NopExternals,
/// ).expect("failed to execute export"),
/// Some(RuntimeValue::I32(8)),
/// );
/// # }
/// ```
pub fn invoke_export<E: Externals>(