main.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. #include "bh_getopt.h"
  8. #include "my_context.h"
  9. int32_t
  10. add_native(int32_t n);
  11. void *my_context_key;
  12. struct my_context my_context;
  13. int my_dtor_called;
  14. wasm_module_inst_t module_inst = NULL;
  15. void
  16. print_usage(void)
  17. {
  18. fprintf(stdout, "Options:\r\n");
  19. fprintf(stdout, " -f [path of wasm file] \n");
  20. }
  21. void
  22. my_context_dtor(wasm_module_inst_t inst, void *ctx)
  23. {
  24. printf("%s called\n", __func__);
  25. my_dtor_called++;
  26. bh_assert(ctx == &my_context);
  27. bh_assert(inst == module_inst);
  28. }
  29. int
  30. main(int argc, char *argv_main[])
  31. {
  32. static char global_heap_buf[512 * 1024];
  33. char *buffer;
  34. char error_buf[128];
  35. int opt;
  36. char *wasm_path = NULL;
  37. wasm_module_t module = NULL;
  38. wasm_exec_env_t exec_env = NULL;
  39. uint32 buf_size, stack_size = 8092, heap_size = 8092;
  40. RuntimeInitArgs init_args;
  41. memset(&init_args, 0, sizeof(RuntimeInitArgs));
  42. while ((opt = getopt(argc, argv_main, "hf:")) != -1) {
  43. switch (opt) {
  44. case 'f':
  45. wasm_path = optarg;
  46. break;
  47. case 'h':
  48. print_usage();
  49. return 0;
  50. case '?':
  51. print_usage();
  52. return 0;
  53. }
  54. }
  55. if (optind == 1) {
  56. print_usage();
  57. return 0;
  58. }
  59. // Define an array of NativeSymbol for the APIs to be exported.
  60. // Note: the array must be static defined since runtime
  61. // will keep it after registration
  62. // For the function signature specifications, goto the link:
  63. // https://github.com/bytecodealliance/wasm-micro-runtime/blob/main/doc/export_native_api.md
  64. static NativeSymbol native_symbols[] = { { "add_native", add_native, "(i)i",
  65. NULL } };
  66. init_args.mem_alloc_type = Alloc_With_Pool;
  67. init_args.mem_alloc_option.pool.heap_buf = global_heap_buf;
  68. init_args.mem_alloc_option.pool.heap_size = sizeof(global_heap_buf);
  69. // Native symbols need below registration phase
  70. init_args.n_native_symbols = sizeof(native_symbols) / sizeof(NativeSymbol);
  71. init_args.native_module_name = "env";
  72. init_args.native_symbols = native_symbols;
  73. if (!wasm_runtime_full_init(&init_args)) {
  74. printf("Init runtime environment failed.\n");
  75. return -1;
  76. }
  77. my_context_key = wasm_runtime_create_context_key(my_context_dtor);
  78. if (!my_context_key) {
  79. printf("wasm_runtime_create_context_key failed.\n");
  80. return -1;
  81. }
  82. buffer = bh_read_file_to_buffer(wasm_path, &buf_size);
  83. if (!buffer) {
  84. printf("Open wasm app file [%s] failed.\n", wasm_path);
  85. goto fail;
  86. }
  87. module = wasm_runtime_load((uint8 *)buffer, buf_size, error_buf,
  88. sizeof(error_buf));
  89. if (!module) {
  90. printf("Load wasm module failed. error: %s\n", error_buf);
  91. goto fail;
  92. }
  93. module_inst = wasm_runtime_instantiate(module, stack_size, heap_size,
  94. error_buf, sizeof(error_buf));
  95. if (!module_inst) {
  96. printf("Instantiate wasm module failed. error: %s\n", error_buf);
  97. goto fail;
  98. }
  99. my_context.x = 100;
  100. wasm_runtime_set_context(module_inst, my_context_key, &my_context);
  101. exec_env = wasm_runtime_create_exec_env(module_inst, stack_size);
  102. if (!exec_env) {
  103. printf("Create wasm execution environment failed.\n");
  104. goto fail;
  105. }
  106. wasm_function_inst_t func3 =
  107. wasm_runtime_lookup_function(module_inst, "calculate", NULL);
  108. if (!func3) {
  109. printf("The wasm function calculate is not found.\n");
  110. goto fail;
  111. }
  112. uint32_t argv3[1] = { 3 };
  113. if (wasm_runtime_call_wasm(exec_env, func3, 1, argv3)) {
  114. uint32_t result = *(uint32_t *)argv3;
  115. printf("Native finished calling wasm function: calculate, return: %d\n",
  116. result);
  117. bh_assert(result == 103); /* argv3[0] + my_context.x */
  118. }
  119. else {
  120. printf("call wasm function calculate failed. error: %s\n",
  121. wasm_runtime_get_exception(module_inst));
  122. goto fail;
  123. }
  124. fail:
  125. if (exec_env)
  126. wasm_runtime_destroy_exec_env(exec_env);
  127. if (module_inst) {
  128. bh_assert(my_dtor_called == 0);
  129. wasm_runtime_deinstantiate(module_inst);
  130. bh_assert(my_dtor_called == 1);
  131. }
  132. if (module)
  133. wasm_runtime_unload(module);
  134. if (buffer)
  135. BH_FREE(buffer);
  136. if (my_context_key)
  137. wasm_runtime_destroy_context_key(my_context_key);
  138. wasm_runtime_destroy();
  139. return 0;
  140. }