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