Skip to content

Commit

Permalink
Update WASI example to use shared Wasi::add_to_config.
Browse files Browse the repository at this point in the history
This commit updates the WASI example to use `Wasi::add_to_config`.

As only a single store and instance are used in the example, it has no semantic
difference from the previous example, but the intention is to steer users
towards defining WASI on the config and only using `Wasi::add_to_linker` when
more explicit scoping of the WASI context is required.
  • Loading branch information
peterhuene committed Mar 10, 2021
1 parent 3849cf3 commit 7e96169
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions examples/wasi/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,25 @@ fn main() -> Result<()> {
.with_ansi(true)
.init();

let store = Store::default();
let mut linker = Linker::new(&store);
// Define the WASI functions globally on the `Config`.
let mut config = Config::default();
Wasi::add_to_config(&mut config);

let store = Store::new(&Engine::new(&config)?);

// Create an instance of `Wasi` which contains a `WasiCtx`. Note that
// `WasiCtx` provides a number of ways to configure what the target program
// Set the WASI context in the store; all instances in the store share this context.
// `WasiCtxBuilder` provides a number of ways to configure what the target program
// will have access to.
let wasi = Wasi::new(
assert!(Wasi::set_context(
&store,
WasiCtxBuilder::new()
.inherit_stdio()
.inherit_args()?
.build()?,
);
wasi.add_to_linker(&mut linker)?;
.build()?
)
.is_ok());

let mut linker = Linker::new(&store);

// Instantiate our module with the imports we've created, and run it.
let module = Module::from_file(store.engine(), "target/wasm32-wasi/debug/wasi.wasm")?;
Expand Down

0 comments on commit 7e96169

Please sign in to comment.