xref: /wasmtime-44.0.1/examples/multi.c (revision bd374fd6)
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
8    cc examples/multi.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 multi
14    ./multi
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 Also note that this example was taken from
20 https://github.com/WebAssembly/wasm-c-api/blob/master/example/multi.c
21 originally
22 */
23 
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <inttypes.h>
28 #include <wasm.h>
29 #include <wasmtime.h>
30 
31 static void exit_with_error(const char *message, wasmtime_error_t *error, wasm_trap_t *trap);
32 
33 // A function to be called from Wasm code.
34 wasm_trap_t* callback(
35   const wasm_val_t args[], wasm_val_t results[]
36 ) {
37   printf("Calling back...\n");
38   printf("> %"PRIu32" %"PRIu64"\n", args[0].of.i32, args[1].of.i64);
39   printf("\n");
40 
41   wasm_val_copy(&results[0], &args[1]);
42   wasm_val_copy(&results[1], &args[0]);
43   return NULL;
44 }
45 
46 
47 // A function closure.
48 wasm_trap_t* closure_callback(
49   void* env, const wasm_val_t args[], wasm_val_t results[]
50 ) {
51   int i = *(int*)env;
52   printf("Calling back closure...\n");
53   printf("> %d\n", i);
54 
55   results[0].kind = WASM_I32;
56   results[0].of.i32 = (int32_t)i;
57   return NULL;
58 }
59 
60 
61 int main(int argc, const char* argv[]) {
62   // Initialize.
63   printf("Initializing...\n");
64   wasm_config_t *config = wasm_config_new();
65   assert(config != NULL);
66   wasmtime_config_wasm_multi_value_set(config, true);
67   wasm_engine_t* engine = wasm_engine_new_with_config(config);
68   wasm_store_t* store = wasm_store_new(engine);
69 
70   // Load our input file to parse it next
71   FILE* file = fopen("examples/multi.wat", "r");
72   if (!file) {
73     printf("> Error loading file!\n");
74     return 1;
75   }
76   fseek(file, 0L, SEEK_END);
77   size_t file_size = ftell(file);
78   fseek(file, 0L, SEEK_SET);
79   wasm_byte_vec_t wat;
80   wasm_byte_vec_new_uninitialized(&wat, file_size);
81   if (fread(wat.data, file_size, 1, file) != 1) {
82     printf("> Error loading module!\n");
83     return 1;
84   }
85   fclose(file);
86 
87   // Parse the wat into the binary wasm format
88   wasm_byte_vec_t binary;
89   wasmtime_error_t *error = wasmtime_wat2wasm(&wat, &binary);
90   if (error != NULL)
91     exit_with_error("failed to parse wat", error, NULL);
92   wasm_byte_vec_delete(&wat);
93 
94   // Compile.
95   printf("Compiling module...\n");
96   wasm_module_t* module = NULL;
97   error = wasmtime_module_new(store, &binary, &module);
98   if (error)
99     exit_with_error("failed to compile module", error, NULL);
100 
101   wasm_byte_vec_delete(&binary);
102 
103   // Create external print functions.
104   printf("Creating callback...\n");
105   wasm_functype_t* callback_type = wasm_functype_new_2_2(
106       wasm_valtype_new_i32(),
107       wasm_valtype_new_i64(),
108       wasm_valtype_new_i64(),
109       wasm_valtype_new_i32()
110   );
111   wasm_func_t* callback_func =
112     wasm_func_new(store, callback_type, callback);
113 
114   wasm_functype_delete(callback_type);
115 
116   // Instantiate.
117   printf("Instantiating module...\n");
118   const wasm_extern_t* imports[] = {wasm_func_as_extern(callback_func)};
119   wasm_instance_t* instance = NULL;
120   wasm_trap_t* trap = NULL;
121   error = wasmtime_instance_new(module, imports, 1, &instance, &trap);
122   if (!instance)
123     exit_with_error("failed to instantiate", error, trap);
124 
125   wasm_func_delete(callback_func);
126 
127   // Extract export.
128   printf("Extracting export...\n");
129   wasm_extern_vec_t exports;
130   wasm_instance_exports(instance, &exports);
131   if (exports.size == 0) {
132     printf("> Error accessing exports!\n");
133     return 1;
134   }
135   wasm_func_t* run_func = wasm_extern_as_func(exports.data[0]);
136   if (run_func == NULL) {
137     printf("> Error accessing export!\n");
138     return 1;
139   }
140 
141   wasm_module_delete(module);
142   wasm_instance_delete(instance);
143 
144   // Call.
145   printf("Calling export...\n");
146   wasm_val_t args[2];
147   args[0].kind = WASM_I32;
148   args[0].of.i32 = 1;
149   args[1].kind = WASM_I64;
150   args[1].of.i64 = 2;
151   wasm_val_t results[2];
152   error = wasmtime_func_call(run_func, args, 2, results, 2, &trap);
153   if (error != NULL || trap != NULL)
154     exit_with_error("failed to call run", error, trap);
155 
156   wasm_extern_vec_delete(&exports);
157 
158   // Print result.
159   printf("Printing result...\n");
160   printf("> %"PRIu64" %"PRIu32"\n",
161     results[0].of.i64, results[1].of.i32);
162 
163   assert(results[0].kind == WASM_I64);
164   assert(results[0].of.i64 == 2);
165   assert(results[1].kind == WASM_I32);
166   assert(results[1].of.i32 == 1);
167 
168   // Shut down.
169   printf("Shutting down...\n");
170   wasm_store_delete(store);
171   wasm_engine_delete(engine);
172 
173   // All done.
174   printf("Done.\n");
175   return 0;
176 }
177 
178 static void exit_with_error(const char *message, wasmtime_error_t *error, wasm_trap_t *trap) {
179   fprintf(stderr, "error: %s\n", message);
180   wasm_byte_vec_t error_message;
181   if (error != NULL) {
182     wasmtime_error_message(error, &error_message);
183     wasmtime_error_delete(error);
184   } else {
185     wasm_trap_message(trap, &error_message);
186     wasm_trap_delete(trap);
187   }
188   fprintf(stderr, "%.*s\n", (int) error_message.size, error_message.data);
189   wasm_byte_vec_delete(&error_message);
190   exit(1);
191 }
192