hello.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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_t args[], wasm_val_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. fseek(file, 0L, SEEK_END);
  32. size_t file_size = ftell(file);
  33. fseek(file, 0L, SEEK_SET);
  34. wasm_byte_vec_t binary;
  35. wasm_byte_vec_new_uninitialized(&binary, file_size);
  36. if (fread(binary.data, file_size, 1, file) != 1) {
  37. printf("> Error loading module!\n");
  38. fclose(file);
  39. return 1;
  40. }
  41. fclose(file);
  42. // Compile.
  43. printf("Compiling module...\n");
  44. own wasm_module_t* module = wasm_module_new(store, &binary);
  45. if (!module) {
  46. printf("> Error compiling module!\n");
  47. return 1;
  48. }
  49. wasm_byte_vec_delete(&binary);
  50. // Create external print functions.
  51. printf("Creating callback...\n");
  52. own wasm_functype_t* hello_type = wasm_functype_new_0_0();
  53. own wasm_func_t* hello_func =
  54. wasm_func_new(store, hello_type, hello_callback);
  55. wasm_functype_delete(hello_type);
  56. // Instantiate.
  57. printf("Instantiating module...\n");
  58. const wasm_extern_t* imports[] = { wasm_func_as_extern(hello_func) };
  59. own wasm_instance_t* instance =
  60. wasm_instance_new(store, module, imports, NULL);
  61. if (!instance) {
  62. printf("> Error instantiating module!\n");
  63. return 1;
  64. }
  65. wasm_func_delete(hello_func);
  66. // Extract export.
  67. printf("Extracting export...\n");
  68. own wasm_extern_vec_t exports;
  69. wasm_instance_exports(instance, &exports);
  70. if (exports.size == 0) {
  71. printf("> Error accessing exports!\n");
  72. return 1;
  73. }
  74. const wasm_func_t* run_func = wasm_extern_as_func(exports.data[0]);
  75. if (run_func == NULL) {
  76. printf("> Error accessing export!\n");
  77. return 1;
  78. }
  79. wasm_module_delete(module);
  80. wasm_instance_delete(instance);
  81. // Call.
  82. printf("Calling export...\n");
  83. if (wasm_func_call(run_func, NULL, NULL)) {
  84. printf("> Error calling function!\n");
  85. return 1;
  86. }
  87. wasm_extern_vec_delete(&exports);
  88. // Shut down.
  89. printf("Shutting down...\n");
  90. wasm_store_delete(store);
  91. wasm_engine_delete(engine);
  92. // All done.
  93. printf("Done.\n");
  94. return 0;
  95. }