reflect.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <inttypes.h>
  5. #include "wasm_c_api.h"
  6. #define own
  7. void print_mutability(wasm_mutability_t mut) {
  8. switch (mut) {
  9. case WASM_VAR: printf("var"); break;
  10. case WASM_CONST: printf("const"); break;
  11. }
  12. }
  13. void print_limits(const wasm_limits_t* limits) {
  14. if (!limits) {
  15. printf("unknown limits");
  16. return;
  17. }
  18. printf("%ud", limits->min);
  19. if (limits->max < wasm_limits_max_default) printf(" %ud", limits->max);
  20. }
  21. void print_valtype(const wasm_valtype_t* type) {
  22. switch (wasm_valtype_kind(type)) {
  23. case WASM_I32: printf("i32"); break;
  24. case WASM_I64: printf("i64"); break;
  25. case WASM_F32: printf("f32"); break;
  26. case WASM_F64: printf("f64"); break;
  27. case WASM_ANYREF: printf("anyref"); break;
  28. case WASM_FUNCREF: printf("funcref"); break;
  29. }
  30. }
  31. void print_valtypes(const wasm_valtype_vec_t* types) {
  32. bool first = true;
  33. for (size_t i = 0; i < types->size; ++i) {
  34. if (first) {
  35. first = false;
  36. } else {
  37. printf(" ");
  38. }
  39. print_valtype(types->data[i]);
  40. }
  41. }
  42. void print_externtype(const wasm_externtype_t* type) {
  43. if (!type) {
  44. printf("unknown extern type");
  45. return;
  46. }
  47. switch (wasm_externtype_kind(type)) {
  48. case WASM_EXTERN_FUNC: {
  49. const wasm_functype_t* functype =
  50. wasm_externtype_as_functype_const(type);
  51. printf("func ");
  52. print_valtypes(wasm_functype_params(functype));
  53. printf(" -> ");
  54. print_valtypes(wasm_functype_results(functype));
  55. } break;
  56. case WASM_EXTERN_GLOBAL: {
  57. const wasm_globaltype_t* globaltype =
  58. wasm_externtype_as_globaltype_const(type);
  59. printf("global ");
  60. print_mutability(wasm_globaltype_mutability(globaltype));
  61. printf(" ");
  62. print_valtype(wasm_globaltype_content(globaltype));
  63. } break;
  64. case WASM_EXTERN_TABLE: {
  65. const wasm_tabletype_t* tabletype =
  66. wasm_externtype_as_tabletype_const(type);
  67. printf("table ");
  68. print_limits(wasm_tabletype_limits(tabletype));
  69. printf(" ");
  70. print_valtype(wasm_tabletype_element(tabletype));
  71. } break;
  72. case WASM_EXTERN_MEMORY: {
  73. const wasm_memorytype_t* memorytype =
  74. wasm_externtype_as_memorytype_const(type);
  75. printf("memory ");
  76. print_limits(wasm_memorytype_limits(memorytype));
  77. } break;
  78. }
  79. }
  80. void print_name(const wasm_name_t* name) {
  81. if (!name) {
  82. printf("unknown name");
  83. return;
  84. }
  85. printf("\"%.*s\"", (int)name->size, name->data);
  86. }
  87. int main(int argc, const char* argv[]) {
  88. // Initialize.
  89. printf("Initializing...\n");
  90. wasm_engine_t* engine = wasm_engine_new();
  91. wasm_store_t* store = wasm_store_new(engine);
  92. // Load binary.
  93. printf("Loading binary...\n");
  94. #if WASM_ENABLE_AOT != 0 && WASM_ENABLE_INTERP == 0
  95. FILE* file = fopen("reflect.aot", "rb");
  96. #else
  97. FILE* file = fopen("reflect.wasm", "rb");
  98. #endif
  99. if (!file) {
  100. printf("> Error loading module!\n");
  101. return 1;
  102. }
  103. int ret = fseek(file, 0L, SEEK_END);
  104. if (ret == -1) {
  105. printf("> Error loading module!\n");
  106. fclose(file);
  107. return 1;
  108. }
  109. long file_size = ftell(file);
  110. if (file_size == -1) {
  111. printf("> Error loading module!\n");
  112. fclose(file);
  113. return 1;
  114. }
  115. ret = fseek(file, 0L, SEEK_SET);
  116. if (ret == -1) {
  117. printf("> Error loading module!\n");
  118. fclose(file);
  119. return 1;
  120. }
  121. wasm_byte_vec_t binary;
  122. wasm_byte_vec_new_uninitialized(&binary, file_size);
  123. if (fread(binary.data, file_size, 1, file) != 1) {
  124. printf("> Error loading module!\n");
  125. fclose(file);
  126. return 1;
  127. }
  128. fclose(file);
  129. // Compile.
  130. printf("Compiling module...\n");
  131. own wasm_module_t* module = wasm_module_new(store, &binary);
  132. if (!module) {
  133. printf("> Error compiling module!\n");
  134. return 1;
  135. }
  136. wasm_byte_vec_delete(&binary);
  137. // Instantiate.
  138. printf("Instantiating module...\n");
  139. wasm_extern_vec_t imports = WASM_EMPTY_VEC;
  140. own wasm_instance_t* instance =
  141. wasm_instance_new(store, module, &imports, NULL);
  142. if (!instance) {
  143. printf("> Error instantiating module!\n");
  144. return 1;
  145. }
  146. // Extract export.
  147. printf("Extracting export...\n");
  148. own wasm_exporttype_vec_t export_types;
  149. own wasm_extern_vec_t exports;
  150. wasm_module_exports(module, &export_types);
  151. wasm_instance_exports(instance, &exports);
  152. assert(exports.size == export_types.size);
  153. for (size_t i = 0; i < exports.size; ++i) {
  154. assert(wasm_extern_kind(exports.data[i]) ==
  155. wasm_externtype_kind(wasm_exporttype_type(export_types.data[i])));
  156. printf("> export %zu ", i);
  157. print_name(wasm_exporttype_name(export_types.data[i]));
  158. printf("\n");
  159. printf(">> initial: ");
  160. print_externtype(wasm_exporttype_type(export_types.data[i]));
  161. printf("\n");
  162. printf(">> current: ");
  163. own wasm_externtype_t* current = wasm_extern_type(exports.data[i]);
  164. print_externtype(current);
  165. wasm_externtype_delete(current);
  166. printf("\n");
  167. if (wasm_extern_kind(exports.data[i]) == WASM_EXTERN_FUNC) {
  168. wasm_func_t* func = wasm_extern_as_func(exports.data[i]);
  169. printf(">> in-arity: %zu", wasm_func_param_arity(func));
  170. printf(", out-arity: %zu\n", wasm_func_result_arity(func));
  171. }
  172. }
  173. wasm_module_delete(module);
  174. wasm_instance_delete(instance);
  175. wasm_extern_vec_delete(&exports);
  176. wasm_exporttype_vec_delete(&export_types);
  177. // Shut down.
  178. printf("Shutting down...\n");
  179. wasm_store_delete(store);
  180. wasm_engine_delete(engine);
  181. // All done.
  182. printf("Done.\n");
  183. return 0;
  184. }