xref: /wasmtime-44.0.1/examples/multi.c (revision eb650f6f)
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/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_engine_t* engine = wasm_engine_new();
65   wasm_store_t* store = wasm_store_new(engine);
66 
67   // Load our input file to parse it next
68   FILE* file = fopen("examples/multi.wat", "r");
69   if (!file) {
70     printf("> Error loading file!\n");
71     return 1;
72   }
73   fseek(file, 0L, SEEK_END);
74   size_t file_size = ftell(file);
75   fseek(file, 0L, SEEK_SET);
76   wasm_byte_vec_t wat;
77   wasm_byte_vec_new_uninitialized(&wat, file_size);
78   if (fread(wat.data, file_size, 1, file) != 1) {
79     printf("> Error loading module!\n");
80     return 1;
81   }
82   fclose(file);
83 
84   // Parse the wat into the binary wasm format
85   wasm_byte_vec_t binary;
86   wasmtime_error_t *error = wasmtime_wat2wasm(&wat, &binary);
87   if (error != NULL)
88     exit_with_error("failed to parse wat", error, NULL);
89   wasm_byte_vec_delete(&wat);
90 
91   // Compile.
92   printf("Compiling module...\n");
93   wasm_module_t* module = NULL;
94   error = wasmtime_module_new(engine, &binary, &module);
95   if (error)
96     exit_with_error("failed to compile module", error, NULL);
97 
98   wasm_byte_vec_delete(&binary);
99 
100   // Create external print functions.
101   printf("Creating callback...\n");
102   wasm_functype_t* callback_type = wasm_functype_new_2_2(
103       wasm_valtype_new_i32(),
104       wasm_valtype_new_i64(),
105       wasm_valtype_new_i64(),
106       wasm_valtype_new_i32()
107   );
108   wasm_func_t* callback_func =
109     wasm_func_new(store, callback_type, callback);
110 
111   wasm_functype_delete(callback_type);
112 
113   // Instantiate.
114   printf("Instantiating module...\n");
115   const wasm_extern_t* imports[] = {wasm_func_as_extern(callback_func)};
116   wasm_instance_t* instance = NULL;
117   wasm_trap_t* trap = NULL;
118   error = wasmtime_instance_new(store, module, imports, 1, &instance, &trap);
119   if (!instance)
120     exit_with_error("failed to instantiate", error, trap);
121 
122   wasm_func_delete(callback_func);
123 
124   // Extract export.
125   printf("Extracting export...\n");
126   wasm_extern_vec_t exports;
127   wasm_instance_exports(instance, &exports);
128   if (exports.size == 0) {
129     printf("> Error accessing exports!\n");
130     return 1;
131   }
132   wasm_func_t* run_func = wasm_extern_as_func(exports.data[0]);
133   if (run_func == NULL) {
134     printf("> Error accessing export!\n");
135     return 1;
136   }
137 
138   wasm_module_delete(module);
139   wasm_instance_delete(instance);
140 
141   // Call.
142   printf("Calling export...\n");
143   wasm_val_t args[2];
144   args[0].kind = WASM_I32;
145   args[0].of.i32 = 1;
146   args[1].kind = WASM_I64;
147   args[1].of.i64 = 2;
148   wasm_val_t results[2];
149   error = wasmtime_func_call(run_func, args, 2, results, 2, &trap);
150   if (error != NULL || trap != NULL)
151     exit_with_error("failed to call run", error, trap);
152 
153   wasm_extern_vec_delete(&exports);
154 
155   // Print result.
156   printf("Printing result...\n");
157   printf("> %"PRIu64" %"PRIu32"\n",
158     results[0].of.i64, results[1].of.i32);
159 
160   assert(results[0].kind == WASM_I64);
161   assert(results[0].of.i64 == 2);
162   assert(results[1].kind == WASM_I32);
163   assert(results[1].of.i32 == 1);
164 
165   // Shut down.
166   printf("Shutting down...\n");
167   wasm_store_delete(store);
168   wasm_engine_delete(engine);
169 
170   // All done.
171   printf("Done.\n");
172   return 0;
173 }
174 
175 static void exit_with_error(const char *message, wasmtime_error_t *error, wasm_trap_t *trap) {
176   fprintf(stderr, "error: %s\n", message);
177   wasm_byte_vec_t error_message;
178   if (error != NULL) {
179     wasmtime_error_message(error, &error_message);
180     wasmtime_error_delete(error);
181   } else {
182     wasm_trap_message(trap, &error_message);
183     wasm_trap_delete(trap);
184   }
185   fprintf(stderr, "%.*s\n", (int) error_message.size, error_message.data);
186   wasm_byte_vec_delete(&error_message);
187   exit(1);
188 }
189