1 /* 2 Example of instantiating of the WebAssembly module and invoking its exported 3 function. 4 5 You can compile and run this example on Linux with: 6 7 cargo build --release -p wasmtime-c-api 8 cc examples/gcd.c \ 9 -I crates/c-api/include \ 10 -I crates/c-api/wasm-c-api/include \ 11 target/release/libwasmtime.a \ 12 -lpthread -ldl -lm \ 13 -o gcd 14 ./gcd 15 16 Note that on Windows and macOS the command will be similar, but you'll need 17 to tweak the `-lpthread` and such annotations. 18 19 You can also build using cmake: 20 21 mkdir build && cd build && cmake .. && cmake --build . --target wasmtime-gcd 22 */ 23 24 #include <assert.h> 25 #include <stdio.h> 26 #include <stdlib.h> 27 #include <wasm.h> 28 #include <wasmtime.h> 29 30 static void exit_with_error(const char *message, wasmtime_error_t *error, wasm_trap_t *trap); 31 32 int main() { 33 int ret = 0; 34 // Set up our context 35 wasm_engine_t *engine = wasm_engine_new(); 36 assert(engine != NULL); 37 wasmtime_store_t *store = wasmtime_store_new(engine, NULL, NULL); 38 assert(store != NULL); 39 wasmtime_context_t *context = wasmtime_store_context(store); 40 41 // Load our input file to parse it next 42 FILE* file = fopen("examples/gcd.wat", "r"); 43 if (!file) { 44 printf("> Error loading file!\n"); 45 return 1; 46 } 47 fseek(file, 0L, SEEK_END); 48 size_t file_size = ftell(file); 49 fseek(file, 0L, SEEK_SET); 50 wasm_byte_vec_t wat; 51 wasm_byte_vec_new_uninitialized(&wat, file_size); 52 if (fread(wat.data, file_size, 1, file) != 1) { 53 printf("> Error loading module!\n"); 54 return 1; 55 } 56 fclose(file); 57 58 // Parse the wat into the binary wasm format 59 wasm_byte_vec_t wasm; 60 wasmtime_error_t *error = wasmtime_wat2wasm(wat.data, wat.size, &wasm); 61 if (error != NULL) 62 exit_with_error("failed to parse wat", error, NULL); 63 wasm_byte_vec_delete(&wat); 64 65 // Compile and instantiate our module 66 wasmtime_module_t *module = NULL; 67 error = wasmtime_module_new(engine, (uint8_t*) wasm.data, wasm.size, &module); 68 if (module == NULL) 69 exit_with_error("failed to compile module", error, NULL); 70 wasm_byte_vec_delete(&wasm); 71 72 wasm_trap_t *trap = NULL; 73 wasmtime_instance_t instance; 74 error = wasmtime_instance_new(context, module, NULL, 0, &instance, &trap); 75 if (error != NULL || trap != NULL) 76 exit_with_error("failed to instantiate", error, trap); 77 78 // Lookup our `gcd` export function 79 wasmtime_extern_t gcd; 80 bool ok = wasmtime_instance_export_get(context, &instance, "gcd", 3, &gcd); 81 assert(ok); 82 assert(gcd.kind == WASMTIME_EXTERN_FUNC); 83 84 // And call it! 85 int a = 6; 86 int b = 27; 87 wasmtime_val_t params[2]; 88 params[0].kind = WASMTIME_I32; 89 params[0].of.i32 = a; 90 params[1].kind = WASMTIME_I32; 91 params[1].of.i32 = b; 92 wasmtime_val_t results[1]; 93 error = wasmtime_func_call(context, &gcd.of.func, params, 2, results, 1, &trap); 94 if (error != NULL || trap != NULL) 95 exit_with_error("failed to call gcd", error, trap); 96 assert(results[0].kind == WASMTIME_I32); 97 98 printf("gcd(%d, %d) = %d\n", a, b, results[0].of.i32); 99 100 // Clean up after ourselves at this point 101 ret = 0; 102 103 wasmtime_module_delete(module); 104 wasmtime_store_delete(store); 105 wasm_engine_delete(engine); 106 return ret; 107 } 108 109 static void exit_with_error(const char *message, wasmtime_error_t *error, wasm_trap_t *trap) { 110 fprintf(stderr, "error: %s\n", message); 111 wasm_byte_vec_t error_message; 112 if (error != NULL) { 113 wasmtime_error_message(error, &error_message); 114 } else { 115 wasm_trap_message(trap, &error_message); 116 } 117 fprintf(stderr, "%.*s\n", (int) error_message.size, error_message.data); 118 wasm_byte_vec_delete(&error_message); 119 exit(1); 120 } 121