Skip to content

Commit

Permalink
Update to wit-bindgen 0.20.0 (#80)
Browse files Browse the repository at this point in the history
* Update to wit-bindgen 0.20.0

This commit updates the bindings generator to 0.20.0 and refactors the
existing support for exporting macros to use the new features of 0.20.0
as well. The `macros` crate feature is now no longer required (yay!) and
all the traits to implement are already present in the crate now.

* Add more generated files to gitattributes

* Remove macros feature from CI

* Fix regenerate script

* Rename macros back to their old names

* Document some internal structure

* More internal comments

* Rename command macro

* Fix doc test
  • Loading branch information
alexcrichton authored Mar 5, 2024
1 parent ded1722 commit e62af39
Show file tree
Hide file tree
Showing 11 changed files with 4,633 additions and 3,431 deletions.
4 changes: 3 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# This is generated by `wit-bindgen`
# These are generated by `wit-bindgen`
src/bindings.rs linguist-generated=true
src/proxy.rs linguist-generated=true
src/command.rs linguist-generated=true

# This is copied from other repos so diffs aren't super interesting by default
wit/deps linguist-generated=true
5 changes: 3 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ jobs:
- run: cargo build --no-default-features
- run: cargo build --target wasm32-wasi
- run: cargo build --target wasm32-wasi --no-default-features
- run: cargo test --doc
- name: Install Wasmtime
uses: bytecodealliance/actions/wasmtime/setup@v1
with:
Expand All @@ -29,7 +30,7 @@ jobs:
- run: curl -LO https://github.com/bytecodealliance/wasmtime/releases/download/v17.0.0/wasi_snapshot_preview1.command.wasm
- run: wasm-tools component new ./target/wasm32-wasi/debug/examples/hello-world.wasm --adapt ./wasi_snapshot_preview1.command.wasm -o component.wasm
- run: wasmtime run component.wasm
- run: cargo build --examples --target wasm32-unknown-unknown --features macros
- run: cargo build --examples --target wasm32-unknown-unknown
- run: wasm-tools component new ./target/wasm32-unknown-unknown/debug/examples/cli_command.wasm -o component.wasm
- run: wasmtime run component.wasm
- run: wasm-tools component new ./target/wasm32-unknown-unknown/debug/examples/http_proxy.wasm -o component.wasm
Expand All @@ -52,6 +53,6 @@ jobs:
- uses: actions/checkout@v4
- name: Install Rust
run: rustup update stable && rustup default stable
- run: cargo install wit-bindgen-cli@0.19.2 --locked
- run: cargo install wit-bindgen-cli@0.20.0 --locked
- run: ./ci/regenerate.sh
- run: git diff --exit-code
13 changes: 2 additions & 11 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,26 @@ readme = "README.md"
documentation = "https://docs.rs/wasi"

[dependencies]
wit-bindgen = { version = "0.19.1", default-features = false, features = ['realloc'] }
wit-bindgen = { version = "0.20.0", default-features = false, features = ['realloc'] }

# When built as part of libstd
compiler_builtins = { version = "0.1", optional = true }
core = { version = "1.0", optional = true, package = "rustc-std-workspace-core" }
rustc-std-workspace-alloc = { version = "1.0", optional = true }

[build-dependencies]
quote = { version = "1.0", optional = true }

[features]
default = ["std"]
std = []
macros = ["quote", "wit-bindgen/macros"]
# Unstable feature to support being a libstd dependency
rustc-dep-of-std = ["compiler_builtins", "core", "rustc-std-workspace-alloc"]

[package.metadata.docs.rs]
features = ["macros"]

[[example]]
name = "cli-command"
crate-type = ["cdylib"]
required-features = ["macros"]

[[example]]
name = "http-proxy"
crate-type = ["cdylib"]
required-features = ["macros"]

[badges]
maintenance = { status = "experimental" }
maintenance = { status = "experimental" }
105 changes: 0 additions & 105 deletions build.rs

This file was deleted.

85 changes: 76 additions & 9 deletions ci/regenerate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,81 @@

set -ex

wit-bindgen rust wit --out-dir src --std-feature --type-section-suffix rust-wasi-from-crates-io
generate() {
file="$1"
shift
wit-bindgen rust wit --out-dir src --std-feature "$@"

# rustfmt chokes on the raw output of wit-bindgen right now due to trailling
# whitespace (unsure as to why), so format it with some options first to get it
# into a better state before applying the final format with default options
# which gets this to succeed.
# rustfmt chokes on the raw output of wit-bindgen right now due to trailling
# whitespace (unsure as to why), so format it with some options first to get it
# into a better state before applying the final format with default options
# which gets this to succeed.
#
# NB: this should be considered a bug in wit-bindgen that this is required to do
# twice. Passing `--rustfmt` to `wit-bindgen` should work.
rustfmt $file --edition 2021 --config-path ./ci/rustfmt-bindings.toml
rustfmt $file --edition 2021
}

# Generate the main body of the bindings which includes all imports from the two
# worlds below.
generate src/bindings.rs --type-section-suffix rust-wasi-from-crates-io

# Generate bindings for the `wasi:cli/command` world specifically, namely the
# macro `export_command`.
#
# NB: this should be considered a bug in wit-bindgen that this is required to do
# twice. Passing `--rustfmt` to `wit-bindgen` should work.
rustfmt src/bindings.rs --edition 2021 --config-path ./ci/rustfmt-bindings.toml
rustfmt src/bindings.rs --edition 2021
# Note that `--with` is used to point at the previously generated bindings.
with="wasi:cli/[email protected]=crate::cli::environment"
with="$with,wasi:cli/[email protected]=crate::cli::exit"
with="$with,wasi:cli/[email protected]=crate::cli::stdin"
with="$with,wasi:cli/[email protected]=crate::cli::stdout"
with="$with,wasi:cli/[email protected]=crate::cli::stderr"
with="$with,wasi:cli/[email protected]=crate::cli::terminal_input"
with="$with,wasi:cli/[email protected]=crate::cli::terminal_output"
with="$with,wasi:cli/[email protected]=crate::cli::terminal_stdin"
with="$with,wasi:cli/[email protected]=crate::cli::terminal_stdout"
with="$with,wasi:cli/[email protected]=crate::cli::terminal_stderr"
with="$with,wasi:clocks/[email protected]=crate::clocks::monotonic_clock"
with="$with,wasi:clocks/[email protected]=crate::clocks::wall_clock"
with="$with,wasi:filesystem/[email protected]=crate::filesystem::types"
with="$with,wasi:filesystem/[email protected]=crate::filesystem::preopens"
with="$with,wasi:io/[email protected]=crate::io::error"
with="$with,wasi:io/[email protected]=crate::io::poll"
with="$with,wasi:io/[email protected]=crate::io::streams"
with="$with,wasi:random/[email protected]=crate::random::random"
with="$with,wasi:random/[email protected]=crate::random::insecure"
with="$with,wasi:random/[email protected]=crate::random::insecure_seed"
with="$with,wasi:sockets/[email protected]=crate::sockets::network"
with="$with,wasi:sockets/[email protected]=crate::sockets::instance_network"
with="$with,wasi:sockets/[email protected]=crate::sockets::tcp"
with="$with,wasi:sockets/[email protected]=crate::sockets::tcp_create_socket"
with="$with,wasi:sockets/[email protected]=crate::sockets::udp"
with="$with,wasi:sockets/[email protected]=crate::sockets::udp_create_socket"
with="$with,wasi:sockets/[email protected]=crate::sockets::ip_name_lookup"
generate src/command.rs \
--world wasi:cli/command \
--with "$with" \
--type-section-suffix rust-wasi-from-crates-io-command-world \
--default-bindings-module wasi \
--pub-export-macro \
--export-macro-name _export_command

# Same as the `command` world, but for the proxy world.
with="wasi:cli/[email protected]=crate::cli::stdin"
with="$with,wasi:cli/[email protected]=crate::cli::stdout"
with="$with,wasi:cli/[email protected]=crate::cli::stderr"
with="$with,wasi:clocks/[email protected]=crate::clocks::monotonic_clock"
with="$with,wasi:clocks/[email protected]=crate::clocks::wall_clock"
with="$with,wasi:io/[email protected]=crate::io::error"
with="$with,wasi:io/[email protected]=crate::io::poll"
with="$with,wasi:io/[email protected]=crate::io::streams"
with="$with,wasi:random/[email protected]=crate::random::random"
with="$with,wasi:http/[email protected]=crate::http::types"
with="$with,wasi:http/[email protected]=crate::http::outgoing_handler"
generate src/proxy.rs \
--world wasi:http/proxy \
--with "$with" \
--type-section-suffix rust-wasi-from-crates-io-proxy-world \
--default-bindings-module wasi \
--pub-export-macro \
--export-macro-name _export_proxy
4 changes: 2 additions & 2 deletions examples/cli-command.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
wasi::cli::run::export!(Example);
wasi::cli::command::export!(Example);

struct Example;

impl exports::wasi::cli::run::Guest for Example {
impl wasi::exports::cli::run::Guest for Example {
fn run() -> Result<(), ()> {
let stdout = wasi::cli::stdout::get_stdout();
stdout.blocking_write_and_flush(b"Hello, WASI!").unwrap();
Expand Down
4 changes: 2 additions & 2 deletions examples/http-proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ use wasi::http::types::{
Fields, IncomingRequest, OutgoingBody, OutgoingResponse, ResponseOutparam,
};

wasi::http::incoming_handler::export!(Example);
wasi::http::proxy::export!(Example);

struct Example;

impl exports::wasi::http::incoming_handler::Guest for Example {
impl wasi::exports::http::incoming_handler::Guest for Example {
fn handle(_request: IncomingRequest, response_out: ResponseOutparam) {
let resp = OutgoingResponse::new(Fields::new());
let body = resp.body().unwrap();
Expand Down
Loading

0 comments on commit e62af39

Please sign in to comment.