123640b6cSNick Fitzgerald /*
223640b6cSNick Fitzgerald Example of using `externref` values.
323640b6cSNick Fitzgerald
457ba95e9SMasashi Yoshimura You can build using cmake:
523640b6cSNick Fitzgerald
623640b6cSNick Fitzgerald mkdir build && cd build && cmake .. && \
723640b6cSNick Fitzgerald cmake --build . --target wasmtime-externref
823640b6cSNick Fitzgerald */
923640b6cSNick Fitzgerald
1023640b6cSNick Fitzgerald #include <assert.h>
1123640b6cSNick Fitzgerald #include <stdio.h>
1223640b6cSNick Fitzgerald #include <stdlib.h>
1323640b6cSNick Fitzgerald #include <string.h>
1423640b6cSNick Fitzgerald #include <wasm.h>
1523640b6cSNick Fitzgerald #include <wasmtime.h>
1623640b6cSNick Fitzgerald
1723640b6cSNick Fitzgerald static void exit_with_error(const char *message, wasmtime_error_t *error,
1823640b6cSNick Fitzgerald wasm_trap_t *trap);
1923640b6cSNick Fitzgerald
main()2023640b6cSNick Fitzgerald int main() {
2123640b6cSNick Fitzgerald bool ok = true;
2223640b6cSNick Fitzgerald // Create a new configuration with Wasm reference types enabled.
2323640b6cSNick Fitzgerald printf("Initializing...\n");
2423640b6cSNick Fitzgerald wasm_config_t *config = wasm_config_new();
2523640b6cSNick Fitzgerald assert(config != NULL);
2623640b6cSNick Fitzgerald wasmtime_config_wasm_reference_types_set(config, true);
2723640b6cSNick Fitzgerald
2823640b6cSNick Fitzgerald // Create an *engine*, which is a compilation context, with our configured
2923640b6cSNick Fitzgerald // options.
3023640b6cSNick Fitzgerald wasm_engine_t *engine = wasm_engine_new_with_config(config);
3123640b6cSNick Fitzgerald assert(engine != NULL);
3223640b6cSNick Fitzgerald
3323640b6cSNick Fitzgerald // With an engine we can create a *store* which is a group of wasm instances
3423640b6cSNick Fitzgerald // that can interact with each other.
3523640b6cSNick Fitzgerald wasmtime_store_t *store = wasmtime_store_new(engine, NULL, NULL);
3623640b6cSNick Fitzgerald assert(store != NULL);
3723640b6cSNick Fitzgerald wasmtime_context_t *context = wasmtime_store_context(store);
3823640b6cSNick Fitzgerald
3923640b6cSNick Fitzgerald // Read our input file, which in this case is a wasm text file.
4023640b6cSNick Fitzgerald FILE *file = fopen("examples/externref.wat", "r");
4123640b6cSNick Fitzgerald assert(file != NULL);
4223640b6cSNick Fitzgerald fseek(file, 0L, SEEK_END);
4323640b6cSNick Fitzgerald size_t file_size = ftell(file);
4423640b6cSNick Fitzgerald fseek(file, 0L, SEEK_SET);
4523640b6cSNick Fitzgerald wasm_byte_vec_t wat;
4623640b6cSNick Fitzgerald wasm_byte_vec_new_uninitialized(&wat, file_size);
4723640b6cSNick Fitzgerald if (fread(wat.data, file_size, 1, file) != 1) {
4823640b6cSNick Fitzgerald printf("> Error loading module!\n");
4923640b6cSNick Fitzgerald return 1;
5023640b6cSNick Fitzgerald }
5123640b6cSNick Fitzgerald fclose(file);
5223640b6cSNick Fitzgerald
5323640b6cSNick Fitzgerald // Parse the wat into the binary wasm format
5423640b6cSNick Fitzgerald wasm_byte_vec_t wasm;
5523640b6cSNick Fitzgerald wasmtime_error_t *error = wasmtime_wat2wasm(wat.data, wat.size, &wasm);
5623640b6cSNick Fitzgerald if (error != NULL)
5723640b6cSNick Fitzgerald exit_with_error("failed to parse wat", error, NULL);
5823640b6cSNick Fitzgerald wasm_byte_vec_delete(&wat);
5923640b6cSNick Fitzgerald
6023640b6cSNick Fitzgerald // Now that we've got our binary webassembly we can compile our module.
6123640b6cSNick Fitzgerald printf("Compiling module...\n");
6223640b6cSNick Fitzgerald wasmtime_module_t *module = NULL;
6323640b6cSNick Fitzgerald error = wasmtime_module_new(engine, (uint8_t *)wasm.data, wasm.size, &module);
6423640b6cSNick Fitzgerald wasm_byte_vec_delete(&wasm);
6523640b6cSNick Fitzgerald if (error != NULL)
6623640b6cSNick Fitzgerald exit_with_error("failed to compile module", error, NULL);
6723640b6cSNick Fitzgerald
6823640b6cSNick Fitzgerald // Instantiate the module.
6923640b6cSNick Fitzgerald printf("Instantiating module...\n");
7023640b6cSNick Fitzgerald wasm_trap_t *trap = NULL;
7123640b6cSNick Fitzgerald wasmtime_instance_t instance;
7223640b6cSNick Fitzgerald error = wasmtime_instance_new(context, module, NULL, 0, &instance, &trap);
7323640b6cSNick Fitzgerald if (error != NULL || trap != NULL)
7423640b6cSNick Fitzgerald exit_with_error("failed to instantiate", error, trap);
7523640b6cSNick Fitzgerald
7623640b6cSNick Fitzgerald printf("Creating new `externref`...\n");
7723640b6cSNick Fitzgerald
7823640b6cSNick Fitzgerald // Create a new `externref` value.
7923640b6cSNick Fitzgerald //
8023640b6cSNick Fitzgerald // Note that the NULL here is a finalizer callback, but we don't need one for
8123640b6cSNick Fitzgerald // this example.
8277405cc8SAlex Crichton wasmtime_externref_t externref;
8377405cc8SAlex Crichton ok = wasmtime_externref_new(context, "Hello, World!", NULL, &externref);
8477405cc8SAlex Crichton assert(ok);
8523640b6cSNick Fitzgerald
8623640b6cSNick Fitzgerald // The `externref`'s wrapped data should be the string "Hello, World!".
8777405cc8SAlex Crichton void *data = wasmtime_externref_data(context, &externref);
8823640b6cSNick Fitzgerald assert(strcmp((char *)data, "Hello, World!") == 0);
8923640b6cSNick Fitzgerald
9023640b6cSNick Fitzgerald wasmtime_extern_t item;
9181d71873SAlex Crichton wasmtime_val_t externref_val;
9281d71873SAlex Crichton externref_val.kind = WASMTIME_EXTERNREF;
9381d71873SAlex Crichton externref_val.of.externref = externref;
9423640b6cSNick Fitzgerald
9523640b6cSNick Fitzgerald // Lookup the `table` export.
9681d71873SAlex Crichton printf("Touching `externref` table...\n");
9781d71873SAlex Crichton {
9823640b6cSNick Fitzgerald ok = wasmtime_instance_export_get(context, &instance, "table",
9923640b6cSNick Fitzgerald strlen("table"), &item);
10023640b6cSNick Fitzgerald assert(ok);
10123640b6cSNick Fitzgerald assert(item.kind == WASMTIME_EXTERN_TABLE);
10223640b6cSNick Fitzgerald
10323640b6cSNick Fitzgerald // Set `table[3]` to our `externref`.
10423640b6cSNick Fitzgerald error = wasmtime_table_set(context, &item.of.table, 3, &externref_val);
10523640b6cSNick Fitzgerald if (error != NULL)
10623640b6cSNick Fitzgerald exit_with_error("failed to set table", error, NULL);
10723640b6cSNick Fitzgerald
10823640b6cSNick Fitzgerald // `table[3]` should now be our `externref`.
10923640b6cSNick Fitzgerald wasmtime_val_t elem;
11023640b6cSNick Fitzgerald ok = wasmtime_table_get(context, &item.of.table, 3, &elem);
11123640b6cSNick Fitzgerald assert(ok);
11223640b6cSNick Fitzgerald assert(elem.kind == WASMTIME_EXTERNREF);
11377405cc8SAlex Crichton assert(strcmp((char *)wasmtime_externref_data(context, &elem.of.externref),
11423640b6cSNick Fitzgerald "Hello, World!") == 0);
115*adff9d9dSAlex Crichton wasmtime_val_unroot(&elem);
11681d71873SAlex Crichton }
11723640b6cSNick Fitzgerald
11823640b6cSNick Fitzgerald printf("Touching `externref` global...\n");
11923640b6cSNick Fitzgerald
12023640b6cSNick Fitzgerald // Lookup the `global` export.
12181d71873SAlex Crichton {
12223640b6cSNick Fitzgerald ok = wasmtime_instance_export_get(context, &instance, "global",
12323640b6cSNick Fitzgerald strlen("global"), &item);
12423640b6cSNick Fitzgerald assert(ok);
12523640b6cSNick Fitzgerald assert(item.kind == WASMTIME_EXTERN_GLOBAL);
12623640b6cSNick Fitzgerald
12723640b6cSNick Fitzgerald // Set the global to our `externref`.
12823640b6cSNick Fitzgerald error = wasmtime_global_set(context, &item.of.global, &externref_val);
12923640b6cSNick Fitzgerald if (error != NULL)
13023640b6cSNick Fitzgerald exit_with_error("failed to set global", error, NULL);
13123640b6cSNick Fitzgerald
13223640b6cSNick Fitzgerald // Get the global, and it should return our `externref` again.
13323640b6cSNick Fitzgerald wasmtime_val_t global_val;
13423640b6cSNick Fitzgerald wasmtime_global_get(context, &item.of.global, &global_val);
13523640b6cSNick Fitzgerald assert(global_val.kind == WASMTIME_EXTERNREF);
13681d71873SAlex Crichton assert(strcmp((char *)wasmtime_externref_data(context,
13777405cc8SAlex Crichton &global_val.of.externref),
13823640b6cSNick Fitzgerald "Hello, World!") == 0);
139*adff9d9dSAlex Crichton wasmtime_val_unroot(&global_val);
14081d71873SAlex Crichton }
14123640b6cSNick Fitzgerald
14223640b6cSNick Fitzgerald printf("Calling `externref` func...\n");
14323640b6cSNick Fitzgerald
14423640b6cSNick Fitzgerald // Lookup the `func` export.
14581d71873SAlex Crichton {
14681d71873SAlex Crichton ok = wasmtime_instance_export_get(context, &instance, "func",
14781d71873SAlex Crichton strlen("func"), &item);
14823640b6cSNick Fitzgerald assert(ok);
14923640b6cSNick Fitzgerald assert(item.kind == WASMTIME_EXTERN_FUNC);
15023640b6cSNick Fitzgerald
15123640b6cSNick Fitzgerald // And call it!
15223640b6cSNick Fitzgerald wasmtime_val_t results[1];
15381d71873SAlex Crichton error = wasmtime_func_call(context, &item.of.func, &externref_val, 1,
15481d71873SAlex Crichton results, 1, &trap);
15523640b6cSNick Fitzgerald if (error != NULL || trap != NULL)
15623640b6cSNick Fitzgerald exit_with_error("failed to call function", error, trap);
15723640b6cSNick Fitzgerald
15881d71873SAlex Crichton // `func` returns the same reference we gave it, so `results[0]` should be
15981d71873SAlex Crichton // our `externref`.
16023640b6cSNick Fitzgerald assert(results[0].kind == WASMTIME_EXTERNREF);
16181d71873SAlex Crichton assert(strcmp((char *)wasmtime_externref_data(context,
16277405cc8SAlex Crichton &results[0].of.externref),
16323640b6cSNick Fitzgerald "Hello, World!") == 0);
164*adff9d9dSAlex Crichton wasmtime_val_unroot(&results[0]);
16581d71873SAlex Crichton }
166*adff9d9dSAlex Crichton wasmtime_val_unroot(&externref_val);
16723640b6cSNick Fitzgerald
16823640b6cSNick Fitzgerald // We can GC any now-unused references to our externref that the store is
16923640b6cSNick Fitzgerald // holding.
17023640b6cSNick Fitzgerald printf("GCing within the store...\n");
17123640b6cSNick Fitzgerald wasmtime_context_gc(context);
17223640b6cSNick Fitzgerald
17323640b6cSNick Fitzgerald // Clean up after ourselves at this point
17423640b6cSNick Fitzgerald printf("All finished!\n");
17523640b6cSNick Fitzgerald
17623640b6cSNick Fitzgerald wasmtime_store_delete(store);
17723640b6cSNick Fitzgerald wasmtime_module_delete(module);
17823640b6cSNick Fitzgerald wasm_engine_delete(engine);
17923640b6cSNick Fitzgerald return 0;
18023640b6cSNick Fitzgerald }
18123640b6cSNick Fitzgerald
exit_with_error(const char * message,wasmtime_error_t * error,wasm_trap_t * trap)18223640b6cSNick Fitzgerald static void exit_with_error(const char *message, wasmtime_error_t *error,
18323640b6cSNick Fitzgerald wasm_trap_t *trap) {
18423640b6cSNick Fitzgerald fprintf(stderr, "error: %s\n", message);
18523640b6cSNick Fitzgerald wasm_byte_vec_t error_message;
18623640b6cSNick Fitzgerald if (error != NULL) {
18723640b6cSNick Fitzgerald wasmtime_error_message(error, &error_message);
18823640b6cSNick Fitzgerald wasmtime_error_delete(error);
18923640b6cSNick Fitzgerald } else {
19023640b6cSNick Fitzgerald wasm_trap_message(trap, &error_message);
19123640b6cSNick Fitzgerald wasm_trap_delete(trap);
19223640b6cSNick Fitzgerald }
19323640b6cSNick Fitzgerald fprintf(stderr, "%.*s\n", (int)error_message.size, error_message.data);
19423640b6cSNick Fitzgerald wasm_byte_vec_delete(&error_message);
19523640b6cSNick Fitzgerald exit(1);
19623640b6cSNick Fitzgerald }
197