| 1 |
# `wit-component` |
| 2 |
|
| 3 |
`wit-component` is a crate for creating and interacting with WebAssembly |
| 4 |
components based on the [component model proposal][model]. |
| 5 |
|
| 6 |
## CLI usage |
| 7 |
|
| 8 |
The `wit-component` crate is available through the `wasm-tools` CLI suite under |
| 9 |
two subcommands: |
| 10 |
|
| 11 |
``` |
| 12 |
# Create a component from the input core wasm module |
| 13 |
$ wasm-tools component new core.wasm -o component.wasm |
| 14 |
|
| 15 |
# Extract a `*.wit` interface from a component |
| 16 |
$ wasm-tools component wit component.wasm |
| 17 |
``` |
| 18 |
|
| 19 |
## Features |
| 20 |
|
| 21 |
* Creates WebAssembly [component binaries][model] from input core WebAssembly |
| 22 |
modules. Input modules communicate with the [canonical ABI] to imported and |
| 23 |
exported interfaces described with [`*.wit` files][wit]. The wit interface is |
| 24 |
required to be embedded directly in the core wasm binary. |
| 25 |
|
| 26 |
* Supports "adapters" which can be used to bridge legacy core WebAssembly |
| 27 |
imported functions into [component model][model] functions. Adapters are |
| 28 |
themselves core wasm binaries which will be embedded into the final component. |
| 29 |
An adapter's exports can be imported by the main core wasm binary and the |
| 30 |
adapter can then call component model imports. |
| 31 |
|
| 32 |
* A [`*.wit`][wit] interface can be extracted from an existing component to |
| 33 |
see the interface that it exports and intends to import. |
| 34 |
|
| 35 |
[model]: https://github.com/webassembly/component-model |
| 36 |
[canonical ABI]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/CanonicalABI.md |
| 37 |
[wit]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/WIT.md |
| 38 |
|
| 39 |
## Usage |
| 40 |
|
| 41 |
Note that this crate is intended to be a low-level detail of tooling for |
| 42 |
components. Developers will not necessarily interact with this tooling |
| 43 |
day-to-day, instead using wrappers such as |
| 44 |
[`cargo-component`](https://github.com/bytecodealliance/cargo-component) which |
| 45 |
will automatically execute `wit-component` to produce component binaries. |
| 46 |
|
| 47 |
First `wit-component` supports the wasm-based encoding of a WIT package: |
| 48 |
|
| 49 |
```sh |
| 50 |
$ cat demo.wit |
| 51 |
package my:demo; |
| 52 |
|
| 53 |
interface host { |
| 54 |
hello: func(); |
| 55 |
} |
| 56 |
|
| 57 |
world demo { |
| 58 |
import host; |
| 59 |
} |
| 60 |
|
| 61 |
$ wasm-tools component wit demo.wit -o demo.wasm --wasm |
| 62 |
|
| 63 |
# The output `demo.wasm` is a valid component binary |
| 64 |
$ wasm-tools validate --features component-model demo.wasm |
| 65 |
$ wasm-tools print demo.wasm |
| 66 |
|
| 67 |
# The `*.wit` file can be recovered from the `demo.wasm` as well |
| 68 |
$ wasm-tools component wit demo.wasm |
| 69 |
``` |
| 70 |
|
| 71 |
Toolchain authors can use `wit-component` to embed this component types section |
| 72 |
into a core wasm binary. For a small demo here a raw `*.wat` wasm text file will |
| 73 |
be used where the `demo.wit` argument is specified manually, however. |
| 74 |
|
| 75 |
```sh |
| 76 |
$ cat demo.core.wat |
| 77 |
(module |
| 78 |
(import "my:demo/host" "hello" (func)) |
| 79 |
) |
| 80 |
|
| 81 |
$ wasm-tools component embed demo.wit --world demo demo.core.wat -o demo.wasm |
| 82 |
|
| 83 |
# See that there's a new `component-type` custom section |
| 84 |
$ wasm-tools objdump demo.wasm |
| 85 |
|
| 86 |
# Convert the core wasm into a component now |
| 87 |
$ wasm-tools component new demo.wasm -o demo.component.wasm |
| 88 |
|
| 89 |
# Like before the output `demo.wasm` is a valid component binary |
| 90 |
$ wasm-tools validate --features component-model demo.component.wasm |
| 91 |
$ wasm-tools print demo.component.wasm |
| 92 |
|
| 93 |
# Additionally like before the `*.wit` interface can still be extracted |
| 94 |
$ wasm-tools component wit demo.component.wasm |
| 95 |
``` |
| 96 |
|
| 97 |
Here the `demo.component.wasm` can now be shipped to a component runtime or |
| 98 |
embedded into hosts. |
| 99 |
|
| 100 |
## Custom Section Format |
| 101 |
|
| 102 |
Toolchains producing components typically have a workflow that looks something |
| 103 |
like this: |
| 104 |
|
| 105 |
1. A bindings generator, often based on [`wit-bindgen`], is used to generate |
| 106 |
source code. |
| 107 |
2. A user writes source code against these bindings. |
| 108 |
3. The language's compiler runs and produces a core WebAssembly module. |
| 109 |
4. The core WebAssembly module is passed to this crate, producing a component. |
| 110 |
|
| 111 |
Steps (1) and (4) both take WIT as input and the goal is to make it such that |
| 112 |
you don't have to specify the same WIT on both ends. Instead the WIT from (1) |
| 113 |
should be seamlessly passed through to (4) without developers having to |
| 114 |
explicitly opt-in to it otherwise. To do this, the `wit-component` crate and |
| 115 |
componentization process uses a custom section. |
| 116 |
|
| 117 |
Part of the generated bindings from (1) above is source code (or similar) to |
| 118 |
instruct the language to embed a custom section in the final binary. The custom |
| 119 |
section's name must start with `component-type` but can have any number of |
| 120 |
characters following it (e.g. it must match the regex `^component-type`). A |
| 121 |
module is allowed to have any number of custom sections, including zero. |
| 122 |
|
| 123 |
Each custom section describes a WIT `world`. The unit of bindings generation is |
| 124 |
a `world`, and each `world` used during step (1), which may possibly be in |
| 125 |
multiple separate libraries, will be encoded into custom sections. Custom |
| 126 |
sections typically have a name that includes the bindings generator, |
| 127 |
the bindings generator's version, the world, and an optional use-provided |
| 128 |
string. The contents of the custom section is a wasm-encoded WIT world. |
| 129 |
|
| 130 |
An example of this is that for this document: |
| 131 |
|
| 132 |
```wit |
| 133 |
package a:b; |
| 134 |
|
| 135 |
world foo { |
| 136 |
import host: func(); |
| 137 |
export guest: func(); |
| 138 |
} |
| 139 |
``` |
| 140 |
|
| 141 |
this WIT world is encoded to wasm as: |
| 142 |
|
| 143 |
```wasm |
| 144 |
(component |
| 145 |
(type (;0;) |
| 146 |
(component |
| 147 |
(type (;0;) |
| 148 |
(component |
| 149 |
(type (;0;) (func)) |
| 150 |
(import "host" (func (;0;) (type 0))) |
| 151 |
(export (;1;) "guest" (func (type 0))) |
| 152 |
) |
| 153 |
) |
| 154 |
(export (;0;) "a:b/foo" (component (type 0))) |
| 155 |
) |
| 156 |
) |
| 157 |
(export (;1;) "foo" (type 0)) |
| 158 |
(@custom "package-docs" "\00{}") |
| 159 |
(@producers |
| 160 |
(processed-by "wit-component" "0.218.0") |
| 161 |
) |
| 162 |
) |
| 163 |
``` |
| 164 |
|
| 165 |
More details about the wasm encoding of WIT can [be found |
| 166 |
upstream](https://github.com/WebAssembly/component-model/blob/main/design/mvp/WIT.md). |
| 167 |
|
| 168 |
Note here that the WIT world is just the single `component`-type export needed |
| 169 |
to decode a single world, no other items are encoded into this wasm. |
| 170 |
|
| 171 |
When determining the `world` for a wasm component the componentization process |
| 172 |
will read the input module, remove all sections that match `^component-type`, |
| 173 |
extract the `world` that the type represents, and then "merge" all of the worlds |
| 174 |
together. This merging process can fail if there are conflicts, but otherwise |
| 175 |
duplicate imports are unified together. |
| 176 |
|
| 177 |
The final componentization process then understands the WIT `world` that's being |
| 178 |
used and assumes that the component follows the naming scheme in |
| 179 |
[`BuildTargets.md`](https://github.com/WebAssembly/component-model/pull/378) |
| 180 |
upstream. (note that legacy names are also accepted at this time) |
| 181 |
|
| 182 |
## Merging Worlds |
| 183 |
|
| 184 |
Part of what `wit-component` does when creating a component is that it will |
| 185 |
merge WIT `world`s from a number of sources. For example each custom section |
| 186 |
above may have a world. For tools like `wasm-component-ld` worlds may also be |
| 187 |
supplied on the command line. The final component, however, can only have one |
| 188 |
set of imports and exports so a single `world` needs to be created from all |
| 189 |
these input worlds. |
| 190 |
|
| 191 |
There are two workhorse methods for performing this merging process and both |
| 192 |
live within the `wit-parser` crate: |
| 193 |
|
| 194 |
* [`wit_parser::Resolve::merge_worlds`] |
| 195 |
* [`wit_parser::Resolve::merge_world_imports_based_on_semver`] |
| 196 |
|
| 197 |
The first is unconditionally used to merge all worlds together. Each custom |
| 198 |
section, input argument, etc, are all merged with the |
| 199 |
[`wit_parser::Resolve::merge_worlds`] method. This method may fail in certain |
| 200 |
situations, primarily if there are duplicate exports specified in different |
| 201 |
worlds (even if they're the same name they can't unify to one export as it's not |
| 202 |
clear which export to pick). There are some other niche situations as well in |
| 203 |
which the merging process can fail. |
| 204 |
|
| 205 |
Once all worlds are merged together with [`wit_parser::Resolve::merge_worlds`] |
| 206 |
there is then an optional, but enabled by default, step to "trim" the world |
| 207 |
down based on semver versions. This method is |
| 208 |
[`wit_parser::Resolve::merge_world_imports_based_on_semver`] and is used to |
| 209 |
change a world such as this: |
| 210 |
|
| 211 |
```wit |
| 212 |
world { |
| 213 |
import wasi:cli/[email protected]; |
| 214 |
import wasi:cli/[email protected]; |
| 215 |
} |
| 216 |
``` |
| 217 |
|
| 218 |
into this |
| 219 |
|
| 220 |
```wit |
| 221 |
world { |
| 222 |
import wasi:cli/[email protected]; |
| 223 |
} |
| 224 |
``` |
| 225 |
|
| 226 |
Notably imports are deduplicated based on their semver versions of interfaces. |
| 227 |
The maximum version of the interface is always selected in the end. Only |
| 228 |
interfaces which are semver compatible are deduplicated, for example this world |
| 229 |
cannot be deduplicated further: |
| 230 |
|
| 231 |
```wit |
| 232 |
world { |
| 233 |
import wasi:cli/[email protected]; |
| 234 |
import wasi:cli/[email protected]; |
| 235 |
} |
| 236 |
``` |
| 237 |
|
| 238 |
The main purpose for this deduplication is to enable internally within a |
| 239 |
component various libraries and runtimes to evolve at a different pace with |
| 240 |
respect to interface versions. By definition in WIT it should always be possible |
| 241 |
to use a larger versions, so older imports are automatically "upgraded" to |
| 242 |
newer imports in these situations. This ensures that, for example, only one |
| 243 |
`wasi:io/poll/pollable` resource will be imported into the final component. If |
| 244 |
two were imported there would be no way to actually functionally use the |
| 245 |
resulting component. |
| 246 |
|
| 247 |
Note that this semver-deduplication process is enabled by default but can be |
| 248 |
disabled through various options and flags in tooling. |
| 249 |
|
| 250 |
[`wit_parser::Resolve::merge_worlds`]: https://docs.rs/wit-parser/latest/wit_parser/struct.Resolve.html#method.merge_worlds |
| 251 |
[`wit_parser::Resolve::merge_world_imports_based_on_semver`]: https://docs.rs/wit-parser/latest/wit_parser/struct.Resolve.html#method.merge_world_imports_based_on_semver |