table.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. // A function to be called from Wasm code.
  8. own wasm_trap_t* neg_callback(
  9. const wasm_val_vec_t* args, wasm_val_vec_t* results
  10. ) {
  11. printf("Calling back...\n");
  12. results->data[0].kind = WASM_I32;
  13. results->data[0].of.i32 = -args->data[0].of.i32;
  14. return NULL;
  15. }
  16. wasm_table_t* get_export_table(const wasm_extern_vec_t* exports, size_t i) {
  17. if (exports->size <= i || !wasm_extern_as_table(exports->data[i])) {
  18. printf("> Error accessing table export %zu!\n", i);
  19. exit(1);
  20. }
  21. return wasm_extern_as_table(exports->data[i]);
  22. }
  23. wasm_func_t* get_export_func(const wasm_extern_vec_t* exports, size_t i) {
  24. if (exports->size <= i || !wasm_extern_as_func(exports->data[i])) {
  25. printf("> Error accessing function export %zu!\n", i);
  26. exit(1);
  27. }
  28. return wasm_extern_as_func(exports->data[i]);
  29. }
  30. void check(bool success) {
  31. if (!success) {
  32. printf("> Error, expected success\n");
  33. exit(1);
  34. }
  35. }
  36. void check_table(wasm_table_t* table, int32_t i, bool expect_set) {
  37. own wasm_ref_t* ref = wasm_table_get(table, i);
  38. check((ref != NULL) == expect_set);
  39. if (ref) wasm_ref_delete(ref);
  40. }
  41. void check_call(wasm_func_t* func, int32_t arg1, int32_t arg2, int32_t expected) {
  42. wasm_val_t vs[2] = { WASM_I32_VAL(arg1), WASM_I32_VAL(arg2) };
  43. wasm_val_t r[1] = { WASM_INIT_VAL };
  44. wasm_val_vec_t args = WASM_ARRAY_VEC(vs);
  45. wasm_val_vec_t results = WASM_ARRAY_VEC(r);
  46. wasm_trap_t *trap = wasm_func_call(func, &args, &results);
  47. if (trap) {
  48. printf("> Error on calling\n");
  49. wasm_trap_delete(trap);
  50. exit(1);
  51. }
  52. if (r[0].of.i32 != expected) {
  53. printf("> Error on result\n");
  54. exit(1);
  55. }
  56. }
  57. void check_trap(wasm_func_t* func, int32_t arg1, int32_t arg2) {
  58. wasm_val_t vs[2] = { WASM_I32_VAL(arg1), WASM_I32_VAL(arg2) };
  59. wasm_val_t r[1] = { WASM_INIT_VAL };
  60. wasm_val_vec_t args = WASM_ARRAY_VEC(vs);
  61. wasm_val_vec_t results = WASM_ARRAY_VEC(r);
  62. own wasm_trap_t* trap = wasm_func_call(func, &args, &results);
  63. if (! trap) {
  64. printf("> Error on result, expected trap\n");
  65. exit(1);
  66. }
  67. wasm_trap_delete(trap);
  68. }
  69. int main(int argc, const char* argv[]) {
  70. // Initialize.
  71. printf("Initializing...\n");
  72. wasm_engine_t* engine = wasm_engine_new();
  73. wasm_store_t* store = wasm_store_new(engine);
  74. // Load binary.
  75. printf("Loading binary...\n");
  76. #if WASM_ENABLE_AOT != 0 && WASM_ENABLE_INTERP == 0
  77. FILE* file = fopen("table.aot", "rb");
  78. #else
  79. FILE* file = fopen("table.wasm", "rb");
  80. #endif
  81. if (!file) {
  82. printf("> Error loading module!\n");
  83. return 1;
  84. }
  85. int ret = fseek(file, 0L, SEEK_END);
  86. if (ret == -1) {
  87. printf("> Error loading module!\n");
  88. fclose(file);
  89. return 1;
  90. }
  91. long file_size = ftell(file);
  92. if (file_size == -1) {
  93. printf("> Error loading module!\n");
  94. fclose(file);
  95. return 1;
  96. }
  97. ret = fseek(file, 0L, SEEK_SET);
  98. if (ret == -1) {
  99. printf("> Error loading module!\n");
  100. fclose(file);
  101. return 1;
  102. }
  103. wasm_byte_vec_t binary;
  104. wasm_byte_vec_new_uninitialized(&binary, file_size);
  105. if (fread(binary.data, file_size, 1, file) != 1) {
  106. printf("> Error loading module!\n");
  107. fclose(file);
  108. return 1;
  109. }
  110. fclose(file);
  111. // Compile.
  112. printf("Compiling module...\n");
  113. own wasm_module_t* module = wasm_module_new(store, &binary);
  114. if (!module) {
  115. printf("> Error compiling module!\n");
  116. return 1;
  117. }
  118. wasm_byte_vec_delete(&binary);
  119. // Instantiate.
  120. printf("Instantiating module...\n");
  121. wasm_extern_vec_t imports = WASM_EMPTY_VEC;
  122. own wasm_instance_t* instance =
  123. wasm_instance_new(store, module, &imports, NULL);
  124. if (!instance) {
  125. printf("> Error instantiating module!\n");
  126. return 1;
  127. }
  128. // Extract export.
  129. printf("Extracting exports...\n");
  130. own wasm_extern_vec_t exports;
  131. wasm_instance_exports(instance, &exports);
  132. size_t i = 0;
  133. wasm_table_t* table = get_export_table(&exports, i++);
  134. wasm_func_t* call_indirect = get_export_func(&exports, i++);
  135. wasm_func_t* f = get_export_func(&exports, i++);
  136. wasm_func_t* g = get_export_func(&exports, i++);
  137. wasm_module_delete(module);
  138. // Create external function.
  139. printf("Creating callback...\n");
  140. own wasm_functype_t* neg_type = wasm_functype_new_1_1(wasm_valtype_new_i32(), wasm_valtype_new_i32());
  141. own wasm_func_t* h = wasm_func_new(store, neg_type, neg_callback);
  142. wasm_functype_delete(neg_type);
  143. // Try cloning.
  144. own wasm_table_t* copy = wasm_table_copy(table);
  145. assert(wasm_table_same(table, copy));
  146. wasm_table_delete(copy);
  147. // Check initial table.
  148. printf("Checking table...\n");
  149. check(wasm_table_size(table) == 2);
  150. check_table(table, 0, false);
  151. check_table(table, 1, true);
  152. check_trap(call_indirect, 0, 0);
  153. check_call(call_indirect, 7, 1, 7);
  154. check_trap(call_indirect, 0, 2);
  155. // Mutate table.
  156. printf("Mutating table...\n");
  157. check(wasm_table_set(table, 0, wasm_func_as_ref(g)));
  158. check(wasm_table_set(table, 1, NULL));
  159. wasm_ref_t *ref_f = wasm_func_as_ref(f);
  160. check(! wasm_table_set(table, 2, ref_f));
  161. wasm_ref_delete(ref_f);
  162. check_table(table, 0, true);
  163. check_table(table, 1, false);
  164. check_call(call_indirect, 7, 0, 666);
  165. check_trap(call_indirect, 0, 1);
  166. check_trap(call_indirect, 0, 2);
  167. // Grow table.
  168. // DO NOT SUPPORT
  169. printf("Bypass Growing table...\n");
  170. wasm_func_delete(h);
  171. wasm_extern_vec_delete(&exports);
  172. wasm_instance_delete(instance);
  173. // Create stand-alone table.
  174. // DO NOT SUPPORT
  175. // TODO(wasm+): Once Wasm allows multiple tables, turn this into import.
  176. printf("Bypass Creating stand-alone table...\n");
  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. }