callback.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <inttypes.h>
  5. #include "wasm_c_api.h"
  6. #include "wasm_export.h"
  7. #include "bh_platform.h"
  8. extern bool
  9. reader(const char *module_name, uint8 **p_buffer, uint32 *p_size);
  10. extern void
  11. destroyer(uint8 *buffer, uint32 size);
  12. #define own
  13. // Print a Wasm value
  14. void wasm_val_print(wasm_val_t val) {
  15. switch (val.kind) {
  16. case WASM_I32: {
  17. printf("%" PRIu32, val.of.i32);
  18. } break;
  19. case WASM_I64: {
  20. printf("%" PRIu64, val.of.i64);
  21. } break;
  22. case WASM_F32: {
  23. printf("%f", val.of.f32);
  24. } break;
  25. case WASM_F64: {
  26. printf("%g", val.of.f64);
  27. } break;
  28. case WASM_ANYREF:
  29. case WASM_FUNCREF: {
  30. if (val.of.ref == NULL) {
  31. printf("null");
  32. } else {
  33. printf("ref(%p)", val.of.ref);
  34. }
  35. } break;
  36. }
  37. }
  38. // A function to be called from Wasm code.
  39. own wasm_trap_t* print_callback(
  40. const wasm_val_t args[], wasm_val_t results[]
  41. ) {
  42. printf("Calling back...\n> ");
  43. wasm_val_print(args[0]);
  44. printf("\n");
  45. wasm_val_copy(&results[0], &args[0]);
  46. return NULL;
  47. }
  48. // A function closure.
  49. own wasm_trap_t* closure_callback(
  50. void* env, const wasm_val_t args[], wasm_val_t results[]
  51. ) {
  52. int i = *(int*)env;
  53. printf("Calling back closure...\n");
  54. printf("> %d\n", i);
  55. results[0].kind = WASM_I32;
  56. results[0].of.i32 = (int32_t)i;
  57. return NULL;
  58. }
  59. int main(int argc, const char* argv[]) {
  60. wasm_runtime_set_module_reader(reader, destroyer);
  61. // Initialize.
  62. printf("Initializing...\n");
  63. wasm_engine_t* engine = wasm_engine_new();
  64. wasm_store_t* store = wasm_store_new(engine);
  65. // Load binary.
  66. printf("Loading binary...\n");
  67. #if WASM_ENABLE_AOT != 0 && WASM_ENABLE_INTERP == 0
  68. FILE* file = fopen("callback.aot", "rb");
  69. #else
  70. FILE* file = fopen("callback.wasm", "rb");
  71. #endif
  72. if (!file) {
  73. printf("> Error loading module!\n");
  74. return 1;
  75. }
  76. fseek(file, 0L, SEEK_END);
  77. size_t file_size = ftell(file);
  78. fseek(file, 0L, SEEK_SET);
  79. wasm_byte_vec_t binary;
  80. wasm_byte_vec_new_uninitialized(&binary, file_size);
  81. if (fread(binary.data, file_size, 1, file) != 1) {
  82. printf("> Error loading module!\n");
  83. return 1;
  84. }
  85. fclose(file);
  86. // Compile.
  87. printf("Compiling module...\n");
  88. own wasm_module_t* module = wasm_module_new(store, &binary);
  89. if (!module) {
  90. printf("> Error compiling module!\n");
  91. return 1;
  92. }
  93. wasm_byte_vec_delete(&binary);
  94. // Create external print functions.
  95. printf("Creating callback...\n");
  96. own wasm_functype_t* print_type = wasm_functype_new_1_1(wasm_valtype_new_i32(), wasm_valtype_new_i32());
  97. own wasm_func_t* print_func = wasm_func_new(store, print_type, print_callback);
  98. int i = 42;
  99. own wasm_functype_t* closure_type = wasm_functype_new_0_1(wasm_valtype_new_i32());
  100. own wasm_func_t* closure_func = wasm_func_new_with_env(store, closure_type, closure_callback, &i, NULL);
  101. wasm_functype_delete(print_type);
  102. wasm_functype_delete(closure_type);
  103. // Instantiate.
  104. printf("Instantiating module...\n");
  105. const wasm_extern_t* imports[] = {
  106. wasm_func_as_extern(print_func), wasm_func_as_extern(closure_func)
  107. };
  108. own wasm_instance_t* instance =
  109. wasm_instance_new(store, module, imports, NULL);
  110. if (!instance) {
  111. printf("> Error instantiating module!\n");
  112. return 1;
  113. }
  114. wasm_func_delete(print_func);
  115. wasm_func_delete(closure_func);
  116. // Extract export.
  117. printf("Extracting export...\n");
  118. own wasm_extern_vec_t exports;
  119. wasm_instance_exports(instance, &exports);
  120. if (exports.size == 0) {
  121. printf("> Error accessing exports!\n");
  122. return 1;
  123. }
  124. const wasm_func_t* run_func = wasm_extern_as_func(exports.data[0]);
  125. if (run_func == NULL) {
  126. printf("> Error accessing export!\n");
  127. return 1;
  128. }
  129. wasm_module_delete(module);
  130. wasm_instance_delete(instance);
  131. // Call.
  132. printf("Calling export...\n");
  133. wasm_val_t args[2];
  134. args[0].kind = WASM_I32;
  135. args[0].of.i32 = 3;
  136. args[1].kind = WASM_I32;
  137. args[1].of.i32 = 4;
  138. wasm_val_t results[1];
  139. if (wasm_func_call(run_func, args, results)) {
  140. printf("> Error calling function!\n");
  141. return 1;
  142. }
  143. wasm_extern_vec_delete(&exports);
  144. // Print result.
  145. printf("Printing result...\n");
  146. printf("> %u\n", results[0].of.i32);
  147. // Shut down.
  148. printf("Shutting down...\n");
  149. wasm_store_delete(store);
  150. wasm_engine_delete(engine);
  151. // All done.
  152. printf("Done.\n");
  153. return 0;
  154. }