Skip to content

Files

Latest commit

 

History

History
61 lines (41 loc) · 2.27 KB

examples-rust-wasi.md

File metadata and controls

61 lines (41 loc) · 2.27 KB

WASIp2

You can also browse this source code online and clone the wasmtime repository to run the example locally.

This example shows how to use the wasmtime-wasi crate to define WASI functions within a Linker which can then be used to instantiate a WebAssembly component.

WebAssembly Component Source Code

For this WASI example, this Hello World program is compiled to a WebAssembly component using the WASIp2 API.

wasi.rs

{{#include ../examples/wasm/wasi.rs}}

Building instructions:

  1. Have Rust installed
  2. Add WASIp2 target if you haven't already: rustup target add wasm32-wasip2
  3. cargo build --target wasm32-wasip2

Building this program generates target/wasm32-wasip2/debug/wasi.wasm, used below.

Invoke the WASM component

This example shows adding and configuring the WASI imports to invoke the above WASM component.

main.rs

{{#include ../examples/wasip2/main.rs}}

Async example

This async example code shows how to use the wasmtime-wasi crate to execute the same WASIp2 component from the example above. This example requires the wasmtime crate async feature to be enabled.

This does not require any change to the WASIp2 component, it's just the WASIp2 API host functions which are implemented to be async. See wasmtime async support.

{{#include ../examples/wasip2-async/main.rs}}

You can also browse this source code online and clone the wasmtime repository to run the example locally.

Beyond Basics

Please see these references:

  • The book for understanding the component model of WASIp2.
  • Bindgen Examples for implementing WASIp2 hosts and guests.