table.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. if (wasm_func_call(func, &args, &results) || r[0].of.i32 != expected) {
  47. printf("> Error on result\n");
  48. exit(1);
  49. }
  50. }
  51. void check_trap(wasm_func_t* func, int32_t arg1, int32_t arg2) {
  52. wasm_val_t vs[2] = { WASM_I32_VAL(arg1), WASM_I32_VAL(arg2) };
  53. wasm_val_t r[1] = { WASM_INIT_VAL };
  54. wasm_val_vec_t args = WASM_ARRAY_VEC(vs);
  55. wasm_val_vec_t results = WASM_ARRAY_VEC(r);
  56. own wasm_trap_t* trap = wasm_func_call(func, &args, &results);
  57. if (! trap) {
  58. printf("> Error on result, expected trap\n");
  59. exit(1);
  60. }
  61. wasm_trap_delete(trap);
  62. }
  63. int main(int argc, const char* argv[]) {
  64. // Initialize.
  65. printf("Initializing...\n");
  66. wasm_engine_t* engine = wasm_engine_new();
  67. wasm_store_t* store = wasm_store_new(engine);
  68. // Load binary.
  69. printf("Loading binary...\n");
  70. #if WASM_ENABLE_AOT != 0 && WASM_ENABLE_INTERP == 0
  71. FILE* file = fopen("table.aot", "rb");
  72. #else
  73. FILE* file = fopen("table.wasm", "rb");
  74. #endif
  75. if (!file) {
  76. printf("> Error loading module!\n");
  77. return 1;
  78. }
  79. fseek(file, 0L, SEEK_END);
  80. size_t file_size = ftell(file);
  81. fseek(file, 0L, SEEK_SET);
  82. wasm_byte_vec_t binary;
  83. wasm_byte_vec_new_uninitialized(&binary, file_size);
  84. if (fread(binary.data, file_size, 1, file) != 1) {
  85. printf("> Error loading module!\n");
  86. fclose(file);
  87. return 1;
  88. }
  89. fclose(file);
  90. // Compile.
  91. printf("Compiling module...\n");
  92. own wasm_module_t* module = wasm_module_new(store, &binary);
  93. if (!module) {
  94. printf("> Error compiling module!\n");
  95. return 1;
  96. }
  97. wasm_byte_vec_delete(&binary);
  98. // Instantiate.
  99. printf("Instantiating module...\n");
  100. wasm_extern_vec_t imports = WASM_EMPTY_VEC;
  101. own wasm_instance_t* instance =
  102. wasm_instance_new(store, module, &imports, NULL);
  103. if (!instance) {
  104. printf("> Error instantiating module!\n");
  105. return 1;
  106. }
  107. // Extract export.
  108. printf("Extracting exports...\n");
  109. own wasm_extern_vec_t exports;
  110. wasm_instance_exports(instance, &exports);
  111. size_t i = 0;
  112. wasm_table_t* table = get_export_table(&exports, i++);
  113. wasm_func_t* call_indirect = get_export_func(&exports, i++);
  114. wasm_func_t* f = get_export_func(&exports, i++);
  115. wasm_func_t* g = get_export_func(&exports, i++);
  116. wasm_module_delete(module);
  117. // Create external function.
  118. printf("Creating callback...\n");
  119. own wasm_functype_t* neg_type = wasm_functype_new_1_1(wasm_valtype_new_i32(), wasm_valtype_new_i32());
  120. own wasm_func_t* h = wasm_func_new(store, neg_type, neg_callback);
  121. wasm_functype_delete(neg_type);
  122. // Try cloning.
  123. own wasm_table_t* copy = wasm_table_copy(table);
  124. assert(wasm_table_same(table, copy));
  125. wasm_table_delete(copy);
  126. // Check initial table.
  127. printf("Checking table...\n");
  128. check(wasm_table_size(table) == 2);
  129. check_table(table, 0, false);
  130. check_table(table, 1, true);
  131. check_trap(call_indirect, 0, 0);
  132. check_call(call_indirect, 7, 1, 7);
  133. check_trap(call_indirect, 0, 2);
  134. // Mutate table.
  135. printf("Mutating table...\n");
  136. check(wasm_table_set(table, 0, wasm_func_as_ref(g)));
  137. check(wasm_table_set(table, 1, NULL));
  138. wasm_ref_t *ref_f = wasm_func_as_ref(f);
  139. check(! wasm_table_set(table, 2, ref_f));
  140. wasm_ref_delete(ref_f);
  141. check_table(table, 0, true);
  142. check_table(table, 1, false);
  143. check_call(call_indirect, 7, 0, 666);
  144. check_trap(call_indirect, 0, 1);
  145. check_trap(call_indirect, 0, 2);
  146. // Grow table.
  147. // DO NOT SUPPORT
  148. printf("Bypass Growing table...\n");
  149. wasm_func_delete(h);
  150. wasm_extern_vec_delete(&exports);
  151. wasm_instance_delete(instance);
  152. // Create stand-alone table.
  153. // DO NOT SUPPORT
  154. // TODO(wasm+): Once Wasm allows multiple tables, turn this into import.
  155. printf("Bypass Creating stand-alone table...\n");
  156. // Shut down.
  157. printf("Shutting down...\n");
  158. wasm_store_delete(store);
  159. wasm_engine_delete(engine);
  160. // All done.
  161. printf("Done.\n");
  162. return 0;
  163. }