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

Function main

examples/linking.c:25–108  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

23static void read_wat_file(wasm_byte_vec_t *bytes, const char *file);
24
25int main() {
26 // Set up our context
27 wasm_engine_t *engine = wasm_engine_new();
28 assert(engine != NULL);
29 wasmtime_store_t *store = wasmtime_store_new(engine, NULL, NULL);
30 assert(store != NULL);
31 wasmtime_context_t *context = wasmtime_store_context(store);
32
33 wasm_byte_vec_t linking1_wasm, linking2_wasm;
34 read_wat_file(&linking1_wasm, "examples/linking1.wat");
35 read_wat_file(&linking2_wasm, "examples/linking2.wat");
36
37 // Compile our two modules
38 wasmtime_error_t *error;
39 wasmtime_module_t *linking1_module = NULL;
40 wasmtime_module_t *linking2_module = NULL;
41 error = wasmtime_module_new(engine, (uint8_t *)linking1_wasm.data,
42 linking1_wasm.size, &linking1_module);
43 if (error != NULL)
44 exit_with_error("failed to compile linking1", error, NULL);
45 error = wasmtime_module_new(engine, (uint8_t *)linking2_wasm.data,
46 linking2_wasm.size, &linking2_module);
47 if (error != NULL)
48 exit_with_error("failed to compile linking2", error, NULL);
49 wasm_byte_vec_delete(&linking1_wasm);
50 wasm_byte_vec_delete(&linking2_wasm);
51
52 // Configure WASI and store it within our `wasmtime_store_t`
53 wasi_config_t *wasi_config = wasi_config_new();
54 assert(wasi_config);
55 wasi_config_inherit_argv(wasi_config);
56 wasi_config_inherit_env(wasi_config);
57 wasi_config_inherit_stdin(wasi_config);
58 wasi_config_inherit_stdout(wasi_config);
59 wasi_config_inherit_stderr(wasi_config);
60 wasm_trap_t *trap = NULL;
61 error = wasmtime_context_set_wasi(context, wasi_config);
62 if (error != NULL)
63 exit_with_error("failed to instantiate wasi", NULL, trap);
64
65 // Create our linker which will be linking our modules together, and then add
66 // our WASI instance to it.
67 wasmtime_linker_t *linker = wasmtime_linker_new(engine);
68 error = wasmtime_linker_define_wasi(linker);
69 if (error != NULL)
70 exit_with_error("failed to link wasi", error, NULL);
71
72 // Instantiate `linking2` with our linker.
73 wasmtime_instance_t linking2;
74 error = wasmtime_linker_instantiate(linker, context, linking2_module,
75 &linking2, &trap);
76 if (error != NULL || trap != NULL)
77 exit_with_error("failed to instantiate linking2", error, trap);
78
79 // Register our new `linking2` instance with the linker
80 error = wasmtime_linker_define_instance(linker, context, "linking2",
81 strlen("linking2"), &linking2);
82 if (error != NULL)

Callers

nothing calls this directly

Calls 15

wasm_engine_newFunction · 0.85
assertFunction · 0.85
wasmtime_store_newFunction · 0.85
wasmtime_store_contextFunction · 0.85
read_wat_fileFunction · 0.85
wasmtime_module_newFunction · 0.85
wasi_config_newFunction · 0.85
wasi_config_inherit_argvFunction · 0.85
wasi_config_inherit_envFunction · 0.85

Tested by

no test coverage detected