summaryrefslogtreecommitdiff
path: root/examples/wasi_p1.rs
diff options
context:
space:
mode:
authorMica White <botahamec@outlook.com>2026-10-31 11:10:49 -0400
committerMica White <botahamec@outlook.com>2026-10-31 11:10:49 -0400
commit264ea48cc8c6dd9c3c9e0fe1a559252863fc9962 (patch)
tree84a4a58de14442f4fcfa06bd0d82480508f82759 /examples/wasi_p1.rs
parent7877eca4e8be283e7c51168ee3a3b05d9000f39c (diff)
parentb45a98a85c776d956a797773acd2f198c06b40ed (diff)
Merge branch 'main' into bota-tortuise
Diffstat (limited to 'examples/wasi_p1.rs')
-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(())
+}