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

Function main

examples/interrupt.c:47–116  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

45 wasm_trap_t *trap);
46
47int main() {
48 // Create a `wasm_store_t` with interrupts enabled
49 wasm_config_t *config = wasm_config_new();
50 assert(config != NULL);
51 wasmtime_config_epoch_interruption_set(config, true);
52 wasm_engine_t *engine = wasm_engine_new_with_config(config);
53 assert(engine != NULL);
54 wasmtime_store_t *store = wasmtime_store_new(engine, NULL, NULL);
55 assert(store != NULL);
56 wasmtime_context_t *context = wasmtime_store_context(store);
57
58 // Configure the epoch deadline after which WebAssembly code will trap.
59 wasmtime_context_set_epoch_deadline(context, 1);
60
61 // Read our input file, which in this case is a wasm text file.
62 FILE *file = fopen("examples/interrupt.wat", "r");
63 assert(file != NULL);
64 fseek(file, 0L, SEEK_END);
65 size_t file_size = ftell(file);
66 fseek(file, 0L, SEEK_SET);
67 wasm_byte_vec_t wat;
68 wasm_byte_vec_new_uninitialized(&wat, file_size);
69 if (fread(wat.data, file_size, 1, file) != 1) {
70 printf("> Error loading module!\n");
71 return 1;
72 }
73 fclose(file);
74
75 // Parse the wat into the binary wasm format
76 wasm_byte_vec_t wasm;
77 wasmtime_error_t *error = wasmtime_wat2wasm(wat.data, wat.size, &wasm);
78 if (error != NULL)
79 exit_with_error("failed to parse wat", error, NULL);
80 wasm_byte_vec_delete(&wat);
81
82 // Now that we've got our binary webassembly we can compile our module.
83 wasmtime_module_t *module = NULL;
84 error = wasmtime_module_new(engine, (uint8_t *)wasm.data, wasm.size, &module);
85 wasm_byte_vec_delete(&wasm);
86 if (error != NULL)
87 exit_with_error("failed to compile module", error, NULL);
88
89 wasm_trap_t *trap = NULL;
90 wasmtime_instance_t instance;
91 error = wasmtime_instance_new(context, module, NULL, 0, &instance, &trap);
92 if (error != NULL || trap != NULL)
93 exit_with_error("failed to instantiate", error, trap);
94 wasmtime_module_delete(module);
95
96 // Lookup our `run` export function
97 wasmtime_extern_t run;
98 bool ok = wasmtime_instance_export_get(context, &instance, "run", 3, &run);
99 assert(ok);
100 assert(run.kind == WASMTIME_EXTERN_FUNC);
101
102 // Spawn a thread to send us an interrupt after a period of time.
103 spawn_interrupt(engine);
104

Callers

nothing calls this directly

Calls 14

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
spawn_interruptFunction · 0.85

Tested by

no test coverage detected