mirror of
https://github.com/mii443/wasmer.git
synced 2025-12-07 13:18:20 +00:00
54 lines
1.5 KiB
Rust
54 lines
1.5 KiB
Rust
//! Running a WASI compiled WebAssembly module with Wasmer.
|
|
//!
|
|
//! This example illustrates how to run WASI modules with
|
|
//! Wasmer. To run WASI we have to have to do mainly 3 steps:
|
|
//!
|
|
//! 1. Create a `WasiEnv` instance
|
|
//! 2. Attach the imports from the `WasiEnv` to a new instance
|
|
//! 3. Run the `WASI` module.
|
|
//!
|
|
//! You can run the example directly by executing in Wasmer root:
|
|
//!
|
|
//! ```shell
|
|
//! cargo run --example wasi --release --features "cranelift,wasi"
|
|
//! ```
|
|
//!
|
|
//! Ready?
|
|
|
|
use wasmer::{Instance, Module, Store};
|
|
use wasmer_compiler_cranelift::Cranelift;
|
|
use wasmer_wasi::WasiEnv;
|
|
|
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
let wasm_path = concat!(
|
|
env!("CARGO_MANIFEST_DIR"),
|
|
"/tests/wasi-wast/wasi/unstable/hello.wasm"
|
|
);
|
|
// Let's declare the Wasm module with the text representation.
|
|
let wasm_bytes = std::fs::read(wasm_path)?;
|
|
|
|
// Create a Store.
|
|
// Note that we don't need to specify the engine/compiler if we want to use
|
|
// the default provided by Wasmer.
|
|
// You can use `Store::default()` for that.
|
|
let mut store = Store::new(Cranelift::default());
|
|
|
|
println!("Compiling module...");
|
|
// Let's compile the Wasm module.
|
|
let module = Module::new(&store, wasm_bytes)?;
|
|
|
|
// Run the module.
|
|
let mut wasi_env = WasiEnv::builder("hello")
|
|
// .args(&["world"])
|
|
// .env("KEY", "Value")
|
|
.run_with_store(module, &mut store)?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
#[cfg(feature = "wasi")]
|
|
fn test_wasi() -> Result<(), Box<dyn std::error::Error>> {
|
|
main()
|
|
}
|