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

Function main

examples/wasip1/main.c:38–110  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

36 wasm_trap_t *trap);
37
38int main() {
39 // Set up our context
40 wasm_engine_t *engine = wasm_engine_new();
41 assert(engine != NULL);
42 wasmtime_store_t *store = wasmtime_store_new(engine, NULL, NULL);
43 assert(store != NULL);
44 wasmtime_context_t *context = wasmtime_store_context(store);
45
46 // Create a linker with WASI functions defined
47 wasmtime_linker_t *linker = wasmtime_linker_new(engine);
48 wasmtime_error_t *error = wasmtime_linker_define_wasi(linker);
49 if (error != NULL)
50 exit_with_error("failed to link wasi", error, NULL);
51
52 wasm_byte_vec_t wasm;
53 // Load our input file to parse it next
54 FILE *file = fopen("target/wasm32-wasip1/debug/wasi.wasm", "rb");
55 if (!file) {
56 printf("> Error loading file!\n");
57 exit(1);
58 }
59 fseek(file, 0L, SEEK_END);
60 size_t file_size = ftell(file);
61 wasm_byte_vec_new_uninitialized(&wasm, file_size);
62 fseek(file, 0L, SEEK_SET);
63 if (fread(wasm.data, file_size, 1, file) != 1) {
64 printf("> Error loading module!\n");
65 exit(1);
66 }
67 fclose(file);
68
69 // Compile our modules
70 wasmtime_module_t *module = NULL;
71 error = wasmtime_module_new(engine, (uint8_t *)wasm.data, wasm.size, &module);
72 if (!module)
73 exit_with_error("failed to compile module", error, NULL);
74 wasm_byte_vec_delete(&wasm);
75
76 // Instantiate wasi
77 wasi_config_t *wasi_config = wasi_config_new();
78 assert(wasi_config);
79 wasi_config_inherit_argv(wasi_config);
80 wasi_config_inherit_env(wasi_config);
81 wasi_config_inherit_stdin(wasi_config);
82 wasi_config_inherit_stdout(wasi_config);
83 wasi_config_inherit_stderr(wasi_config);
84 wasm_trap_t *trap = NULL;
85 error = wasmtime_context_set_wasi(context, wasi_config);
86 if (error != NULL)
87 exit_with_error("failed to instantiate WASI", error, NULL);
88
89 // Instantiate the module
90 error = wasmtime_linker_module(linker, context, "", 0, module);
91 if (error != NULL)
92 exit_with_error("failed to instantiate module", error, NULL);
93
94 // Run it.
95 wasmtime_func_t func;

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
wasmtime_linker_newFunction · 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