You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
+
8
55
`wasm-tools` can be used to create a component from WAT.
9
56
Here's how to create a component from WAT
10
57
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.
21
68
```
22
69
23
70
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
0 commit comments