main.c 6.8 KB

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