Example WebAssembly Component Model composition using wac

Demonstrates composing a name component and a greeter component with either:

  1. a cli component
  2. a http component

Updated to use the wasm32-wasip2 target in Rust nightly.

Setup

  • Rust nightly — rustup toolchain install nightly
  • waccargo binstall wac-cli
  • wasmtime — brew install wasmtime

1. The name component

reads the NAME environment variable and returns it.

world name {
    import wasi:cli/environment@0.2.0;
    export name: func() -> string;
}
impl Guest for Component {
    fn name() -> String {
        wasi::cli::environment::get_environment()
            .into_iter()
            .find(|(k, _)| k == "NAME")
            .map(|(_, v)| v)
            .unwrap_or("World".to_string())
    }
}

2. The greeter component

calls into the name component and returns a greeting.

world greeter {
  import name: func() -> string;
  export greet: func() -> string;
}
impl Guest for Component {
    fn greet() -> String {
        let name = name();
        format!("Hello, {name}!")
    }
}

Run as a CLI

NAME=Stu ./cli.fish

outputs:

Hello, Stu!

Run as an HTTP server

NAME=Stu ./http.fish

Then in a new terminal:

curl http://localhost:8080

outputs:

Hello, Stu!