|
| 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 |
| 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 print_trap(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 | + int ret = 0; |
| 34 | + // Set up our context |
| 35 | + wasm_engine_t *engine = wasm_engine_new(); |
| 36 | + assert(engine != NULL); |
| 37 | + wasm_store_t *store = wasm_store_new(engine); |
| 38 | + assert(store != NULL); |
| 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 | + wasm_module_t *linking1_module = wasm_module_new(store, &linking1_wasm); |
| 46 | + assert(linking1_module != NULL); |
| 47 | + wasm_module_t *linking2_module = wasm_module_new(store, &linking2_wasm); |
| 48 | + assert(linking2_module != NULL); |
| 49 | + wasm_byte_vec_delete(&linking1_wasm); |
| 50 | + wasm_byte_vec_delete(&linking2_wasm); |
| 51 | + |
| 52 | + // Instantiate wasi |
| 53 | + wasi_config_t *wasi_config = wasi_config_new(); |
| 54 | + assert(wasi_config); |
| 55 | + wasi_config_inherit_argv(wasi_config); |
| 56 | + wasi_config_inherit_env(wasi_config); |
| 57 | + wasi_config_inherit_stdin(wasi_config); |
| 58 | + wasi_config_inherit_stdout(wasi_config); |
| 59 | + wasi_config_inherit_stderr(wasi_config); |
| 60 | + wasm_trap_t *trap = NULL; |
| 61 | + wasi_instance_t *wasi = wasi_instance_new(store, wasi_config, &trap); |
| 62 | + if (wasi == NULL) { |
| 63 | + print_trap(trap); |
| 64 | + exit(1); |
| 65 | + } |
| 66 | + |
| 67 | + // Create imports for `linking2` |
| 68 | + wasm_importtype_vec_t linking2_import_types; |
| 69 | + wasm_module_imports(linking2_module, &linking2_import_types); |
| 70 | + const wasm_extern_t **linking2_imports = calloc(linking2_import_types.size, sizeof(void*)); |
| 71 | + assert(linking2_imports); |
| 72 | + for (int i = 0; i < linking2_import_types.size; i++) { |
| 73 | + const wasm_extern_t *binding = wasi_instance_bind_import(wasi, linking2_import_types.data[i]); |
| 74 | + if (binding != NULL) { |
| 75 | + linking2_imports[i] = binding; |
| 76 | + } else { |
| 77 | + printf("> Failed to satisfy import\n"); |
| 78 | + exit(1); |
| 79 | + } |
| 80 | + } |
| 81 | + wasm_importtype_vec_delete(&linking2_import_types); |
| 82 | + |
| 83 | + // Instantiate `linking2` |
| 84 | + wasm_instance_t *linking2 = wasm_instance_new(store, linking2_module, linking2_imports, &trap); |
| 85 | + if (linking2 == NULL) { |
| 86 | + print_trap(trap); |
| 87 | + exit(1); |
| 88 | + } |
| 89 | + free(linking2_imports); |
| 90 | + wasm_extern_vec_t linking2_externs; |
| 91 | + wasm_instance_exports(linking2, &linking2_externs); |
| 92 | + wasm_exporttype_vec_t linking2_exports; |
| 93 | + wasm_module_exports(linking2_module, &linking2_exports); |
| 94 | + |
| 95 | + // Create imports for `linking1` |
| 96 | + wasm_importtype_vec_t linking1_import_types; |
| 97 | + wasm_module_imports(linking1_module, &linking1_import_types); |
| 98 | + const wasm_extern_t **linking1_imports = calloc(linking1_import_types.size, sizeof(void*)); |
| 99 | + assert(linking1_imports); |
| 100 | + for (int i = 0; i < linking1_import_types.size; i++) { |
| 101 | + const wasm_importtype_t *import = linking1_import_types.data[i]; |
| 102 | + const wasm_name_t *module = wasm_importtype_module(import); |
| 103 | + const wasm_name_t *name = wasm_importtype_name(import); |
| 104 | + if (strncmp(module->data, "linking2", module->size) == 0) { |
| 105 | + const wasm_extern_t *e = NULL; |
| 106 | + for (int j = 0; j < linking2_exports.size; j++) { |
| 107 | + const wasm_name_t *export_name = wasm_exporttype_name(linking2_exports.data[j]); |
| 108 | + if (name->size == export_name->size && |
| 109 | + strncmp(name->data, export_name->data, name->size) == 0) { |
| 110 | + e = linking2_externs.data[j]; |
| 111 | + break; |
| 112 | + } |
| 113 | + } |
| 114 | + if (e) { |
| 115 | + linking1_imports[i] = e; |
| 116 | + continue; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + printf("> Failed to satisfy import\n"); |
| 121 | + exit(1); |
| 122 | + } |
| 123 | + wasm_importtype_vec_delete(&linking1_import_types); |
| 124 | + |
| 125 | + // Instantiate `linking1` |
| 126 | + wasm_instance_t *linking1 = wasm_instance_new(store, linking1_module, linking1_imports, &trap); |
| 127 | + if (linking1 == NULL) { |
| 128 | + print_trap(trap); |
| 129 | + exit(1); |
| 130 | + } |
| 131 | + |
| 132 | + // Lookup our `run` export function |
| 133 | + wasm_extern_vec_t linking1_externs; |
| 134 | + wasm_instance_exports(linking1, &linking1_externs); |
| 135 | + assert(linking1_externs.size == 1); |
| 136 | + wasm_func_t *run = wasm_extern_as_func(linking1_externs.data[0]); |
| 137 | + assert(run != NULL); |
| 138 | + trap = wasm_func_call(run, NULL, NULL); |
| 139 | + if (trap != NULL) { |
| 140 | + print_trap(trap); |
| 141 | + exit(1); |
| 142 | + } |
| 143 | + |
| 144 | + // Clean up after ourselves at this point |
| 145 | + wasm_extern_vec_delete(&linking1_externs); |
| 146 | + wasm_extern_vec_delete(&linking2_externs); |
| 147 | + wasm_instance_delete(linking1); |
| 148 | + wasm_instance_delete(linking2); |
| 149 | + wasm_module_delete(linking1_module); |
| 150 | + wasm_module_delete(linking2_module); |
| 151 | + wasm_store_delete(store); |
| 152 | + wasm_engine_delete(engine); |
| 153 | + return 0; |
| 154 | +} |
| 155 | + |
| 156 | +static void read_wat_file( |
| 157 | + wasm_engine_t *engine, |
| 158 | + wasm_byte_vec_t *bytes, |
| 159 | + const char *filename |
| 160 | +) { |
| 161 | + wasm_byte_vec_t wat; |
| 162 | + // Load our input file to parse it next |
| 163 | + FILE* file = fopen(filename, "r"); |
| 164 | + if (!file) { |
| 165 | + printf("> Error loading file!\n"); |
| 166 | + exit(1); |
| 167 | + } |
| 168 | + fseek(file, 0L, SEEK_END); |
| 169 | + size_t file_size = ftell(file); |
| 170 | + wasm_byte_vec_new_uninitialized(&wat, file_size); |
| 171 | + fseek(file, 0L, SEEK_SET); |
| 172 | + if (fread(wat.data, file_size, 1, file) != 1) { |
| 173 | + printf("> Error loading module!\n"); |
| 174 | + exit(1); |
| 175 | + } |
| 176 | + fclose(file); |
| 177 | + |
| 178 | + // Parse the wat into the binary wasm format |
| 179 | + wasm_byte_vec_t error; |
| 180 | + if (wasmtime_wat2wasm(engine, &wat, bytes, &error) == 0) { |
| 181 | + fprintf(stderr, "failed to parse wat %.*s\n", (int) error.size, error.data); |
| 182 | + exit(0); |
| 183 | + } |
| 184 | + wasm_byte_vec_delete(&wat); |
| 185 | +} |
| 186 | + |
| 187 | +static void print_trap(wasm_trap_t *trap) { |
| 188 | + assert(trap != NULL); |
| 189 | + wasm_message_t message; |
| 190 | + wasm_trap_message(trap, &message); |
| 191 | + fprintf(stderr, "failed to instantiate module %.*s\n", (int) message.size, message.data); |
| 192 | + wasm_byte_vec_delete(&message); |
| 193 | + wasm_trap_delete(trap); |
| 194 | +} |
0 commit comments