Guest User

Untitled

a guest
May 2nd, 2020
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.91 KB | None | 0 0
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <inttypes.h>
  6.  
  7.  
  8. #include "wasm.h"
  9.  
  10.  
  11.  
  12. #define own
  13.  
  14.  
  15. void print_mutability(wasm_mutability_t mut) {
  16. switch (mut) {
  17. case WASM_VAR: printf("var"); break;
  18. case WASM_CONST: printf("const"); break;
  19. }
  20. }
  21.  
  22. void print_limits(const wasm_limits_t* limits) {
  23. printf("%ud", limits->min);
  24. if (limits->max < wasm_limits_max_default) printf(" %ud", limits->max);
  25. }
  26.  
  27. void print_valtype(const wasm_valtype_t* type) {
  28. switch (wasm_valtype_kind(type)) {
  29. case WASM_I32: printf("i32"); break;
  30. case WASM_I64: printf("i64"); break;
  31. case WASM_F32: printf("f32"); break;
  32. case WASM_F64: printf("f64"); break;
  33. case WASM_ANYREF: printf("anyref"); break;
  34. case WASM_FUNCREF: printf("funcref"); break;
  35. }
  36. }
  37.  
  38. void print_valtypes(const wasm_valtype_vec_t* types) {
  39. bool first = true;
  40. for (size_t i = 0; i < types->size; ++i) {
  41. if (first) {
  42. first = false;
  43. } else {
  44. printf(" ");
  45. }
  46. print_valtype(types->data[i]);
  47. }
  48. }
  49.  
  50. void print_externtype(const wasm_externtype_t* type) {
  51. switch (wasm_externtype_kind(type)) {
  52. case WASM_EXTERN_FUNC: {
  53. const wasm_functype_t* functype =
  54. wasm_externtype_as_functype_const(type);
  55. printf("func ");
  56. print_valtypes(wasm_functype_params(functype));
  57. printf(" -> ");
  58. print_valtypes(wasm_functype_results(functype));
  59. } break;
  60. case WASM_EXTERN_GLOBAL: {
  61. const wasm_globaltype_t* globaltype =
  62. wasm_externtype_as_globaltype_const(type);
  63. printf("global ");
  64. print_mutability(wasm_globaltype_mutability(globaltype));
  65. printf(" ");
  66. print_valtype(wasm_globaltype_content(globaltype));
  67. } break;
  68. case WASM_EXTERN_TABLE: {
  69. const wasm_tabletype_t* tabletype =
  70. wasm_externtype_as_tabletype_const(type);
  71. printf("table ");
  72. print_limits(wasm_tabletype_limits(tabletype));
  73. printf(" ");
  74. print_valtype(wasm_tabletype_element(tabletype));
  75. } break;
  76. case WASM_EXTERN_MEMORY: {
  77. const wasm_memorytype_t* memorytype =
  78. wasm_externtype_as_memorytype_const(type);
  79. printf("memory ");
  80. print_limits(wasm_memorytype_limits(memorytype));
  81. } break;
  82. }
  83. }
  84.  
  85. void print_name(const wasm_name_t* name) {
  86. printf("\"%.*s\"", (int)name->size, name->data);
  87. }
  88.  
  89.  
  90. int main(int argc, char **argv)
  91. {
  92. // Initialize.
  93. printf("Initializing...\n");
  94. wasm_engine_t* engine = wasm_engine_new();
  95. wasm_store_t* store = wasm_store_new(engine);
  96.  
  97.  
  98. // Load binary.
  99. printf("Loading binary...\n");
  100.  
  101. FILE* file = fopen("imports.wasm", "rb");
  102.  
  103. if (!file) {
  104. printf("> Error loading module!\n");
  105. return 1;
  106. }
  107. fseek(file, 0L, SEEK_END);
  108. size_t file_size = ftell(file);
  109. fseek(file, 0L, SEEK_SET);
  110. wasm_byte_vec_t binary;
  111. wasm_byte_vec_new_uninitialized(&binary, file_size);
  112.  
  113. if (fread(binary.data, file_size, 1, file) != 1) {
  114. printf("> Error loading module!\n");
  115. return 1;
  116. }
  117.  
  118. fclose(file);
  119.  
  120. // Compile.
  121. printf("Compiling module...\n");
  122. own wasm_module_t* module = wasm_module_new(store, &binary);
  123. if (!module) {
  124. printf("> Error compiling module!\n");
  125. return 1;
  126. }
  127.  
  128. wasm_byte_vec_delete(&binary);
  129.  
  130.  
  131.  
  132. // Scan for imports ...
  133. printf("Extracting imports...\n");
  134. own wasm_importtype_vec_t import_types;
  135. wasm_module_imports(module, &import_types);
  136.  
  137.  
  138. for (size_t i = 0; i < import_types.size; ++i)
  139. {
  140. printf("import %zu ", i);
  141. print_name(wasm_importtype_name(import_types.data[i]));
  142. printf("\t\t");
  143. print_externtype(wasm_importtype_type(import_types.data[i]));
  144. printf("\n");
  145. }
  146.  
  147. wasm_module_delete(module);
  148. wasm_importtype_vec_delete(&import_types);
  149.  
  150. // Shut down.
  151. printf("Shutting down...\n");
  152. wasm_store_delete(store);
  153. wasm_engine_delete(engine);
  154.  
  155. // All done.
  156. printf("Done.\n");
  157. return 0;
  158. }
Add Comment
Please, Sign In to add comment