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