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