main.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. int
  9. intToStr(int x, char *str, int str_len, int digit);
  10. int
  11. get_pow(int x, int y);
  12. int32_t
  13. calculate_native(int32_t n, int32_t func1, int32_t func2);
  14. void
  15. my_log(uint32 log_level, const char *file, int line, const char *fmt, ...)
  16. {
  17. char buf[200];
  18. snprintf(buf, 200,
  19. log_level == WASM_LOG_LEVEL_VERBOSE ? "[WamrLogger - VERBOSE] %s"
  20. : "[WamrLogger] %s",
  21. fmt);
  22. va_list ap;
  23. va_start(ap, fmt);
  24. vprintf(buf, ap);
  25. va_end(ap);
  26. }
  27. int
  28. my_vprintf(const char *format, va_list ap)
  29. {
  30. /* Print in blue */
  31. char buf[200];
  32. snprintf(buf, 200, "\x1b[34m%s\x1b[0m", format);
  33. return vprintf(buf, ap);
  34. }
  35. void
  36. print_usage(void)
  37. {
  38. fprintf(stdout, "Options:\r\n");
  39. fprintf(stdout, " -f [path of wasm file] \n");
  40. }
  41. int
  42. main(int argc, char *argv_main[])
  43. {
  44. static char global_heap_buf[512 * 1024];
  45. char *buffer, error_buf[128];
  46. int opt;
  47. char *wasm_path = NULL;
  48. wasm_module_t module = NULL;
  49. wasm_module_inst_t module_inst = NULL;
  50. wasm_exec_env_t exec_env = NULL;
  51. uint32 buf_size, stack_size = 8092, heap_size = 8092;
  52. wasm_function_inst_t func = NULL;
  53. wasm_function_inst_t func2 = NULL;
  54. char *native_buffer = NULL;
  55. uint64_t wasm_buffer = 0;
  56. RuntimeInitArgs init_args;
  57. memset(&init_args, 0, sizeof(RuntimeInitArgs));
  58. while ((opt = getopt(argc, argv_main, "hf:")) != -1) {
  59. switch (opt) {
  60. case 'f':
  61. wasm_path = optarg;
  62. break;
  63. case 'h':
  64. print_usage();
  65. return 0;
  66. case '?':
  67. print_usage();
  68. return 0;
  69. }
  70. }
  71. if (optind == 1) {
  72. print_usage();
  73. return 0;
  74. }
  75. // Define an array of NativeSymbol for the APIs to be exported.
  76. // Note: the array must be static defined since runtime
  77. // will keep it after registration
  78. // For the function signature specifications, goto the link:
  79. // https://github.com/bytecodealliance/wasm-micro-runtime/blob/main/doc/export_native_api.md
  80. static NativeSymbol native_symbols[] = {
  81. {
  82. "intToStr", // the name of WASM function name
  83. intToStr, // the native function pointer
  84. "(i*~i)i", // the function prototype signature, avoid to use i32
  85. NULL // attachment is NULL
  86. },
  87. {
  88. "get_pow", // the name of WASM function name
  89. get_pow, // the native function pointer
  90. "(ii)i", // the function prototype signature, avoid to use i32
  91. NULL // attachment is NULL
  92. },
  93. { "calculate_native", calculate_native, "(iii)i", NULL }
  94. };
  95. init_args.mem_alloc_type = Alloc_With_Pool;
  96. init_args.mem_alloc_option.pool.heap_buf = global_heap_buf;
  97. init_args.mem_alloc_option.pool.heap_size = sizeof(global_heap_buf);
  98. // Native symbols need below registration phase
  99. init_args.n_native_symbols = sizeof(native_symbols) / sizeof(NativeSymbol);
  100. init_args.native_module_name = "env";
  101. init_args.native_symbols = native_symbols;
  102. if (!wasm_runtime_full_init(&init_args)) {
  103. printf("Init runtime environment failed.\n");
  104. return -1;
  105. }
  106. wasm_runtime_set_log_level(WASM_LOG_LEVEL_VERBOSE);
  107. buffer = bh_read_file_to_buffer(wasm_path, &buf_size);
  108. if (!buffer) {
  109. printf("Open wasm app file [%s] failed.\n", wasm_path);
  110. goto fail;
  111. }
  112. module = wasm_runtime_load((uint8 *)buffer, buf_size, error_buf,
  113. sizeof(error_buf));
  114. if (!module) {
  115. printf("Load wasm module failed. error: %s\n", error_buf);
  116. goto fail;
  117. }
  118. module_inst = wasm_runtime_instantiate(module, stack_size, heap_size,
  119. error_buf, sizeof(error_buf));
  120. if (!module_inst) {
  121. printf("Instantiate wasm module failed. error: %s\n", error_buf);
  122. goto fail;
  123. }
  124. exec_env = wasm_runtime_create_exec_env(module_inst, stack_size);
  125. if (!exec_env) {
  126. printf("Create wasm execution environment failed.\n");
  127. goto fail;
  128. }
  129. if (!(func = wasm_runtime_lookup_function(module_inst, "generate_float"))) {
  130. printf("The generate_float wasm function is not found.\n");
  131. goto fail;
  132. }
  133. wasm_val_t results[1] = { { .kind = WASM_F32, .of.f32 = 0 } };
  134. wasm_val_t arguments[3] = {
  135. { .kind = WASM_I32, .of.i32 = 10 },
  136. { .kind = WASM_F64, .of.f64 = 0.000101 },
  137. { .kind = WASM_F32, .of.f32 = 300.002 },
  138. };
  139. // pass 4 elements for function arguments
  140. if (!wasm_runtime_call_wasm_a(exec_env, func, 1, results, 3, arguments)) {
  141. printf("call wasm function generate_float failed. %s\n",
  142. wasm_runtime_get_exception(module_inst));
  143. goto fail;
  144. }
  145. float ret_val;
  146. ret_val = results[0].of.f32;
  147. printf("Native finished calling wasm function generate_float(), returned a "
  148. "float value: %ff\n",
  149. ret_val);
  150. // Next we will pass a buffer to the WASM function
  151. uint32 argv2[5];
  152. // must allocate buffer from wasm instance memory space (never use pointer
  153. // from host runtime)
  154. wasm_buffer =
  155. wasm_runtime_module_malloc(module_inst, 100, (void **)&native_buffer);
  156. memcpy(argv2, &ret_val, sizeof(float)); // the first argument
  157. argv2[1] = wasm_buffer; // the second argument is the wasm buffer address
  158. argv2[3] = 100; // the third argument is the wasm buffer size
  159. argv2[4] = 3; // the last argument is the digits after decimal point for
  160. // converting float to string
  161. if (!(func2 =
  162. wasm_runtime_lookup_function(module_inst, "float_to_string"))) {
  163. printf(
  164. "The wasm function float_to_string wasm function is not found.\n");
  165. goto fail;
  166. }
  167. if (wasm_runtime_call_wasm(exec_env, func2, 4, argv2)) {
  168. printf("Native finished calling wasm function: float_to_string, "
  169. "returned a formatted string: %s\n",
  170. native_buffer);
  171. }
  172. else {
  173. printf("call wasm function float_to_string failed. error: %s\n",
  174. wasm_runtime_get_exception(module_inst));
  175. goto fail;
  176. }
  177. wasm_function_inst_t func3 =
  178. wasm_runtime_lookup_function(module_inst, "calculate");
  179. if (!func3) {
  180. printf("The wasm function calculate is not found.\n");
  181. goto fail;
  182. }
  183. uint32_t argv3[1] = { 3 };
  184. if (wasm_runtime_call_wasm(exec_env, func3, 1, argv3)) {
  185. uint32_t result = *(uint32_t *)argv3;
  186. printf("Native finished calling wasm function: calculate, return: %d\n",
  187. result);
  188. }
  189. else {
  190. printf("call wasm function calculate failed. error: %s\n",
  191. wasm_runtime_get_exception(module_inst));
  192. goto fail;
  193. }
  194. fail:
  195. if (exec_env)
  196. wasm_runtime_destroy_exec_env(exec_env);
  197. if (module_inst) {
  198. if (wasm_buffer)
  199. wasm_runtime_module_free(module_inst, (uint64)wasm_buffer);
  200. wasm_runtime_deinstantiate(module_inst);
  201. }
  202. if (module)
  203. wasm_runtime_unload(module);
  204. if (buffer)
  205. BH_FREE(buffer);
  206. wasm_runtime_destroy();
  207. return 0;
  208. }