xref: /wasmtime-44.0.1/examples/serialize.c (revision a7f592a0)
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/hello.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 hello
14    ./hello
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 as well as the name of the
18 `libwasmtime.a` file on Windows.
19 */
20 
21 #include <assert.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <wasm.h>
25 #include <wasmtime.h>
26 
27 static void exit_with_error(const char *message, wasmtime_error_t *error, wasm_trap_t *trap);
28 
29 static wasm_trap_t* hello_callback(
30     void *env,
31     wasmtime_caller_t *caller,
32     const wasmtime_val_t* args,
33     size_t nargs,
34     wasmtime_val_t* results,
35     size_t nresults) {
36   printf("Calling back...\n");
37   printf("> Hello World!\n");
38   return NULL;
39 }
40 
41 int serialize(wasm_byte_vec_t* buffer) {
42   // Set up our compilation context. Note that we could also work with a
43   // `wasm_config_t` here to configure what feature are enabled and various
44   // compilation settings.
45   printf("Initializing...\n");
46   wasm_engine_t *engine = wasm_engine_new();
47   assert(engine != NULL);
48 
49   // Read our input file, which in this case is a wasm text file.
50   FILE* file = fopen("examples/hello.wat", "r");
51   assert(file != NULL);
52   fseek(file, 0L, SEEK_END);
53   size_t file_size = ftell(file);
54   fseek(file, 0L, SEEK_SET);
55   wasm_byte_vec_t wat;
56   wasm_byte_vec_new_uninitialized(&wat, file_size);
57   assert(fread(wat.data, file_size, 1, file) == 1);
58   fclose(file);
59 
60   // Parse the wat into the binary wasm format
61   wasm_byte_vec_t wasm;
62   wasmtime_error_t *error = wasmtime_wat2wasm(wat.data, wat.size, &wasm);
63   if (error != NULL)
64     exit_with_error("failed to parse wat", error, NULL);
65   wasm_byte_vec_delete(&wat);
66 
67   // Now that we've got our binary webassembly we can compile our module
68   // and serialize into buffer.
69   printf("Compiling and serializing module...\n");
70   wasmtime_module_t *module = NULL;
71   error = wasmtime_module_new(engine, (uint8_t*)wasm.data, wasm.size, &module);
72   wasm_byte_vec_delete(&wasm);
73   if (error != NULL)
74     exit_with_error("failed to compile module", error, NULL);
75   error = wasmtime_module_serialize(module, buffer);
76   wasmtime_module_delete(module);
77   if (error != NULL)
78     exit_with_error("failed to serialize module", error, NULL);
79 
80   printf("Serialized.\n");
81 
82   wasm_engine_delete(engine);
83   return 0;
84 }
85 
86 int deserialize(wasm_byte_vec_t* buffer) {
87   // Set up our compilation context. Note that we could also work with a
88   // `wasm_config_t` here to configure what feature are enabled and various
89   // compilation settings.
90   printf("Initializing...\n");
91   wasm_engine_t *engine = wasm_engine_new();
92   assert(engine != NULL);
93 
94   // With an engine we can create a *store* which is a long-lived group of wasm
95   // modules.
96   wasmtime_store_t *store = wasmtime_store_new(engine, NULL, NULL);
97   assert(store != NULL);
98   wasmtime_context_t *context = wasmtime_store_context(store);
99 
100   // Deserialize compiled module.
101   printf("Deserialize module...\n");
102   wasmtime_module_t *module = NULL;
103   wasmtime_error_t *error = wasmtime_module_deserialize(engine, (uint8_t*) buffer->data, buffer->size, &module);
104   if (error != NULL)
105     exit_with_error("failed to compile module", error, NULL);
106 
107   // Next up we need to create the function that the wasm module imports. Here
108   // we'll be hooking up a thunk function to the `hello_callback` native
109   // function above.
110   printf("Creating callback...\n");
111   wasm_functype_t *hello_ty = wasm_functype_new_0_0();
112   wasmtime_func_t hello;
113   wasmtime_func_new(context, hello_ty, hello_callback, NULL, NULL, &hello);
114 
115   // With our callback function we can now instantiate the compiled module,
116   // giving us an instance we can then execute exports from. Note that
117   // instantiation can trap due to execution of the `start` function, so we need
118   // to handle that here too.
119   printf("Instantiating module...\n");
120   wasm_trap_t *trap = NULL;
121   wasmtime_instance_t instance;
122   wasmtime_extern_t imports[1];
123   imports[0].kind = WASMTIME_EXTERN_FUNC;
124   imports[0].of.func = hello;
125   error = wasmtime_instance_new(context, module, imports, 1, &instance, &trap);
126   if (error != NULL || trap != NULL)
127     exit_with_error("failed to instantiate", error, trap);
128   wasmtime_module_delete(module);
129 
130   // Lookup our `run` export function
131   wasmtime_extern_t run;
132   bool ok = wasmtime_instance_export_get(context, &instance, "run", 3, &run);
133   assert(ok);
134   assert(run.kind == WASMTIME_EXTERN_FUNC);
135 
136   // And call it!
137   printf("Calling export...\n");
138   error = wasmtime_func_call(context, &run.of.func, NULL, 0, NULL, 0, &trap);
139   if (error != NULL || trap != NULL)
140     exit_with_error("failed to call function", error, trap);
141 
142   // Clean up after ourselves at this point
143   printf("All finished!\n");
144 
145   wasmtime_store_delete(store);
146   wasm_engine_delete(engine);
147   return 0;
148 }
149 
150 int main() {
151   wasm_byte_vec_t buffer;
152   if (serialize(&buffer)) {
153     return 1;
154   }
155   if (deserialize(&buffer)) {
156     return 1;
157   }
158   wasm_byte_vec_delete(&buffer);
159   return 0;
160 }
161 
162 static void exit_with_error(const char *message, wasmtime_error_t *error, wasm_trap_t *trap) {
163   fprintf(stderr, "error: %s\n", message);
164   wasm_byte_vec_t error_message;
165   if (error != NULL) {
166     wasmtime_error_message(error, &error_message);
167     wasmtime_error_delete(error);
168   } else {
169     wasm_trap_message(trap, &error_message);
170     wasm_trap_delete(trap);
171   }
172   fprintf(stderr, "%.*s\n", (int) error_message.size, error_message.data);
173   wasm_byte_vec_delete(&error_message);
174   exit(1);
175 }
176