MCPcopy Create free account
hub / github.com/bytecodealliance/wasmtime / main

Function main

examples/fuel.rs:7–33  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

5use wasmtime::*;
6
7fn main() -> Result<()> {
8 let mut config = Config::new();
9 config.consume_fuel(true);
10 let engine = Engine::new(&config)?;
11 let mut store = Store::new(&engine, ());
12 store.set_fuel(10_000)?;
13 let module = Module::from_file(store.engine(), "examples/fuel.wat")?;
14 let instance = Instance::new(&mut store, &module, &[])?;
15
16 // Invoke `fibonacci` export with higher and higher numbers until we exhaust our fuel.
17 let fibonacci = instance.get_typed_func::<i32, i32>(&mut store, "fibonacci")?;
18 for n in 1.. {
19 let fuel_before = store.get_fuel().unwrap();
20 let output = match fibonacci.call(&mut store, n) {
21 Ok(v) => v,
22 Err(e) => {
23 assert_eq!(e.downcast::<Trap>()?, Trap::OutOfFuel);
24 println!("Exhausted fuel computing fib({n})");
25 break;
26 }
27 };
28 let fuel_consumed = fuel_before - store.get_fuel().unwrap();
29 println!("fib({n}) = {output} [consumed {fuel_consumed} fuel]");
30 store.set_fuel(10_000)?;
31 }
32 Ok(())
33}

Callers

nothing calls this directly

Calls 8

OkFunction · 0.85
newFunction · 0.50
consume_fuelMethod · 0.45
set_fuelMethod · 0.45
engineMethod · 0.45
unwrapMethod · 0.45
get_fuelMethod · 0.45
callMethod · 0.45

Tested by

no test coverage detected