19
19
20
20
## About
21
21
22
- > ** Note** : this project is still relatively young and active development with
23
- > large changes is still happening. If you're considering depending on this at
24
- > this time it's recommended to reach out to the authors on [ zulip] and get more
25
- > information first.
26
-
27
22
[ zulip ] : https://bytecodealliance.zulipchat.com/#narrow/stream/327223-wit-bindgen
28
23
29
24
This project is a suite of bindings generators for languages that are compiled
30
25
to WebAssembly and use the [ component model] . Bindings are described with
31
26
[ ` *.wit ` files] [ WIT ] which specify imports, exports, and facilitate reuse
32
27
between bindings definitions.
33
28
34
- [ WIT ] : https://github.com/WebAssembly/ component-model/blob/main/ design/mvp/WIT.md
29
+ [ WIT ] : https://component-model.bytecodealliance.org/ design/wit.html
35
30
[ component model ] : https://github.com/WebAssembly/component-model
36
31
37
32
The ` wit-bindgen ` repository is currently focused on ** guest** programs which
38
33
are those compiled to WebAssembly. Executing a component in a host is not
39
34
managed in this repository, and some options of how to do so are [ described
40
- below] [ hosts ] .
35
+ below] [ hosts ] . Languages developed in this repository are Rust, C, Java (TeaVM
36
+ Java), Go (TinyGo), and C#. If you encounter any problems feel free to [ open an
37
+ issue] ( https://github.com/bytecodealliance/wit-bindgen/issues/new ) or chat with
38
+ us on [ Zulip] [ zulip ] .
41
39
42
40
## [ WIT] as an IDL
43
41
@@ -97,8 +95,9 @@ etc. This can then be imported wholesale into the `my-game` world via the
97
95
` plugin ` namespace. The structure of a [ WIT] document and world will affect the
98
96
generated bindings per-language.
99
97
100
- For more information about WIT and its syntax see the [ upstream description of
101
- WIT] [ WIT ] .
98
+ For more information about WIT and its syntax see the [ online documentation for
99
+ WIT] [ WIT ] as well as its [ upstream
100
+ reference] ( https://github.com/WebAssembly/component-model/blob/main/design/mvp/WIT.md ) .
102
101
103
102
## Creating a Component
104
103
@@ -139,23 +138,26 @@ that are using `wasi_snapshot_preview1` APIs.
139
138
140
139
The ` wasm-tools component new ` subcommand takes an ` --adapt ` argument which acts
141
140
as a way to polyfill non-component-model APIs, like ` wasi_snapshot_preview1 ` ,
142
- with component model APIs. The [ preview2-prototyping] project is the current
143
- go-to location to acquire a polyfill from ` wasi_snapshot_preview1 ` to an
144
- in-development version of "wasi preview2" which is specified with [ WIT]
145
- and the component model.
146
-
147
- Notably you'll want to download [ one of the adapter modules] [ preview1-build ]
148
- and name it ` wasi_snapshot_preview1.wasm ` locally to pass as an ` --adapt `
149
- argument to ` wasm-tools component new ` . Note that there are two modules
150
- provided on the downloads page, [ one is for non-commands] [ non-command ] which
151
- don't have a ` _start ` entrypoint in the generated core wasm module (e.g. the
152
- ` cdylib ` crate type in Rust) and [ one that is for command modules] [ command ]
153
- which has a ` _start ` entrypoint (e.g. a ` src/main.rs ` in Rust).
154
-
155
- [ preview2-prototyping ] : https://github.com/bytecodealliance/preview2-prototyping
141
+ with component model APIs. The [ Wasmtime] runtime publishes [ adapter
142
+ modules] [ preview1-build ] with each release that are suitable to use with
143
+ ` --adapt ` to implement ` wasi_snapshot_preview1 ` in terms of WASI 0.2. On
144
+ Wasmtime's releases page you'll see three modules to choose from:
145
+
146
+ * [ ` wasi_snapshot_preview1.command.wasm ` ] - use this for CLI applications.
147
+ * [ ` wasi_snapshot_preview1.reactor.wasm ` ] - use this for applications that don't
148
+ have a ` main ` function for example: for example a process that responds to an
149
+ event.
150
+ * [ ` wasi_snapshot_preview1.proxy.wasm ` ] - use this for applications fed into
151
+ ` wasmtime serve ` for example.
152
+
153
+ Only one adapter is necessary and be sure to look for the [ latest
154
+ versions] [ preview1-build ] as well.
155
+
156
156
[ preview1-build ] : https://github.com/bytecodealliance/wasmtime/releases/latest
157
- [ non-command ] : https://github.com/bytecodealliance/wasmtime/releases/latest/download/wasi_snapshot_preview1.reactor.wasm
158
- [ command ] : https://github.com/bytecodealliance/wasmtime/releases/latest/download/wasi_snapshot_preview1.command.wasm
157
+ [ wasmtime ] : https://github.com/bytecodealliance/wasmtime
158
+ [ `wasi_snapshot_preview1.command.wasm` ] : https://github.com/bytecodealliance/wasmtime/releases/download/v17.0.0/wasi_snapshot_preview1.command.wasm
159
+ [ `wasi_snapshot_preview1.reactor.wasm` ] : https://github.com/bytecodealliance/wasmtime/releases/download/v17.0.0/wasi_snapshot_preview1.reactor.wasm
160
+ [ `wasi_snapshot_preview1.proxy.wasm` ] : https://github.com/bytecodealliance/wasmtime/releases/download/v17.0.0/wasi_snapshot_preview1.proxy.wasm
159
161
160
162
## Supported Guest Languages
161
163
@@ -183,14 +185,15 @@ world host {
183
185
184
186
### Guest: Rust
185
187
186
- The Rust compiler supports a native ` wasm32-wasi ` target and can be added to any
187
- ` rustup ` -based toolchain with:
188
+ The Rust compiler supports a native ` wasm32-wasi ` target and can be added to
189
+ any ` rustup ` -based toolchain with:
188
190
189
191
``` sh
190
192
rustup target add wasm32-wasi
191
193
```
192
194
193
- In order to compile a wasi dynamic library, the following must be added to the ` Cargo.toml ` file:
195
+ In order to compile a wasi dynamic library, the following must be added to the
196
+ ` Cargo.toml ` file:
194
197
195
198
``` toml
196
199
[lib ]
@@ -200,7 +203,7 @@ crate-type = ["cdylib"]
200
203
Projects can then depend on ` wit-bindgen ` by executing:
201
204
202
205
``` sh
203
- cargo add --git https://github.com/bytecodealliance/wit-bindgen wit-bindgen
206
+ cargo add wit-bindgen
204
207
```
205
208
206
209
WIT files are currently added to a ` wit/ ` folder adjacent to your ` Cargo.toml `
@@ -237,14 +240,17 @@ impl Guest for MyHost {
237
240
```
238
241
239
242
By using [ ` cargo expand ` ] ( https://github.com/dtolnay/cargo-expand ) or `cargo
240
- doc` you can also explore the generated code.
243
+ doc` you can also explore the generated code. If there's a bug in ` wit-bindgen`
244
+ and the generated bindings do not compile or if there's an error in the
245
+ generated code (which is probably also a bug in ` wit-bindgen ` ), you can use
246
+ ` WIT_BINDGEN_DEBUG=1 ` as an environment variable to help debug this.
241
247
242
248
This project can then be built with:
243
249
244
250
``` sh
245
251
cargo build --target wasm32-wasi
246
252
wasm-tools component new ./target/wasm32-wasi/debug/my-project.wasm \
247
- -o my-component.wasm --adapt ./wasi_snapshot_preview1.wasm
253
+ -o my-component.wasm --adapt ./wasi_snapshot_preview1.reactor. wasm
248
254
```
249
255
250
256
This creates a ` my-component.wasm ` file which is suitable to execute in any
@@ -370,7 +376,7 @@ This can then be compiled with `tinygo` and assembled into a component with:
370
376
go generate # generate bindings for Go
371
377
tinygo build -target=wasi -o main.wasm my-component.go # compile
372
378
wasm-tools component embed --world host ./wit main.wasm -o main.embed.wasm # create a component
373
- wasm-tools component new main.embed.wasm --adapt wasi_snapshot_preview1.wasm -o main.component.wasm
379
+ wasm-tools component new main.embed.wasm --adapt wasi_snapshot_preview1.command. wasm -o main.component.wasm
374
380
wasm-tools validate main.component.wasm --features component-model
375
381
```
376
382
@@ -395,7 +401,7 @@ the following cargo command. This will let you generate the bindings for any
395
401
supported language.
396
402
397
403
```
398
- cargo install --git https://github.com/bytecodealliance/wit-bindgen wit-bindgen-cli
404
+ cargo install wit-bindgen-cli
399
405
```
400
406
401
407
This CLI ** IS NOT** stable and may change, do not expect it to be or rely on it
@@ -420,7 +426,7 @@ components:
420
426
takes a [ WIT] package as input and generates ` trait ` -based bindings for the
421
427
runtime to implement and use.
422
428
423
- - JS: the [ ` js-component-tools ` ] project can be used to execute components in JS
429
+ - JS: the [ ` jco ` ] project can be used to execute components in JS
424
430
either on the web or outside the browser in a runtime such as ` node ` . This
425
431
project generates a polyfill for a single concrete component to execute in a
426
432
JS environment by extracting the core WebAssembly modules that make up a
@@ -438,7 +444,7 @@ components:
438
444
inspect the WIT-based interface of a component with `wasm-tools component
439
445
wit` . You can link two components together with ` wasm-tools compose` as well.
440
446
441
- [ `js-component-tools ` ] : https://github.com/bytecodealliance/js-component-tools
447
+ [ `jco ` ] : https://github.com/bytecodealliance/jco
442
448
443
449
Note that the runtimes above are generally intended to work with arbitrary
444
450
components, not necessarily only those created by ` wit-bindgen ` . This is also
@@ -452,4 +458,4 @@ To build the cli:
452
458
cargo build
453
459
```
454
460
455
- Learn more how to run the tests in the [ testing document] ( tests/README.md ) .
461
+ Learn more how to run the tests in the [ testing document] ( tests/README.md ) .
0 commit comments