|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | + |
| 4 | +#include "bh_platform.h" |
| 5 | +#include "bh_read_file.h" |
| 6 | +#include "wasm_export_checked.h" |
| 7 | + |
| 8 | +#define VERIFY_API_RESULT(callee, result, fail_label) \ |
| 9 | + do { \ |
| 10 | + if (result.error_code != 0) { \ |
| 11 | + printf("%s failed with error code: %d\n", #callee, \ |
| 12 | + result.error_code); \ |
| 13 | + goto fail_label; \ |
| 14 | + } \ |
| 15 | + } while (0) |
| 16 | + |
| 17 | +int |
| 18 | +main(int argc, char *argv_main[]) |
| 19 | +{ |
| 20 | + Result api_result; |
| 21 | + wasm_module_t module = NULL; |
| 22 | + uint32 buf_size, stack_size = 8092, heap_size = 8092; |
| 23 | + wasm_module_inst_t module_inst = NULL; |
| 24 | + wasm_function_inst_t func = NULL; |
| 25 | + wasm_exec_env_t exec_env = NULL; |
| 26 | + int ret = EXIT_FAILURE; |
| 27 | + |
| 28 | + RuntimeInitArgs init_args; |
| 29 | + // 512Kb |
| 30 | + static char global_heap_buf[512 * 1024]; |
| 31 | + char *wasm_path = "fib.wasm"; |
| 32 | + char *buffer; |
| 33 | + char error_buf[128]; |
| 34 | + |
| 35 | + memset(&init_args, 0, sizeof(RuntimeInitArgs)); |
| 36 | + init_args.mem_alloc_type = Alloc_With_Pool; |
| 37 | + init_args.mem_alloc_option.pool.heap_buf = global_heap_buf; |
| 38 | + init_args.mem_alloc_option.pool.heap_size = sizeof(global_heap_buf); |
| 39 | + |
| 40 | + api_result = wasm_runtime_full_init_checked(&init_args); |
| 41 | + VERIFY_API_RESULT(wasm_runtime_full_init_checked, api_result, fail); |
| 42 | + |
| 43 | + api_result = wasm_runtime_set_log_level_checked(WASM_LOG_LEVEL_VERBOSE); |
| 44 | + VERIFY_API_RESULT(wasm_runtime_set_log_level_checked, api_result, |
| 45 | + release_runtime); |
| 46 | + |
| 47 | + buffer = bh_read_file_to_buffer(wasm_path, &buf_size); |
| 48 | + if (buffer == NULL) { |
| 49 | + printf("Open wasm app file [%s] failed.\n", wasm_path); |
| 50 | + goto release_runtime; |
| 51 | + } |
| 52 | + |
| 53 | + api_result = wasm_runtime_load_checked((uint8 *)buffer, buf_size, error_buf, |
| 54 | + sizeof(error_buf)); |
| 55 | + VERIFY_API_RESULT(wasm_runtime_load_checked, api_result, release_file); |
| 56 | + module = api_result.value.wasm_module_t_value; |
| 57 | + |
| 58 | + api_result = wasm_runtime_instantiate_checked(module, stack_size, heap_size, |
| 59 | + error_buf, sizeof(error_buf)); |
| 60 | + VERIFY_API_RESULT(wasm_runtime_instantiate_checked, api_result, |
| 61 | + release_module); |
| 62 | + module_inst = api_result.value.wasm_module_inst_t_value; |
| 63 | + |
| 64 | + api_result = wasm_runtime_create_exec_env_checked(module_inst, stack_size); |
| 65 | + VERIFY_API_RESULT(wasm_runtime_create_exec_env_checked, api_result, |
| 66 | + release_instance); |
| 67 | + exec_env = api_result.value.wasm_exec_env_t_value; |
| 68 | + |
| 69 | + api_result = wasm_runtime_lookup_function_checked(module_inst, "fib"); |
| 70 | + VERIFY_API_RESULT(wasm_runtime_lookup_function_checked, api_result, |
| 71 | + release_exec_env); |
| 72 | + func = api_result.value.wasm_function_inst_t_value; |
| 73 | + |
| 74 | + wasm_val_t result[1] = { { .kind = WASM_I32 } }; |
| 75 | + wasm_val_t arguments[1] = { |
| 76 | + { .kind = WASM_I32, .of.i32 = 6 }, |
| 77 | + }; |
| 78 | + |
| 79 | + api_result = wasm_runtime_call_wasm_a_checked(exec_env, func, 1, result, 1, |
| 80 | + arguments); |
| 81 | + VERIFY_API_RESULT(wasm_runtime_call_wasm_a_checked, api_result, |
| 82 | + release_runtime); |
| 83 | + printf("Native finished calling wasm function: fib, returned: %d\n", |
| 84 | + result[0].of.i32); |
| 85 | + |
| 86 | + ret = EXIT_SUCCESS; |
| 87 | + |
| 88 | +release_exec_env: |
| 89 | + wasm_runtime_destroy_exec_env_checked(exec_env); |
| 90 | +release_instance: |
| 91 | + wasm_runtime_deinstantiate_checked(module_inst); |
| 92 | +release_module: |
| 93 | + wasm_runtime_unload_checked(module); |
| 94 | +release_file: |
| 95 | + wasm_runtime_free(buffer); |
| 96 | +release_runtime: |
| 97 | + wasm_runtime_destroy_checked(); |
| 98 | +fail: |
| 99 | + return ret; |
| 100 | +} |
0 commit comments