MCPcopy Create free account
hub / github.com/bytecodealliance/wasmtime / main

Function main

examples/anyref.c:21–198  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

19 wasm_trap_t *trap);
20
21int main() {
22 bool ok = true;
23 // Create a new configuration with Wasm GC enabled.
24 printf("Initializing...\n");
25 wasm_config_t *config = wasm_config_new();
26 assert(config != NULL);
27 wasmtime_config_wasm_reference_types_set(config, true);
28 wasmtime_config_wasm_function_references_set(config, true);
29 wasmtime_config_wasm_gc_set(config, true);
30
31 // Create an *engine*, which is a compilation context, with our configured
32 // options.
33 wasm_engine_t *engine = wasm_engine_new_with_config(config);
34 assert(engine != NULL);
35
36 // With an engine we can create a *store* which is a group of wasm instances
37 // that can interact with each other.
38 wasmtime_store_t *store = wasmtime_store_new(engine, NULL, NULL);
39 assert(store != NULL);
40 wasmtime_context_t *context = wasmtime_store_context(store);
41
42 // Read our input file, which in this case is a wasm text file.
43 FILE *file = fopen("examples/anyref.wat", "r");
44 assert(file != NULL);
45 fseek(file, 0L, SEEK_END);
46 size_t file_size = ftell(file);
47 fseek(file, 0L, SEEK_SET);
48 wasm_byte_vec_t wat;
49 wasm_byte_vec_new_uninitialized(&wat, file_size);
50 if (fread(wat.data, file_size, 1, file) != 1) {
51 printf("> Error loading module!\n");
52 return 1;
53 }
54 fclose(file);
55
56 // Parse the wat into the binary wasm format
57 wasm_byte_vec_t wasm;
58 wasmtime_error_t *error = wasmtime_wat2wasm(wat.data, wat.size, &wasm);
59 if (error != NULL)
60 exit_with_error("failed to parse wat", error, NULL);
61 wasm_byte_vec_delete(&wat);
62
63 // Now that we've got our binary webassembly we can compile our module.
64 printf("Compiling module...\n");
65 wasmtime_module_t *module = NULL;
66 error = wasmtime_module_new(engine, (uint8_t *)wasm.data, wasm.size, &module);
67 wasm_byte_vec_delete(&wasm);
68 if (error != NULL)
69 exit_with_error("failed to compile module", error, NULL);
70
71 // Instantiate the module.
72 printf("Instantiating module...\n");
73 wasm_trap_t *trap = NULL;
74 wasmtime_instance_t instance;
75 error = wasmtime_instance_new(context, module, NULL, 0, &instance, &trap);
76 if (error != NULL || trap != NULL)
77 exit_with_error("failed to instantiate", error, trap);
78

Callers

nothing calls this directly

Calls 15

wasm_config_newFunction · 0.85
assertFunction · 0.85
wasmtime_store_newFunction · 0.85
wasmtime_store_contextFunction · 0.85
wasmtime_wat2wasmFunction · 0.85
wasmtime_module_newFunction · 0.85
wasmtime_instance_newFunction · 0.85
wasmtime_anyref_from_i31Function · 0.85

Tested by

no test coverage detected