1 /* 2 Example of compiling, instantiating, and linking two WebAssembly modules 3 together. 4 5 You can compile and run this example on Linux with: 6 7 cargo build --release -p wasmtime-c-api 8 cc examples/linking.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 linking 14 ./linking 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. 18 */ 19 20 #include <assert.h> 21 #include <stdio.h> 22 #include <stdlib.h> 23 #include <wasm.h> 24 #include <wasi.h> 25 #include <wasmtime.h> 26 27 #define MIN(a, b) ((a) < (b) ? (a) : (b)) 28 29 static void exit_with_error(const char *message, wasmtime_error_t *error, wasm_trap_t *trap); 30 static void read_wat_file(wasm_engine_t *engine, wasm_byte_vec_t *bytes, const char *file); 31 32 int main() { 33 // Set up our context 34 wasm_engine_t *engine = wasm_engine_new(); 35 assert(engine != NULL); 36 wasmtime_store_t *store = wasmtime_store_new(engine, NULL, NULL); 37 assert(store != NULL); 38 wasmtime_context_t *context = wasmtime_store_context(store); 39 40 wasm_byte_vec_t linking1_wasm, linking2_wasm; 41 read_wat_file(engine, &linking1_wasm, "examples/linking1.wat"); 42 read_wat_file(engine, &linking2_wasm, "examples/linking2.wat"); 43 44 // Compile our two modules 45 wasmtime_error_t *error; 46 wasmtime_module_t *linking1_module = NULL; 47 wasmtime_module_t *linking2_module = NULL; 48 error = wasmtime_module_new(engine, (uint8_t*) linking1_wasm.data, linking1_wasm.size, &linking1_module); 49 if (error != NULL) 50 exit_with_error("failed to compile linking1", error, NULL); 51 error = wasmtime_module_new(engine, (uint8_t*) linking2_wasm.data, linking2_wasm.size, &linking2_module); 52 if (error != NULL) 53 exit_with_error("failed to compile linking2", error, NULL); 54 wasm_byte_vec_delete(&linking1_wasm); 55 wasm_byte_vec_delete(&linking2_wasm); 56 57 // Configure WASI and store it within our `wasmtime_store_t` 58 wasi_config_t *wasi_config = wasi_config_new(); 59 assert(wasi_config); 60 wasi_config_inherit_argv(wasi_config); 61 wasi_config_inherit_env(wasi_config); 62 wasi_config_inherit_stdin(wasi_config); 63 wasi_config_inherit_stdout(wasi_config); 64 wasi_config_inherit_stderr(wasi_config); 65 wasm_trap_t *trap = NULL; 66 error = wasmtime_context_set_wasi(context, wasi_config); 67 if (error != NULL) 68 exit_with_error("failed to instantiate wasi", NULL, trap); 69 70 // Create our linker which will be linking our modules together, and then add 71 // our WASI instance to it. 72 wasmtime_linker_t *linker = wasmtime_linker_new(engine); 73 error = wasmtime_linker_define_wasi(linker); 74 if (error != NULL) 75 exit_with_error("failed to link wasi", error, NULL); 76 77 // Instantiate `linking2` with our linker. 78 wasmtime_instance_t linking2; 79 error = wasmtime_linker_instantiate(linker, context, linking2_module, &linking2, &trap); 80 if (error != NULL || trap != NULL) 81 exit_with_error("failed to instantiate linking2", error, trap); 82 83 // Register our new `linking2` instance with the linker 84 error = wasmtime_linker_define_instance(linker, context, "linking2", strlen("linking2"), &linking2); 85 if (error != NULL) 86 exit_with_error("failed to link linking2", error, NULL); 87 88 // Instantiate `linking1` with the linker now that `linking2` is defined 89 wasmtime_instance_t linking1; 90 error = wasmtime_linker_instantiate(linker, context, linking1_module, &linking1, &trap); 91 if (error != NULL || trap != NULL) 92 exit_with_error("failed to instantiate linking1", error, trap); 93 94 // Lookup our `run` export function 95 wasmtime_extern_t run; 96 bool ok = wasmtime_instance_export_get(context, &linking1, "run", 3, &run); 97 assert(ok); 98 assert(run.kind == WASMTIME_EXTERN_FUNC); 99 error = wasmtime_func_call(context, &run.of.func, NULL, 0, NULL, 0, &trap); 100 if (error != NULL || trap != NULL) 101 exit_with_error("failed to call run", error, trap); 102 103 // Clean up after ourselves at this point 104 wasmtime_linker_delete(linker); 105 wasmtime_module_delete(linking1_module); 106 wasmtime_module_delete(linking2_module); 107 wasmtime_store_delete(store); 108 wasm_engine_delete(engine); 109 return 0; 110 } 111 112 static void read_wat_file( 113 wasm_engine_t *engine, 114 wasm_byte_vec_t *bytes, 115 const char *filename 116 ) { 117 wasm_byte_vec_t wat; 118 // Load our input file to parse it next 119 FILE* file = fopen(filename, "r"); 120 if (!file) { 121 printf("> Error loading file!\n"); 122 exit(1); 123 } 124 fseek(file, 0L, SEEK_END); 125 size_t file_size = ftell(file); 126 wasm_byte_vec_new_uninitialized(&wat, file_size); 127 fseek(file, 0L, SEEK_SET); 128 if (fread(wat.data, file_size, 1, file) != 1) { 129 printf("> Error loading module!\n"); 130 exit(1); 131 } 132 fclose(file); 133 134 // Parse the wat into the binary wasm format 135 wasmtime_error_t *error = wasmtime_wat2wasm(wat.data, wat.size, bytes); 136 if (error != NULL) 137 exit_with_error("failed to parse wat", error, NULL); 138 wasm_byte_vec_delete(&wat); 139 } 140 141 static void exit_with_error(const char *message, wasmtime_error_t *error, wasm_trap_t *trap) { 142 fprintf(stderr, "error: %s\n", message); 143 wasm_byte_vec_t error_message; 144 if (error != NULL) { 145 wasmtime_error_message(error, &error_message); 146 wasmtime_error_delete(error); 147 } else { 148 wasm_trap_message(trap, &error_message); 149 wasm_trap_delete(trap); 150 } 151 fprintf(stderr, "%.*s\n", (int) error_message.size, error_message.data); 152 wasm_byte_vec_delete(&error_message); 153 exit(1); 154 } 155