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