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 20 #include <assert.h> 21 #include <stdio.h> 22 #include <stdlib.h> 23 #include <string.h> 24 #include <wasm.h> 25 #include <wasmtime.h> 26 27 static void exit_with_error(const char *message, wasmtime_error_t *error, wasm_trap_t *trap); 28 29 int main() { 30 int ret = 0; 31 bool ok = true; 32 // Create a new configuration with Wasm reference types enabled. 33 printf("Initializing...\n"); 34 wasm_config_t *config = wasm_config_new(); 35 assert(config != NULL); 36 wasmtime_config_wasm_reference_types_set(config, true); 37 38 // Create an *engine*, which is a compilation context, with our configured 39 // options. 40 wasm_engine_t *engine = wasm_engine_new_with_config(config); 41 assert(engine != NULL); 42 43 // With an engine we can create a *store* which is a long-lived group of wasm 44 // modules. 45 wasm_store_t *store = wasm_store_new(engine); 46 assert(store != NULL); 47 48 // Read our input file, which in this case is a wasm text file. 49 FILE* file = fopen("examples/externref.wat", "r"); 50 assert(file != NULL); 51 fseek(file, 0L, SEEK_END); 52 size_t file_size = ftell(file); 53 fseek(file, 0L, SEEK_SET); 54 wasm_byte_vec_t wat; 55 wasm_byte_vec_new_uninitialized(&wat, file_size); 56 assert(fread(wat.data, file_size, 1, file) == 1); 57 fclose(file); 58 59 // Parse the wat into the binary wasm format 60 wasm_byte_vec_t wasm; 61 wasmtime_error_t *error = wasmtime_wat2wasm(&wat, &wasm); 62 if (error != NULL) 63 exit_with_error("failed to parse wat", error, NULL); 64 wasm_byte_vec_delete(&wat); 65 66 // Now that we've got our binary webassembly we can compile our module. 67 printf("Compiling module...\n"); 68 wasm_module_t *module = NULL; 69 error = wasmtime_module_new(engine, &wasm, &module); 70 wasm_byte_vec_delete(&wasm); 71 if (error != NULL) 72 exit_with_error("failed to compile module", error, NULL); 73 74 // Instantiate the module. 75 printf("Instantiating module...\n"); 76 wasm_trap_t *trap = NULL; 77 wasm_instance_t *instance = NULL; 78 error = wasmtime_instance_new(store, module, NULL, 0, &instance, &trap); 79 if (instance == NULL) 80 exit_with_error("failed to instantiate", error, trap); 81 82 printf("Creating new `externref`...\n"); 83 84 // Create a new `externref` value. 85 // 86 // Note that if you need clean up for after the externref is reclaimed, you 87 // can use `wasmtime_externref_new_with_finalizer`. 88 wasm_val_t externref; 89 wasmtime_externref_new("Hello, World!", &externref); 90 assert(externref.kind == WASM_ANYREF); 91 92 // The `externref`'s wrapped data should be the string "Hello, World!". 93 void* data = NULL; 94 ok = wasmtime_externref_data(&externref, &data); 95 assert(ok); 96 assert(strcmp((char*)data, "Hello, World!") == 0); 97 98 printf("Touching `externref` table...\n"); 99 100 // Lookup the `table` export. 101 wasm_extern_vec_t externs; 102 wasm_instance_exports(instance, &externs); 103 assert(externs.size == 3); 104 wasm_table_t *table = wasm_extern_as_table(externs.data[0]); 105 assert(table != NULL); 106 107 // Set `table[3]` to our `externref`. 108 wasm_val_t elem; 109 wasm_val_copy(&elem, &externref); 110 assert(elem.kind == WASM_ANYREF); 111 ok = wasm_table_set(table, 3, elem.of.ref); 112 assert(ok); 113 114 // `table[3]` should now be our `externref`. 115 wasm_ref_delete(elem.of.ref); 116 elem.of.ref = wasm_table_get(table, 3); 117 assert(elem.of.ref != NULL); 118 assert(wasm_ref_same(elem.of.ref, externref.of.ref)); 119 120 printf("Touching `externref` global...\n"); 121 122 // Lookup the `global` export. 123 wasm_global_t *global = wasm_extern_as_global(externs.data[1]); 124 assert(global != NULL); 125 126 // Set the global to our `externref`. 127 wasm_global_set(global, &externref); 128 129 // Get the global, and it should return our `externref` again. 130 wasm_val_t global_val; 131 wasm_global_get(global, &global_val); 132 assert(global_val.kind == WASM_ANYREF); 133 assert(wasm_ref_same(global_val.of.ref, externref.of.ref)); 134 135 printf("Calling `externref` func...\n"); 136 137 // Lookup the `func` export. 138 wasm_func_t *func = wasm_extern_as_func(externs.data[2]); 139 assert(func != NULL); 140 141 // And call it! 142 wasm_val_t args[1]; 143 wasm_val_copy(&args[0], &externref); 144 wasm_val_t results[1]; 145 error = wasmtime_func_call(func, args, 1, results, 1, &trap); 146 if (error != NULL || trap != NULL) 147 exit_with_error("failed to call function", error, trap); 148 149 // `func` returns the same reference we gave it, so `results[0]` should be our 150 // `externref`. 151 assert(results[0].kind == WASM_ANYREF); 152 assert(wasm_ref_same(results[0].of.ref, externref.of.ref)); 153 154 // We can GC any now-unused references to our externref that the store is 155 // holding. 156 printf("GCing within the store...\n"); 157 wasmtime_store_gc(store); 158 159 // Clean up after ourselves at this point 160 printf("All finished!\n"); 161 ret = 0; 162 163 wasm_val_delete(&results[0]); 164 wasm_val_delete(&args[0]); 165 wasm_val_delete(&global_val); 166 wasm_val_delete(&elem); 167 wasm_extern_vec_delete(&externs); 168 wasm_val_delete(&externref); 169 wasm_instance_delete(instance); 170 wasm_module_delete(module); 171 wasm_store_delete(store); 172 wasm_engine_delete(engine); 173 return ret; 174 } 175 176 static void exit_with_error(const char *message, wasmtime_error_t *error, wasm_trap_t *trap) { 177 fprintf(stderr, "error: %s\n", message); 178 wasm_byte_vec_t error_message; 179 if (error != NULL) { 180 wasmtime_error_message(error, &error_message); 181 wasmtime_error_delete(error); 182 } else { 183 wasm_trap_message(trap, &error_message); 184 wasm_trap_delete(trap); 185 } 186 fprintf(stderr, "%.*s\n", (int) error_message.size, error_message.data); 187 wasm_byte_vec_delete(&error_message); 188 exit(1); 189 } 190