MCPcopy Create free account
hub / github.com/WebAssembly/wasm-c-api / main

Function main

example/hostref.c:125–275  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

123
124
125int main(int argc, const char* argv[]) {
126 int rc = 0;
127
128 // Initialize.
129 printf("Initializing...\n");
130 wasm_engine_t* engine = wasm_engine_new();
131 wasm_store_t* store = wasm_store_new(engine);
132
133 // Load binary.
134 printf("Loading binary...\n");
135 wasm_byte_vec_t binary = WASM_EMPTY_VEC;
136 FILE* file = fopen("hostref.wasm", "rb");
137 if (!file) {
138 printf("> Error loading module!\n");
139 rc = 1; goto cleanup;
140 }
141 fseek(file, 0L, SEEK_END);
142 size_t file_size = ftell(file);
143 fseek(file, 0L, SEEK_SET);
144 wasm_byte_vec_new_uninitialized(&binary, file_size);
145 if (fread(binary.data, file_size, 1, file) != 1) {
146 printf("> Error loading module!\n");
147 fclose(file);
148 rc = 1; goto cleanup;
149 }
150 fclose(file);
151
152 // Compile.
153 printf("Compiling module...\n");
154 own wasm_module_t* module = wasm_module_new(store, &binary);
155 wasm_byte_vec_delete(&binary);
156 binary = (wasm_byte_vec_t)WASM_EMPTY_VEC;
157 if (!module) {
158 printf("> Error compiling module!\n");
159 rc = 1; goto cleanup;
160 }
161
162 // Create external callback function.
163 printf("Creating callback...\n");
164 own wasm_functype_t* callback_type = wasm_functype_new_1_1(
165 wasm_valtype_new(WASM_EXTERNREF), wasm_valtype_new(WASM_EXTERNREF));
166 own wasm_func_t* callback_func =
167 wasm_func_new(store, callback_type, callback);
168
169 wasm_functype_delete(callback_type);
170
171 // Instantiate.
172 printf("Instantiating module...\n");
173 wasm_extern_t* externs[] = { wasm_func_as_extern(callback_func) };
174 wasm_extern_vec_t imports = WASM_ARRAY_VEC(externs);
175 own wasm_instance_t* instance =
176 wasm_instance_new(store, module, &imports, NULL);
177 if (!instance) {
178 printf("> Error instantiating module!\n");
179 rc = 1; goto cleanup;
180 }
181
182 wasm_func_delete(callback_func);

Callers

nothing calls this directly

Calls 15

wasm_engine_newFunction · 0.85
wasm_store_newFunction · 0.85
wasm_module_newFunction · 0.85
wasm_valtype_newFunction · 0.85
wasm_func_newFunction · 0.85
wasm_func_as_externFunction · 0.85
wasm_instance_newFunction · 0.85
wasm_instance_exportsFunction · 0.85
wasm_foreign_newFunction · 0.85
wasm_val_deleteFunction · 0.85
wasm_global_getFunction · 0.85
wasm_global_setFunction · 0.85

Tested by

no test coverage detected