xref: /wasmtime-44.0.1/examples/gcd.c (revision d74b34ff)
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        target/release/libwasmtime.a \
11        -lpthread -ldl -lm \
12        -o gcd
13    ./gcd
14 
15 Note that on Windows and macOS the command will be similar, but you'll need
16 to tweak the `-lpthread` and such annotations.
17 
18 You can also build using cmake:
19 
20 mkdir build && cd build && cmake .. && cmake --build . --target wasmtime-gcd
21 */
22 
23 #include <assert.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <wasm.h>
27 #include <wasmtime.h>
28 
29 static void exit_with_error(const char *message, wasmtime_error_t *error,
30                             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 =
94       wasmtime_func_call(context, &gcd.of.func, params, 2, results, 1, &trap);
95   if (error != NULL || trap != NULL)
96     exit_with_error("failed to call gcd", error, trap);
97   assert(results[0].kind == WASMTIME_I32);
98 
99   printf("gcd(%d, %d) = %d\n", a, b, results[0].of.i32);
100 
101   // Clean up after ourselves at this point
102   ret = 0;
103 
104   wasmtime_module_delete(module);
105   wasmtime_store_delete(store);
106   wasm_engine_delete(engine);
107   return ret;
108 }
109 
110 static void exit_with_error(const char *message, wasmtime_error_t *error,
111                             wasm_trap_t *trap) {
112   fprintf(stderr, "error: %s\n", message);
113   wasm_byte_vec_t error_message;
114   if (error != NULL) {
115     wasmtime_error_message(error, &error_message);
116   } else {
117     wasm_trap_message(trap, &error_message);
118   }
119   fprintf(stderr, "%.*s\n", (int)error_message.size, error_message.data);
120   wasm_byte_vec_delete(&error_message);
121   exit(1);
122 }
123