xref: /wasmtime-44.0.1/examples/fib-debug/main.c (revision d74b34ff)
1 #include <inttypes.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <wasm.h>
6 #include <wasmtime.h>
7 
8 #define own
9 
10 static void exit_with_error(const char *message, wasmtime_error_t *error,
11                             wasm_trap_t *trap);
12 
13 int main(int argc, const char *argv[]) {
14   // Configuring engine to support generating of DWARF info.
15   // lldb can be used to attach to the program and observe
16   // original fib-wasm.c source code and variables.
17   wasm_config_t *config = wasm_config_new();
18   wasmtime_config_debug_info_set(config, true);
19 
20   // Initialize.
21   printf("Initializing...\n");
22   wasm_engine_t *engine = wasm_engine_new_with_config(config);
23   wasmtime_store_t *store = wasmtime_store_new(engine, NULL, NULL);
24   wasmtime_context_t *context = wasmtime_store_context(store);
25 
26   // Load binary.
27   printf("Loading binary...\n");
28   FILE *file = fopen("target/wasm32-unknown-unknown/debug/fib.wasm", "rb");
29   if (!file) {
30     printf("> Error opening module!\n");
31     return 1;
32   }
33   fseek(file, 0L, SEEK_END);
34   size_t file_size = ftell(file);
35   fseek(file, 0L, SEEK_SET);
36   wasm_byte_vec_t binary;
37   wasm_byte_vec_new_uninitialized(&binary, file_size);
38   if (fread(binary.data, file_size, 1, file) != 1) {
39     printf("> Error reading module!\n");
40     return 1;
41   }
42   fclose(file);
43 
44   // Compile.
45   printf("Compiling module...\n");
46   wasmtime_module_t *module = NULL;
47   wasmtime_error_t *error =
48       wasmtime_module_new(engine, (uint8_t *)binary.data, binary.size, &module);
49   if (!module)
50     exit_with_error("failed to compile module", error, NULL);
51   wasm_byte_vec_delete(&binary);
52 
53   // Instantiate.
54   printf("Instantiating module...\n");
55   wasmtime_instance_t instance;
56   wasm_trap_t *trap = NULL;
57   error = wasmtime_instance_new(context, module, NULL, 0, &instance, &trap);
58   if (error != NULL || trap != NULL)
59     exit_with_error("failed to instantiate", error, trap);
60   wasmtime_module_delete(module);
61 
62   // Extract export.
63   wasmtime_extern_t fib;
64   bool ok = wasmtime_instance_export_get(context, &instance, "fib", 3, &fib);
65   assert(ok);
66 
67   // Call.
68   printf("Calling fib...\n");
69   wasmtime_val_t params[1];
70   params[0].kind = WASMTIME_I32;
71   params[0].of.i32 = 6;
72   wasmtime_val_t results[1];
73   error =
74       wasmtime_func_call(context, &fib.of.func, params, 1, results, 1, &trap);
75   if (error != NULL || trap != NULL)
76     exit_with_error("failed to call function", error, trap);
77 
78   assert(results[0].kind == WASMTIME_I32);
79   printf("> fib(6) = %d\n", results[0].of.i32);
80 
81   // Shut down.
82   printf("Shutting down...\n");
83   wasmtime_store_delete(store);
84   wasm_engine_delete(engine);
85 
86   // All done.
87   printf("Done.\n");
88   return 0;
89 }
90 
91 static void exit_with_error(const char *message, wasmtime_error_t *error,
92                             wasm_trap_t *trap) {
93   fprintf(stderr, "error: %s\n", message);
94   wasm_byte_vec_t error_message;
95   if (error != NULL) {
96     wasmtime_error_message(error, &error_message);
97   } else {
98     wasm_trap_message(trap, &error_message);
99   }
100   fprintf(stderr, "%.*s\n", (int)error_message.size, error_message.data);
101   wasm_byte_vec_delete(&error_message);
102   exit(1);
103 }
104