xref: /wasmtime-44.0.1/examples/externref.c (revision 9ce3ffe1)
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   if (fread(wat.data, file_size, 1, file) != 1) {
64     printf("> Error loading module!\n");
65     return 1;
66   }
67   fclose(file);
68 
69   // Parse the wat into the binary wasm format
70   wasm_byte_vec_t wasm;
71   wasmtime_error_t *error = wasmtime_wat2wasm(wat.data, wat.size, &wasm);
72   if (error != NULL)
73     exit_with_error("failed to parse wat", error, NULL);
74   wasm_byte_vec_delete(&wat);
75 
76   // Now that we've got our binary webassembly we can compile our module.
77   printf("Compiling module...\n");
78   wasmtime_module_t *module = NULL;
79   error = wasmtime_module_new(engine, (uint8_t *)wasm.data, wasm.size, &module);
80   wasm_byte_vec_delete(&wasm);
81   if (error != NULL)
82     exit_with_error("failed to compile module", error, NULL);
83 
84   // Instantiate the module.
85   printf("Instantiating module...\n");
86   wasm_trap_t *trap = NULL;
87   wasmtime_instance_t instance;
88   error = wasmtime_instance_new(context, module, NULL, 0, &instance, &trap);
89   if (error != NULL || trap != NULL)
90     exit_with_error("failed to instantiate", error, trap);
91 
92   printf("Creating new `externref`...\n");
93 
94   // Create a new `externref` value.
95   //
96   // Note that the NULL here is a finalizer callback, but we don't need one for
97   // this example.
98   wasmtime_externref_t *externref =
99       wasmtime_externref_new("Hello, World!", NULL);
100 
101   // The `externref`'s wrapped data should be the string "Hello, World!".
102   void *data = wasmtime_externref_data(externref);
103   assert(strcmp((char *)data, "Hello, World!") == 0);
104 
105   printf("Touching `externref` table...\n");
106 
107   wasmtime_extern_t item;
108 
109   // Lookup the `table` export.
110   ok = wasmtime_instance_export_get(context, &instance, "table",
111                                     strlen("table"), &item);
112   assert(ok);
113   assert(item.kind == WASMTIME_EXTERN_TABLE);
114 
115   // Set `table[3]` to our `externref`.
116   wasmtime_val_t externref_val;
117   externref_val.kind = WASMTIME_EXTERNREF;
118   externref_val.of.externref = 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(elem.of.externref),
129                 "Hello, World!") == 0);
130   wasmtime_val_delete(&elem);
131 
132   printf("Touching `externref` global...\n");
133 
134   // Lookup the `global` export.
135   ok = wasmtime_instance_export_get(context, &instance, "global",
136                                     strlen("global"), &item);
137   assert(ok);
138   assert(item.kind == WASMTIME_EXTERN_GLOBAL);
139 
140   // Set the global to our `externref`.
141   error = wasmtime_global_set(context, &item.of.global, &externref_val);
142   if (error != NULL)
143     exit_with_error("failed to set global", error, NULL);
144 
145   // Get the global, and it should return our `externref` again.
146   wasmtime_val_t global_val;
147   wasmtime_global_get(context, &item.of.global, &global_val);
148   assert(global_val.kind == WASMTIME_EXTERNREF);
149   assert(strcmp((char *)wasmtime_externref_data(elem.of.externref),
150                 "Hello, World!") == 0);
151   wasmtime_val_delete(&global_val);
152 
153   printf("Calling `externref` func...\n");
154 
155   // Lookup the `func` export.
156   ok = wasmtime_instance_export_get(context, &instance, "func", strlen("func"),
157                                     &item);
158   assert(ok);
159   assert(item.kind == WASMTIME_EXTERN_FUNC);
160 
161   // And call it!
162   wasmtime_val_t results[1];
163   error = wasmtime_func_call(context, &item.of.func, &externref_val, 1, results,
164                              1, &trap);
165   if (error != NULL || trap != NULL)
166     exit_with_error("failed to call function", error, trap);
167 
168   // `func` returns the same reference we gave it, so `results[0]` should be our
169   // `externref`.
170   assert(results[0].kind == WASMTIME_EXTERNREF);
171   assert(strcmp((char *)wasmtime_externref_data(results[0].of.externref),
172                 "Hello, World!") == 0);
173   wasmtime_val_delete(&results[0]);
174 
175   // We can GC any now-unused references to our externref that the store is
176   // holding.
177   printf("GCing within the store...\n");
178   wasmtime_context_gc(context);
179 
180   // Clean up after ourselves at this point
181   printf("All finished!\n");
182   ret = 0;
183 
184   wasmtime_store_delete(store);
185   wasmtime_module_delete(module);
186   wasm_engine_delete(engine);
187   return 0;
188 }
189 
190 static void exit_with_error(const char *message, wasmtime_error_t *error,
191                             wasm_trap_t *trap) {
192   fprintf(stderr, "error: %s\n", message);
193   wasm_byte_vec_t error_message;
194   if (error != NULL) {
195     wasmtime_error_message(error, &error_message);
196     wasmtime_error_delete(error);
197   } else {
198     wasm_trap_message(trap, &error_message);
199     wasm_trap_delete(trap);
200   }
201   fprintf(stderr, "%.*s\n", (int)error_message.size, error_message.data);
202   wasm_byte_vec_delete(&error_message);
203   exit(1);
204 }
205