Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify lib.rs doc example #1330

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
simplify lib doc example
Robbepop committed Dec 4, 2024
commit a1e6ab43a3b477bcd01db1e78eddeb4a407175c8
48 changes: 22 additions & 26 deletions crates/wasmi/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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)))
@@ -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(())
//! }
//! ```