1919
2020## About
2121
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-
2722[ zulip ] : https://bytecodealliance.zulipchat.com/#narrow/stream/327223-wit-bindgen
2823
2924This project is a suite of bindings generators for languages that are compiled
3025to WebAssembly and use the [ component model] . Bindings are described with
3126[ ` *.wit ` files] [ WIT ] which specify imports, exports, and facilitate reuse
3227between bindings definitions.
3328
34- [ WIT ] : https://github.com/WebAssembly/ component-model/blob/main/ design/mvp/WIT.md
29+ [ WIT ] : https://component-model.bytecodealliance.org/ design/wit.html
3530[ component model ] : https://github.com/WebAssembly/component-model
3631
3732The ` wit-bindgen ` repository is currently focused on ** guest** programs which
3833are those compiled to WebAssembly. Executing a component in a host is not
3934managed 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 ] .
4139
4240## [ WIT] as an IDL
4341
@@ -97,8 +95,9 @@ etc. This can then be imported wholesale into the `my-game` world via the
9795` plugin ` namespace. The structure of a [ WIT] document and world will affect the
9896generated bindings per-language.
9997
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 ) .
102101
103102## Creating a Component
104103
@@ -139,23 +138,26 @@ that are using `wasi_snapshot_preview1` APIs.
139138
140139The ` wasm-tools component new ` subcommand takes an ` --adapt ` argument which acts
141140as 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+
156156[ 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
159161
160162## Supported Guest Languages
161163
@@ -183,14 +185,15 @@ world host {
183185
184186### Guest: Rust
185187
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:
188190
189191``` sh
190192rustup target add wasm32-wasi
191193```
192194
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:
194197
195198``` toml
196199[lib ]
@@ -200,7 +203,7 @@ crate-type = ["cdylib"]
200203Projects can then depend on ` wit-bindgen ` by executing:
201204
202205``` sh
203- cargo add --git https://github.com/bytecodealliance/wit-bindgen wit-bindgen
206+ cargo add wit-bindgen
204207```
205208
206209WIT files are currently added to a ` wit/ ` folder adjacent to your ` Cargo.toml `
@@ -237,14 +240,17 @@ impl Guest for MyHost {
237240```
238241
239242By 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.
241247
242248This project can then be built with:
243249
244250``` sh
245251cargo build --target wasm32-wasi
246252wasm-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
248254```
249255
250256This 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:
370376go generate # generate bindings for Go
371377tinygo build -target=wasi -o main.wasm my-component.go # compile
372378wasm-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
374380wasm-tools validate main.component.wasm --features component-model
375381```
376382
@@ -395,7 +401,7 @@ the following cargo command. This will let you generate the bindings for any
395401supported language.
396402
397403```
398- cargo install --git https://github.com/bytecodealliance/wit-bindgen wit-bindgen-cli
404+ cargo install wit-bindgen-cli
399405```
400406
401407This CLI ** IS NOT** stable and may change, do not expect it to be or rely on it
@@ -420,7 +426,7 @@ components:
420426 takes a [ WIT] package as input and generates ` trait ` -based bindings for the
421427 runtime to implement and use.
422428
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
424430 either on the web or outside the browser in a runtime such as ` node ` . This
425431 project generates a polyfill for a single concrete component to execute in a
426432 JS environment by extracting the core WebAssembly modules that make up a
@@ -438,7 +444,7 @@ components:
438444 inspect the WIT-based interface of a component with `wasm-tools component
439445wit` . You can link two components together with ` wasm-tools compose` as well.
440446
441- [ `js-component-tools ` ] : https://github.com/bytecodealliance/js-component-tools
447+ [ `jco ` ] : https://github.com/bytecodealliance/jco
442448
443449Note that the runtimes above are generally intended to work with arbitrary
444450components, not necessarily only those created by ` wit-bindgen ` . This is also
@@ -452,4 +458,4 @@ To build the cli:
452458cargo build
453459```
454460
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