summaryrefslogtreecommitdiff
path: root/examples/resumable.rs
diff options
context:
space:
mode:
authorHenry <mail@henrygressmann.de>2026-04-03 00:36:57 +0200
committerHenry <mail@henrygressmann.de>2026-04-03 00:36:57 +0200
commitca6c66af87106b097a599e344d5c0cb538250abb (patch)
tree1caf44f96a8dd82883e68fe1df8e89ccd418b37b /examples/resumable.rs
parentbfeefda25407d29a843d2aec713943843f8f1239 (diff)
feat: add resumable calls / fuel tracking
Signed-off-by: Henry <mail@henrygressmann.de>
Diffstat (limited to 'examples/resumable.rs')
-rw-r--r--examples/resumable.rs46
1 files changed, 46 insertions, 0 deletions
diff --git a/examples/resumable.rs b/examples/resumable.rs
new file mode 100644
index 0000000..6838e20
--- /dev/null
+++ b/examples/resumable.rs
@@ -0,0 +1,46 @@
+use eyre::Result;
+use tinywasm::{ExecProgress, Module, Store};
+
+const WASM: &str = r#"
+(module
+ (func (export "count_down") (param $n i32) (result i32)
+ (local $cur i32)
+ local.get $n
+ local.set $cur
+ block
+ loop
+ local.get $cur
+ i32.eqz
+ br_if 1
+ local.get $cur
+ i32.const 1
+ i32.sub
+ local.set $cur
+ br 0
+ end
+ end
+ local.get $cur))
+"#;
+
+fn main() -> Result<()> {
+ let wasm = wat::parse_str(WASM)?;
+ let module = Module::parse_bytes(&wasm)?;
+ let mut store = Store::default();
+ let instance = module.instantiate(&mut store, None)?;
+ let count_down = instance.exported_func::<i32, i32>(&store, "count_down")?;
+
+ let mut execution = count_down.call_resumable(&mut store, 10_000)?;
+ let fuel_per_round = 128;
+ let mut fuel_rounds = 0;
+
+ let result = loop {
+ fuel_rounds += 1;
+ match execution.resume_with_fuel(fuel_per_round)? {
+ ExecProgress::Completed(value) => break value,
+ ExecProgress::Suspended => {}
+ }
+ };
+
+ println!("completed in {fuel_rounds} rounds of {fuel_per_round} fuel, result={result}");
+ Ok(())
+}