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

Function main

examples/externref.c:21–181  ·  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 reference types 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
29 // Create an *engine*, which is a compilation context, with our configured
30 // options.
31 wasm_engine_t *engine = wasm_engine_new_with_config(config);
32 assert(engine != NULL);
33
34 // With an engine we can create a *store* which is a group of wasm instances
35 // that can interact with each other.
36 wasmtime_store_t *store = wasmtime_store_new(engine, NULL, NULL);
37 assert(store != NULL);
38 wasmtime_context_t *context = wasmtime_store_context(store);
39
40 // Read our input file, which in this case is a wasm text file.
41 FILE *file = fopen("examples/externref.wat", "r");
42 assert(file != NULL);
43 fseek(file, 0L, SEEK_END);
44 size_t file_size = ftell(file);
45 fseek(file, 0L, SEEK_SET);
46 wasm_byte_vec_t wat;
47 wasm_byte_vec_new_uninitialized(&wat, file_size);
48 if (fread(wat.data, file_size, 1, file) != 1) {
49 printf("> Error loading module!\n");
50 return 1;
51 }
52 fclose(file);
53
54 // Parse the wat into the binary wasm format
55 wasm_byte_vec_t wasm;
56 wasmtime_error_t *error = wasmtime_wat2wasm(wat.data, wat.size, &wasm);
57 if (error != NULL)
58 exit_with_error("failed to parse wat", error, NULL);
59 wasm_byte_vec_delete(&wat);
60
61 // Now that we've got our binary webassembly we can compile our module.
62 printf("Compiling module...\n");
63 wasmtime_module_t *module = NULL;
64 error = wasmtime_module_new(engine, (uint8_t *)wasm.data, wasm.size, &module);
65 wasm_byte_vec_delete(&wasm);
66 if (error != NULL)
67 exit_with_error("failed to compile module", error, NULL);
68
69 // Instantiate the module.
70 printf("Instantiating module...\n");
71 wasm_trap_t *trap = NULL;
72 wasmtime_instance_t instance;
73 error = wasmtime_instance_new(context, module, NULL, 0, &instance, &trap);
74 if (error != NULL || trap != NULL)
75 exit_with_error("failed to instantiate", error, trap);
76
77 printf("Creating new `externref`...\n");
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_externref_newFunction · 0.85
wasmtime_externref_dataFunction · 0.85

Tested by

no test coverage detected