Skip to content

Commit d14eb3d

Browse files
committed
Split "Language Agnostic Tooling" into its own page and fix example
1 parent e516c56 commit d14eb3d

File tree

3 files changed

+72
-62
lines changed

3 files changed

+72
-62
lines changed

component-model/src/SUMMARY.md

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

1616
# Using WebAssembly Components
1717

18-
- [Language Support for Components](./language-support.md)
18+
- [Creating Components in Specific Source Languages](./language-support.md)
19+
- [Language-Agnostic Tooling](./language-support/language-agnostic.md)
1920
- [C/C++](./language-support/c.md)
2021
- [C#](./language-support/csharp.md)
2122
- [Go](./language-support/go.md)
Lines changed: 8 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
# Wasm Language Support
1+
# Creating components in specific source languages
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 language.
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.
@@ -20,10 +20,9 @@ components within and among toolchains.
2020
Each section covers how to build and
2121
run components for a given toolchain:
2222

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)
23+
- [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)
25+
- [Running a Component with Wasmtime](./language-support/language-agnostic.md#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,3 @@ 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.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
## Language Agnostic Tooling
2+
3+
### Building a Component with `wasm-tools`
4+
5+
[`wasm-tools`](https://github.com/bytecodealliance/wasm-tools) provides a suite of subcommands for
6+
working with WebAssembly modules and components.
7+
8+
`wasm-tools` can be used to create a component from WAT.
9+
Here's how to create a component from WAT
10+
that implements the [`adder` world](https://github.com/bytecodealliance/component-docs/blob/main/component-model/examples/tutorial/wit/adder/world.wit)
11+
and simply adds two numbers.
12+
13+
1. Install [`wasm-tools`](https://github.com/bytecodealliance/wasm-tools/tree/main#installation), a
14+
tool for low-level manipulation of Wasm modules and components.
15+
2. The `add` function is defined inside the following world.
16+
Create a file called `adder.wit` whose contents are as follows:
17+
18+
```wit
19+
package docs:adder@0.1.0;
20+
21+
interface add {
22+
add: func(x: u32, y: u32) -> u32;
23+
}
24+
25+
world adder {
26+
export add;
27+
}
28+
```
29+
30+
3. Define an `add` core module in WAT that exports an `add` function that adds two parameters.
31+
Create a file called `add.wat` whose contents are as follows:
32+
33+
```wat
34+
(module
35+
(func $add (param $lhs i32) (param $rhs i32) (result i32)
36+
local.get $lhs
37+
local.get $rhs
38+
i32.add)
39+
(export "docs:adder/add@0.1.0#add" (func $add))
40+
)
41+
```
42+
43+
4. Use `wasm-tools` to create a binary core module with component metadata embedded inside it:
44+
45+
```sh
46+
$ wasm-tools component embed adder.wit add.wat -o add.wasm
47+
```
48+
49+
5. Use `wasm-tools` to create a new component `.wasm` file
50+
from the binary core module you just created:
51+
52+
```sh
53+
$ wasm-tools component new add.wasm -o add.component.wasm
54+
```
55+
56+
### Running a Component with Wasmtime
57+
58+
You can "run" a component by calling one of its exports. Hosts and runtimes often only support
59+
running components with certain exports. The [`wasmtime`](https://github.com/bytecodealliance/wasmtime) CLI can only run "command" components, so in
60+
order to run the `add` function above, it first must be composed with a primary "command" component
61+
that calls it. See [documentation on running components](../running-components/wasmtime.md) for more details.
62+

0 commit comments

Comments
 (0)