xref: /wasmtime-44.0.1/examples/anyref.c (revision adff9d9d)
1 /*
2 Example of using `anyref` values.
3 
4 You can build using cmake:
5 
6 mkdir build && cd build && cmake .. && \
7   cmake --build . --target wasmtime-anyref
8 */
9 
10 #include <assert.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <wasm.h>
15 #include <wasmtime.h>
16 
17 static void exit_with_error(const char *message, wasmtime_error_t *error,
18                             wasm_trap_t *trap);
19 
main()20 int main() {
21   bool ok = true;
22   // Create a new configuration with Wasm GC enabled.
23   printf("Initializing...\n");
24   wasm_config_t *config = wasm_config_new();
25   assert(config != NULL);
26   wasmtime_config_wasm_reference_types_set(config, true);
27   wasmtime_config_wasm_function_references_set(config, true);
28   wasmtime_config_wasm_gc_set(config, true);
29 
30   // Create an *engine*, which is a compilation context, with our configured
31   // options.
32   wasm_engine_t *engine = wasm_engine_new_with_config(config);
33   assert(engine != NULL);
34 
35   // With an engine we can create a *store* which is a group of wasm instances
36   // that can interact with each other.
37   wasmtime_store_t *store = wasmtime_store_new(engine, NULL, NULL);
38   assert(store != NULL);
39   wasmtime_context_t *context = wasmtime_store_context(store);
40 
41   // Read our input file, which in this case is a wasm text file.
42   FILE *file = fopen("examples/anyref.wat", "r");
43   assert(file != NULL);
44   fseek(file, 0L, SEEK_END);
45   size_t file_size = ftell(file);
46   fseek(file, 0L, SEEK_SET);
47   wasm_byte_vec_t wat;
48   wasm_byte_vec_new_uninitialized(&wat, file_size);
49   if (fread(wat.data, file_size, 1, file) != 1) {
50     printf("> Error loading module!\n");
51     return 1;
52   }
53   fclose(file);
54 
55   // Parse the wat into the binary wasm format
56   wasm_byte_vec_t wasm;
57   wasmtime_error_t *error = wasmtime_wat2wasm(wat.data, wat.size, &wasm);
58   if (error != NULL)
59     exit_with_error("failed to parse wat", error, NULL);
60   wasm_byte_vec_delete(&wat);
61 
62   // Now that we've got our binary webassembly we can compile our module.
63   printf("Compiling module...\n");
64   wasmtime_module_t *module = NULL;
65   error = wasmtime_module_new(engine, (uint8_t *)wasm.data, wasm.size, &module);
66   wasm_byte_vec_delete(&wasm);
67   if (error != NULL)
68     exit_with_error("failed to compile module", error, NULL);
69 
70   // Instantiate the module.
71   printf("Instantiating module...\n");
72   wasm_trap_t *trap = NULL;
73   wasmtime_instance_t instance;
74   error = wasmtime_instance_new(context, module, NULL, 0, &instance, &trap);
75   if (error != NULL || trap != NULL)
76     exit_with_error("failed to instantiate", error, trap);
77 
78   printf("Creating new `anyref` from i31...\n");
79 
80   // Create a new `anyref` value from an i31 (`i31ref` is a subtype of
81   // `anyref`).
82   wasmtime_anyref_t anyref;
83   wasmtime_anyref_from_i31(context, 1234, &anyref);
84   assert(!wasmtime_anyref_is_null(&anyref));
85 
86   // The `anyref`'s inner i31 value should be 1234.
87   uint32_t i31val = 0;
88   bool is_i31 = wasmtime_anyref_i31_get_u(context, &anyref, &i31val);
89   assert(is_i31);
90   assert(i31val == 1234);
91 
92   printf("Touching `anyref` table...\n");
93 
94   wasmtime_extern_t item;
95 
96   // Lookup the `table` export.
97   ok = wasmtime_instance_export_get(context, &instance, "table",
98                                     strlen("table"), &item);
99   assert(ok);
100   assert(item.kind == WASMTIME_EXTERN_TABLE);
101 
102   // Set `table[3]` to our `anyref`.
103   wasmtime_val_t anyref_val;
104   anyref_val.kind = WASMTIME_ANYREF;
105   anyref_val.of.anyref = anyref;
106   error = wasmtime_table_set(context, &item.of.table, 3, &anyref_val);
107   if (error != NULL)
108     exit_with_error("failed to set table", error, NULL);
109 
110   // `table[3]` should now be our `anyref`.
111   wasmtime_val_t elem;
112   ok = wasmtime_table_get(context, &item.of.table, 3, &elem);
113   assert(ok);
114   assert(elem.kind == WASMTIME_ANYREF);
115   is_i31 = false;
116   i31val = 0;
117   is_i31 = wasmtime_anyref_i31_get_u(context, &elem.of.anyref, &i31val);
118   assert(is_i31);
119   assert(i31val == 1234);
120   wasmtime_val_unroot(&elem);
121 
122   printf("Touching `anyref` global...\n");
123 
124   // Lookup the `global` export.
125   ok = wasmtime_instance_export_get(context, &instance, "global",
126                                     strlen("global"), &item);
127   assert(ok);
128   assert(item.kind == WASMTIME_EXTERN_GLOBAL);
129 
130   // Set the global to our `anyref`.
131   error = wasmtime_global_set(context, &item.of.global, &anyref_val);
132   if (error != NULL)
133     exit_with_error("failed to set global", error, NULL);
134 
135   // Get the global, and it should return our `anyref` again.
136   wasmtime_val_t global_val;
137   wasmtime_global_get(context, &item.of.global, &global_val);
138   assert(global_val.kind == WASMTIME_ANYREF);
139   is_i31 = false;
140   i31val = 0;
141   is_i31 = wasmtime_anyref_i31_get_u(context, &global_val.of.anyref, &i31val);
142   assert(is_i31);
143   assert(i31val == 1234);
144   wasmtime_val_unroot(&global_val);
145 
146   printf("Passing `anyref` into func...\n");
147 
148   // Lookup the `take_anyref` export.
149   ok = wasmtime_instance_export_get(context, &instance, "take_anyref",
150                                     strlen("take_anyref"), &item);
151   assert(ok);
152   assert(item.kind == WASMTIME_EXTERN_FUNC);
153 
154   // And call it!
155   error = wasmtime_func_call(context, &item.of.func, &anyref_val, 1, NULL, 0,
156                              &trap);
157   if (error != NULL || trap != NULL)
158     exit_with_error("failed to call function", error, trap);
159 
160   printf("Getting `anyref` from func...\n");
161 
162   // Lookup the `return_anyref` export.
163   ok = wasmtime_instance_export_get(context, &instance, "return_anyref",
164                                     strlen("return_anyref"), &item);
165   assert(ok);
166   assert(item.kind == WASMTIME_EXTERN_FUNC);
167 
168   // And call it!
169   wasmtime_val_t results[1];
170   error =
171       wasmtime_func_call(context, &item.of.func, NULL, 0, results, 1, &trap);
172   if (error != NULL || trap != NULL)
173     exit_with_error("failed to call function", error, trap);
174 
175   // `return_anyfunc` returns an `i31ref` that wraps `42`.
176   assert(results[0].kind == WASMTIME_ANYREF);
177   is_i31 = false;
178   i31val = 0;
179   is_i31 = wasmtime_anyref_i31_get_u(context, &results[0].of.anyref, &i31val);
180   assert(is_i31);
181   assert(i31val == 42);
182   wasmtime_val_unroot(&results[0]);
183   wasmtime_val_unroot(&anyref_val);
184 
185   // We can GC any now-unused references to our anyref that the store is
186   // holding.
187   printf("GCing within the store...\n");
188   wasmtime_context_gc(context);
189 
190   // Clean up after ourselves at this point
191   printf("All finished!\n");
192 
193   wasmtime_store_delete(store);
194   wasmtime_module_delete(module);
195   wasm_engine_delete(engine);
196   return 0;
197 }
198 
exit_with_error(const char * message,wasmtime_error_t * error,wasm_trap_t * trap)199 static void exit_with_error(const char *message, wasmtime_error_t *error,
200                             wasm_trap_t *trap) {
201   fprintf(stderr, "error: %s\n", message);
202   wasm_byte_vec_t error_message;
203   if (error != NULL) {
204     wasmtime_error_message(error, &error_message);
205     wasmtime_error_delete(error);
206   } else {
207     wasm_trap_message(trap, &error_message);
208     wasm_trap_delete(trap);
209   }
210   fprintf(stderr, "%.*s\n", (int)error_message.size, error_message.data);
211   wasm_byte_vec_delete(&error_message);
212   exit(1);
213 }
214