hostref.c 8.7 KB

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