| 19 | wasm_trap_t *trap); |
| 20 | |
| 21 | int main() { |
| 22 | int ret = 0; |
| 23 | // Set up our context |
| 24 | wasm_engine_t *engine = wasm_engine_new(); |
| 25 | assert(engine != NULL); |
| 26 | wasmtime_store_t *store = wasmtime_store_new(engine, NULL, NULL); |
| 27 | assert(store != NULL); |
| 28 | wasmtime_context_t *context = wasmtime_store_context(store); |
| 29 | |
| 30 | // Load our input file to parse it next |
| 31 | FILE *file = fopen("examples/gcd.wat", "r"); |
| 32 | if (!file) { |
| 33 | printf("> Error loading file!\n"); |
| 34 | return 1; |
| 35 | } |
| 36 | fseek(file, 0L, SEEK_END); |
| 37 | size_t file_size = ftell(file); |
| 38 | fseek(file, 0L, SEEK_SET); |
| 39 | wasm_byte_vec_t wat; |
| 40 | wasm_byte_vec_new_uninitialized(&wat, file_size); |
| 41 | if (fread(wat.data, file_size, 1, file) != 1) { |
| 42 | printf("> Error loading module!\n"); |
| 43 | return 1; |
| 44 | } |
| 45 | fclose(file); |
| 46 | |
| 47 | // Parse the wat into the binary wasm format |
| 48 | wasm_byte_vec_t wasm; |
| 49 | wasmtime_error_t *error = wasmtime_wat2wasm(wat.data, wat.size, &wasm); |
| 50 | if (error != NULL) |
| 51 | exit_with_error("failed to parse wat", error, NULL); |
| 52 | wasm_byte_vec_delete(&wat); |
| 53 | |
| 54 | // Compile and instantiate our module |
| 55 | wasmtime_module_t *module = NULL; |
| 56 | error = wasmtime_module_new(engine, (uint8_t *)wasm.data, wasm.size, &module); |
| 57 | if (module == NULL) |
| 58 | exit_with_error("failed to compile module", error, NULL); |
| 59 | wasm_byte_vec_delete(&wasm); |
| 60 | |
| 61 | wasm_trap_t *trap = NULL; |
| 62 | wasmtime_instance_t instance; |
| 63 | error = wasmtime_instance_new(context, module, NULL, 0, &instance, &trap); |
| 64 | if (error != NULL || trap != NULL) |
| 65 | exit_with_error("failed to instantiate", error, trap); |
| 66 | |
| 67 | // Lookup our `gcd` export function |
| 68 | wasmtime_extern_t gcd; |
| 69 | bool ok = wasmtime_instance_export_get(context, &instance, "gcd", 3, &gcd); |
| 70 | assert(ok); |
| 71 | assert(gcd.kind == WASMTIME_EXTERN_FUNC); |
| 72 | |
| 73 | // And call it! |
| 74 | int a = 6; |
| 75 | int b = 27; |
| 76 | wasmtime_val_t params[2]; |
| 77 | params[0].kind = WASMTIME_I32; |
| 78 | params[0].of.i32 = a; |
nothing calls this directly
no test coverage detected