hello.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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* hello_callback(
  9. const wasm_val_vec_t* args, wasm_val_vec_t* results
  10. ) {
  11. printf("Calling back...\n");
  12. printf("> Hello World!\n");
  13. return NULL;
  14. }
  15. int main(int argc, const char* argv[]) {
  16. // Initialize.
  17. printf("Initializing...\n");
  18. wasm_engine_t* engine = wasm_engine_new();
  19. wasm_store_t* store = wasm_store_new(engine);
  20. // Load binary.
  21. printf("Loading binary...\n");
  22. #if WASM_ENABLE_AOT != 0 && WASM_ENABLE_INTERP == 0
  23. FILE* file = fopen("hello.aot", "rb");
  24. #else
  25. FILE* file = fopen("hello.wasm", "rb");
  26. #endif
  27. if (!file) {
  28. printf("> Error loading module!\n");
  29. return 1;
  30. }
  31. int ret = fseek(file, 0L, SEEK_END);
  32. if (ret == -1) {
  33. printf("> Error loading module!\n");
  34. fclose(file);
  35. return 1;
  36. }
  37. long file_size = ftell(file);
  38. if (file_size == -1) {
  39. printf("> Error loading module!\n");
  40. fclose(file);
  41. return 1;
  42. }
  43. ret = fseek(file, 0L, SEEK_SET);
  44. if (ret == -1) {
  45. printf("> Error loading module!\n");
  46. fclose(file);
  47. return 1;
  48. }
  49. wasm_byte_vec_t binary;
  50. wasm_byte_vec_new_uninitialized(&binary, file_size);
  51. if (fread(binary.data, file_size, 1, file) != 1) {
  52. printf("> Error loading module!\n");
  53. fclose(file);
  54. return 1;
  55. }
  56. fclose(file);
  57. // Compile.
  58. printf("Compiling module...\n");
  59. own wasm_module_t* module = wasm_module_new(store, &binary);
  60. if (!module) {
  61. printf("> Error compiling module!\n");
  62. return 1;
  63. }
  64. wasm_byte_vec_delete(&binary);
  65. // Create external print functions.
  66. printf("Creating callback...\n");
  67. own wasm_functype_t* hello_type = wasm_functype_new_0_0();
  68. own wasm_func_t* hello_func =
  69. wasm_func_new(store, hello_type, hello_callback);
  70. wasm_functype_delete(hello_type);
  71. // Instantiate.
  72. printf("Instantiating module...\n");
  73. wasm_extern_t* externs[] = { wasm_func_as_extern(hello_func) };
  74. wasm_extern_vec_t imports = WASM_ARRAY_VEC(externs);
  75. own wasm_instance_t* instance =
  76. wasm_instance_new(store, module, &imports, NULL);
  77. if (!instance) {
  78. printf("> Error instantiating module!\n");
  79. return 1;
  80. }
  81. wasm_func_delete(hello_func);
  82. // Extract export.
  83. printf("Extracting export...\n");
  84. own wasm_extern_vec_t exports;
  85. wasm_instance_exports(instance, &exports);
  86. if (exports.size == 0) {
  87. printf("> Error accessing exports!\n");
  88. return 1;
  89. }
  90. const wasm_func_t* run_func = wasm_extern_as_func(exports.data[0]);
  91. if (run_func == NULL) {
  92. printf("> Error accessing export!\n");
  93. return 1;
  94. }
  95. wasm_module_delete(module);
  96. wasm_instance_delete(instance);
  97. // Call.
  98. printf("Calling export...\n");
  99. wasm_val_vec_t args = WASM_EMPTY_VEC;
  100. wasm_val_vec_t results = WASM_EMPTY_VEC;
  101. wasm_trap_t *trap = wasm_func_call(run_func, &args, &results);
  102. if (trap) {
  103. printf("> Error calling function!\n");
  104. wasm_trap_delete(trap);
  105. return 1;
  106. }
  107. wasm_extern_vec_delete(&exports);
  108. // Shut down.
  109. printf("Shutting down...\n");
  110. wasm_store_delete(store);
  111. wasm_engine_delete(engine);
  112. // All done.
  113. printf("Done.\n");
  114. return 0;
  115. }