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