Skip to content

Commit 25b3b6b

Browse files
refactor(lang/js): move advanced sections, rework runnable functions
1 parent 9cdab08 commit 25b3b6b

File tree

6 files changed

+400
-335
lines changed

6 files changed

+400
-335
lines changed

component-model/src/SUMMARY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@
2929
- [Other Languages](./language-support/building-a-simple-component/other-languages.md)
3030
- [Importing and reusing components](./importing-and-reusing-components.md)
3131
- [Rust](./language-support/importing-and-reusing-components/rust.md)
32+
- [Javascript](./language-support/importing-and-reusing-components/javascript.md)
3233
- [Creating runnable components](./creating-runnable-components.md)
3334
- [Rust](./language-support/creating-runnable-components/rust.md)
35+
- [Javascript](./language-support/creating-runnable-components/javascript.md)
3436
- [Using WIT resources](./using-wit-resources.md)
3537
- [Rust](./language-support/using-wit-resources/rust.md)
3638
- [Running Components](./running-components.md)

component-model/src/creating-runnable-components.md

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,55 @@
22

33
This section contains language-specific guides on how to create runnable components.
44

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+
537
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`):
739

840
1. Creating a "command" component
9-
2. Exporting the `wasi:cli/run` interface
41+
2. Exporting the [`wasi:cli/run` interface][wasi-cli-run]
1042

1143
This section explores how to do the above in relevant languages.
1244

45+
[wasi-cli-iface-run]: https://github.com/WebAssembly/wasi-cli/tree/main/wit/run.wit
46+
1347
## Languages
1448

1549
This guide is implemented for various languages:
1650

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) |
2055

2156
[docs-wit]: ./design/wit.md

component-model/src/importing-and-reusing-components.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ The `calculator` component has the following interface:
1919

2020
This guide is implemented for various languages:
2121

22-
| Language |
23-
|---------------------------------------------------------------------|
24-
| [Rust](./language-support/importing-and-reusing-components/rust.md) |
22+
| Language |
23+
|---------------------------------------------------------------------------------|
24+
| [Rust](./language-support/importing-and-reusing-components/rust.md) |
25+
| [Javascript](./language-support/importing-and-reusing-components/javascript.md) |
2526

2627
[docs-wit]: ./design/wit.md

0 commit comments

Comments
 (0)