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

Function main

examples/fuel.cc:37–63  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

35const size_t kStoreFuel = 10000;
36
37int main() {
38 Config config;
39 config.consume_fuel(true);
40 Engine engine(std::move(config));
41 Store store(engine);
42 store.context().set_fuel(kStoreFuel).unwrap();
43
44 auto wat = readFile("examples/fuel.wat");
45 Module module = Module::compile(engine, wat).unwrap();
46 Instance instance = Instance::create(store, module, {}).unwrap();
47 Func fib = std::get<Func>(*instance.get(store, "fibonacci"));
48
49 // Call it repeatedly until it fails
50 for (int32_t n = 1;; n++) {
51 auto result = fib.call(store, {n});
52 if (!result) {
53 std::cout << "Exhausted fuel computing fib(" << n << ")\n";
54 break;
55 }
56 uint64_t consumed = kStoreFuel - store.context().get_fuel().unwrap();
57 auto fib_result = std::move(result).unwrap()[0].i32();
58
59 std::cout << "fib(" << n << ") = " << fib_result << " [consumed "
60 << consumed << " fuel]\n";
61 store.context().set_fuel(kStoreFuel).unwrap();
62 }
63}

Callers

nothing calls this directly

Calls 11

readFileFunction · 0.70
compileFunction · 0.50
createFunction · 0.50
consume_fuelMethod · 0.45
unwrapMethod · 0.45
set_fuelMethod · 0.45
contextMethod · 0.45
getMethod · 0.45
callMethod · 0.45
get_fuelMethod · 0.45
i32Method · 0.45

Tested by

no test coverage detected