From abeec7ab7fcfc5b3c85cf6dfe4b6e3a6f2b1de45 Mon Sep 17 00:00:00 2001 From: Xin Liu Date: Wed, 31 May 2023 13:42:23 +0000 Subject: [PATCH] chore(rust-sdk): update rustdoc Signed-off-by: Xin Liu --- src/lib.rs | 65 ++++++++++++++++++++++------------------- src/vm.rs | 86 ++++++++++++++++++++++++++++-------------------------- 2 files changed, 79 insertions(+), 72 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index f6305ccac..24136b1d1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -35,6 +35,7 @@ //! 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 @@ -42,6 +43,7 @@ //! //! // 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(_caller: Caller, _args: Vec, _data: Option<&mut T>) -> Result, HostFuncError> { //! println!("Hello, world!"); @@ -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(()) //! } diff --git a/src/vm.rs b/src/vm.rs index 48adbba64..5e0e0a1f9 100644 --- a/src/vm.rs +++ b/src/vm.rs @@ -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> { -/// // 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(()) /// }