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