|
2 | 2 |
|
3 | 3 | This section contains language-specific guides on how to create runnable components. |
4 | 4 |
|
| 5 | +## Running arbitrary functions from reactor (library-like) components |
| 6 | + |
| 7 | +In practice, any interface that is exported from a WebAssembly component can be run by either: |
| 8 | + |
| 9 | +- Creating a custom host/component that imports and reuses the functionality |
| 10 | +- Using high level generic tooling like `wasmtime run --invoke` |
| 11 | + |
| 12 | +For example, given a WebAssembly component which satisfies the following WIT: |
| 13 | + |
| 14 | +```wit |
| 15 | +{{#include ../examples/tutorial/wit/adder/world.wit}} |
| 16 | +``` |
| 17 | + |
| 18 | +Use of the exported `add` function inside the `add` interface requires writing a host or other component that is |
| 19 | +built to import and use that functionality. This is exemplified by the [`example-host` available in this repo][example-host]. |
| 20 | + |
| 21 | +Alternatively tooling that works generically over components `wasmtime run --invoke`: |
| 22 | + |
| 23 | +```sh |
| 24 | +wasmtime run --invoke 'add(1, 2)' add.component.wasm |
| 25 | +``` |
| 26 | + |
| 27 | +Wasmtime contains code that can generically interpret exports, convert arguments to WebAssembly arguments, and execute |
| 28 | +an existing component dynamically. |
| 29 | + |
| 30 | +[example-host](https://github.com/bytecodealliance/component-docs/blob/main/component-model/examples/example-host/README.md) |
| 31 | + |
| 32 | +## Creating components that behave like binaries |
| 33 | + |
| 34 | +While running arbitrary functions require either a custom host/platform or a dynamic tool like `wasmtime run --invoke`, |
| 35 | +components that are treatable as binaries (i.e. a CLI application) can also be built. |
| 36 | + |
5 | 37 | At a high level there are at least two ways to create components that are more like binaries than libraries |
6 | | -(i.e. that are easy to run from a tool like `wasmtime`): |
| 38 | +(i.e. that are easy to run from a tool like `wasmtime run`): |
7 | 39 |
|
8 | 40 | 1. Creating a "command" component |
9 | | -2. Exporting the `wasi:cli/run` interface |
| 41 | +2. Exporting the [`wasi:cli/run` interface][wasi-cli-run] |
10 | 42 |
|
11 | 43 | This section explores how to do the above in relevant languages. |
12 | 44 |
|
| 45 | +[wasi-cli-iface-run]: https://github.com/WebAssembly/wasi-cli/tree/main/wit/run.wit |
| 46 | + |
13 | 47 | ## Languages |
14 | 48 |
|
15 | 49 | This guide is implemented for various languages: |
16 | 50 |
|
17 | | -| Language | |
18 | | -|-----------------------------------------------------------------| |
19 | | -| [Rust](./language-support/creating-runnable-components/rust.md) | |
| 51 | +| Language | |
| 52 | +|-----------------------------------------------------------------------------| |
| 53 | +| [Rust](./language-support/creating-runnable-components/rust.md) | |
| 54 | +| [Javascript](./language-support/creating-runnable-components/javascript.md) | |
20 | 55 |
|
21 | 56 | [docs-wit]: ./design/wit.md |
0 commit comments