1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <inttypes.h>
5
6 #include "wasm.h"
7
8 #define own
9
print_mutability(wasm_mutability_t mut)10 void print_mutability(wasm_mutability_t mut) {
11 switch (mut) {
12 case WASM_VAR: printf("var"); break;
13 case WASM_CONST: printf("const"); break;
14 }
15 }
16
print_limits(const wasm_limits_t * limits)17 void print_limits(const wasm_limits_t* limits) {
18 printf("%ud", limits->min);
19 if (limits->max < wasm_limits_max_default) printf(" %ud", limits->max);
20 }
21
print_valtype(const wasm_valtype_t * type)22 void print_valtype(const wasm_valtype_t* type) {
23 switch (wasm_valtype_kind(type)) {
24 case WASM_I32: printf("i32"); break;
25 case WASM_I64: printf("i64"); break;
26 case WASM_F32: printf("f32"); break;
27 case WASM_F64: printf("f64"); break;
28 case WASM_ANYREF: printf("anyref"); break;
29 case WASM_FUNCREF: printf("funcref"); break;
30 }
31 }
32
print_valtypes(const wasm_valtype_vec_t * types)33 void print_valtypes(const wasm_valtype_vec_t* types) {
34 bool first = true;
35 for (size_t i = 0; i < types->size; ++i) {
36 if (first) {
37 first = false;
38 } else {
39 printf(" ");
40 }
41 print_valtype(types->data[i]);
42 }
43 }
44
print_externtype(const wasm_externtype_t * type)45 void print_externtype(const wasm_externtype_t* type) {
46 switch (wasm_externtype_kind(type)) {
47 case WASM_EXTERN_FUNC: {
48 const wasm_functype_t* functype =
49 wasm_externtype_as_functype_const(type);
50 printf("func ");
51 print_valtypes(wasm_functype_params(functype));
52 printf(" -> ");
53 print_valtypes(wasm_functype_results(functype));
54 } break;
55 case WASM_EXTERN_GLOBAL: {
56 const wasm_globaltype_t* globaltype =
57 wasm_externtype_as_globaltype_const(type);
58 printf("global ");
59 print_mutability(wasm_globaltype_mutability(globaltype));
60 printf(" ");
61 print_valtype(wasm_globaltype_content(globaltype));
62 } break;
63 case WASM_EXTERN_TABLE: {
64 const wasm_tabletype_t* tabletype =
65 wasm_externtype_as_tabletype_const(type);
66 printf("table ");
67 print_limits(wasm_tabletype_limits(tabletype));
68 printf(" ");
69 print_valtype(wasm_tabletype_element(tabletype));
70 } break;
71 case WASM_EXTERN_MEMORY: {
72 const wasm_memorytype_t* memorytype =
73 wasm_externtype_as_memorytype_const(type);
74 printf("memory ");
75 print_limits(wasm_memorytype_limits(memorytype));
76 } break;
77 }
78 }
79
print_name(const wasm_name_t * name)80 void print_name(const wasm_name_t* name) {
81 printf("\"%.*s\"", (int)name->size, name->data);
82 }
83
84
main(int argc,const char * argv[])85 int 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", "r");
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 own wasm_instance_t* instance = wasm_instance_new(store, module, NULL, NULL);
122 if (!instance) {
123 printf("> Error instantiating module!\n");
124 return 1;
125 }
126
127 // Extract export.
128 printf("Extracting export...\n");
129 own wasm_exporttype_vec_t export_types;
130 own wasm_extern_vec_t exports;
131 wasm_module_exports(module, &export_types);
132 wasm_instance_exports(instance, &exports);
133 assert(exports.size == export_types.size);
134
135 for (size_t i = 0; i < exports.size; ++i) {
136 assert(wasm_extern_kind(exports.data[i]) ==
137 wasm_externtype_kind(wasm_exporttype_type(export_types.data[i])));
138 printf("> export %zu ", i);
139 print_name(wasm_exporttype_name(export_types.data[i]));
140 printf("\n");
141 printf(">> initial: ");
142 print_externtype(wasm_exporttype_type(export_types.data[i]));
143 printf("\n");
144 printf(">> current: ");
145 own wasm_externtype_t* current = wasm_extern_type(exports.data[i]);
146 print_externtype(current);
147 wasm_externtype_delete(current);
148 printf("\n");
149 if (wasm_extern_kind(exports.data[i]) == WASM_EXTERN_FUNC) {
150 wasm_func_t* func = wasm_extern_as_func(exports.data[i]);
151 printf(">> in-arity: %zu", wasm_func_param_arity(func));
152 printf(", out-arity: %zu\n", wasm_func_result_arity(func));
153 }
154 }
155
156 wasm_module_delete(module);
157 wasm_instance_delete(instance);
158 wasm_extern_vec_delete(&exports);
159 wasm_exporttype_vec_delete(&export_types);
160
161 // Shut down.
162 printf("Shutting down...\n");
163 wasm_store_delete(store);
164 wasm_engine_delete(engine);
165
166 // All done.
167 printf("Done.\n");
168 return 0;
169 }
170