Creating Runnable Components
This section contains language-specific guides on how to create runnable components.
Running arbitrary functions from reactor (library-like) components
In practice, any interface that is exported from a WebAssembly component can be run by either:
- Creating a custom host/component that imports and reuses the functionality
- Using high level generic tooling like
wasmtime run --invoke
For example, given a WebAssembly component which satisfies the following WIT:
package docs:adder@0.1.0;
interface add {
add: func(x: u32, y: u32) -> u32;
}
world adder {
export add;
}
Use of the exported add function inside the add interface requires writing a host or other component that is
built to import and use that functionality. This is exemplified by the [example-host available in this repo][example-host].
Alternatively tooling that works generically over components wasmtime run --invoke:
wasmtime run --invoke 'add(1, 2)' add.component.wasm
Wasmtime contains code that can generically interpret exports, convert arguments to WebAssembly arguments, and execute an existing component dynamically.
Creating components that behave like binaries
While running arbitrary functions require either a custom host/platform or a dynamic tool like wasmtime run --invoke,
components that are treatable as binaries (i.e. a CLI application) can also be built.
At a high level there are at least two ways to create components that are more like binaries than libraries
(i.e. that are easy to run from a tool like wasmtime run):
- Creating a "command" component
- Exporting the [
wasi:cli/runinterface][wasi-cli-run]
While command components simply have a _start export, components that export the wasi:cli/run run interface
are able to make use of more robust standardized interfaces that mimic a CLI environment like stdout, stderr, via WASI.
This section explores how to create components that are trivially runanbel (like binaries) in relevant languages.
Languages
This guide is implemented for various languages:
| Language |
|---|
| Rust |
| Javascript |
| Other Languages |