1 /* 2 Example of using `externref` values. 3 4 You can compile and run this example on Linux with: 5 6 cargo build --release -p wasmtime-c-api 7 cc examples/externref.c \ 8 -I crates/c-api/include \ 9 -I crates/c-api/wasm-c-api/include \ 10 target/release/libwasmtime.a \ 11 -lpthread -ldl -lm \ 12 -o externref 13 ./externref 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 .. && \ 22 cmake --build . --target wasmtime-externref 23 */ 24 25 #include <assert.h> 26 #include <stdio.h> 27 #include <stdlib.h> 28 #include <string.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 int main() { 36 int ret = 0; 37 bool ok = true; 38 // Create a new configuration with Wasm reference types enabled. 39 printf("Initializing...\n"); 40 wasm_config_t *config = wasm_config_new(); 41 assert(config != NULL); 42 wasmtime_config_wasm_reference_types_set(config, true); 43 44 // Create an *engine*, which is a compilation context, with our configured 45 // options. 46 wasm_engine_t *engine = wasm_engine_new_with_config(config); 47 assert(engine != NULL); 48 49 // With an engine we can create a *store* which is a long-lived group of wasm 50 // modules. 51 wasmtime_store_t *store = wasmtime_store_new(engine, NULL, NULL); 52 assert(store != NULL); 53 wasmtime_context_t *context = wasmtime_store_context(store); 54 55 // Read our input file, which in this case is a wasm text file. 56 FILE *file = fopen("examples/externref.wat", "r"); 57 assert(file != NULL); 58 fseek(file, 0L, SEEK_END); 59 size_t file_size = ftell(file); 60 fseek(file, 0L, SEEK_SET); 61 wasm_byte_vec_t wat; 62 wasm_byte_vec_new_uninitialized(&wat, file_size); 63 assert(fread(wat.data, file_size, 1, file) == 1); 64 fclose(file); 65 66 // Parse the wat into the binary wasm format 67 wasm_byte_vec_t wasm; 68 wasmtime_error_t *error = wasmtime_wat2wasm(wat.data, wat.size, &wasm); 69 if (error != NULL) 70 exit_with_error("failed to parse wat", error, NULL); 71 wasm_byte_vec_delete(&wat); 72 73 // Now that we've got our binary webassembly we can compile our module. 74 printf("Compiling module...\n"); 75 wasmtime_module_t *module = NULL; 76 error = wasmtime_module_new(engine, (uint8_t *)wasm.data, wasm.size, &module); 77 wasm_byte_vec_delete(&wasm); 78 if (error != NULL) 79 exit_with_error("failed to compile module", error, NULL); 80 81 // Instantiate the module. 82 printf("Instantiating module...\n"); 83 wasm_trap_t *trap = NULL; 84 wasmtime_instance_t instance; 85 error = wasmtime_instance_new(context, module, NULL, 0, &instance, &trap); 86 if (error != NULL || trap != NULL) 87 exit_with_error("failed to instantiate", error, trap); 88 89 printf("Creating new `externref`...\n"); 90 91 // Create a new `externref` value. 92 // 93 // Note that the NULL here is a finalizer callback, but we don't need one for 94 // this example. 95 wasmtime_externref_t *externref = 96 wasmtime_externref_new("Hello, World!", NULL); 97 98 // The `externref`'s wrapped data should be the string "Hello, World!". 99 void *data = wasmtime_externref_data(externref); 100 assert(strcmp((char *)data, "Hello, World!") == 0); 101 102 printf("Touching `externref` table...\n"); 103 104 wasmtime_extern_t item; 105 106 // Lookup the `table` export. 107 ok = wasmtime_instance_export_get(context, &instance, "table", 108 strlen("table"), &item); 109 assert(ok); 110 assert(item.kind == WASMTIME_EXTERN_TABLE); 111 112 // Set `table[3]` to our `externref`. 113 wasmtime_val_t externref_val; 114 externref_val.kind = WASMTIME_EXTERNREF; 115 externref_val.of.externref = externref; 116 error = wasmtime_table_set(context, &item.of.table, 3, &externref_val); 117 if (error != NULL) 118 exit_with_error("failed to set table", error, NULL); 119 120 // `table[3]` should now be our `externref`. 121 wasmtime_val_t elem; 122 ok = wasmtime_table_get(context, &item.of.table, 3, &elem); 123 assert(ok); 124 assert(elem.kind == WASMTIME_EXTERNREF); 125 assert(strcmp((char *)wasmtime_externref_data(elem.of.externref), 126 "Hello, World!") == 0); 127 wasmtime_val_delete(&elem); 128 129 printf("Touching `externref` global...\n"); 130 131 // Lookup the `global` export. 132 ok = wasmtime_instance_export_get(context, &instance, "global", 133 strlen("global"), &item); 134 assert(ok); 135 assert(item.kind == WASMTIME_EXTERN_GLOBAL); 136 137 // Set the global to our `externref`. 138 error = wasmtime_global_set(context, &item.of.global, &externref_val); 139 if (error != NULL) 140 exit_with_error("failed to set global", error, NULL); 141 142 // Get the global, and it should return our `externref` again. 143 wasmtime_val_t global_val; 144 wasmtime_global_get(context, &item.of.global, &global_val); 145 assert(global_val.kind == WASMTIME_EXTERNREF); 146 assert(strcmp((char *)wasmtime_externref_data(elem.of.externref), 147 "Hello, World!") == 0); 148 wasmtime_val_delete(&global_val); 149 150 printf("Calling `externref` func...\n"); 151 152 // Lookup the `func` export. 153 ok = wasmtime_instance_export_get(context, &instance, "func", strlen("func"), 154 &item); 155 assert(ok); 156 assert(item.kind == WASMTIME_EXTERN_FUNC); 157 158 // And call it! 159 wasmtime_val_t results[1]; 160 error = wasmtime_func_call(context, &item.of.func, &externref_val, 1, results, 161 1, &trap); 162 if (error != NULL || trap != NULL) 163 exit_with_error("failed to call function", error, trap); 164 165 // `func` returns the same reference we gave it, so `results[0]` should be our 166 // `externref`. 167 assert(results[0].kind == WASMTIME_EXTERNREF); 168 assert(strcmp((char *)wasmtime_externref_data(results[0].of.externref), 169 "Hello, World!") == 0); 170 wasmtime_val_delete(&results[0]); 171 172 // We can GC any now-unused references to our externref that the store is 173 // holding. 174 printf("GCing within the store...\n"); 175 wasmtime_context_gc(context); 176 177 // Clean up after ourselves at this point 178 printf("All finished!\n"); 179 ret = 0; 180 181 wasmtime_store_delete(store); 182 wasmtime_module_delete(module); 183 wasm_engine_delete(engine); 184 return 0; 185 } 186 187 static void exit_with_error(const char *message, wasmtime_error_t *error, 188 wasm_trap_t *trap) { 189 fprintf(stderr, "error: %s\n", message); 190 wasm_byte_vec_t error_message; 191 if (error != NULL) { 192 wasmtime_error_message(error, &error_message); 193 wasmtime_error_delete(error); 194 } else { 195 wasm_trap_message(trap, &error_message); 196 wasm_trap_delete(trap); 197 } 198 fprintf(stderr, "%.*s\n", (int)error_message.size, error_message.data); 199 wasm_byte_vec_delete(&error_message); 200 exit(1); 201 } 202