main.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. uint32_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. NULL))) {
  131. printf("The generate_float wasm function is not found.\n");
  132. goto fail;
  133. }
  134. wasm_val_t results[1] = { { .kind = WASM_F32, .of.f32 = 0 } };
  135. wasm_val_t arguments[3] = {
  136. { .kind = WASM_I32, .of.i32 = 10 },
  137. { .kind = WASM_F64, .of.f64 = 0.000101 },
  138. { .kind = WASM_F32, .of.f32 = 300.002 },
  139. };
  140. // pass 4 elements for function arguments
  141. if (!wasm_runtime_call_wasm_a(exec_env, func, 1, results, 3, arguments)) {
  142. printf("call wasm function generate_float failed. %s\n",
  143. wasm_runtime_get_exception(module_inst));
  144. goto fail;
  145. }
  146. float ret_val;
  147. ret_val = results[0].of.f32;
  148. printf("Native finished calling wasm function generate_float(), returned a "
  149. "float value: %ff\n",
  150. ret_val);
  151. // Next we will pass a buffer to the WASM function
  152. uint32 argv2[4];
  153. // must allocate buffer from wasm instance memory space (never use pointer
  154. // from host runtime)
  155. wasm_buffer =
  156. wasm_runtime_module_malloc(module_inst, 100, (void **)&native_buffer);
  157. memcpy(argv2, &ret_val, sizeof(float)); // the first argument
  158. argv2[1] = wasm_buffer; // the second argument is the wasm buffer address
  159. argv2[2] = 100; // the third argument is the wasm buffer size
  160. argv2[3] = 3; // the last argument is the digits after decimal point for
  161. // converting float to string
  162. if (!(func2 = wasm_runtime_lookup_function(module_inst, "float_to_string",
  163. NULL))) {
  164. printf(
  165. "The wasm function float_to_string wasm function is not found.\n");
  166. goto fail;
  167. }
  168. if (wasm_runtime_call_wasm(exec_env, func2, 4, argv2)) {
  169. printf("Native finished calling wasm function: float_to_string, "
  170. "returned a formatted string: %s\n",
  171. native_buffer);
  172. }
  173. else {
  174. printf("call wasm function float_to_string failed. error: %s\n",
  175. wasm_runtime_get_exception(module_inst));
  176. goto fail;
  177. }
  178. wasm_function_inst_t func3 =
  179. wasm_runtime_lookup_function(module_inst, "calculate", NULL);
  180. if (!func3) {
  181. printf("The wasm function calculate is not found.\n");
  182. goto fail;
  183. }
  184. uint32_t argv3[1] = { 3 };
  185. if (wasm_runtime_call_wasm(exec_env, func3, 1, argv3)) {
  186. uint32_t result = *(uint32_t *)argv3;
  187. printf("Native finished calling wasm function: calculate, return: %d\n",
  188. result);
  189. }
  190. else {
  191. printf("call wasm function calculate failed. error: %s\n",
  192. wasm_runtime_get_exception(module_inst));
  193. goto fail;
  194. }
  195. fail:
  196. if (exec_env)
  197. wasm_runtime_destroy_exec_env(exec_env);
  198. if (module_inst) {
  199. if (wasm_buffer)
  200. wasm_runtime_module_free(module_inst, wasm_buffer);
  201. wasm_runtime_deinstantiate(module_inst);
  202. }
  203. if (module)
  204. wasm_runtime_unload(module);
  205. if (buffer)
  206. BH_FREE(buffer);
  207. wasm_runtime_destroy();
  208. return 0;
  209. }