Skip to content

Commit d2ba9f4

Browse files
authored
Update WASI bindings to 0.2.0 and the component model (#72)
* Remove old wasi-ephemeral/witx-bindgen crates * Copy WIT files from Wasmtime * Update to WASI 0.2.0 bindings * Update some manifest fields * Add a small example * CI tweaks * Install wit-bindgen on CI * Specify versions of wasmtime/wasm-tools on CI * Fix wit-bindgen-cli install * Flag wit/deps as generated as well * Fix yml syntax * Fix wasmtime tag * Update README
1 parent 9ec04a7 commit d2ba9f4

54 files changed

Lines changed: 19156 additions & 3385 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitattributes

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# This is generated by `wit-bindgen`
2+
src/bindings.rs linguist-generated=true
3+
4+
# This is copied from other repos so diffs aren't super interesting by default
5+
wit/deps linguist-generated=true

.github/workflows/main.yml

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,36 +9,44 @@ jobs:
99
matrix:
1010
rust: [stable, beta, nightly]
1111
steps:
12-
- uses: actions/checkout@v1
13-
with:
14-
submodules: true
12+
- uses: actions/checkout@v4
1513
- name: Install Rust
1614
run: rustup update ${{ matrix.rust }} && rustup default ${{ matrix.rust }} && rustup component add rustfmt
1715
- run: rustup target add wasm32-wasi
1816
- run: cargo build
1917
- run: cargo build --no-default-features
2018
- run: cargo build --target wasm32-wasi
2119
- run: cargo build --target wasm32-wasi --no-default-features
22-
- run: cargo build -p wasi-ephemeral
20+
- name: Install Wasmtime
21+
uses: bytecodealliance/actions/wasmtime/setup@v1
22+
with:
23+
version: "v17.0.0"
24+
- name: Install wasm-tools
25+
uses: bytecodealliance/actions/wasm-tools/setup@v1
26+
with:
27+
version: "1.0.57"
28+
- run: cargo build --examples --target wasm32-wasi
29+
- run: curl -LO https://github.com/bytecodealliance/wasmtime/releases/download/v17.0.0/wasi_snapshot_preview1.command.wasm
30+
- run: wasm-tools component new ./target/wasm32-wasi/debug/examples/hello-world.wasm --adapt ./wasi_snapshot_preview1.command.wasm -o component.wasm
31+
- run: wasmtime run component.wasm
32+
2333

2434
rustfmt:
2535
name: Rustfmt
2636
runs-on: ubuntu-latest
2737
steps:
28-
- uses: actions/checkout@v1
29-
with:
30-
submodules: true
38+
- uses: actions/checkout@v4
3139
- name: Install Rust
3240
run: rustup update stable && rustup default stable && rustup component add rustfmt
3341
- run: cargo fmt -- --check
3442

3543
generate:
36-
name: Test Generated Code
44+
name: Ensure generated code up-to-date
3745
runs-on: ubuntu-latest
3846
steps:
39-
- uses: actions/checkout@v1
40-
with:
41-
submodules: true
47+
- uses: actions/checkout@v4
4248
- name: Install Rust
4349
run: rustup update stable && rustup default stable
44-
- run: cargo test -p witx-bindgen
50+
- run: cargo install wit-bindgen-cli@0.16.0
51+
- run: ./ci/regenerate.sh
52+
- run: git diff --exit-code

.gitmodules

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +0,0 @@
1-
[submodule "crates/witx-bindgen/WASI"]
2-
path = crates/witx-bindgen/WASI
3-
url = https://github.com/WebAssembly/WASI

Cargo.toml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
[package]
22
name = "wasi"
3-
version = "0.11.0+wasi-snapshot-preview1"
3+
version = "0.12.0+wasi-0.2.0"
44
authors = ["The Cranelift Project Developers"]
55
license = "Apache-2.0 WITH LLVM-exception OR Apache-2.0 OR MIT"
6-
description = "Experimental WASI API bindings for Rust"
7-
edition = "2018"
6+
description = "WASI API bindings for Rust"
7+
edition = "2021"
88
categories = ["no-std", "wasm"]
99
keywords = ["webassembly", "wasm"]
1010
repository = "https://github.com/bytecodealliance/wasi"
1111
readme = "README.md"
1212
documentation = "https://docs.rs/wasi"
1313

14-
[workspace]
15-
members = ['crates/witx-bindgen', 'crates/wasi-ephemeral']
16-
1714
[dependencies]
15+
wit-bindgen = { version = "0.16", default-features = false, features = ['realloc'] }
16+
1817
# When built as part of libstd
1918
compiler_builtins = { version = "0.1", optional = true }
2019
core = { version = "1.0", optional = true, package = "rustc-std-workspace-core" }

README.md

Lines changed: 51 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -14,74 +14,88 @@
1414
</p>
1515
</div>
1616

17-
This crate contains API bindings for [WASI](https://github.com/WebAssembly/WASI)
18-
system calls in Rust, and currently reflects the `wasi_snapshot_preview1`
19-
module. This crate is quite low-level and provides conceptually a "system call"
20-
interface. In most settings, it's better to use the Rust standard library, which
21-
has WASI support.
17+
This crate contains bindings for [WASI](https://github.com/WebAssembly/WASI)
18+
APIs for the worlds:
2219

23-
The `wasi` crate is also entirely procedurally generated from the `*.witx` files
24-
describing the WASI apis. While some conveniences are provided the bindings here
25-
are intentionally low-level!
20+
* [`wasi:cli/command`]
21+
* [`wasi:http/proxy`]
22+
23+
This crate is procedurally generated from [WIT] files using [`wit-bindgen`].
24+
25+
[`wasi:cli/command`]: https://github.com/WebAssembly/wasi-cli
26+
[`wasi:http/proxy`]: https://github.com/WebAssembly/wasi-http
27+
[WIT]: https://component-model.bytecodealliance.org/design/wit.html
28+
[`wit-bindgen`]: https://github.com/bytecodealliance/wit-bindgen
29+
[components]: https://component-model.bytecodealliance.org/
30+
[`wasm-tools`]: https://github.com/bytecodealliance/wasm-tools
2631

2732
# Usage
2833

2934
First you can depend on this crate via `Cargo.toml`:
3035

3136
```toml
3237
[dependencies]
33-
wasi = "0.8.0"
38+
wasi = "0.12.0"
3439
```
3540

3641
Next you can use the APIs in the root of the module like so:
3742

3843
```rust
3944
fn main() {
40-
let stdout = 1;
41-
let message = "Hello, World!\n";
42-
let data = [wasi::Ciovec {
43-
buf: message.as_ptr(),
44-
buf_len: message.len(),
45-
}];
46-
wasi::fd_write(stdout, &data).unwrap();
45+
let stdout = wasi::cli::stdout::get_stdout();
46+
stdout.blocking_write_and_flush(b"Hello, world!\n").unwrap();
4747
}
4848
```
4949

50-
Next you can use a tool like [`cargo
51-
wasi`](https://github.com/bytecodealliance/cargo-wasi) to compile and run your
52-
project:
50+
This crate is intended to target [components] but today you need to go through
51+
the intermediate build step of a core WebAssembly module using the `wasm32-wasi`
52+
target:
5353

54-
To compile Rust projects to wasm using WASI, use the `wasm32-wasi` target,
55-
like this:
54+
```
55+
$ cargo build --target wasm32-wasi
56+
```
57+
58+
Next you'll want an "adapter" to convert the Rust standard library's usage of
59+
`wasi_snapshot_preview1` to the component model. An example adapter can be found
60+
from [Wasmtime's release page](https://github.com/bytecodealliance/wasmtime/releases/download/v17.0.0/wasi_snapshot_preview1.command.wasm).
5661

5762
```
58-
$ cargo wasi run
59-
Compiling wasi v0.8.0+wasi-snapshot-preview1
60-
Compiling wut v0.1.0 (/code)
61-
Finished dev [unoptimized + debuginfo] target(s) in 0.34s
62-
Running `/.cargo/bin/cargo-wasi target/wasm32-wasi/debug/wut.wasm`
63-
Running `target/wasm32-wasi/debug/wut.wasm`
64-
Hello, World!
63+
$ curl -LO https://github.com/bytecodealliance/wasmtime/releases/download/v17.0.0/wasi_snapshot_preview1.command.wasm
6564
```
6665

67-
# Development
66+
Next to create a component you'll use the [`wasm-tools`] CLI to create a
67+
component:
6868

69-
The bulk of the `wasi` crate is generated by the `witx-bindgen` tool, which lives at
70-
`crates/witx-bindgen` and is part of the cargo workspace.
69+
```
70+
$ cargo install wasm-tools
71+
$ wasm-tools component new target/wasm32-wasi/debug/foo.wasm \
72+
--adapt ./wasi_snapshot_preview1.command.wasm \
73+
-o component.wasm
74+
```
7175

72-
The `src/lib_generated.rs` file can be re-generated with the following
73-
command:
76+
And finally the component can be run by a runtime that has Component Model
77+
support, such as [Wasmtime]:
7478

7579
```
76-
cargo run -p witx-bindgen -- crates/witx-bindgen/WASI/phases/snapshot/witx/wasi_snapshot_preview1.witx > src/lib_generated.rs
80+
$ wasmtime run component.wasm
81+
Hello, world!
7782
```
7883

79-
Note that this uses the WASI standard repository as a submodule. If you do not
80-
have this submodule present in your source tree, run:
84+
[Wasmtime]: https://github.com/bytecodealliance/wasmtime
85+
86+
# Development
87+
88+
The bulk of the `wasi` crate is generated by the [`wit-bindgen`] tool. The
89+
`src/bindings.rs` file can be regenerated with:
90+
8191
```
82-
git submodule update --init
92+
$ ./ci/regenerate.sh
8393
```
8494

95+
WASI definitions are located in the `wit` directory of this repository.
96+
Currently they're copied from upstream repositories but are hoped to be better
97+
managed in the future.
98+
8599
# License
86100

87101
This project is licensed under the Apache 2.0 license with the LLVM exception.

ci/regenerate.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/sh
2+
3+
set -ex
4+
5+
wit-bindgen rust wit --out-dir src --std-feature
6+
7+
# rustfmt chokes on the raw output of wit-bindgen right now due to trailling
8+
# whitespace (unsure as to why), so format it with some options first to get it
9+
# into a better state before applying the final format with default options
10+
# which gets this to succeed.
11+
#
12+
# NB: this should be considered a bug in wit-bindgen that this is required to do
13+
# twice. Passing `--rustfmt` to `wit-bindgen` should work.
14+
rustfmt src/bindings.rs --edition 2021 --config-path ./ci/rustfmt-bindings.toml
15+
rustfmt src/bindings.rs --edition 2021

ci/rustfmt-bindings.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
max_width = 100
2+
tab_spaces = 2

crates/wasi-ephemeral/Cargo.toml

Lines changed: 0 additions & 21 deletions
This file was deleted.

crates/wasi-ephemeral/README.md

Lines changed: 0 additions & 18 deletions
This file was deleted.

crates/wasi-ephemeral/build.rs

Lines changed: 0 additions & 34 deletions
This file was deleted.

0 commit comments

Comments
 (0)