hostref.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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* callback(
  9. const wasm_val_vec_t* args, wasm_val_vec_t* results
  10. ) {
  11. printf("Calling back...\n> ");
  12. printf("> %p\n",
  13. args->data[0].of.ref ? wasm_ref_get_host_info(args->data[0].of.ref) : NULL);
  14. wasm_val_copy(&results->data[0], &args->data[0]);
  15. return NULL;
  16. }
  17. wasm_func_t* get_export_func(const wasm_extern_vec_t* exports, size_t i) {
  18. if (exports->size <= i || !wasm_extern_as_func(exports->data[i])) {
  19. printf("> Error accessing function export %zu!\n", i);
  20. exit(1);
  21. }
  22. return wasm_extern_as_func(exports->data[i]);
  23. }
  24. wasm_global_t* get_export_global(const wasm_extern_vec_t* exports, size_t i) {
  25. if (exports->size <= i || !wasm_extern_as_global(exports->data[i])) {
  26. printf("> Error accessing global export %zu!\n", i);
  27. exit(1);
  28. }
  29. return wasm_extern_as_global(exports->data[i]);
  30. }
  31. wasm_table_t* get_export_table(const wasm_extern_vec_t* exports, size_t i) {
  32. if (exports->size <= i || !wasm_extern_as_table(exports->data[i])) {
  33. printf("> Error accessing table export %zu!\n", i);
  34. exit(1);
  35. }
  36. return wasm_extern_as_table(exports->data[i]);
  37. }
  38. own wasm_ref_t* call_v_r(const wasm_func_t* func) {
  39. printf("call_v_r... "); fflush(stdout);
  40. wasm_val_t rs[] = { WASM_INIT_VAL };
  41. wasm_val_vec_t args = WASM_EMPTY_VEC;
  42. wasm_val_vec_t results = WASM_ARRAY_VEC(rs);
  43. if (wasm_func_call(func, &args, &results)) {
  44. printf("> Error calling function!\n");
  45. exit(1);
  46. }
  47. printf("okay\n");
  48. return rs[0].of.ref;
  49. }
  50. void call_r_v(const wasm_func_t* func, wasm_ref_t* ref) {
  51. printf("call_r_v... "); fflush(stdout);
  52. wasm_val_t vs[1] = { WASM_REF_VAL(ref) };
  53. wasm_val_vec_t args = WASM_ARRAY_VEC(vs);
  54. wasm_val_vec_t results = WASM_EMPTY_VEC;
  55. if (wasm_func_call(func, &args, &results)) {
  56. printf("> Error calling function!\n");
  57. exit(1);
  58. }
  59. printf("okay\n");
  60. }
  61. own wasm_ref_t* call_r_r(const wasm_func_t* func, wasm_ref_t* ref) {
  62. printf("call_r_r... "); fflush(stdout);
  63. wasm_val_t vs[1] = { WASM_REF_VAL(ref) };
  64. wasm_val_t rs[1] = { WASM_INIT_VAL };
  65. wasm_val_vec_t args = WASM_ARRAY_VEC(vs);
  66. wasm_val_vec_t results = WASM_ARRAY_VEC(rs);
  67. if (wasm_func_call(func, &args, &results)) {
  68. printf("> Error calling function!\n");
  69. exit(1);
  70. }
  71. printf("okay\n");
  72. return rs[0].of.ref;
  73. }
  74. void call_ir_v(const wasm_func_t* func, int32_t i, wasm_ref_t* ref) {
  75. printf("call_ir_v... "); fflush(stdout);
  76. wasm_val_t vs[2] = { WASM_I32_VAL(i), WASM_REF_VAL(ref) };
  77. wasm_val_vec_t args = WASM_ARRAY_VEC(vs);
  78. wasm_val_vec_t results = WASM_EMPTY_VEC;
  79. if (wasm_func_call(func, &args, &results)) {
  80. printf("> Error calling function!\n");
  81. exit(1);
  82. }
  83. printf("okay\n");
  84. }
  85. own wasm_ref_t* call_i_r(const wasm_func_t* func, int32_t i) {
  86. printf("call_i_r... "); fflush(stdout);
  87. wasm_val_t vs[1] = { WASM_I32_VAL(i) };
  88. wasm_val_t rs[1] = { WASM_INIT_VAL };
  89. wasm_val_vec_t args = WASM_ARRAY_VEC(vs);
  90. wasm_val_vec_t results = WASM_ARRAY_VEC(rs);
  91. if (wasm_func_call(func, &args, &results)) {
  92. printf("> Error calling function!\n");
  93. exit(1);
  94. }
  95. printf("okay\n");
  96. return rs[0].of.ref;
  97. }
  98. void check(own wasm_ref_t* actual, const wasm_ref_t* expected) {
  99. if (actual != expected &&
  100. !(actual && expected && wasm_ref_same(actual, expected))) {
  101. printf("> Error reading reference, expected %p, got %p\n",
  102. expected ? wasm_ref_get_host_info(expected) : NULL,
  103. actual ? wasm_ref_get_host_info(actual) : NULL);
  104. exit(1);
  105. }
  106. // if (actual) wasm_ref_delete(actual);
  107. }
  108. int main(int argc, const char* argv[]) {
  109. // Initialize.
  110. printf("Initializing...\n");
  111. wasm_engine_t* engine = wasm_engine_new();
  112. wasm_store_t* store = wasm_store_new(engine);
  113. // Load binary.
  114. printf("Loading binary...\n");
  115. #if WASM_ENABLE_AOT != 0 && WASM_ENABLE_INTERP == 0
  116. FILE* file = fopen("hostref.aot", "rb");
  117. #else
  118. FILE* file = fopen("hostref.wasm", "rb");
  119. #endif
  120. if (!file) {
  121. printf("> Error loading module!\n");
  122. return 1;
  123. }
  124. int ret = fseek(file, 0L, SEEK_END);
  125. if (ret == -1) {
  126. printf("> Error loading module!\n");
  127. fclose(file);
  128. return 1;
  129. }
  130. long file_size = ftell(file);
  131. if (file_size == -1) {
  132. printf("> Error loading module!\n");
  133. fclose(file);
  134. return 1;
  135. }
  136. ret = fseek(file, 0L, SEEK_SET);
  137. if (ret == -1) {
  138. printf("> Error loading module!\n");
  139. fclose(file);
  140. return 1;
  141. }
  142. wasm_byte_vec_t binary;
  143. wasm_byte_vec_new_uninitialized(&binary, file_size);
  144. if (fread(binary.data, file_size, 1, file) != 1) {
  145. printf("> Error loading module!\n");
  146. fclose(file);
  147. return 1;
  148. }
  149. fclose(file);
  150. // Compile.
  151. printf("Compiling module...\n");
  152. own wasm_module_t* module = wasm_module_new(store, &binary);
  153. if (!module) {
  154. printf("> Error compiling module!\n");
  155. return 1;
  156. }
  157. wasm_byte_vec_delete(&binary);
  158. // Create external callback function.
  159. printf("Creating callback...\n");
  160. own wasm_functype_t* callback_type = wasm_functype_new_1_1(
  161. wasm_valtype_new(WASM_ANYREF), wasm_valtype_new(WASM_ANYREF));
  162. own wasm_func_t* callback_func =
  163. wasm_func_new(store, callback_type, callback);
  164. wasm_functype_delete(callback_type);
  165. // Instantiate.
  166. printf("Instantiating module...\n");
  167. wasm_extern_t* externs[] = { wasm_func_as_extern(callback_func) };
  168. wasm_extern_vec_t imports = WASM_ARRAY_VEC(externs);
  169. own wasm_instance_t* instance =
  170. wasm_instance_new(store, module, &imports, NULL);
  171. if (!instance) {
  172. printf("> Error instantiating module!\n");
  173. return 1;
  174. }
  175. wasm_func_delete(callback_func);
  176. wasm_module_delete(module);
  177. // Extract export.
  178. printf("Extracting exports...\n");
  179. own wasm_extern_vec_t exports;
  180. wasm_instance_exports(instance, &exports);
  181. size_t i = 0;
  182. wasm_global_t* global = get_export_global(&exports, i++);
  183. wasm_table_t* table = get_export_table(&exports, i++);
  184. wasm_func_t* global_set = get_export_func(&exports, i++);
  185. wasm_func_t* global_get = get_export_func(&exports, i++);
  186. wasm_func_t* table_set = get_export_func(&exports, i++);
  187. wasm_func_t* table_get = get_export_func(&exports, i++);
  188. wasm_func_t* func_call = get_export_func(&exports, i++);
  189. wasm_instance_delete(instance);
  190. // Create host references.
  191. printf("Creating host references...\n");
  192. own wasm_ref_t* host1 = wasm_foreign_as_ref(wasm_foreign_new(store));
  193. own wasm_ref_t* host2 = wasm_foreign_as_ref(wasm_foreign_new(store));
  194. wasm_ref_set_host_info(host1, (void*)1);
  195. wasm_ref_set_host_info(host2, (void*)2);
  196. // Some sanity checks.
  197. check(NULL, NULL);
  198. wasm_ref_t *host1_cp = wasm_ref_copy(host1);
  199. wasm_ref_t *host2_cp = wasm_ref_copy(host2);
  200. check(host1_cp, host1);
  201. check(host2_cp, host2);
  202. wasm_ref_delete(host1_cp);
  203. wasm_ref_delete(host2_cp);
  204. own wasm_val_t val;
  205. val.kind = WASM_ANYREF;
  206. val.of.ref = wasm_ref_copy(host1);
  207. wasm_ref_t *ref_cp = wasm_ref_copy(val.of.ref);
  208. check(ref_cp, host1);
  209. check(val.of.ref, host1);
  210. wasm_ref_delete(val.of.ref);
  211. wasm_ref_delete(ref_cp);
  212. // Interact.
  213. printf("Accessing global...\n");
  214. check(call_v_r(global_get), NULL);
  215. call_r_v(global_set, host1);
  216. check(call_v_r(global_get), host1);
  217. call_r_v(global_set, host2);
  218. check(call_v_r(global_get), host2);
  219. call_r_v(global_set, NULL);
  220. check(call_v_r(global_get), NULL);
  221. wasm_global_get(global, &val);
  222. assert(val.kind == WASM_ANYREF);
  223. assert(val.of.ref == NULL);
  224. val.of.ref = host2;
  225. wasm_global_set(global, &val);
  226. wasm_global_get(global, &val);
  227. assert(val.kind == WASM_ANYREF);
  228. assert(val.of.ref == host2);
  229. printf("Accessing table...\n");
  230. check(call_i_r(table_get, 0), NULL);
  231. check(call_i_r(table_get, 1), NULL);
  232. call_ir_v(table_set, 0, host1);
  233. call_ir_v(table_set, 1, host2);
  234. check(call_i_r(table_get, 0), host1);
  235. check(call_i_r(table_get, 1), host2);
  236. call_ir_v(table_set, 0, NULL);
  237. check(call_i_r(table_get, 0), NULL);
  238. check(wasm_table_get(table, 2), NULL);
  239. wasm_table_set(table, 2, host1);
  240. check(call_i_r(table_get, 2), host1);
  241. check(wasm_table_get(table, 2), host1);
  242. printf("Accessing function...\n");
  243. check(call_r_r(func_call, NULL), NULL);
  244. check(call_r_r(func_call, host1), host1);
  245. check(call_r_r(func_call, host2), host2);
  246. wasm_ref_delete(host1);
  247. wasm_ref_delete(host2);
  248. wasm_extern_vec_delete(&exports);
  249. // Shut down.
  250. printf("Shutting down...\n");
  251. wasm_store_delete(store);
  252. wasm_engine_delete(engine);
  253. // All done.
  254. printf("Done.\n");
  255. return 0;
  256. }