| 1 |
<div align="center"> |
| 2 |
<h1><code>wasi</code></h1> |
| 3 |
|
| 4 |
<strong>A <a href="https://bytecodealliance.org/">Bytecode Alliance</a> project</strong> |
| 5 |
|
| 6 |
<p> |
| 7 |
<strong>WASI API Bindings for Rust</strong> |
| 8 |
</p> |
| 9 |
|
| 10 |
<p> |
| 11 |
<a href="https://crates.io/crates/wasi"><img src="https://img.shields.io/crates/v/wasi.svg?style=flat-square" alt="Crates.io version" /></a> |
| 12 |
<a href="https://crates.io/crates/wasi"><img src="https://img.shields.io/crates/d/wasi.svg?style=flat-square" alt="Download" /></a> |
| 13 |
<a href="https://docs.rs/wasi/"><img src="https://img.shields.io/badge/docs-latest-blue.svg?style=flat-square" alt="docs.rs docs" /></a> |
| 14 |
</p> |
| 15 |
</div> |
| 16 |
|
| 17 |
This crate contains bindings for [WASI](https://github.com/WebAssembly/WASI) |
| 18 |
APIs for the worlds: |
| 19 |
|
| 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 |
| 31 |
|
| 32 |
# Usage |
| 33 |
|
| 34 |
Depending on this crate can be done by adding it to your dependencies: |
| 35 |
|
| 36 |
```sh |
| 37 |
$ cargo add wasi |
| 38 |
``` |
| 39 |
|
| 40 |
Next you can use the APIs in the root of the module like so: |
| 41 |
|
| 42 |
```rust |
| 43 |
fn main() { |
| 44 |
let stdout = wasi::cli::stdout::get_stdout(); |
| 45 |
stdout.blocking_write_and_flush(b"Hello, world!\n").unwrap(); |
| 46 |
} |
| 47 |
``` |
| 48 |
|
| 49 |
This crate can currently be used in three main ways. |
| 50 |
|
| 51 |
- One is to use it and compile for the [`wasm32-wasip2` target] in Rust 1.82 and later. |
| 52 |
This is the simplest approach, as all the tools needed are included in the |
| 53 |
Rust tooling, however it doesn't yet support some of the features of the |
| 54 |
other approaches. |
| 55 |
|
| 56 |
- Another is to use it and compile using [`cargo component`]. This is essentially |
| 57 |
the same as the next option, except that `cargo component` handles most of the |
| 58 |
steps for you. `cargo component` also has a number of additional features for |
| 59 |
working with dependencies and custom WIT interfaces. |
| 60 |
|
| 61 |
- And the third is to compile for the `wasm32-wasip1` target, and then adapt |
| 62 |
the resulting modules into component using `wasm-tools component new`; see |
| 63 |
the next section here for details. |
| 64 |
|
| 65 |
[`wasm32-wasip2` target]: https://blog.rust-lang.org/2024/11/26/wasip2-tier-2.html |
| 66 |
[`cargo component`]: https://github.com/bytecodealliance/cargo-component |
| 67 |
|
| 68 |
## Building with wasm32-wasip1 and `cargo component new`. |
| 69 |
|
| 70 |
The `wasm32-wasip2` target works with a simple `cargo build --target=wasm32-wasip2` |
| 71 |
and doesn't need a lot of documentation here, and `cargo component` has its own |
| 72 |
documentation, so here we have some documentation for the `wasm32-wasip1` way. |
| 73 |
|
| 74 |
``` |
| 75 |
$ cargo build --target wasm32-wasip1 |
| 76 |
``` |
| 77 |
|
| 78 |
Next you'll want an "adapter" to convert the Rust standard library's usage of |
| 79 |
`wasi_snapshot_preview1` to the component model. An example adapter can be found |
| 80 |
from [Wasmtime's release page](https://github.com/bytecodealliance/wasmtime/releases/download/v17.0.0/wasi_snapshot_preview1.command.wasm). |
| 81 |
|
| 82 |
``` |
| 83 |
$ curl -LO https://github.com/bytecodealliance/wasmtime/releases/download/v17.0.0/wasi_snapshot_preview1.command.wasm |
| 84 |
``` |
| 85 |
|
| 86 |
Next to create a component you'll use the [`wasm-tools`] CLI to create a |
| 87 |
component: |
| 88 |
|
| 89 |
``` |
| 90 |
$ cargo install wasm-tools |
| 91 |
$ wasm-tools component new target/wasm32-wasip1/debug/foo.wasm \ |
| 92 |
--adapt ./wasi_snapshot_preview1.command.wasm \ |
| 93 |
-o component.wasm |
| 94 |
``` |
| 95 |
|
| 96 |
And finally the component can be run by a runtime that has Component Model |
| 97 |
support, such as [Wasmtime]: |
| 98 |
|
| 99 |
``` |
| 100 |
$ wasmtime run component.wasm |
| 101 |
Hello, world! |
| 102 |
``` |
| 103 |
|
| 104 |
[Wasmtime]: https://github.com/bytecodealliance/wasmtime |
| 105 |
|
| 106 |
# WASIp2 vs WASIp1 |
| 107 |
|
| 108 |
In January 2024 the WASI subgroup published WASI 0.2.0, colloquially known as |
| 109 |
"WASIp2". Around the same time the subgroup additionally decided to name the |
| 110 |
previous iteration of WASI as "WASIp1", historically known as "WASI preview1". |
| 111 |
This now-historical snapshot of WASI was defined with an entirely different set |
| 112 |
of primitives and worked very differently. This crate now targets WASIp2 and no |
| 113 |
longer targets WASIp1. |
| 114 |
|
| 115 |
## Support for WASIp1 |
| 116 |
|
| 117 |
The last version of the `wasi` crate to support WASIp1 was the |
| 118 |
[0.11.0+wasi-snapshot-preview1 |
| 119 |
version](https://crates.io/crates/wasi/0.11.0+wasi-snapshot-preview1). This |
| 120 |
version of the crate supported all WASIp1 APIs. WASIp1 was historically defined |
| 121 |
with `*.witx` files and used a bindings generator called `witx-bindgen`. |
| 122 |
|
| 123 |
## Should I use WASIp1 or WASIp2? |
| 124 |
|
| 125 |
This is a bit of a nuanced question/answer but the short answer is to probably |
| 126 |
use the 0.11.0 release of `wasi` for now if you're unsure. |
| 127 |
|
| 128 |
The longer-form answer of this is that it depends on the Rust targets that you |
| 129 |
want to support. Rust WebAssembly targets include: |
| 130 |
|
| 131 |
* `wasm32-unknown-unknown` - do not use this crate because this target indicates |
| 132 |
that WASI is not desired. |
| 133 |
* `wasm32-wasip1` - this target has been present in Rust for quite some time and |
| 134 |
was previously known as `wasm32-wasi`. For this target you probably want the |
| 135 |
0.11.0 track of this crate. |
| 136 |
* `wasm32-wasip2` - this target is a recent addition to rustc (as of the time of |
| 137 |
this writing it's not merged yet into rustc). This is what the 0.12.0 version |
| 138 |
of the crate is intended for. |
| 139 |
|
| 140 |
Note that if you use `wasm32-wasip1` it's not necessarily guaranteed you want |
| 141 |
0.11.0 of this crate. If your users are producing components then you probably |
| 142 |
want 0.12.0 instead. If you don't know what your users are producing then you |
| 143 |
should probably stick with 0.11.0. |
| 144 |
|
| 145 |
Long story short, it's a bit complicated. We're in a transition period from |
| 146 |
WASIp1 to WASIp2 and things aren't going to be perfect every step of the way, so |
| 147 |
understanding is appreciated! |
| 148 |
|
| 149 |
# Development |
| 150 |
|
| 151 |
The bulk of the `wasi` crate is generated by the [`wit-bindgen`] tool. The |
| 152 |
`src/bindings.rs` file can be regenerated with: |
| 153 |
|
| 154 |
``` |
| 155 |
$ ./ci/regenerate.sh |
| 156 |
``` |
| 157 |
|
| 158 |
WASI definitions are located in the `wit` directory of this repository. |
| 159 |
Currently they're copied from upstream repositories but are hoped to be better |
| 160 |
managed in the future. |
| 161 |
|
| 162 |
# License |
| 163 |
|
| 164 |
This project is triple licenced under the Apache 2/ Apache 2 with LLVM exceptions/ MIT licences. The reasoning for this is: |
| 165 |
- Apache 2/ MIT is common in the rust ecosystem. |
| 166 |
- Apache 2/ MIT is used in the rust standard library, and some of this code may be migrated there. |
| 167 |
- Some of this code may be used in compiler output, and the Apache 2 with LLVM exceptions licence is useful for this. |
| 168 |
|
| 169 |
For more details see |
| 170 |
- [Apache 2 Licence](LICENSE-APACHE) |
| 171 |
- [Apache 2 Licence with LLVM exceptions](LICENSE-Apache-2.0_WITH_LLVM-exception) |
| 172 |
- [MIT Licence](LICENSE-MIT) |
| 173 |
|
| 174 |
### Contribution |
| 175 |
|
| 176 |
Unless you explicitly state otherwise, any contribution intentionally submitted |
| 177 |
for inclusion in this project by you, as defined in the Apache 2/ Apache 2 with LLVM exceptions/ MIT licenses, |
| 178 |
shall be licensed as above, without any additional terms or conditions. |