reflect.c 4.9 KB

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