main.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "wasm_export.h"
  6. #include "bh_read_file.h"
  7. int
  8. main(int argc, char *argv_main[])
  9. {
  10. int exit_code = EXIT_FAILURE;
  11. /* runtime */
  12. if (!wasm_runtime_init()) {
  13. printf("Init runtime failed.\n");
  14. return EXIT_FAILURE;
  15. }
  16. wasm_runtime_set_log_level(WASM_LOG_LEVEL_VERBOSE);
  17. /* wasm module file */
  18. char *buffer;
  19. uint32 buf_size;
  20. #if WASM_ENABLE_AOT != 0
  21. printf("Loading AOT file...\n");
  22. buffer = bh_read_file_to_buffer("import_table.aot", &buf_size);
  23. #else
  24. printf("Loading WASM file...\n");
  25. buffer = bh_read_file_to_buffer("import_table.wasm", &buf_size);
  26. #endif
  27. if (!buffer) {
  28. printf("Open wasm file failed.\n");
  29. goto destroy_runtime;
  30. }
  31. /* wasm module */
  32. char error_buf[128];
  33. wasm_module_t module = wasm_runtime_load((uint8 *)buffer, buf_size,
  34. error_buf, sizeof(error_buf));
  35. if (!module) {
  36. printf("Load wasm file failed: %s\n", error_buf);
  37. goto release_file_buffer;
  38. }
  39. /* import type */
  40. int32_t import_count = wasm_runtime_get_import_count(module);
  41. if (import_count < 0)
  42. goto unload_module;
  43. wasm_import_t import_type = { 0 };
  44. int32_t import_table_index = -1;
  45. for (int32_t i = 0; i < import_count; i++) {
  46. wasm_runtime_get_import_type(module, (uint32_t)i, &import_type);
  47. if (import_type.kind == WASM_IMPORT_EXPORT_KIND_TABLE) {
  48. import_table_index = i;
  49. break;
  50. }
  51. }
  52. if (import_table_index == -1) {
  53. printf("No memory import found.\n");
  54. goto unload_module;
  55. }
  56. /* host table */
  57. wasm_table_type_t table_type = import_type.u.table_type;
  58. wasm_table_inst_t table = wasm_runtime_create_table(module, table_type);
  59. if (!table) {
  60. printf("Create table failed.\n");
  61. goto unload_module;
  62. }
  63. /* import list */
  64. WASMExternInstance import_list[10] = { 0 };
  65. import_list[import_table_index].module_name = "host";
  66. import_list[import_table_index].field_name = "__indirect_function_table";
  67. import_list[import_table_index].kind = WASM_IMPORT_EXPORT_KIND_TABLE;
  68. import_list[import_table_index].u.table = table;
  69. /* wasm instance */
  70. InstantiationArgs inst_args = {
  71. .imports = import_list,
  72. .import_count = 10,
  73. };
  74. wasm_module_inst_t inst = wasm_runtime_instantiate_ex(
  75. module, &inst_args, error_buf, sizeof(error_buf));
  76. if (!inst) {
  77. printf("Instantiate wasm file failed: %s\n", error_buf);
  78. wasm_runtime_destroy_table(module, table);
  79. table = NULL;
  80. goto unload_module;
  81. }
  82. /* export function */
  83. if (!wasm_application_execute_main(inst, 0, NULL)) {
  84. const char *exception = wasm_runtime_get_exception(inst);
  85. printf("call wasm function main() failed. %s\n", exception);
  86. goto destroy_inst;
  87. }
  88. exit_code = EXIT_SUCCESS;
  89. destroy_inst:
  90. wasm_runtime_deinstantiate(inst);
  91. unload_module:
  92. wasm_runtime_unload(module);
  93. release_file_buffer:
  94. wasm_runtime_free(buffer);
  95. destroy_runtime:
  96. wasm_runtime_destroy();
  97. return exit_code;
  98. }