empty_imports.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #include <stdio.h>
  2. #include "wasm_c_api.h"
  3. #define own
  4. int main(int argc, const char* argv[]) {
  5. // Initialize.
  6. printf("Initializing...\n");
  7. wasm_engine_t* engine = wasm_engine_new();
  8. wasm_store_t* store = wasm_store_new(engine);
  9. // Load binary.
  10. printf("Loading binary...\n");
  11. #if WASM_ENABLE_AOT != 0 && WASM_ENABLE_INTERP == 0
  12. FILE* file = fopen("empty_imports.aot", "rb");
  13. #else
  14. FILE* file = fopen("empty_imports.wasm", "rb");
  15. #endif
  16. if (!file) {
  17. printf("> Error loading module!\n");
  18. return 1;
  19. }
  20. int ret = fseek(file, 0L, SEEK_END);
  21. if (ret == -1) {
  22. printf("> Error loading module!\n");
  23. fclose(file);
  24. return 1;
  25. }
  26. long file_size = ftell(file);
  27. if (file_size == -1) {
  28. printf("> Error loading module!\n");
  29. fclose(file);
  30. return 1;
  31. }
  32. ret = fseek(file, 0L, SEEK_SET);
  33. if (ret == -1) {
  34. printf("> Error loading module!\n");
  35. fclose(file);
  36. return 1;
  37. }
  38. wasm_byte_vec_t binary;
  39. wasm_byte_vec_new_uninitialized(&binary, file_size);
  40. if (fread(binary.data, file_size, 1, file) != 1) {
  41. printf("> Error loading module!\n");
  42. fclose(file);
  43. return 1;
  44. }
  45. fclose(file);
  46. // Compile.
  47. printf("Compiling module...\n");
  48. own wasm_module_t* module = wasm_module_new(store, &binary);
  49. if (!module) {
  50. printf("> Error compiling module!\n");
  51. return 1;
  52. }
  53. wasm_byte_vec_delete(&binary);
  54. // Instantiate with non-null but empty imports array.
  55. printf("Instantiating module...\n");
  56. wasm_extern_vec_t imports = WASM_EMPTY_VEC;
  57. own wasm_instance_t* instance =
  58. wasm_instance_new(store, module, &imports, NULL);
  59. if (!instance) {
  60. printf("> Error instantiating module!\n");
  61. return 1;
  62. }
  63. // Run an exported function to verify that the instance was created correctly.
  64. printf("Extracting export...\n");
  65. own wasm_extern_vec_t exports;
  66. wasm_instance_exports(instance, &exports);
  67. if (exports.size == 0) {
  68. printf("> Error accessing exports!\n");
  69. return 1;
  70. }
  71. const wasm_func_t* add_func = wasm_extern_as_func(exports.data[0]);
  72. if (add_func == NULL) {
  73. printf("> Error accessing export!\n");
  74. return 1;
  75. }
  76. wasm_module_delete(module);
  77. wasm_instance_delete(instance);
  78. printf("Calling export...\n");
  79. wasm_val_t args[2] = { WASM_I32_VAL(3), WASM_I32_VAL(4) };
  80. wasm_val_vec_t args_vec = WASM_ARRAY_VEC(args);
  81. wasm_val_t results[1] = { WASM_INIT_VAL };
  82. wasm_val_vec_t results_vec = WASM_ARRAY_VEC(results);
  83. wasm_trap_t *trap = wasm_func_call(add_func, &args_vec, &results_vec);
  84. if (trap) {
  85. printf("> Error calling function!\n");
  86. wasm_trap_delete(trap);
  87. return 1;
  88. }
  89. if (results_vec.data[0].of.i32 != 7) {
  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. }