Skip to content

Commit 49bdac0

Browse files
committed
Added section introducing WAT
1 parent 95cb5dc commit 49bdac0

3 files changed

Lines changed: 57 additions & 8 deletions

File tree

component-model/examples/tutorial/wat/adder/add.wat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
local.get $lhs
44
local.get $rhs
55
i32.add)
6-
(export "docs:adder/add@0.1.0#add" (func $add))
6+
(export "docs:adder/add@0.1.0#add" (func $add))
77
)

component-model/src/language-support.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ Each section covers how to build and
2121
run components for a given toolchain:
2222

2323
- [Language Agnostic Tooling](./language-support/language-agnostic.md)
24-
- [Building a Component with `wasm-tools`](./language-support/language-agnostic.md#building-a-component-with-wasm-tools)
24+
- [WAT (WebAssembly Text Format)](./language-support/language-agnostic.md#wat-webassembly-text-format)
25+
- [Building a Component from WAT with `wasm-tools`](./language-support/language-agnostic.md#building-a-component-with-wasm-tools)
2526
- [Running a Component with Wasmtime](./language-support/language-agnostic.md#running-a-component-with-wasmtime)
2627
- [C/C++ Tooling](./language-support/c.md)
2728
- [Building a Component with `wit-bindgen` and `wasm-tools`](./language-support/c.md#building-a-component-with-wit-bindgen-and-wasm-tools)

component-model/src/language-support/language-agnostic.md

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,57 @@
11
## Language Agnostic Tooling
22

3-
### Building a Component from WebAssembly Text Format (WAT) with `wasm-tools`
4-
53
[`wasm-tools`](https://github.com/bytecodealliance/wasm-tools) provides a suite of subcommands for
64
working with WebAssembly modules and components.
75

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+
855
`wasm-tools` can be used to create a component from WAT.
956
Here's how to create a component from WAT
1057
that implements the [`adder` world](https://github.com/bytecodealliance/component-docs/blob/main/component-model/examples/tutorial/wit/adder/world.wit)
@@ -21,11 +68,12 @@ and simply adds two numbers.
2168
```
2269

2370
3. Define an `add` core module in WAT that exports an `add` function that adds two parameters.
24-
Create a file called `add.wat` whose contents are as follows:
71+
Create a file called `add.wat` whose contents are as follows
72+
(the same as the example in the WAT section):
2573

26-
```wat
27-
{{#include ../../examples/tutorial/wat/adder/add.wat}}
28-
```
74+
```wat
75+
{{#include ../../examples/tutorial/wat/adder/add.wat}}
76+
```
2977

3078
4. Use `wasm-tools` to create a binary core module with component metadata embedded inside it:
3179

0 commit comments

Comments
 (0)