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