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

Function main

examples/multi.c:62–160  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

60}
61
62int main() {
63 // Initialize.
64 printf("Initializing...\n");
65 wasm_engine_t *engine = wasm_engine_new();
66 wasmtime_store_t *store = wasmtime_store_new(engine, NULL, NULL);
67 wasmtime_context_t *context = wasmtime_store_context(store);
68
69 // Load our input file to parse it next
70 FILE *file = fopen("examples/multi.wat", "r");
71 if (!file) {
72 printf("> Error loading file!\n");
73 return 1;
74 }
75 fseek(file, 0L, SEEK_END);
76 size_t file_size = ftell(file);
77 fseek(file, 0L, SEEK_SET);
78 wasm_byte_vec_t wat;
79 wasm_byte_vec_new_uninitialized(&wat, file_size);
80 if (fread(wat.data, file_size, 1, file) != 1) {
81 printf("> Error loading module!\n");
82 return 1;
83 }
84 fclose(file);
85
86 // Parse the wat into the binary wasm format
87 wasm_byte_vec_t binary;
88 wasmtime_error_t *error = wasmtime_wat2wasm(wat.data, wat.size, &binary);
89 if (error != NULL)
90 exit_with_error("failed to parse wat", error, NULL);
91 wasm_byte_vec_delete(&wat);
92
93 // Compile.
94 printf("Compiling module...\n");
95 wasmtime_module_t *module = NULL;
96 error =
97 wasmtime_module_new(engine, (uint8_t *)binary.data, binary.size, &module);
98 if (error)
99 exit_with_error("failed to compile module", error, NULL);
100 wasm_byte_vec_delete(&binary);
101
102 // Create external print functions.
103 printf("Creating callback...\n");
104 wasm_functype_t *callback_type =
105 wasm_functype_new_2_2(wasm_valtype_new_i32(), wasm_valtype_new_i64(),
106 wasm_valtype_new_i64(), wasm_valtype_new_i32());
107 wasmtime_func_t callback_func;
108 wasmtime_func_new(context, callback_type, callback, NULL, NULL,
109 &callback_func);
110 wasm_functype_delete(callback_type);
111
112 // Instantiate.
113 printf("Instantiating module...\n");
114 wasmtime_extern_t imports[1];
115 imports[0].kind = WASMTIME_EXTERN_FUNC;
116 imports[0].of.func = callback_func;
117 wasmtime_instance_t instance;
118 wasm_trap_t *trap = NULL;
119 error = wasmtime_instance_new(context, module, imports, 1, &instance, &trap);

Callers

nothing calls this directly

Calls 11

wasm_engine_newFunction · 0.85
wasmtime_store_newFunction · 0.85
wasmtime_store_contextFunction · 0.85
wasmtime_wat2wasmFunction · 0.85
wasmtime_module_newFunction · 0.85
wasmtime_func_newFunction · 0.85
wasmtime_instance_newFunction · 0.85
assertFunction · 0.85
wasmtime_func_callFunction · 0.85
exit_with_errorFunction · 0.70

Tested by

no test coverage detected