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