hello.c 2.9 KB

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