global.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. wasm_global_t* get_export_global(const wasm_extern_vec_t* exports, size_t i) {
  8. if (exports->size <= i || !wasm_extern_as_global(exports->data[i])) {
  9. printf("> Error accessing global export %zu!\n", i);
  10. exit(1);
  11. }
  12. return wasm_extern_as_global(exports->data[i]);
  13. }
  14. wasm_func_t* get_export_func(const wasm_extern_vec_t* exports, size_t i) {
  15. if (exports->size <= i || !wasm_extern_as_func(exports->data[i])) {
  16. printf("> Error accessing function export %zu!\n", i);
  17. exit(1);
  18. }
  19. return wasm_extern_as_func(exports->data[i]);
  20. }
  21. #define check(val, type, expected) \
  22. if (val.of.type != expected) { \
  23. printf("> Error reading value\n"); \
  24. exit(1); \
  25. }
  26. #define check_global(global, type, expected) \
  27. { \
  28. wasm_val_t val; \
  29. wasm_global_get(global, &val); \
  30. check(val, type, expected); \
  31. }
  32. #define check_call(func, type, expected) \
  33. { \
  34. wasm_val_t results[1]; \
  35. wasm_func_call(func, NULL, results); \
  36. check(results[0], type, expected); \
  37. }
  38. int main(int argc, const char* argv[]) {
  39. // Initialize.
  40. printf("Initializing...\n");
  41. wasm_engine_t* engine = wasm_engine_new();
  42. wasm_store_t* store = wasm_store_new(engine);
  43. // Load binary.
  44. printf("Loading binary...\n");
  45. #if WASM_ENABLE_AOT != 0 && WASM_ENABLE_INTERP == 0
  46. FILE* file = fopen("global.aot", "rb");
  47. #else
  48. FILE* file = fopen("global.wasm", "rb");
  49. #endif
  50. if (!file) {
  51. printf("> Error loading module!\n");
  52. return 1;
  53. }
  54. fseek(file, 0L, SEEK_END);
  55. size_t file_size = ftell(file);
  56. fseek(file, 0L, SEEK_SET);
  57. wasm_byte_vec_t binary;
  58. wasm_byte_vec_new_uninitialized(&binary, file_size);
  59. if (fread(binary.data, file_size, 1, file) != 1) {
  60. printf("> Error loading module!\n");
  61. return 1;
  62. }
  63. fclose(file);
  64. // Compile.
  65. printf("Compiling module...\n");
  66. own wasm_module_t* module = wasm_module_new(store, &binary);
  67. if (!module) {
  68. printf("> Error compiling module!\n");
  69. return 1;
  70. }
  71. wasm_byte_vec_delete(&binary);
  72. // Create external globals.
  73. printf("Creating globals...\n");
  74. own wasm_globaltype_t* const_f32_type = wasm_globaltype_new(
  75. wasm_valtype_new(WASM_F32), WASM_CONST);
  76. own wasm_globaltype_t* const_i64_type = wasm_globaltype_new(
  77. wasm_valtype_new(WASM_I64), WASM_CONST);
  78. own wasm_globaltype_t* var_f32_type = wasm_globaltype_new(
  79. wasm_valtype_new(WASM_F32), WASM_VAR);
  80. own wasm_globaltype_t* var_i64_type = wasm_globaltype_new(
  81. wasm_valtype_new(WASM_I64), WASM_VAR);
  82. wasm_val_t val_f32_1 = {.kind = WASM_F32, .of = {.f32 = 1}};
  83. own wasm_global_t* const_f32_import =
  84. wasm_global_new(store, const_f32_type, &val_f32_1);
  85. wasm_val_t val_i64_2 = {.kind = WASM_I64, .of = {.i64 = 2}};
  86. own wasm_global_t* const_i64_import =
  87. wasm_global_new(store, const_i64_type, &val_i64_2);
  88. wasm_val_t val_f32_3 = {.kind = WASM_F32, .of = {.f32 = 3}};
  89. own wasm_global_t* var_f32_import =
  90. wasm_global_new(store, var_f32_type, &val_f32_3);
  91. wasm_val_t val_i64_4 = {.kind = WASM_I64, .of = {.i64 = 4}};
  92. own wasm_global_t* var_i64_import =
  93. wasm_global_new(store, var_i64_type, &val_i64_4);
  94. wasm_globaltype_delete(const_f32_type);
  95. wasm_globaltype_delete(const_i64_type);
  96. wasm_globaltype_delete(var_f32_type);
  97. wasm_globaltype_delete(var_i64_type);
  98. // Instantiate.
  99. printf("Instantiating module...\n");
  100. const wasm_extern_t* imports[] = {
  101. wasm_global_as_extern(const_f32_import),
  102. wasm_global_as_extern(const_i64_import),
  103. wasm_global_as_extern(var_f32_import),
  104. wasm_global_as_extern(var_i64_import)
  105. };
  106. own wasm_instance_t* instance =
  107. wasm_instance_new(store, module, imports, NULL);
  108. if (!instance) {
  109. printf("> Error instantiating module!\n");
  110. return 1;
  111. }
  112. wasm_module_delete(module);
  113. // Extract export.
  114. printf("Extracting exports...\n");
  115. own wasm_extern_vec_t exports;
  116. wasm_instance_exports(instance, &exports);
  117. size_t i = 0;
  118. wasm_global_t* const_f32_export = get_export_global(&exports, i++);
  119. wasm_global_t* const_i64_export = get_export_global(&exports, i++);
  120. wasm_global_t* var_f32_export = get_export_global(&exports, i++);
  121. wasm_global_t* var_i64_export = get_export_global(&exports, i++);
  122. wasm_func_t* get_const_f32_import = get_export_func(&exports, i++);
  123. wasm_func_t* get_const_i64_import = get_export_func(&exports, i++);
  124. wasm_func_t* get_var_f32_import = get_export_func(&exports, i++);
  125. wasm_func_t* get_var_i64_import = get_export_func(&exports, i++);
  126. wasm_func_t* get_const_f32_export = get_export_func(&exports, i++);
  127. wasm_func_t* get_const_i64_export = get_export_func(&exports, i++);
  128. wasm_func_t* get_var_f32_export = get_export_func(&exports, i++);
  129. wasm_func_t* get_var_i64_export = get_export_func(&exports, i++);
  130. wasm_func_t* set_var_f32_import = get_export_func(&exports, i++);
  131. wasm_func_t* set_var_i64_import = get_export_func(&exports, i++);
  132. wasm_func_t* set_var_f32_export = get_export_func(&exports, i++);
  133. wasm_func_t* set_var_i64_export = get_export_func(&exports, i++);
  134. // Try cloning.
  135. own wasm_global_t* copy = wasm_global_copy(var_f32_import);
  136. assert(wasm_global_same(var_f32_import, copy));
  137. wasm_global_delete(copy);
  138. // Interact.
  139. printf("Accessing globals...\n");
  140. // Check initial values.
  141. check_global(const_f32_import, f32, 1);
  142. check_global(const_i64_import, i64, 2);
  143. check_global(var_f32_import, f32, 3);
  144. check_global(var_i64_import, i64, 4);
  145. check_global(const_f32_export, f32, 5);
  146. check_global(const_i64_export, i64, 6);
  147. check_global(var_f32_export, f32, 7);
  148. check_global(var_i64_export, i64, 8);
  149. check_call(get_const_f32_import, f32, 1);
  150. check_call(get_const_i64_import, i64, 2);
  151. check_call(get_var_f32_import, f32, 3);
  152. check_call(get_var_i64_import, i64, 4);
  153. check_call(get_const_f32_export, f32, 5);
  154. check_call(get_const_i64_export, i64, 6);
  155. check_call(get_var_f32_export, f32, 7);
  156. check_call(get_var_i64_export, i64, 8);
  157. // Modify variables through API and check again.
  158. wasm_val_t val33 = {.kind = WASM_F32, .of = {.f32 = 33}};
  159. wasm_global_set(var_f32_import, &val33);
  160. wasm_val_t val34 = {.kind = WASM_I64, .of = {.i64 = 34}};
  161. wasm_global_set(var_i64_import, &val34);
  162. wasm_val_t val37 = {.kind = WASM_F32, .of = {.f32 = 37}};
  163. wasm_global_set(var_f32_export, &val37);
  164. wasm_val_t val38 = {.kind = WASM_I64, .of = {.i64 = 38}};
  165. wasm_global_set(var_i64_export, &val38);
  166. check_global(var_f32_import, f32, 33);
  167. check_global(var_i64_import, i64, 34);
  168. check_global(var_f32_export, f32, 37);
  169. check_global(var_i64_export, i64, 38);
  170. check_call(get_var_f32_import, f32, 33);
  171. check_call(get_var_i64_import, i64, 34);
  172. check_call(get_var_f32_export, f32, 37);
  173. check_call(get_var_i64_export, i64, 38);
  174. // Modify variables through calls and check again.
  175. wasm_val_t args73[] = { {.kind = WASM_F32, .of = {.f32 = 73}} };
  176. wasm_func_call(set_var_f32_import, args73, NULL);
  177. wasm_val_t args74[] = { {.kind = WASM_I64, .of = {.i64 = 74}} };
  178. wasm_func_call(set_var_i64_import, args74, NULL);
  179. wasm_val_t args77[] = { {.kind = WASM_F32, .of = {.f32 = 77}} };
  180. wasm_func_call(set_var_f32_export, args77, NULL);
  181. wasm_val_t args78[] = { {.kind = WASM_I64, .of = {.i64 = 78}} };
  182. wasm_func_call(set_var_i64_export, args78, NULL);
  183. check_global(var_f32_import, f32, 73);
  184. check_global(var_i64_import, i64, 74);
  185. check_global(var_f32_export, f32, 77);
  186. check_global(var_i64_export, i64, 78);
  187. check_call(get_var_f32_import, f32, 73);
  188. check_call(get_var_i64_import, i64, 74);
  189. check_call(get_var_f32_export, f32, 77);
  190. check_call(get_var_i64_export, i64, 78);
  191. wasm_global_delete(const_f32_import);
  192. wasm_global_delete(const_i64_import);
  193. wasm_global_delete(var_f32_import);
  194. wasm_global_delete(var_i64_import);
  195. wasm_extern_vec_delete(&exports);
  196. wasm_instance_delete(instance);
  197. // Shut down.
  198. printf("Shutting down...\n");
  199. wasm_store_delete(store);
  200. wasm_engine_delete(engine);
  201. // All done.
  202. printf("Done.\n");
  203. return 0;
  204. }