From cc24d8a77a799c7f4863313d8b9fdc5433a15e37 Mon Sep 17 00:00:00 2001 From: Sergey Pepyakin Date: Thu, 25 Jan 2018 19:56:52 +0300 Subject: [PATCH] Fix idents --- src/host.rs | 119 ++++++++++++++++++++++++------------------------- src/imports.rs | 6 +-- src/lib.rs | 69 ++++++++++++++-------------- src/module.rs | 59 +++++++++++++----------- 4 files changed, 128 insertions(+), 125 deletions(-) diff --git a/src/host.rs b/src/host.rs index 590f7bc..a58abdb 100644 --- a/src/host.rs +++ b/src/host.rs @@ -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::().unwrap(); -/// assert_eq!(my_error.code, 1312); -/// } -/// _ => panic!(), +/// Err(Error::Host(host_error)) => { +/// let my_error = host_error.downcast_ref::().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, 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, 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) = 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) = 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 { -/// 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 { +/// 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 { diff --git a/src/imports.rs b/src/imports.rs index 0252967..1028bc4 100644 --- a/src/imports.rs +++ b/src/imports.rs @@ -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(()) diff --git a/src/lib.rs b/src/lib.rs index 93d7fbc..887a8ef 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -54,46 +54,45 @@ //! use wasmi::{ModuleInstance, ImportsBuilder, NopExternals, RuntimeValue}; //! //! fn main() { -//! // Parse WAT (WebAssembly Text format) into wasm bytecode. -//! let wasm_binary: Vec = -//! 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 = +//! 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)] diff --git a/src/module.rs b/src/module.rs index 1dc1641..f2904b6 100644 --- a/src/module.rs +++ b/src/module.rs @@ -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); @@ -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 = 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 = 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(