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

Function main

example/multi.c:42–156  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

40
41
42int main(int argc, const char* argv[]) {
43 // Initialize.
44 printf("Initializing...\n");
45 wasm_engine_t* engine = wasm_engine_new();
46 wasm_store_t* store = wasm_store_new(engine);
47
48 // Load binary.
49 printf("Loading binary...\n");
50 FILE* file = fopen("multi.wasm", "rb");
51 if (!file) {
52 printf("> Error loading module!\n");
53 return 1;
54 }
55 fseek(file, 0L, SEEK_END);
56 size_t file_size = ftell(file);
57 fseek(file, 0L, SEEK_SET);
58 wasm_byte_vec_t binary;
59 wasm_byte_vec_new_uninitialized(&binary, file_size);
60 if (fread(binary.data, file_size, 1, file) != 1) {
61 printf("> Error loading module!\n");
62 return 1;
63 }
64 fclose(file);
65
66 // Compile.
67 printf("Compiling module...\n");
68 own wasm_module_t* module = wasm_module_new(store, &binary);
69 if (!module) {
70 printf("> Error compiling module!\n");
71 return 1;
72 }
73
74 wasm_byte_vec_delete(&binary);
75
76 // Create external print functions.
77 printf("Creating callback...\n");
78 wasm_valtype_t* types[4] = {
79 wasm_valtype_new_i32(), wasm_valtype_new_i64(),
80 wasm_valtype_new_i64(), wasm_valtype_new_i32()
81 };
82 own wasm_valtype_vec_t tuple1, tuple2;
83 wasm_valtype_vec_new(&tuple1, 4, types);
84 wasm_valtype_vec_copy(&tuple2, &tuple1);
85 own wasm_functype_t* callback_type = wasm_functype_new(&tuple1, &tuple2);
86 own wasm_func_t* callback_func =
87 wasm_func_new(store, callback_type, callback);
88
89 wasm_functype_delete(callback_type);
90
91 // Instantiate.
92 printf("Instantiating module...\n");
93 wasm_extern_t* externs[] = { wasm_func_as_extern(callback_func) };
94 wasm_extern_vec_t imports = WASM_ARRAY_VEC(externs);
95 own wasm_instance_t* instance =
96 wasm_instance_new(store, module, &imports, NULL);
97 if (!instance) {
98 printf("> Error instantiating module!\n");
99 return 1;

Callers

nothing calls this directly

Calls 10

wasm_engine_newFunction · 0.85
wasm_store_newFunction · 0.85
wasm_module_newFunction · 0.85
wasm_functype_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_extern_as_funcFunction · 0.85
wasm_func_callFunction · 0.85

Tested by

no test coverage detected