multi.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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("> %"PRIu32" %"PRIu64" %"PRIu64" %"PRIu32"\n",
  13. args->data[0].of.i32, args->data[1].of.i64,
  14. args->data[2].of.i64, args->data[3].of.i32);
  15. printf("\n");
  16. wasm_val_copy(&results->data[0], &args->data[3]);
  17. wasm_val_copy(&results->data[1], &args->data[1]);
  18. wasm_val_copy(&results->data[2], &args->data[2]);
  19. wasm_val_copy(&results->data[3], &args->data[0]);
  20. return NULL;
  21. }
  22. // A function closure.
  23. own wasm_trap_t* closure_callback(
  24. void* env, const wasm_val_vec_t* args, wasm_val_vec_t* results
  25. ) {
  26. int i = *(int*)env;
  27. printf("Calling back closure...\n");
  28. printf("> %d\n", i);
  29. results->data[0].kind = WASM_I32;
  30. results->data[0].of.i32 = (int32_t)i;
  31. return NULL;
  32. }
  33. int main(int argc, const char* argv[]) {
  34. // Initialize.
  35. printf("Initializing...\n");
  36. wasm_engine_t* engine = wasm_engine_new();
  37. wasm_store_t* store = wasm_store_new(engine);
  38. // Load binary.
  39. printf("Loading binary...\n");
  40. #if WASM_ENABLE_AOT != 0 && WASM_ENABLE_INTERP == 0
  41. FILE* file = fopen("multi.aot", "rb");
  42. #else
  43. FILE* file = fopen("multi.wasm", "rb");
  44. #endif
  45. if (!file) {
  46. printf("> Error loading module!\n");
  47. return 1;
  48. }
  49. fseek(file, 0L, SEEK_END);
  50. size_t file_size = ftell(file);
  51. fseek(file, 0L, SEEK_SET);
  52. wasm_byte_vec_t binary;
  53. wasm_byte_vec_new_uninitialized(&binary, file_size);
  54. if (fread(binary.data, file_size, 1, file) != 1) {
  55. printf("> Error loading module!\n");
  56. fclose(file);
  57. return 1;
  58. }
  59. fclose(file);
  60. // Compile.
  61. printf("Compiling module...\n");
  62. own wasm_module_t* module = wasm_module_new(store, &binary);
  63. if (!module) {
  64. printf("> Error compiling module!\n");
  65. return 1;
  66. }
  67. wasm_byte_vec_delete(&binary);
  68. // Create external print functions.
  69. printf("Creating callback...\n");
  70. wasm_valtype_t* types[4] = {
  71. wasm_valtype_new_i32(), wasm_valtype_new_i64(),
  72. wasm_valtype_new_i64(), wasm_valtype_new_i32()
  73. };
  74. own wasm_valtype_vec_t tuple1, tuple2;
  75. wasm_valtype_vec_new(&tuple1, 4, types);
  76. wasm_valtype_vec_copy(&tuple2, &tuple1);
  77. own wasm_functype_t* callback_type = wasm_functype_new(&tuple1, &tuple2);
  78. own wasm_func_t* callback_func =
  79. wasm_func_new(store, callback_type, callback);
  80. wasm_functype_delete(callback_type);
  81. // Instantiate.
  82. printf("Instantiating module...\n");
  83. wasm_extern_t* externs[] = { wasm_func_as_extern(callback_func) };
  84. wasm_extern_vec_t imports = WASM_ARRAY_VEC(externs);
  85. own wasm_instance_t* instance =
  86. wasm_instance_new(store, module, &imports, NULL);
  87. if (!instance) {
  88. printf("> Error instantiating module!\n");
  89. return 1;
  90. }
  91. wasm_func_delete(callback_func);
  92. // Extract export.
  93. printf("Extracting export...\n");
  94. own wasm_extern_vec_t exports;
  95. wasm_instance_exports(instance, &exports);
  96. if (exports.size == 0) {
  97. printf("> Error accessing exports!\n");
  98. return 1;
  99. }
  100. const wasm_func_t* run_func = wasm_extern_as_func(exports.data[0]);
  101. if (run_func == NULL) {
  102. printf("> Error accessing export!\n");
  103. return 1;
  104. }
  105. wasm_module_delete(module);
  106. wasm_instance_delete(instance);
  107. // Call.
  108. printf("Calling export...\n");
  109. wasm_val_t vals[4] = {
  110. WASM_I32_VAL(1), WASM_I64_VAL(2), WASM_I64_VAL(3), WASM_I32_VAL(4)
  111. };
  112. wasm_val_t res[4] = {
  113. WASM_INIT_VAL, WASM_INIT_VAL, WASM_INIT_VAL, WASM_INIT_VAL
  114. };
  115. wasm_val_vec_t args = WASM_ARRAY_VEC(vals);
  116. wasm_val_vec_t results = WASM_ARRAY_VEC(res);
  117. wasm_trap_t *trap = wasm_func_call(run_func, &args, &results);
  118. if (trap) {
  119. printf("> Error calling function!\n");
  120. wasm_trap_delete(trap);
  121. return 1;
  122. }
  123. wasm_extern_vec_delete(&exports);
  124. // Print result.
  125. printf("Printing result...\n");
  126. printf("> %"PRIu32" %"PRIu64" %"PRIu64" %"PRIu32"\n",
  127. res[0].of.i32, res[1].of.i64, res[2].of.i64, res[3].of.i32);
  128. assert(res[0].of.i32 == 4);
  129. assert(res[1].of.i64 == 3);
  130. assert(res[2].of.i64 == 2);
  131. assert(res[3].of.i32 == 1);
  132. // Shut down.
  133. printf("Shutting down...\n");
  134. wasm_store_delete(store);
  135. wasm_engine_delete(engine);
  136. // All done.
  137. printf("Done.\n");
  138. return 0;
  139. }