Skip to content

Commit

Permalink
Simplify lib.rs doc example (#1330)
Browse files Browse the repository at this point in the history
simplify lib doc example
  • Loading branch information
Robbepop authored Dec 4, 2024
1 parent a400e41 commit ef3d1c8
Showing 1 changed file with 22 additions and 26 deletions.
48 changes: 22 additions & 26 deletions crates/wasmi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,11 @@
//! [Wasmtime's API example](https://docs.rs/wasmtime/0.39.1/wasmtime/).
//!
//! ```
//! use anyhow::Result;
//! use wasmi::*;
//! use wat;
//!
//! fn main() -> Result<()> {
//! // First step is to create the Wasm execution engine with some config.
//! // In this example we are using the default configuration.
//! let engine = Engine::default();
//! // In this simple example we are going to compile the below Wasm source,
//! // instantiate a Wasm module from it and call its exported "hello" function.
//! fn main() -> anyhow::Result<()> {
//! let wasm = r#"
//! (module
//! (import "host" "hello" (func $host_hello (param i32)))
Expand All @@ -30,35 +27,34 @@
//! )
//! )
//! "#;
//! // First step is to create the Wasm execution engine with some config.
//! //
//! // In this example we are using the default configuration.
//! let engine = Engine::default();
//! // Now we can compile the above Wasm module with the given Wasm source.
//! let module = Module::new(&engine, wasm)?;
//!
//! // All Wasm objects operate within the context of a `Store`.
//! // Each `Store` has a type parameter to store host-specific data,
//! // which in this case we are using `42` for.
//! // Wasm objects operate within the context of a Wasm `Store`.
//! //
//! // Each `Store` has a type parameter to store host specific data.
//! // In this example the host state is a simple `u32` type with value `42`.
//! type HostState = u32;
//! let mut store = Store::new(&engine, 42);
//! let host_hello = Func::wrap(&mut store, |caller: Caller<'_, HostState>, param: i32| {
//! println!("Got {param} from WebAssembly");
//! println!("My host state is: {}", caller.data());
//! });
//!
//! // In order to create Wasm module instances and link their imports
//! // and exports we require a `Linker`.
//! // A linker can be used to instantiate Wasm modules.
//! // The job of a linker is to satisfy the Wasm module's imports.
//! let mut linker = <Linker<HostState>>::new(&engine);
//! // Instantiation of a Wasm module requires defining its imports and then
//! // afterwards we can fetch exports by name, as well as asserting the
//! // type signature of the function with `get_typed_func`.
//! //
//! // Also before using an instance created this way we need to start it.
//! linker.define("host", "hello", host_hello)?;
//! // We are required to define all imports before instantiating a Wasm module.
//! linker.func_wrap("host", "hello", |caller: Caller<'_, HostState>, param: i32| {
//! println!("Got {param} from WebAssembly and my host state is: {}", caller.data());
//! });
//! let instance = linker
//! .instantiate(&mut store, &module)?
//! .start(&mut store)?;
//! let hello = instance.get_typed_func::<(), ()>(&store, "hello")?;
//!
//! // And finally we can call the wasm!
//! hello.call(&mut store, ())?;
//!
//! // Now we can finally query the exported "hello" function and call it.
//! instance
//! .get_typed_func::<(), ()>(&store, "hello")?
//! .call(&mut store, ())?;
//! Ok(())
//! }
//! ```
Expand Down

0 comments on commit ef3d1c8

Please sign in to comment.