wasmtime::component::bindgen_examples

Module _0_hello_world

Source
Available on crate feature runtime and crate feature component-model and docsrs only.
Expand description

A “hello world” style example.

This example loads a component which has access to a single host function. The exported function is called on an instantiation of the component.

use wasmtime::component::*;
use wasmtime::{Engine, Store};

bindgen!({
  inline: r#"
      package my:project;
      world hello-world {
          import name: func() -> string;
          export greet: func();
      }
  "#,
});

struct MyState {
    name: String,
}

// Imports into the world, like the `name` import for this world, are
// satisfied through traits.
impl HelloWorldImports for MyState {
    fn name(&mut self) -> String {
        self.name.clone()
    }
}

fn main() -> wasmtime::Result<()> {
    // Compile the `Component` that is being run for the application.
    let engine = Engine::default();
    let component = Component::from_file(&engine, "./your-component.wasm")?;

    // Instantiation of bindings always happens through a `Linker`.
    // Configuration of the linker is done through a generated `add_to_linker`
    // method on the bindings structure.
    //
    // Note that the closure provided here is a projection from `T` in
    // `Store<T>` to `&mut U` where `U` implements the `HelloWorldImports`
    // trait. In this case the `T`, `MyState`, is stored directly in the
    // structure so no projection is necessary here.
    let mut linker = Linker::new(&engine);
    HelloWorld::add_to_linker(&mut linker, |state: &mut MyState| state)?;

    // As with the core wasm API of Wasmtime instantiation occurs within a
    // `Store`. The bindings structure contains an `instantiate` method which
    // takes the store, component, and linker. This returns the `bindings`
    // structure which is an instance of `HelloWorld` and supports typed access
    // to the exports of the component.
    let mut store = Store::new(
        &engine,
        MyState {
            name: "me".to_string(),
        },
    );
    let bindings = HelloWorld::instantiate(&mut store, &component, &linker)?;

    // Here our `greet` function doesn't take any parameters for the component,
    // but in the Wasmtime embedding API the first argument is always a `Store`.
    bindings.call_greet(&mut store)?;
    Ok(())
}

Structs§

  • Auto-generated bindings for an instance a component which implements the world hello-world.
  • Auto-generated bindings for index of the exports of hello-world.
  • Auto-generated bindings for a pre-instantiated version of a component which implements the world hello-world.

Traits§