hello.c 3.5 KB

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