xref: /wasmtime-44.0.1/examples/gcd.c (revision 331b0dee)
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 
20 #include <assert.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <wasm.h>
24 #include <wasmtime.h>
25 
26 static void exit_with_error(const char *message, wasmtime_error_t *error, wasm_trap_t *trap);
27 
28 int main() {
29   int ret = 0;
30   // Set up our context
31   wasm_engine_t *engine = wasm_engine_new();
32   assert(engine != NULL);
33   wasmtime_store_t *store = wasmtime_store_new(engine, NULL, NULL);
34   assert(store != NULL);
35   wasmtime_context_t *context = wasmtime_store_context(store);
36 
37   // Load our input file to parse it next
38   FILE* file = fopen("examples/gcd.wat", "r");
39   if (!file) {
40     printf("> Error loading file!\n");
41     return 1;
42   }
43   fseek(file, 0L, SEEK_END);
44   size_t file_size = ftell(file);
45   fseek(file, 0L, SEEK_SET);
46   wasm_byte_vec_t wat;
47   wasm_byte_vec_new_uninitialized(&wat, file_size);
48   if (fread(wat.data, file_size, 1, file) != 1) {
49     printf("> Error loading module!\n");
50     return 1;
51   }
52   fclose(file);
53 
54   // Parse the wat into the binary wasm format
55   wasm_byte_vec_t wasm;
56   wasmtime_error_t *error = wasmtime_wat2wasm(wat.data, wat.size, &wasm);
57   if (error != NULL)
58     exit_with_error("failed to parse wat", error, NULL);
59   wasm_byte_vec_delete(&wat);
60 
61   // Compile and instantiate our module
62   wasmtime_module_t *module = NULL;
63   error = wasmtime_module_new(engine, (uint8_t*) wasm.data, wasm.size, &module);
64   if (module == NULL)
65     exit_with_error("failed to compile module", error, NULL);
66   wasm_byte_vec_delete(&wasm);
67 
68   wasm_trap_t *trap = NULL;
69   wasmtime_instance_t instance;
70   error = wasmtime_instance_new(context, module, NULL, 0, &instance, &trap);
71   if (error != NULL || trap != NULL)
72     exit_with_error("failed to instantiate", error, trap);
73 
74   // Lookup our `gcd` export function
75   wasmtime_extern_t gcd;
76   bool ok = wasmtime_instance_export_get(context, &instance, "gcd", 3, &gcd);
77   assert(ok);
78   assert(gcd.kind == WASMTIME_EXTERN_FUNC);
79 
80   // And call it!
81   int a = 6;
82   int b = 27;
83   wasmtime_val_t params[2];
84   params[0].kind = WASMTIME_I32;
85   params[0].of.i32 = a;
86   params[1].kind = WASMTIME_I32;
87   params[1].of.i32 = b;
88   wasmtime_val_t results[1];
89   error = wasmtime_func_call(context, &gcd.of.func, params, 2, results, 1, &trap);
90   if (error != NULL || trap != NULL)
91     exit_with_error("failed to call gcd", error, trap);
92   assert(results[0].kind == WASMTIME_I32);
93 
94   printf("gcd(%d, %d) = %d\n", a, b, results[0].of.i32);
95 
96   // Clean up after ourselves at this point
97   ret = 0;
98 
99   wasmtime_module_delete(module);
100   wasmtime_store_delete(store);
101   wasm_engine_delete(engine);
102   return ret;
103 }
104 
105 static void exit_with_error(const char *message, wasmtime_error_t *error, wasm_trap_t *trap) {
106   fprintf(stderr, "error: %s\n", message);
107   wasm_byte_vec_t error_message;
108   if (error != NULL) {
109     wasmtime_error_message(error, &error_message);
110   } else {
111     wasm_trap_message(trap, &error_message);
112   }
113   fprintf(stderr, "%.*s\n", (int) error_message.size, error_message.data);
114   wasm_byte_vec_delete(&error_message);
115   exit(1);
116 }
117