1399ee0a5SYury Delendik /*
2399ee0a5SYury Delendik Example of instantiating of the WebAssembly module and invoking its exported
3399ee0a5SYury Delendik function.
4399ee0a5SYury Delendik
557ba95e9SMasashi Yoshimura You can build using cmake:
62ba3025eSTheGreatRambler
7f8fee938STyler Rockwood mkdir build && cd build && cmake .. && \
8f8fee938STyler Rockwood cmake --build . --target wasmtime-serialize
9399ee0a5SYury Delendik */
10399ee0a5SYury Delendik
11399ee0a5SYury Delendik #include <assert.h>
12399ee0a5SYury Delendik #include <stdio.h>
13399ee0a5SYury Delendik #include <stdlib.h>
14399ee0a5SYury Delendik #include <wasm.h>
15399ee0a5SYury Delendik #include <wasmtime.h>
16399ee0a5SYury Delendik
17f8fee938STyler Rockwood static void exit_with_error(const char *message, wasmtime_error_t *error,
18f8fee938STyler Rockwood wasm_trap_t *trap);
19399ee0a5SYury Delendik
hello_callback(void * env,wasmtime_caller_t * caller,const wasmtime_val_t * args,size_t nargs,wasmtime_val_t * results,size_t nresults)20f8fee938STyler Rockwood static wasm_trap_t *hello_callback(void *env, wasmtime_caller_t *caller,
21f8fee938STyler Rockwood const wasmtime_val_t *args, size_t nargs,
22f8fee938STyler Rockwood wasmtime_val_t *results, size_t nresults) {
23*adff9d9dSAlex Crichton (void)env;
24*adff9d9dSAlex Crichton (void)caller;
25*adff9d9dSAlex Crichton (void)args;
26*adff9d9dSAlex Crichton (void)nargs;
27*adff9d9dSAlex Crichton (void)results;
28*adff9d9dSAlex Crichton (void)nresults;
29399ee0a5SYury Delendik printf("Calling back...\n");
30399ee0a5SYury Delendik printf("> Hello World!\n");
31399ee0a5SYury Delendik return NULL;
32399ee0a5SYury Delendik }
33399ee0a5SYury Delendik
serialize(wasm_byte_vec_t * buffer)34399ee0a5SYury Delendik int serialize(wasm_byte_vec_t *buffer) {
35399ee0a5SYury Delendik // Set up our compilation context. Note that we could also work with a
36399ee0a5SYury Delendik // `wasm_config_t` here to configure what feature are enabled and various
37399ee0a5SYury Delendik // compilation settings.
38399ee0a5SYury Delendik printf("Initializing...\n");
39399ee0a5SYury Delendik wasm_engine_t *engine = wasm_engine_new();
40399ee0a5SYury Delendik assert(engine != NULL);
41399ee0a5SYury Delendik
42399ee0a5SYury Delendik // Read our input file, which in this case is a wasm text file.
43399ee0a5SYury Delendik FILE *file = fopen("examples/hello.wat", "r");
44399ee0a5SYury Delendik assert(file != NULL);
45399ee0a5SYury Delendik fseek(file, 0L, SEEK_END);
46399ee0a5SYury Delendik size_t file_size = ftell(file);
47399ee0a5SYury Delendik fseek(file, 0L, SEEK_SET);
48399ee0a5SYury Delendik wasm_byte_vec_t wat;
49399ee0a5SYury Delendik wasm_byte_vec_new_uninitialized(&wat, file_size);
5093580f24SBruce Mitchener if (fread(wat.data, file_size, 1, file) != 1) {
5193580f24SBruce Mitchener printf("> Error loading module!\n");
5293580f24SBruce Mitchener return 1;
5393580f24SBruce Mitchener }
54399ee0a5SYury Delendik fclose(file);
55399ee0a5SYury Delendik
56399ee0a5SYury Delendik // Parse the wat into the binary wasm format
57399ee0a5SYury Delendik wasm_byte_vec_t wasm;
587a1b7cdfSAlex Crichton wasmtime_error_t *error = wasmtime_wat2wasm(wat.data, wat.size, &wasm);
59399ee0a5SYury Delendik if (error != NULL)
60399ee0a5SYury Delendik exit_with_error("failed to parse wat", error, NULL);
61399ee0a5SYury Delendik wasm_byte_vec_delete(&wat);
62399ee0a5SYury Delendik
63399ee0a5SYury Delendik // Now that we've got our binary webassembly we can compile our module
64399ee0a5SYury Delendik // and serialize into buffer.
65399ee0a5SYury Delendik printf("Compiling and serializing module...\n");
667a1b7cdfSAlex Crichton wasmtime_module_t *module = NULL;
677a1b7cdfSAlex Crichton error = wasmtime_module_new(engine, (uint8_t *)wasm.data, wasm.size, &module);
68399ee0a5SYury Delendik wasm_byte_vec_delete(&wasm);
69399ee0a5SYury Delendik if (error != NULL)
70399ee0a5SYury Delendik exit_with_error("failed to compile module", error, NULL);
71399ee0a5SYury Delendik error = wasmtime_module_serialize(module, buffer);
727a1b7cdfSAlex Crichton wasmtime_module_delete(module);
73399ee0a5SYury Delendik if (error != NULL)
74399ee0a5SYury Delendik exit_with_error("failed to serialize module", error, NULL);
75399ee0a5SYury Delendik
76399ee0a5SYury Delendik printf("Serialized.\n");
77399ee0a5SYury Delendik
78399ee0a5SYury Delendik wasm_engine_delete(engine);
79399ee0a5SYury Delendik return 0;
80399ee0a5SYury Delendik }
81399ee0a5SYury Delendik
deserialize(wasm_byte_vec_t * buffer)82399ee0a5SYury Delendik int deserialize(wasm_byte_vec_t *buffer) {
83399ee0a5SYury Delendik // Set up our compilation context. Note that we could also work with a
84399ee0a5SYury Delendik // `wasm_config_t` here to configure what feature are enabled and various
85399ee0a5SYury Delendik // compilation settings.
86399ee0a5SYury Delendik printf("Initializing...\n");
87399ee0a5SYury Delendik wasm_engine_t *engine = wasm_engine_new();
88399ee0a5SYury Delendik assert(engine != NULL);
89399ee0a5SYury Delendik
90399ee0a5SYury Delendik // With an engine we can create a *store* which is a long-lived group of wasm
91399ee0a5SYury Delendik // modules.
927a1b7cdfSAlex Crichton wasmtime_store_t *store = wasmtime_store_new(engine, NULL, NULL);
93399ee0a5SYury Delendik assert(store != NULL);
947a1b7cdfSAlex Crichton wasmtime_context_t *context = wasmtime_store_context(store);
95399ee0a5SYury Delendik
96399ee0a5SYury Delendik // Deserialize compiled module.
97399ee0a5SYury Delendik printf("Deserialize module...\n");
987a1b7cdfSAlex Crichton wasmtime_module_t *module = NULL;
99f8fee938STyler Rockwood wasmtime_error_t *error = wasmtime_module_deserialize(
100f8fee938STyler Rockwood engine, (uint8_t *)buffer->data, buffer->size, &module);
101399ee0a5SYury Delendik if (error != NULL)
102399ee0a5SYury Delendik exit_with_error("failed to compile module", error, NULL);
103399ee0a5SYury Delendik
104399ee0a5SYury Delendik // Next up we need to create the function that the wasm module imports. Here
105399ee0a5SYury Delendik // we'll be hooking up a thunk function to the `hello_callback` native
106399ee0a5SYury Delendik // function above.
107399ee0a5SYury Delendik printf("Creating callback...\n");
108399ee0a5SYury Delendik wasm_functype_t *hello_ty = wasm_functype_new_0_0();
1097a1b7cdfSAlex Crichton wasmtime_func_t hello;
1107a1b7cdfSAlex Crichton wasmtime_func_new(context, hello_ty, hello_callback, NULL, NULL, &hello);
111*adff9d9dSAlex Crichton wasm_functype_delete(hello_ty);
112399ee0a5SYury Delendik
113399ee0a5SYury Delendik // With our callback function we can now instantiate the compiled module,
114399ee0a5SYury Delendik // giving us an instance we can then execute exports from. Note that
115399ee0a5SYury Delendik // instantiation can trap due to execution of the `start` function, so we need
116399ee0a5SYury Delendik // to handle that here too.
117399ee0a5SYury Delendik printf("Instantiating module...\n");
118399ee0a5SYury Delendik wasm_trap_t *trap = NULL;
1197a1b7cdfSAlex Crichton wasmtime_instance_t instance;
1207a1b7cdfSAlex Crichton wasmtime_extern_t imports[1];
1217a1b7cdfSAlex Crichton imports[0].kind = WASMTIME_EXTERN_FUNC;
1227a1b7cdfSAlex Crichton imports[0].of.func = hello;
1237a1b7cdfSAlex Crichton error = wasmtime_instance_new(context, module, imports, 1, &instance, &trap);
1247a1b7cdfSAlex Crichton if (error != NULL || trap != NULL)
125399ee0a5SYury Delendik exit_with_error("failed to instantiate", error, trap);
1267a1b7cdfSAlex Crichton wasmtime_module_delete(module);
127399ee0a5SYury Delendik
128399ee0a5SYury Delendik // Lookup our `run` export function
1297a1b7cdfSAlex Crichton wasmtime_extern_t run;
1307a1b7cdfSAlex Crichton bool ok = wasmtime_instance_export_get(context, &instance, "run", 3, &run);
1317a1b7cdfSAlex Crichton assert(ok);
1327a1b7cdfSAlex Crichton assert(run.kind == WASMTIME_EXTERN_FUNC);
133399ee0a5SYury Delendik
134399ee0a5SYury Delendik // And call it!
135399ee0a5SYury Delendik printf("Calling export...\n");
1367a1b7cdfSAlex Crichton error = wasmtime_func_call(context, &run.of.func, NULL, 0, NULL, 0, &trap);
137399ee0a5SYury Delendik if (error != NULL || trap != NULL)
138399ee0a5SYury Delendik exit_with_error("failed to call function", error, trap);
139399ee0a5SYury Delendik
140399ee0a5SYury Delendik // Clean up after ourselves at this point
141399ee0a5SYury Delendik printf("All finished!\n");
142399ee0a5SYury Delendik
1437a1b7cdfSAlex Crichton wasmtime_store_delete(store);
144399ee0a5SYury Delendik wasm_engine_delete(engine);
145399ee0a5SYury Delendik return 0;
146399ee0a5SYury Delendik }
147399ee0a5SYury Delendik
main()148399ee0a5SYury Delendik int main() {
149399ee0a5SYury Delendik wasm_byte_vec_t buffer;
150399ee0a5SYury Delendik if (serialize(&buffer)) {
151399ee0a5SYury Delendik return 1;
152399ee0a5SYury Delendik }
153399ee0a5SYury Delendik if (deserialize(&buffer)) {
154399ee0a5SYury Delendik return 1;
155399ee0a5SYury Delendik }
156399ee0a5SYury Delendik wasm_byte_vec_delete(&buffer);
157399ee0a5SYury Delendik return 0;
158399ee0a5SYury Delendik }
159399ee0a5SYury Delendik
exit_with_error(const char * message,wasmtime_error_t * error,wasm_trap_t * trap)160f8fee938STyler Rockwood static void exit_with_error(const char *message, wasmtime_error_t *error,
161f8fee938STyler Rockwood wasm_trap_t *trap) {
162399ee0a5SYury Delendik fprintf(stderr, "error: %s\n", message);
163399ee0a5SYury Delendik wasm_byte_vec_t error_message;
164399ee0a5SYury Delendik if (error != NULL) {
165399ee0a5SYury Delendik wasmtime_error_message(error, &error_message);
166399ee0a5SYury Delendik wasmtime_error_delete(error);
167399ee0a5SYury Delendik } else {
168399ee0a5SYury Delendik wasm_trap_message(trap, &error_message);
169399ee0a5SYury Delendik wasm_trap_delete(trap);
170399ee0a5SYury Delendik }
171399ee0a5SYury Delendik fprintf(stderr, "%.*s\n", (int)error_message.size, error_message.data);
172399ee0a5SYury Delendik wasm_byte_vec_delete(&error_message);
173399ee0a5SYury Delendik exit(1);
174399ee0a5SYury Delendik }
175