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 assert(fread(wat.data, file_size, 1, file) == 1); 60 fclose(file); 61 62 // Parse the wat into the binary wasm format 63 wasm_byte_vec_t wasm; 64 wasmtime_error_t *error = wasmtime_wat2wasm(wat.data, wat.size, &wasm); 65 if (error != NULL) 66 exit_with_error("failed to parse wat", error, NULL); 67 wasm_byte_vec_delete(&wat); 68 69 // Now that we've got our binary webassembly we can compile our module 70 // and serialize into buffer. 71 printf("Compiling and serializing module...\n"); 72 wasmtime_module_t *module = NULL; 73 error = wasmtime_module_new(engine, (uint8_t *)wasm.data, wasm.size, &module); 74 wasm_byte_vec_delete(&wasm); 75 if (error != NULL) 76 exit_with_error("failed to compile module", error, NULL); 77 error = wasmtime_module_serialize(module, buffer); 78 wasmtime_module_delete(module); 79 if (error != NULL) 80 exit_with_error("failed to serialize module", error, NULL); 81 82 printf("Serialized.\n"); 83 84 wasm_engine_delete(engine); 85 return 0; 86 } 87 88 int deserialize(wasm_byte_vec_t *buffer) { 89 // Set up our compilation context. Note that we could also work with a 90 // `wasm_config_t` here to configure what feature are enabled and various 91 // compilation settings. 92 printf("Initializing...\n"); 93 wasm_engine_t *engine = wasm_engine_new(); 94 assert(engine != NULL); 95 96 // With an engine we can create a *store* which is a long-lived group of wasm 97 // modules. 98 wasmtime_store_t *store = wasmtime_store_new(engine, NULL, NULL); 99 assert(store != NULL); 100 wasmtime_context_t *context = wasmtime_store_context(store); 101 102 // Deserialize compiled module. 103 printf("Deserialize module...\n"); 104 wasmtime_module_t *module = NULL; 105 wasmtime_error_t *error = wasmtime_module_deserialize( 106 engine, (uint8_t *)buffer->data, buffer->size, &module); 107 if (error != NULL) 108 exit_with_error("failed to compile module", error, NULL); 109 110 // Next up we need to create the function that the wasm module imports. Here 111 // we'll be hooking up a thunk function to the `hello_callback` native 112 // function above. 113 printf("Creating callback...\n"); 114 wasm_functype_t *hello_ty = wasm_functype_new_0_0(); 115 wasmtime_func_t hello; 116 wasmtime_func_new(context, hello_ty, hello_callback, NULL, NULL, &hello); 117 118 // With our callback function we can now instantiate the compiled module, 119 // giving us an instance we can then execute exports from. Note that 120 // instantiation can trap due to execution of the `start` function, so we need 121 // to handle that here too. 122 printf("Instantiating module...\n"); 123 wasm_trap_t *trap = NULL; 124 wasmtime_instance_t instance; 125 wasmtime_extern_t imports[1]; 126 imports[0].kind = WASMTIME_EXTERN_FUNC; 127 imports[0].of.func = hello; 128 error = wasmtime_instance_new(context, module, imports, 1, &instance, &trap); 129 if (error != NULL || trap != NULL) 130 exit_with_error("failed to instantiate", error, trap); 131 wasmtime_module_delete(module); 132 133 // Lookup our `run` export function 134 wasmtime_extern_t run; 135 bool ok = wasmtime_instance_export_get(context, &instance, "run", 3, &run); 136 assert(ok); 137 assert(run.kind == WASMTIME_EXTERN_FUNC); 138 139 // And call it! 140 printf("Calling export...\n"); 141 error = wasmtime_func_call(context, &run.of.func, NULL, 0, NULL, 0, &trap); 142 if (error != NULL || trap != NULL) 143 exit_with_error("failed to call function", error, trap); 144 145 // Clean up after ourselves at this point 146 printf("All finished!\n"); 147 148 wasmtime_store_delete(store); 149 wasm_engine_delete(engine); 150 return 0; 151 } 152 153 int main() { 154 wasm_byte_vec_t buffer; 155 if (serialize(&buffer)) { 156 return 1; 157 } 158 if (deserialize(&buffer)) { 159 return 1; 160 } 161 wasm_byte_vec_delete(&buffer); 162 return 0; 163 } 164 165 static void exit_with_error(const char *message, wasmtime_error_t *error, 166 wasm_trap_t *trap) { 167 fprintf(stderr, "error: %s\n", message); 168 wasm_byte_vec_t error_message; 169 if (error != NULL) { 170 wasmtime_error_message(error, &error_message); 171 wasmtime_error_delete(error); 172 } else { 173 wasm_trap_message(trap, &error_message); 174 wasm_trap_delete(trap); 175 } 176 fprintf(stderr, "%.*s\n", (int)error_message.size, error_message.data); 177 wasm_byte_vec_delete(&error_message); 178 exit(1); 179 } 180