Skip to content

Commit

Permalink
chore(rust-sdk): update rustdoc
Browse files Browse the repository at this point in the history
Signed-off-by: Xin Liu <[email protected]>
  • Loading branch information
apepkuss committed May 31, 2023
1 parent 96abca4 commit abeec7a
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 72 deletions.
65 changes: 35 additions & 30 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,15 @@
//! The example below is using `wasmedge-sdk` to run a WebAssembly module written with its WAT format (textual format). If you would like more examples, please refer to [Examples of WasmEdge RustSDK](https://github.com/second-state/wasmedge-rustsdk-examples).
//!
//! ```rust
//! #[cfg(not(feature = "async"))]
//! use wasmedge_sdk::{
//! error::HostFuncError, host_function, params, wat2wasm, Caller, ImportObjectBuilder, Module,
//! VmBuilder, WasmValue, NeverType
//! };
//!
//! // We define a function to act as our "env" "say_hello" function imported in the
//! // Wasm program above.
//! #[cfg(not(feature = "async"))]
//! #[host_function]
//! pub fn say_hello<T>(_caller: Caller, _args: Vec<WasmValue>, _data: Option<&mut T>) -> Result<Vec<WasmValue>, HostFuncError> {
//! println!("Hello, world!");
Expand All @@ -51,38 +53,41 @@
//!
//! #[cfg_attr(test, test)]
//! fn main() -> anyhow::Result<()> {
//! // create an import module
//! let import = ImportObjectBuilder::new()
//! .with_func::<(), (), NeverType>("say_hello", say_hello, None)?
//! .build("env")?;
//!
//! let wasm_bytes = wat2wasm(
//! br#"
//! (module
//! ;; First we define a type with no parameters and no results.
//! (type $no_args_no_rets_t (func (param) (result)))
//! #[cfg(not(feature = "async"))]
//! {
//! // create an import module
//! let import = ImportObjectBuilder::new()
//! .with_func::<(), (), NeverType>("say_hello", say_hello, None)?
//! .build("env")?;
//!
//! ;; Then we declare that we want to import a function named "env" "say_hello" with
//! ;; that type signature.
//! (import "env" "say_hello" (func $say_hello (type $no_args_no_rets_t)))
//! let wasm_bytes = wat2wasm(
//! br#"
//! (module
//! ;; First we define a type with no parameters and no results.
//! (type $no_args_no_rets_t (func (param) (result)))
//!
//! ;; Then we declare that we want to import a function named "env" "say_hello" with
//! ;; that type signature.
//! (import "env" "say_hello" (func $say_hello (type $no_args_no_rets_t)))
//!
//! ;; Finally we create an entrypoint that calls our imported function.
//! (func $run (type $no_args_no_rets_t)
//! (call $say_hello))
//! ;; And mark it as an exported function named "run".
//! (export "run" (func $run)))
//! "#,
//! )?;
//!
//! ;; Finally we create an entrypoint that calls our imported function.
//! (func $run (type $no_args_no_rets_t)
//! (call $say_hello))
//! ;; And mark it as an exported function named "run".
//! (export "run" (func $run)))
//! "#,
//! )?;
//!
//! // loads a wasm module from the given in-memory bytes
//! let module = Module::from_bytes(None, wasm_bytes)?;
//!
//! // create an executor
//! VmBuilder::new()
//! .build()?
//! .register_import_module(import)?
//! .register_module(Some("extern"), module)?
//! .run_func(Some("extern"), "run", params!())?;
//! // loads a wasm module from the given in-memory bytes
//! let module = Module::from_bytes(None, wasm_bytes)?;
//!
//! // create an executor
//! VmBuilder::new()
//! .build()?
//! .register_import_module(import)?
//! .register_module(Some("extern"), module)?
//! .run_func(Some("extern"), "run", params!())?;
//! }
//!
//! Ok(())
//! }
Expand Down
86 changes: 44 additions & 42 deletions src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,55 +244,57 @@ impl VmBuilder {
/// ```rust
/// // If the version of rust used is less than v1.63, please uncomment the follow attribute.
/// // #![feature(explicit_generic_args_with_impl_trait)]
///
/// use wasmedge_sdk::{params, VmBuilder, WasmVal};
/// use wasmedge_types::{wat2wasm, ValType};
/// #[cfg(not(feature = "async"))]
/// use wasmedge_sdk::{params, VmBuilder, WasmVal, wat2wasm, ValType};
///
/// #[cfg_attr(test, test)]
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
/// // create a Vm context
/// let vm = VmBuilder::new().build()?;
/// #[cfg(not(feature = "async"))]
/// {
/// // create a Vm context
/// let vm = VmBuilder::new().build()?;
///
/// // register a wasm module from the given in-memory wasm bytes
/// let wasm_bytes = wat2wasm(
/// br#"(module
/// (export "fib" (func $fib))
/// (func $fib (param $n i32) (result i32)
/// (if
/// (i32.lt_s
/// (get_local $n)
/// (i32.const 2)
/// )
/// (return
/// (i32.const 1)
/// )
/// )
/// (return
/// (i32.add
/// (call $fib
/// (i32.sub
/// (get_local $n)
/// (i32.const 2)
/// )
/// )
/// (call $fib
/// (i32.sub
/// (get_local $n)
/// (i32.const 1)
/// // register a wasm module from the given in-memory wasm bytes
/// let wasm_bytes = wat2wasm(
/// br#"(module
/// (export "fib" (func $fib))
/// (func $fib (param $n i32) (result i32)
/// (if
/// (i32.lt_s
/// (get_local $n)
/// (i32.const 2)
/// )
/// (return
/// (i32.const 1)
/// )
/// )
/// (return
/// (i32.add
/// (call $fib
/// (i32.sub
/// (get_local $n)
/// (i32.const 2)
/// )
/// )
/// (call $fib
/// (i32.sub
/// (get_local $n)
/// (i32.const 1)
/// )
/// )
/// )
/// )
/// )
/// )
/// )
/// )
/// )
/// )
/// "#,
/// )?;
/// let mut vm = vm.register_module_from_bytes("extern", wasm_bytes)?;
/// "#,
/// )?;
/// let mut vm = vm.register_module_from_bytes("extern", wasm_bytes)?;
///
/// // run `fib` function in the named module instance
/// let returns = vm.run_func(Some("extern"), "fib", params!(10))?;
/// assert_eq!(returns.len(), 1);
/// assert_eq!(returns[0].to_i32(), 89);
/// // run `fib` function in the named module instance
/// let returns = vm.run_func(Some("extern"), "fib", params!(10))?;
/// assert_eq!(returns.len(), 1);
/// assert_eq!(returns[0].to_i32(), 89);
/// }
///
/// Ok(())
/// }
Expand Down

0 comments on commit abeec7a

Please sign in to comment.