Skip to content

Commit

Permalink
Merge pull request #5163 from wasmerio/wasix-readme-example
Browse files Browse the repository at this point in the history
Correct the example of the README in the WASIX crate
  • Loading branch information
xdoardo authored Oct 21, 2024
2 parents 1bb7399 + 1d2e23b commit 8e5f50f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ edition = "2021"
homepage = "https://wasmer.io/"
license = "MIT"
repository = "https://github.com/wasmerio/wasmer"
rust-version = "1.74"
rust-version = "1.81"
version = "5.0.0-rc.1"

[workspace.dependencies]
Expand Down
40 changes: 25 additions & 15 deletions lib/wasix/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,27 +60,37 @@ Hello, Some("Gordon")
… and programatically with the `wasmer` and the `wasmer-wasi` libraries:

```rust
use wasmer::{Store, Module, Instance};
use wasmer_wasix::WasiState;
use std::io::Read;
use wasmer::{Module, Store};
use wasmer_wasix::{Pipe, WasiEnv};

let wasm_path = "hello.wasm";

// Let's declare the Wasm module with the text representation.
let wasm_bytes = std::fs::read(wasm_path)?;

// Create a Store.
let mut store = Store::default();
let module = Module::from_file(&store, "hello.wasm")?;

// Create the `WasiEnv`.
let wasi_env = WasiState::builder("command-name")
.args(&["Gordon"])
.finalize()?;
println!("Compiling module...");
// Let's compile the Wasm module.
let module = Module::new(&store, wasm_bytes)?;

let (stdout_tx, mut stdout_rx) = Pipe::channel();

// Run the module.
WasiEnv::builder("hello")
.args(&["Gordon"])
// .env("KEY", "Value")
.stdout(Box::new(stdout_tx))
.run_with_store(module, &mut store)?;

// Generate an `ImportObject`.
let mut wasi_thread = wasi_env.new_thread();
let import_object = wasi_thread.import_object(&module)?;
eprintln!("Run complete - reading output");

// Let's instantiate the module with the imports.
let instance = Instance::new(&module, &import_object)?;
let mut buf = String::new();
stdout_rx.read_to_string(&mut buf).unwrap();

// Let's call the `_start` function, which is our `main` function in Rust.
let start = instance.exports.get_function("_start")?;
start.call(&[])?;
eprintln!("Output: {buf}");
```

Check the [fully working example using
Expand Down

0 comments on commit 8e5f50f

Please sign in to comment.