summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/wasi_p1.rs42
1 files changed, 42 insertions, 0 deletions
diff --git a/examples/wasi_p1.rs b/examples/wasi_p1.rs
new file mode 100644
index 0000000..22024dc
--- /dev/null
+++ b/examples/wasi_p1.rs
@@ -0,0 +1,42 @@
+use anyhow::Result;
+use tinywasm::{ModuleInstance, Store};
+use tinywasm_wasi::p1::{WasiCtx, imports};
+
+const WASM: &str = r#"
+(module
+ (import "wasi_snapshot_preview1" "fd_write"
+ (func $fd_write (param i32 i32 i32 i32) (result i32)))
+ (memory (export "memory") 1)
+ (data (i32.const 16) "Hello from WASI Preview 1!\n")
+
+ (func (export "_start")
+ i32.const 0
+ i32.const 16
+ i32.store
+ i32.const 4
+ i32.const 27
+ i32.store
+
+ i32.const 1
+ i32.const 0
+ i32.const 1
+ i32.const 8
+ call $fd_write
+ drop))
+"#;
+
+// Requires TinyWasm's `parser`, `std`, and `state` features.
+// Run with `cargo run --example wasi_p1` from the repository root.
+fn main() -> Result<()> {
+ let wasm = wat::parse_str(WASM)?;
+ let module = tinywasm::parse_bytes(&wasm)?;
+
+ // The host explicitly chooses what the guest can observe.
+ let wasi = WasiCtx::new().with_args(["wasi_p1.wasm", "example"])?.with_env([("MODE", "example")])?.inherit_stdio();
+ let mut store = Store::default().with_state(wasi);
+
+ // Preview 1 imports are reusable because their state comes from the Store.
+ let instance = ModuleInstance::instantiate(&mut store, &module, Some(&imports()))?;
+ instance.func::<(), ()>(&store, "_start")?.call(&mut store, ())?;
+ Ok(())
+}