Guest User

Untitled

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