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

Function main

examples/fuel.c:21–123  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

19 wasm_trap_t *trap);
20
21int main() {
22 wasmtime_error_t *error = NULL;
23
24 wasm_config_t *config = wasm_config_new();
25 assert(config != NULL);
26 wasmtime_config_consume_fuel_set(config, true);
27
28 // Create an *engine*, which is a compilation context, with our configured
29 // options.
30 wasm_engine_t *engine = wasm_engine_new_with_config(config);
31 assert(engine != NULL);
32 wasmtime_store_t *store = wasmtime_store_new(engine, NULL, NULL);
33 assert(store != NULL);
34 wasmtime_context_t *context = wasmtime_store_context(store);
35
36 error = wasmtime_context_set_fuel(context, 10000);
37 if (error != NULL)
38 exit_with_error("failed to set fuel", error, NULL);
39
40 // Load our input file to parse it next
41 FILE *file = fopen("examples/fuel.wat", "r");
42 if (!file) {
43 printf("> Error loading file!\n");
44 return 1;
45 }
46 fseek(file, 0L, SEEK_END);
47 size_t file_size = ftell(file);
48 fseek(file, 0L, SEEK_SET);
49 wasm_byte_vec_t wat;
50 wasm_byte_vec_new_uninitialized(&wat, file_size);
51 if (fread(wat.data, file_size, 1, file) != 1) {
52 printf("> Error loading module!\n");
53 return 1;
54 }
55 fclose(file);
56
57 // Parse the wat into the binary wasm format
58 wasm_byte_vec_t wasm;
59 error = wasmtime_wat2wasm(wat.data, wat.size, &wasm);
60 if (error != NULL)
61 exit_with_error("failed to parse wat", error, NULL);
62 wasm_byte_vec_delete(&wat);
63
64 // Compile and instantiate our module
65 wasmtime_module_t *module = NULL;
66 error = wasmtime_module_new(engine, (uint8_t *)wasm.data, wasm.size, &module);
67 if (module == NULL)
68 exit_with_error("failed to compile module", error, NULL);
69 wasm_byte_vec_delete(&wasm);
70
71 wasm_trap_t *trap = NULL;
72 wasmtime_instance_t instance;
73 error = wasmtime_instance_new(context, module, NULL, 0, &instance, &trap);
74 if (error != NULL || trap != NULL)
75 exit_with_error("failed to instantiate", error, trap);
76
77 // Lookup our `fibonacci` export function
78 wasmtime_extern_t fib;

Callers

nothing calls this directly

Calls 15

wasm_config_newFunction · 0.85
assertFunction · 0.85
wasmtime_store_newFunction · 0.85
wasmtime_store_contextFunction · 0.85
wasmtime_wat2wasmFunction · 0.85
wasmtime_module_newFunction · 0.85
wasmtime_instance_newFunction · 0.85

Tested by

no test coverage detected