Pre-existing Components
You don’t need a language toolchain to start working with components.
Components are distributed as .wasm files,
often through OCI registries like the GitHub Container Registry (GHCR),
so you can download a component someone else has built,
inspect its interface, and run it.
This guide walks through downloading a prebuilt component that implements the adder world:
package docs:adder@0.1.0;
interface add {
add: func(x: u32, y: u32) -> u32;
}
world adder {
export add;
}
The component is built from the Rust adder example in this repository
and published to ghcr.io/bytecodealliance/docs/components/adder.
1. Install the tools
You will need:
wkg, to download components from OCI registries. Prebuilt binaries are available on thewasm-pkg-toolsreleases page.wasm-tools, to inspect components.wasmtime, to run components.
This guide was tested with wkg 0.16.3, wasm-tools 1.262.0, and wasmtime 51.0.0.
2. Download the component
Use wkg oci pull to download the component and write it to adder.wasm:
wkg oci pull ghcr.io/bytecodealliance/docs/components/adder:0.1.0 -o adder.wasm
Note
wkg oci pulladdresses the component by its OCI reference.wkgcan also fetch components and WIT packages by package name (such asdocs:adder): see Distributing and Fetching Components and WIT.
3. Inspect the component
Every component carries a description of its imports and exports.
Use wasm-tools component wit to print it as WIT:
$ wasm-tools component wit adder.wasm
package root:component;
world root {
export docs:adder/add@0.1.0;
}
package docs:adder@0.1.0 {
interface add {
add: func(x: u32, y: u32) -> u32;
}
}
The component exports the add interface from the docs:adder package,
which matches the adder world shown above.
It has no imports, so a host does not need to provide it with anything to run it.
4. Run the component
Use wasmtime run --invoke to call the exported add function:
$ wasmtime run --invoke 'add(1, 2)' adder.wasm
3
Try passing other arguments to add() by changing the values inside the parentheses.
Any other runtime that supports components can run this component too. See Running Components for more options.
Next steps
- Build your own
addercomponent in one of the supported languages. - Combine this component with others, without writing any code,
using
wac plug. - Walk through building and composing a complete application in the Tutorial.