Skip to content

Commit c2a76b7

Browse files
refactor: split "Language Agnostic Tooling" into its own page, fix example (#282)
* Split "Language Agnostic Tooling" into its own page and fix example * Update component-model/src/language-support.md Co-authored-by: Victor Adossi <123968127+vados-cosmonic@users.noreply.github.com> * Update component-model/src/language-support/language-agnostic.md Co-authored-by: Victor Adossi <123968127+vados-cosmonic@users.noreply.github.com> * Update component-model/src/language-support/language-agnostic.md Co-authored-by: Victor Adossi <123968127+vados-cosmonic@users.noreply.github.com> * Update component-model/src/language-support/language-agnostic.md Co-authored-by: Victor Adossi <123968127+vados-cosmonic@users.noreply.github.com> * Update component-model/src/language-support/language-agnostic.md Co-authored-by: Victor Adossi <123968127+vados-cosmonic@users.noreply.github.com> * Include examples from files * Note about .component.wasm suffix * Update section on running the component * Added section introducing WAT * Rename language-agnostic.md to language-agnostic-tooling.md * Remove old file * Move WAT section to the end of the list and rename file * fix: title for lang support --------- Co-authored-by: Victor Adossi <123968127+vados-cosmonic@users.noreply.github.com>
1 parent e516c56 commit c2a76b7

File tree

4 files changed

+136
-63
lines changed

4 files changed

+136
-63
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
(module
2+
(func $add (param $lhs i32) (param $rhs i32) (result i32)
3+
local.get $lhs
4+
local.get $rhs
5+
i32.add)
6+
(export "docs:adder/add@0.1.0#add" (func $add))
7+
)

component-model/src/SUMMARY.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@
1515

1616
# Using WebAssembly Components
1717

18-
- [Language Support for Components](./language-support.md)
18+
- [Creating Components](./language-support.md)
1919
- [C/C++](./language-support/c.md)
2020
- [C#](./language-support/csharp.md)
2121
- [Go](./language-support/go.md)
2222
- [JavaScript](./language-support/javascript.md)
2323
- [Python](./language-support/python.md)
2424
- [Rust](./language-support/rust.md)
25+
- [WebAssembly Text Format (WAT)](./language-support/wat.md)
2526
- [Running Components](./running-components.md)
2627
- [Wasmtime](./running-components/wasmtime.md)
2728
- [jco](./running-components/jco.md)
Lines changed: 12 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
# Wasm Language Support
1+
# Creating components
22

3-
WebAssembly can be targeted by the majority of top programming
4-
languages; however, the level of
5-
support varies. This document details the subset of languages that target WASI and support
6-
components.
3+
Many popular programming languages can be compiled to WebAssembly,
4+
but the level of support varies across languages.
5+
This document details languages with compilers and runtimes
6+
that support WebAssembly with WASI as a target platform.
77

88
> This is a living document, so if you are aware of advancements in a toolchain, please do
99
not hesitate to [contribute documentation](https://github.com/bytecodealliance/component-docs/blob/main/CONTRIBUTING.md). You can find more information about the development of support for specific languages in the [Guest Languages Special Interest Group Proposal](https://github.com/bytecodealliance/governance/blob/main/SIGs/SIG-guest-languages/proposal.md) document.
@@ -18,12 +18,11 @@ example host or from an application of that toolchain. This aims to provide a fu
1818
components within and among toolchains.
1919

2020
Each section covers how to build and
21-
run components for a given toolchain:
21+
run components for a given toolchain.
22+
The last section, on WebAssembly Text Format (WAT),
23+
details how to write WebAssembly components by hand,
24+
without using a higher-level language front-end.
2225

23-
- [Wasm Language Support](#wasm-language-support)
24-
- [Language Agnostic Tooling](#language-agnostic-tooling)
25-
- [Building a Component with `wasm-tools`](#building-a-component-with-wasm-tools)
26-
- [Running a Component with Wasmtime](#running-a-component-with-wasmtime)
2726
- [C/C++ Tooling](./language-support/c.md)
2827
- [Building a Component with `wit-bindgen` and `wasm-tools`](./language-support/c.md#building-a-component-with-wit-bindgen-and-wasm-tools)
2928
- [Running a Component from C/C++ Applications](./language-support/c.md#running-a-component-from-cc-applications)
@@ -38,55 +37,6 @@ run components for a given toolchain:
3837
- [Rust Tooling](./language-support/rust.md)
3938
- [Building a Component with `cargo component`](./language-support/rust.md#building-a-component-with-cargo-component)
4039
- [Running a Component from Rust Applications](./language-support/rust.md#running-a-component-from-rust-appliacations)
41-
42-
## Language Agnostic Tooling
43-
44-
### Building a Component with `wasm-tools`
45-
46-
[`wasm-tools`](https://github.com/bytecodealliance/wasm-tools) provides a suite of subcommands for
47-
working with WebAssembly modules and components.
48-
49-
`wasm-tools` can be used to create a component from WebAssembly Text (WAT). This walks through creating a component from WAT that implements the [`adder` world](https://github.com/bytecodealliance/component-docs/blob/main/component-model/examples/tutorial/wit/adder/world.wit) and simply adds two numbers.
50-
51-
1. Install [`wasm-tools`](https://github.com/bytecodealliance/wasm-tools/tree/main#installation), a
52-
tool for low-level manipulation of Wasm modules and components.
53-
2. The `add` function is defined inside the following `world` world:
54-
55-
```wit
56-
package docs:adder@0.1.0;
57-
58-
interface add {
59-
add: func(x: u32, y: u32) -> u32;
60-
}
61-
62-
world adder {
63-
export add;
64-
}
65-
```
66-
67-
3. Define an `add` core module in WAT that exports an `add` function that adds two parameters:
68-
69-
```wat
70-
(module
71-
(func $add (param $lhs i32) (param $rhs i32) (result i32)
72-
local.get $lhs
73-
local.get $rhs
74-
i32.add)
75-
(export "docs:adder/add@0.1.0" (func $add))
76-
)
77-
```
78-
79-
4. Use `wasm-tools` to create a component from the core module, first embedding component metadata
80-
inside the core module and then encoding the WAT to a Wasm binary.
81-
82-
```sh
83-
$ wasm-tools component embed adder/world.wit add.wat -o add.wasm
84-
$ wasm-tools component new add.wasm -o add.component.wasm
85-
```
86-
87-
### Running a Component with Wasmtime
88-
89-
You can "run" a component by calling one of its exports. Hosts and runtimes often only support
90-
running components with certain exports. The [`wasmtime`](https://github.com/bytecodealliance/wasmtime) CLI can only run "command" components, so in
91-
order to run the `add` function above, it first must be composed with a primary "command" component
92-
that calls it. See [documentation on running components](./running-components/wasmtime.md) for more details.
40+
- [WebAssembly Text Format (WAT)](./language-support/wat.md#wat-webassembly-text-format)
41+
- [Building a Component from WAT with `wasm-tools`](./language-support/wat.md#building-a-component-with-wasm-tools)
42+
- [Running a Component with Wasmtime](./language-support/wat.md#running-a-component-with-wasmtime)
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
## Language Agnostic Tooling
2+
3+
[`wasm-tools`](https://github.com/bytecodealliance/wasm-tools) provides a suite of subcommands for
4+
working with WebAssembly modules and components.
5+
6+
### WAT (WebAssembly Text Format)
7+
8+
WAT (WebAssembly Text Format) is a text-based language
9+
that can be compiled to the WebAssembly binary format
10+
by `wasm-tools` and other tools.
11+
It's useful for writing small examples for testing and experimentation.
12+
13+
Here's an example of a module expressed in WAT:
14+
```wat
15+
{{#include ../../examples/tutorial/wat/adder/add.wat}}
16+
```
17+
18+
The module contains two top-level declarations, a function and an export.
19+
20+
The function declaration declares a function named `$add`
21+
with two arguments, `$lhs` and `$rhs`.
22+
(Variable names in WAT always start with a `$`.)
23+
Argument and result types need to be provided explicitly.
24+
In this case, the types of both arguments and the result
25+
are `i32` (32-bit integer).
26+
The body of the function is a list of WebAssembly instructions.
27+
The two `local.get` instructions push the values of `$lhs` and `$rhs`
28+
onto the stack.
29+
The `i32.add` instruction pops the two top values off the stack
30+
and adds them, leaving the result on the stack.
31+
32+
The `export` declaration connects the function that was just declared
33+
to a name that should be used for calling it externally.
34+
We want to use this WAT code to implement the interface specified in a WIT file,
35+
so the external name has to follow a certain convention.
36+
The name `"docs:adder/add@0.1.0#add"` can be broken down as follows:
37+
* `docs` is the package name.
38+
* `adder` is the name of a world inside the `docs` package.
39+
* `add` is the name of an interface defined in that world.
40+
* 0.1.0 is a version number.
41+
* Separately, `add` is the name of a function defined in the `add` interface.
42+
All of these pieces come from the specific `.wit` file we are using
43+
(see below).
44+
45+
There's much more than WAT can do;
46+
see the Mozilla Developer Network's [a detailed guide to WAT](https://developer.mozilla.org/en-US/docs/WebAssembly/Guides/Understanding_the_text_format)
47+
for more information.
48+
49+
The [wat2wasm](https://github.com/WebAssembly/wabt) tool converts
50+
from WAT to the binary `.wasm` format,
51+
but it does not create components.
52+
53+
### Building a Component from WAT with `wasm-tools`
54+
55+
`wasm-tools` can be used to create a component from WAT.
56+
Here's how to create a component from WAT
57+
that implements the [`adder` world](https://github.com/bytecodealliance/component-docs/blob/main/component-model/examples/tutorial/wit/adder/world.wit)
58+
and simply adds two numbers.
59+
60+
1. Install [`wasm-tools`](https://github.com/bytecodealliance/wasm-tools/tree/main#installation), a
61+
tool for low-level manipulation of Wasm modules and components.
62+
63+
2. The `add` function is defined inside the following world.
64+
Create a file called `adder.wit` whose contents are as follows:
65+
66+
```wit
67+
{{#include ../../examples/tutorial/wit/adder/world.wit}}
68+
```
69+
70+
3. Define an `add` core module in WAT that exports an `add` function that adds two parameters.
71+
Create a file called `add.wat` whose contents are as follows
72+
(the same as the example in the WAT section):
73+
74+
```wat
75+
{{#include ../../examples/tutorial/wat/adder/add.wat}}
76+
```
77+
78+
4. Use `wasm-tools` to create a binary core module with component metadata embedded inside it:
79+
80+
```sh
81+
wasm-tools component embed adder.wit add.wat -o add.wasm
82+
```
83+
84+
5. Use `wasm-tools` to create a new component `.wasm` file
85+
from the binary core module you just created:
86+
87+
```sh
88+
wasm-tools component new add.wasm -o add.component.wasm
89+
```
90+
91+
The suffix `.component.wasm` is just a convention.
92+
You could also name the output file `add_component.wasm` or anything else
93+
with the `.wasm` suffix.
94+
95+
### Running a Component with Wasmtime
96+
97+
You can "run" a component by calling one of its exports.
98+
Hosts and runtimes often only support running components with certain exports.
99+
100+
Using the [`wasmtime`](https://github.com/bytecodealliance/wasmtime) CLI,
101+
we can execute the `add` function in the component you just built,
102+
passing in arguments:
103+
104+
```sh
105+
wasmtime run --invoke 'add(1, 2)' add.component.wasm
106+
```
107+
108+
The output is ```3```.
109+
You can try passing other arguments to `add()`
110+
by changing the arguments inside the parentheses.
111+
112+
This example was tested with `wasmtime` 34.0.1.
113+
Earlier versions of `wasmtime` may not support the `--invoke` option.
114+
Any other compliant WebAssembly runtime that supports components
115+
can also run this component.

0 commit comments

Comments
 (0)