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

Function main

example/reflect.c:85–171  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

83
84
85int main(int argc, const char* argv[]) {
86 // Initialize.
87 printf("Initializing...\n");
88 wasm_engine_t* engine = wasm_engine_new();
89 wasm_store_t* store = wasm_store_new(engine);
90
91 // Load binary.
92 printf("Loading binary...\n");
93 FILE* file = fopen("reflect.wasm", "rb");
94 if (!file) {
95 printf("> Error loading module!\n");
96 return 1;
97 }
98 fseek(file, 0L, SEEK_END);
99 size_t file_size = ftell(file);
100 fseek(file, 0L, SEEK_SET);
101 wasm_byte_vec_t binary;
102 wasm_byte_vec_new_uninitialized(&binary, file_size);
103 if (fread(binary.data, file_size, 1, file) != 1) {
104 printf("> Error loading module!\n");
105 return 1;
106 }
107 fclose(file);
108
109 // Compile.
110 printf("Compiling module...\n");
111 own wasm_module_t* module = wasm_module_new(store, &binary);
112 if (!module) {
113 printf("> Error compiling module!\n");
114 return 1;
115 }
116
117 wasm_byte_vec_delete(&binary);
118
119 // Instantiate.
120 printf("Instantiating module...\n");
121 wasm_extern_vec_t imports = WASM_EMPTY_VEC;
122 own wasm_instance_t* instance =
123 wasm_instance_new(store, module, &imports, NULL);
124 if (!instance) {
125 printf("> Error instantiating module!\n");
126 return 1;
127 }
128
129 // Extract export.
130 printf("Extracting export...\n");
131 own wasm_exporttype_vec_t export_types;
132 own wasm_extern_vec_t exports;
133 wasm_module_exports(module, &export_types);
134 wasm_instance_exports(instance, &exports);
135 assert(exports.size == export_types.size);
136
137 for (size_t i = 0; i < exports.size; ++i) {
138 assert(wasm_extern_kind(exports.data[i]) ==
139 wasm_externtype_kind(wasm_exporttype_type(export_types.data[i])));
140 printf("> export %zu ", i);
141 print_name(wasm_exporttype_name(export_types.data[i]));
142 printf("\n");

Callers

nothing calls this directly

Calls 15

wasm_engine_newFunction · 0.85
wasm_store_newFunction · 0.85
wasm_module_newFunction · 0.85
wasm_instance_newFunction · 0.85
wasm_module_exportsFunction · 0.85
wasm_instance_exportsFunction · 0.85
wasm_extern_kindFunction · 0.85
wasm_externtype_kindFunction · 0.85
wasm_exporttype_typeFunction · 0.85
print_nameFunction · 0.85
wasm_exporttype_nameFunction · 0.85
print_externtypeFunction · 0.85

Tested by

no test coverage detected