krillplayer-plugin 0.6.0 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +2 -1
- package/README.md +1 -0
- package/dist/cli.js +351 -73
- package/dist/crypto.js +37 -11
- package/dist/engine/engine.c +6 -3
- package/kernel/Sources/CQuickJS/include/kernel_qjs.h +8 -1
- package/kernel/Sources/CQuickJS/kernel_internal.h +54 -0
- package/kernel/Sources/CQuickJS/kernel_qjs.c +17 -29
- package/kernel/Sources/CQuickJS/kernel_wasm.c +1155 -0
- package/kernel/Sources/CWAMR/include/kernel_wamr.h +151 -0
- package/kernel/Sources/CWAMR/include/kernel_wamr_config.h +66 -0
- package/kernel/Sources/CWAMR/vendor/LICENSE +219 -0
- package/kernel/Sources/CWAMR/vendor/core/config.h +727 -0
- package/kernel/Sources/CWAMR/vendor/core/iwasm/aot/aot_runtime.h +899 -0
- package/kernel/Sources/CWAMR/vendor/core/iwasm/common/arch/invokeNative_general.c +123 -0
- package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_application.c +933 -0
- package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_blocking_op.c +92 -0
- package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_c_api.c +5391 -0
- package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_c_api_internal.h +246 -0
- package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_exec_env.c +336 -0
- package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_exec_env.h +328 -0
- package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_loader_common.c +242 -0
- package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_loader_common.h +62 -0
- package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_memory.c +1999 -0
- package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_memory.h +154 -0
- package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_native.c +1532 -0
- package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_native.h +117 -0
- package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_runtime_common.c +8070 -0
- package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_runtime_common.h +1396 -0
- package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_shared_memory.c +477 -0
- package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_shared_memory.h +63 -0
- package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_suspend_flags.h +52 -0
- package/kernel/Sources/CWAMR/vendor/core/iwasm/compilation/aot.h +366 -0
- package/kernel/Sources/CWAMR/vendor/core/iwasm/include/lib_export.h +60 -0
- package/kernel/Sources/CWAMR/vendor/core/iwasm/include/wasm_c_api.h +910 -0
- package/kernel/Sources/CWAMR/vendor/core/iwasm/include/wasm_export.h +2440 -0
- package/kernel/Sources/CWAMR/vendor/core/iwasm/interpreter/wasm.h +1492 -0
- package/kernel/Sources/CWAMR/vendor/core/iwasm/interpreter/wasm_interp.h +122 -0
- package/kernel/Sources/CWAMR/vendor/core/iwasm/interpreter/wasm_interp_fast.c +7998 -0
- package/kernel/Sources/CWAMR/vendor/core/iwasm/interpreter/wasm_loader.c +16909 -0
- package/kernel/Sources/CWAMR/vendor/core/iwasm/interpreter/wasm_loader.h +80 -0
- package/kernel/Sources/CWAMR/vendor/core/iwasm/interpreter/wasm_opcode.h +1043 -0
- package/kernel/Sources/CWAMR/vendor/core/iwasm/interpreter/wasm_runtime.c +5174 -0
- package/kernel/Sources/CWAMR/vendor/core/iwasm/interpreter/wasm_runtime.h +912 -0
- package/kernel/Sources/CWAMR/vendor/core/iwasm/libraries/thread-mgr/thread_manager.c +1569 -0
- package/kernel/Sources/CWAMR/vendor/core/iwasm/libraries/thread-mgr/thread_manager.h +256 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/mem-alloc/ems/ems_alloc.c +1169 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/mem-alloc/ems/ems_gc.c +497 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/mem-alloc/ems/ems_gc.h +350 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/mem-alloc/ems/ems_gc_internal.h +384 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/mem-alloc/ems/ems_hmu.c +95 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/mem-alloc/ems/ems_kfc.c +547 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/mem-alloc/mem_alloc.c +252 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/mem-alloc/mem_alloc.h +96 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/platform/common/memory/mremap.c +12 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/platform/common/posix/posix_blocking_op.c +70 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/platform/common/posix/posix_malloc.c +72 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/platform/common/posix/posix_memmap.c +302 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/platform/common/posix/posix_sleep.c +20 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/platform/common/posix/posix_thread.c +784 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/platform/common/posix/posix_time.c +28 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/platform/darwin/platform_init.c +43 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/platform/darwin/platform_internal.h +126 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/platform/include/platform_api_extension.h +1676 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/platform/include/platform_api_vmcore.h +192 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/platform/include/platform_common.h +204 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/platform/include/platform_wasi_types.h +610 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/platform/linux/platform_init.c +43 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/platform/linux/platform_internal.h +139 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_assert.c +25 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_assert.h +42 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_atomic.h +300 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_bitmap.c +27 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_bitmap.h +114 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_common.c +217 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_common.h +83 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_hashmap.c +339 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_hashmap.h +168 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_leb128.c +77 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_leb128.h +30 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_list.c +111 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_list.h +109 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_log.c +111 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_log.h +94 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_platform.h +38 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_queue.c +256 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_queue.h +80 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_vector.c +279 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_vector.h +126 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/utils/gnuc.h +14 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/utils/runtime_timer.c +469 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/utils/runtime_timer.h +51 -0
- package/kernel/Sources/CWAMR/vendor/core/version.h +24 -0
- package/kernel/Sources/CWAMR/wamr_bh_assert.c +98 -0
- package/kernel/Sources/CWAMR/wamr_bh_bitmap.c +98 -0
- package/kernel/Sources/CWAMR/wamr_bh_common.c +98 -0
- package/kernel/Sources/CWAMR/wamr_bh_hashmap.c +98 -0
- package/kernel/Sources/CWAMR/wamr_bh_leb128.c +98 -0
- package/kernel/Sources/CWAMR/wamr_bh_list.c +98 -0
- package/kernel/Sources/CWAMR/wamr_bh_log.c +98 -0
- package/kernel/Sources/CWAMR/wamr_bh_queue.c +98 -0
- package/kernel/Sources/CWAMR/wamr_bh_vector.c +98 -0
- package/kernel/Sources/CWAMR/wamr_ems_alloc.c +98 -0
- package/kernel/Sources/CWAMR/wamr_ems_gc.c +98 -0
- package/kernel/Sources/CWAMR/wamr_ems_hmu.c +98 -0
- package/kernel/Sources/CWAMR/wamr_ems_kfc.c +98 -0
- package/kernel/Sources/CWAMR/wamr_invokeNative_general.c +98 -0
- package/kernel/Sources/CWAMR/wamr_mem_alloc.c +98 -0
- package/kernel/Sources/CWAMR/wamr_mremap.c +98 -0
- package/kernel/Sources/CWAMR/wamr_platform_init_darwin.c +100 -0
- package/kernel/Sources/CWAMR/wamr_platform_init_linux.c +100 -0
- package/kernel/Sources/CWAMR/wamr_posix_blocking_op.c +98 -0
- package/kernel/Sources/CWAMR/wamr_posix_malloc.c +98 -0
- package/kernel/Sources/CWAMR/wamr_posix_memmap.c +98 -0
- package/kernel/Sources/CWAMR/wamr_posix_sleep.c +98 -0
- package/kernel/Sources/CWAMR/wamr_posix_thread.c +98 -0
- package/kernel/Sources/CWAMR/wamr_posix_time.c +98 -0
- package/kernel/Sources/CWAMR/wamr_runtime_timer.c +98 -0
- package/kernel/Sources/CWAMR/wamr_thread_manager.c +98 -0
- package/kernel/Sources/CWAMR/wamr_wasm_application.c +98 -0
- package/kernel/Sources/CWAMR/wamr_wasm_blocking_op.c +98 -0
- package/kernel/Sources/CWAMR/wamr_wasm_c_api.c +98 -0
- package/kernel/Sources/CWAMR/wamr_wasm_exec_env.c +98 -0
- package/kernel/Sources/CWAMR/wamr_wasm_interp_fast.c +98 -0
- package/kernel/Sources/CWAMR/wamr_wasm_loader.c +98 -0
- package/kernel/Sources/CWAMR/wamr_wasm_loader_common.c +98 -0
- package/kernel/Sources/CWAMR/wamr_wasm_memory.c +98 -0
- package/kernel/Sources/CWAMR/wamr_wasm_native.c +98 -0
- package/kernel/Sources/CWAMR/wamr_wasm_runtime.c +98 -0
- package/kernel/Sources/CWAMR/wamr_wasm_runtime_common.c +98 -0
- package/kernel/Sources/CWAMR/wamr_wasm_shared_memory.c +98 -0
- package/kernel/Sources/Kernel/PluginPrelude.js +5 -2
- package/kernel/Tests/KernelTests/CasePins.js +50 -0
- package/package.json +2 -2
- package/skill/krillplayer-plugin/SKILL.md +2 -0
- package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/danmaku-send-content-rejected.json +8 -4
- package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/danmaku-send-login-expired.json +8 -4
- package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/danmaku-send-muted.json +8 -4
- package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/danmaku-send-other.json +8 -4
- package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/danmaku-send-rate-limited.json +8 -4
- package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/danmaku-send-sent.json +8 -4
- package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/play-live-logged-in.json +39 -0
- package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/play-live-selected-backup-line.json +8 -0
- package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/play-live-selected-line-gone.json +8 -0
- package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/play-live.json +8 -0
- package/skill/krillplayer-plugin/assets/samples/live-video-platform/plugin.ts +26 -9
- package/skill/krillplayer-plugin/references/cases.md +19 -1
- package/skill/krillplayer-plugin/references/contract.d.ts +104 -11
- package/skill/krillplayer-plugin/references/crypto-tools.md +104 -0
- package/skill/krillplayer-plugin/references/errors-and-fixes.md +5 -1
|
@@ -0,0 +1,933 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
|
3
|
+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
#include "bh_platform.h"
|
|
7
|
+
#if WASM_ENABLE_INTERP != 0
|
|
8
|
+
#include "../interpreter/wasm_runtime.h"
|
|
9
|
+
#endif
|
|
10
|
+
#if WASM_ENABLE_AOT != 0
|
|
11
|
+
#include "../aot/aot_runtime.h"
|
|
12
|
+
#endif
|
|
13
|
+
#if WASM_ENABLE_THREAD_MGR != 0
|
|
14
|
+
#include "../libraries/thread-mgr/thread_manager.h"
|
|
15
|
+
#endif
|
|
16
|
+
#if WASM_ENABLE_GC != 0
|
|
17
|
+
#include "gc/gc_object.h"
|
|
18
|
+
#if WASM_ENABLE_STRINGREF != 0
|
|
19
|
+
#include "string_object.h"
|
|
20
|
+
#endif
|
|
21
|
+
#if WASM_ENABLE_GC_PERF_PROFILING != 0
|
|
22
|
+
#include "../../shared/mem-alloc/mem_alloc.h"
|
|
23
|
+
#endif
|
|
24
|
+
#endif
|
|
25
|
+
|
|
26
|
+
static void
|
|
27
|
+
set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
|
|
28
|
+
{
|
|
29
|
+
if (error_buf != NULL)
|
|
30
|
+
snprintf(error_buf, error_buf_size, "%s", string);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
static void *
|
|
34
|
+
runtime_malloc(uint64 size, WASMModuleInstanceCommon *module_inst,
|
|
35
|
+
char *error_buf, uint32 error_buf_size)
|
|
36
|
+
{
|
|
37
|
+
void *mem;
|
|
38
|
+
|
|
39
|
+
if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) {
|
|
40
|
+
if (module_inst != NULL) {
|
|
41
|
+
wasm_runtime_set_exception(module_inst, "allocate memory failed");
|
|
42
|
+
}
|
|
43
|
+
else if (error_buf != NULL) {
|
|
44
|
+
set_error_buf(error_buf, error_buf_size, "allocate memory failed");
|
|
45
|
+
}
|
|
46
|
+
return NULL;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
memset(mem, 0, (uint32)size);
|
|
50
|
+
return mem;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
static union {
|
|
54
|
+
int a;
|
|
55
|
+
char b;
|
|
56
|
+
} __ue = { .a = 1 };
|
|
57
|
+
|
|
58
|
+
#define is_little_endian() (__ue.b == 1) /* NOLINT */
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Implementation of wasm_application_execute_main()
|
|
62
|
+
*/
|
|
63
|
+
static bool
|
|
64
|
+
check_main_func_type(const WASMFuncType *type, bool is_memory64)
|
|
65
|
+
{
|
|
66
|
+
if (!(type->param_count == 0 || type->param_count == 2)
|
|
67
|
+
|| type->result_count > 1) {
|
|
68
|
+
LOG_ERROR(
|
|
69
|
+
"WASM execute application failed: invalid main function type.\n");
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (type->param_count == 2
|
|
74
|
+
&& !(type->types[0] == VALUE_TYPE_I32
|
|
75
|
+
&& type->types[1]
|
|
76
|
+
== (is_memory64 ? VALUE_TYPE_I64 : VALUE_TYPE_I32))) {
|
|
77
|
+
LOG_ERROR(
|
|
78
|
+
"WASM execute application failed: invalid main function type.\n");
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (type->result_count
|
|
83
|
+
&& type->types[type->param_count] != VALUE_TYPE_I32) {
|
|
84
|
+
LOG_ERROR(
|
|
85
|
+
"WASM execute application failed: invalid main function type.\n");
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return true;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
static bool
|
|
93
|
+
execute_main(WASMModuleInstanceCommon *module_inst, int32 argc, char *argv[])
|
|
94
|
+
{
|
|
95
|
+
WASMFunctionInstanceCommon *func;
|
|
96
|
+
WASMFuncType *func_type = NULL;
|
|
97
|
+
WASMExecEnv *exec_env = NULL;
|
|
98
|
+
uint32 argc1 = 0, argv1[3] = { 0 };
|
|
99
|
+
uint32 total_argv_size = 0;
|
|
100
|
+
uint64 total_size;
|
|
101
|
+
uint64 argv_buf_offset = 0;
|
|
102
|
+
int32 i;
|
|
103
|
+
char *argv_buf, *p, *p_end;
|
|
104
|
+
uint32 *argv_offsets, module_type;
|
|
105
|
+
bool ret, is_import_func = true, is_memory64 = false;
|
|
106
|
+
#if WASM_ENABLE_MEMORY64 != 0
|
|
107
|
+
WASMModuleInstance *wasm_module_inst = (WASMModuleInstance *)module_inst;
|
|
108
|
+
if (wasm_module_inst->memory_count > 0)
|
|
109
|
+
is_memory64 = wasm_module_inst->memories[0]->is_memory64;
|
|
110
|
+
#endif
|
|
111
|
+
|
|
112
|
+
exec_env = wasm_runtime_get_exec_env_singleton(module_inst);
|
|
113
|
+
if (!exec_env) {
|
|
114
|
+
wasm_runtime_set_exception(module_inst,
|
|
115
|
+
"create singleton exec_env failed");
|
|
116
|
+
return false;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
#if WASM_ENABLE_LIBC_WASI != 0
|
|
120
|
+
/* In wasi mode, we should call the function named "_start"
|
|
121
|
+
which initializes the wasi environment and then calls
|
|
122
|
+
the actual main function. Directly calling main function
|
|
123
|
+
may cause exception thrown. */
|
|
124
|
+
if ((func = wasm_runtime_lookup_wasi_start_function(module_inst))) {
|
|
125
|
+
const char *wasi_proc_exit_exception = "wasi proc exit";
|
|
126
|
+
|
|
127
|
+
ret = wasm_runtime_call_wasm(exec_env, func, 0, NULL);
|
|
128
|
+
#if WASM_ENABLE_THREAD_MGR != 0
|
|
129
|
+
if (ret) {
|
|
130
|
+
/* On a successful return from the `_start` function,
|
|
131
|
+
we terminate other threads by mimicking wasi:proc_exit(0).
|
|
132
|
+
|
|
133
|
+
Note:
|
|
134
|
+
- A return from the `main` function is an equivalent of
|
|
135
|
+
exit(). (C standard)
|
|
136
|
+
- When exit code is 0, wasi-libc's `_start` function just
|
|
137
|
+
returns w/o calling `proc_exit`.
|
|
138
|
+
- A process termination should terminate threads in
|
|
139
|
+
the process. */
|
|
140
|
+
|
|
141
|
+
wasm_runtime_set_exception(module_inst, wasi_proc_exit_exception);
|
|
142
|
+
/* exit_code is zero-initialized */
|
|
143
|
+
ret = false;
|
|
144
|
+
}
|
|
145
|
+
#endif
|
|
146
|
+
/* report wasm proc exit as a success */
|
|
147
|
+
WASMModuleInstance *inst = (WASMModuleInstance *)module_inst;
|
|
148
|
+
if (!ret && strstr(inst->cur_exception, wasi_proc_exit_exception)) {
|
|
149
|
+
inst->cur_exception[0] = 0;
|
|
150
|
+
ret = true;
|
|
151
|
+
}
|
|
152
|
+
return ret;
|
|
153
|
+
}
|
|
154
|
+
#endif /* end of WASM_ENABLE_LIBC_WASI */
|
|
155
|
+
|
|
156
|
+
if (!(func = wasm_runtime_lookup_function(module_inst, "main"))
|
|
157
|
+
&& !(func =
|
|
158
|
+
wasm_runtime_lookup_function(module_inst, "__main_argc_argv"))
|
|
159
|
+
&& !(func = wasm_runtime_lookup_function(module_inst, "_main"))) {
|
|
160
|
+
#if WASM_ENABLE_LIBC_WASI != 0
|
|
161
|
+
wasm_runtime_set_exception(
|
|
162
|
+
module_inst, "lookup the entry point symbol (like _start, main, "
|
|
163
|
+
"_main, __main_argc_argv) failed");
|
|
164
|
+
#else
|
|
165
|
+
wasm_runtime_set_exception(module_inst,
|
|
166
|
+
"lookup the entry point symbol (like main, "
|
|
167
|
+
"_main, __main_argc_argv) failed");
|
|
168
|
+
#endif
|
|
169
|
+
return false;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
#if WASM_ENABLE_INTERP != 0
|
|
173
|
+
if (module_inst->module_type == Wasm_Module_Bytecode) {
|
|
174
|
+
is_import_func = ((WASMFunctionInstance *)func)->is_import_func;
|
|
175
|
+
}
|
|
176
|
+
#endif
|
|
177
|
+
#if WASM_ENABLE_AOT != 0
|
|
178
|
+
if (module_inst->module_type == Wasm_Module_AoT) {
|
|
179
|
+
is_import_func = ((AOTFunctionInstance *)func)->is_import_func;
|
|
180
|
+
}
|
|
181
|
+
#endif
|
|
182
|
+
|
|
183
|
+
if (is_import_func) {
|
|
184
|
+
wasm_runtime_set_exception(module_inst, "lookup main function failed");
|
|
185
|
+
return false;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
module_type = module_inst->module_type;
|
|
189
|
+
func_type = wasm_runtime_get_function_type(func, module_type);
|
|
190
|
+
|
|
191
|
+
if (!func_type) {
|
|
192
|
+
LOG_ERROR("invalid module instance type");
|
|
193
|
+
return false;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
if (!check_main_func_type(func_type, is_memory64)) {
|
|
197
|
+
wasm_runtime_set_exception(module_inst,
|
|
198
|
+
"invalid function type of main function");
|
|
199
|
+
return false;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
if (func_type->param_count) {
|
|
203
|
+
for (i = 0; i < argc; i++)
|
|
204
|
+
total_argv_size += (uint32)(strlen(argv[i]) + 1);
|
|
205
|
+
#if WASM_ENABLE_MEMORY64 != 0
|
|
206
|
+
if (is_memory64)
|
|
207
|
+
/* `char **argv` is an array of 64-bit elements in memory64 */
|
|
208
|
+
total_argv_size = align_uint(total_argv_size, 8);
|
|
209
|
+
else
|
|
210
|
+
#endif
|
|
211
|
+
total_argv_size = align_uint(total_argv_size, 4);
|
|
212
|
+
|
|
213
|
+
#if WASM_ENABLE_MEMORY64 != 0
|
|
214
|
+
if (is_memory64)
|
|
215
|
+
/* `char **argv` is an array of 64-bit elements in memory64 */
|
|
216
|
+
total_size =
|
|
217
|
+
(uint64)total_argv_size + sizeof(uint64) * (uint64)argc;
|
|
218
|
+
else
|
|
219
|
+
#endif
|
|
220
|
+
total_size =
|
|
221
|
+
(uint64)total_argv_size + sizeof(uint32) * (uint64)argc;
|
|
222
|
+
|
|
223
|
+
if (total_size >= UINT32_MAX
|
|
224
|
+
|| !(argv_buf_offset = wasm_runtime_module_malloc(
|
|
225
|
+
module_inst, total_size, (void **)&argv_buf))) {
|
|
226
|
+
wasm_runtime_set_exception(module_inst, "allocate memory failed");
|
|
227
|
+
return false;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
p = argv_buf;
|
|
231
|
+
argv_offsets = (uint32 *)(p + total_argv_size);
|
|
232
|
+
p_end = p + total_size;
|
|
233
|
+
|
|
234
|
+
for (i = 0; i < argc; i++) {
|
|
235
|
+
bh_memcpy_s(p, (uint32)(p_end - p), argv[i],
|
|
236
|
+
(uint32)(strlen(argv[i]) + 1));
|
|
237
|
+
#if WASM_ENABLE_MEMORY64 != 0
|
|
238
|
+
if (is_memory64)
|
|
239
|
+
/* `char **argv` is an array of 64-bit elements in memory64 */
|
|
240
|
+
((uint64 *)argv_offsets)[i] =
|
|
241
|
+
(uint32)argv_buf_offset + (uint32)(p - argv_buf);
|
|
242
|
+
else
|
|
243
|
+
#endif
|
|
244
|
+
argv_offsets[i] =
|
|
245
|
+
(uint32)argv_buf_offset + (uint32)(p - argv_buf);
|
|
246
|
+
p += strlen(argv[i]) + 1;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
argv1[0] = (uint32)argc;
|
|
250
|
+
#if WASM_ENABLE_MEMORY64 != 0
|
|
251
|
+
if (is_memory64) {
|
|
252
|
+
argc1 = 3;
|
|
253
|
+
uint64 app_addr =
|
|
254
|
+
wasm_runtime_addr_native_to_app(module_inst, argv_offsets);
|
|
255
|
+
PUT_I64_TO_ADDR(&argv[1], app_addr);
|
|
256
|
+
}
|
|
257
|
+
else
|
|
258
|
+
#endif
|
|
259
|
+
{
|
|
260
|
+
argc1 = 2;
|
|
261
|
+
argv1[1] = (uint32)wasm_runtime_addr_native_to_app(module_inst,
|
|
262
|
+
argv_offsets);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
ret = wasm_runtime_call_wasm(exec_env, func, argc1, argv1);
|
|
267
|
+
if (ret && func_type->result_count > 0 && argc > 0 && argv)
|
|
268
|
+
/* copy the return value */
|
|
269
|
+
*(int *)argv = (int)argv1[0];
|
|
270
|
+
|
|
271
|
+
if (argv_buf_offset)
|
|
272
|
+
wasm_runtime_module_free(module_inst, argv_buf_offset);
|
|
273
|
+
|
|
274
|
+
return ret;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
bool
|
|
278
|
+
wasm_application_execute_main(WASMModuleInstanceCommon *module_inst, int32 argc,
|
|
279
|
+
char *argv[])
|
|
280
|
+
{
|
|
281
|
+
bool ret;
|
|
282
|
+
#if (WASM_ENABLE_MEMORY_PROFILING != 0)
|
|
283
|
+
WASMExecEnv *exec_env;
|
|
284
|
+
#endif
|
|
285
|
+
|
|
286
|
+
ret = execute_main(module_inst, argc, argv);
|
|
287
|
+
|
|
288
|
+
#if WASM_ENABLE_MEMORY_PROFILING != 0
|
|
289
|
+
exec_env = wasm_runtime_get_exec_env_singleton(module_inst);
|
|
290
|
+
if (exec_env) {
|
|
291
|
+
wasm_runtime_dump_mem_consumption(exec_env);
|
|
292
|
+
}
|
|
293
|
+
#endif
|
|
294
|
+
|
|
295
|
+
#if WASM_ENABLE_GC_PERF_PROFILING != 0
|
|
296
|
+
void *handle = wasm_runtime_get_gc_heap_handle(module_inst);
|
|
297
|
+
mem_allocator_dump_perf_profiling(handle);
|
|
298
|
+
#endif
|
|
299
|
+
|
|
300
|
+
#if WASM_ENABLE_PERF_PROFILING != 0
|
|
301
|
+
wasm_runtime_dump_perf_profiling(module_inst);
|
|
302
|
+
#endif
|
|
303
|
+
|
|
304
|
+
if (ret)
|
|
305
|
+
ret = wasm_runtime_get_exception(module_inst) == NULL;
|
|
306
|
+
|
|
307
|
+
return ret;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* Implementation of wasm_application_execute_func()
|
|
312
|
+
*/
|
|
313
|
+
|
|
314
|
+
union ieee754_float {
|
|
315
|
+
float f;
|
|
316
|
+
|
|
317
|
+
/* This is the IEEE 754 single-precision format. */
|
|
318
|
+
union {
|
|
319
|
+
struct {
|
|
320
|
+
unsigned int negative : 1;
|
|
321
|
+
unsigned int exponent : 8;
|
|
322
|
+
unsigned int mantissa : 23;
|
|
323
|
+
} ieee_big_endian;
|
|
324
|
+
struct {
|
|
325
|
+
unsigned int mantissa : 23;
|
|
326
|
+
unsigned int exponent : 8;
|
|
327
|
+
unsigned int negative : 1;
|
|
328
|
+
} ieee_little_endian;
|
|
329
|
+
} ieee;
|
|
330
|
+
};
|
|
331
|
+
|
|
332
|
+
union ieee754_double {
|
|
333
|
+
double d;
|
|
334
|
+
|
|
335
|
+
/* This is the IEEE 754 double-precision format. */
|
|
336
|
+
union {
|
|
337
|
+
struct {
|
|
338
|
+
unsigned int negative : 1;
|
|
339
|
+
unsigned int exponent : 11;
|
|
340
|
+
/* Together these comprise the mantissa. */
|
|
341
|
+
unsigned int mantissa0 : 20;
|
|
342
|
+
unsigned int mantissa1 : 32;
|
|
343
|
+
} ieee_big_endian;
|
|
344
|
+
|
|
345
|
+
struct {
|
|
346
|
+
/* Together these comprise the mantissa. */
|
|
347
|
+
unsigned int mantissa1 : 32;
|
|
348
|
+
unsigned int mantissa0 : 20;
|
|
349
|
+
unsigned int exponent : 11;
|
|
350
|
+
unsigned int negative : 1;
|
|
351
|
+
} ieee_little_endian;
|
|
352
|
+
} ieee;
|
|
353
|
+
};
|
|
354
|
+
|
|
355
|
+
static bool
|
|
356
|
+
execute_func(WASMModuleInstanceCommon *module_inst, const char *name,
|
|
357
|
+
int32 argc, char *argv[])
|
|
358
|
+
{
|
|
359
|
+
WASMFunctionInstanceCommon *target_func;
|
|
360
|
+
WASMFuncType *type = NULL;
|
|
361
|
+
WASMExecEnv *exec_env = NULL;
|
|
362
|
+
#if WASM_ENABLE_GC != 0
|
|
363
|
+
WASMRefTypeMap *ref_type_map;
|
|
364
|
+
WASMLocalObjectRef *local_ref;
|
|
365
|
+
uint32 num_local_ref_pushed = 0;
|
|
366
|
+
#endif
|
|
367
|
+
uint32 argc1, *argv1 = NULL, cell_num = 0, j, k = 0;
|
|
368
|
+
#if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
|
|
369
|
+
uint32 param_size_in_double_world = 0, result_size_in_double_world = 0;
|
|
370
|
+
#endif
|
|
371
|
+
int32 i, p, module_type;
|
|
372
|
+
uint64 total_size;
|
|
373
|
+
char buf[128];
|
|
374
|
+
|
|
375
|
+
bh_assert(argc >= 0);
|
|
376
|
+
LOG_DEBUG("call a function \"%s\" with %d arguments", name, argc);
|
|
377
|
+
|
|
378
|
+
if (!(target_func = wasm_runtime_lookup_function(module_inst, name))) {
|
|
379
|
+
snprintf(buf, sizeof(buf), "lookup function %s failed", name);
|
|
380
|
+
wasm_runtime_set_exception(module_inst, buf);
|
|
381
|
+
goto fail;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
module_type = module_inst->module_type;
|
|
385
|
+
type = wasm_runtime_get_function_type(target_func, module_type);
|
|
386
|
+
|
|
387
|
+
if (!type) {
|
|
388
|
+
LOG_ERROR("invalid module instance type");
|
|
389
|
+
return false;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
if (type->param_count != (uint32)argc) {
|
|
393
|
+
wasm_runtime_set_exception(module_inst, "invalid input argument count");
|
|
394
|
+
goto fail;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
exec_env = wasm_runtime_get_exec_env_singleton(module_inst);
|
|
398
|
+
if (!exec_env) {
|
|
399
|
+
wasm_runtime_set_exception(module_inst,
|
|
400
|
+
"create singleton exec_env failed");
|
|
401
|
+
goto fail;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
#if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
|
|
405
|
+
for (i = 0; i < type->param_count; i++) {
|
|
406
|
+
param_size_in_double_world +=
|
|
407
|
+
wasm_value_type_cell_num_outside(type->types[i]);
|
|
408
|
+
}
|
|
409
|
+
for (i = 0; i < type->result_count; i++) {
|
|
410
|
+
result_size_in_double_world += wasm_value_type_cell_num_outside(
|
|
411
|
+
type->types[type->param_count + i]);
|
|
412
|
+
}
|
|
413
|
+
argc1 = param_size_in_double_world;
|
|
414
|
+
cell_num = (param_size_in_double_world >= result_size_in_double_world)
|
|
415
|
+
? param_size_in_double_world
|
|
416
|
+
: result_size_in_double_world;
|
|
417
|
+
#else
|
|
418
|
+
argc1 = type->param_cell_num;
|
|
419
|
+
cell_num = (argc1 > type->ret_cell_num) ? argc1 : type->ret_cell_num;
|
|
420
|
+
#endif
|
|
421
|
+
|
|
422
|
+
total_size = sizeof(uint32) * (uint64)(cell_num > 2 ? cell_num : 2);
|
|
423
|
+
if ((!(argv1 = runtime_malloc((uint32)total_size, module_inst, NULL, 0)))) {
|
|
424
|
+
goto fail;
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
#if WASM_ENABLE_GC != 0
|
|
428
|
+
ref_type_map = type->ref_type_maps;
|
|
429
|
+
#endif
|
|
430
|
+
/* Parse arguments */
|
|
431
|
+
for (i = 0, p = 0; i < argc; i++) {
|
|
432
|
+
char *endptr = NULL;
|
|
433
|
+
bh_assert(argv[i] != NULL);
|
|
434
|
+
if (argv[i][0] == '\0') {
|
|
435
|
+
snprintf(buf, sizeof(buf), "invalid input argument %" PRId32, i);
|
|
436
|
+
wasm_runtime_set_exception(module_inst, buf);
|
|
437
|
+
goto fail;
|
|
438
|
+
}
|
|
439
|
+
switch (type->types[i]) {
|
|
440
|
+
case VALUE_TYPE_I32:
|
|
441
|
+
argv1[p++] = (uint32)strtoul(argv[i], &endptr, 0);
|
|
442
|
+
break;
|
|
443
|
+
case VALUE_TYPE_I64:
|
|
444
|
+
{
|
|
445
|
+
union {
|
|
446
|
+
uint64 val;
|
|
447
|
+
uint32 parts[2];
|
|
448
|
+
} u;
|
|
449
|
+
u.val = strtoull(argv[i], &endptr, 0);
|
|
450
|
+
argv1[p++] = u.parts[0];
|
|
451
|
+
argv1[p++] = u.parts[1];
|
|
452
|
+
break;
|
|
453
|
+
}
|
|
454
|
+
case VALUE_TYPE_F32:
|
|
455
|
+
{
|
|
456
|
+
float32 f32 = strtof(argv[i], &endptr);
|
|
457
|
+
if (isnan(f32)) {
|
|
458
|
+
#ifdef _MSC_VER
|
|
459
|
+
/*
|
|
460
|
+
* Spec tests require the binary representation of NaN to be
|
|
461
|
+
* 0x7fc00000 for float and 0x7ff8000000000000 for float;
|
|
462
|
+
* however, in MSVC compiler, strtof doesn't return this
|
|
463
|
+
* exact value, causing some of the spec test failures. We
|
|
464
|
+
* use the value returned by nan/nanf as it is the one
|
|
465
|
+
* expected by spec tests.
|
|
466
|
+
*
|
|
467
|
+
*/
|
|
468
|
+
f32 = nanf("");
|
|
469
|
+
#endif
|
|
470
|
+
if (argv[i][0] == '-') {
|
|
471
|
+
union ieee754_float u;
|
|
472
|
+
u.f = f32;
|
|
473
|
+
if (is_little_endian())
|
|
474
|
+
u.ieee.ieee_little_endian.negative = 1;
|
|
475
|
+
else
|
|
476
|
+
u.ieee.ieee_big_endian.negative = 1;
|
|
477
|
+
bh_memcpy_s(&f32, sizeof(float), &u.f, sizeof(float));
|
|
478
|
+
}
|
|
479
|
+
if (endptr[0] == ':') {
|
|
480
|
+
uint32 sig;
|
|
481
|
+
union ieee754_float u;
|
|
482
|
+
sig = (uint32)strtoul(endptr + 1, &endptr, 0);
|
|
483
|
+
u.f = f32;
|
|
484
|
+
if (is_little_endian())
|
|
485
|
+
u.ieee.ieee_little_endian.mantissa = sig;
|
|
486
|
+
else
|
|
487
|
+
u.ieee.ieee_big_endian.mantissa = sig;
|
|
488
|
+
bh_memcpy_s(&f32, sizeof(float), &u.f, sizeof(float));
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
bh_memcpy_s(&argv1[p], (uint32)total_size - p, &f32,
|
|
492
|
+
(uint32)sizeof(float));
|
|
493
|
+
p++;
|
|
494
|
+
break;
|
|
495
|
+
}
|
|
496
|
+
case VALUE_TYPE_F64:
|
|
497
|
+
{
|
|
498
|
+
union {
|
|
499
|
+
float64 val;
|
|
500
|
+
uint32 parts[2];
|
|
501
|
+
} u;
|
|
502
|
+
u.val = strtod(argv[i], &endptr);
|
|
503
|
+
if (isnan(u.val)) {
|
|
504
|
+
#ifdef _MSC_VER
|
|
505
|
+
u.val = nan("");
|
|
506
|
+
#endif
|
|
507
|
+
if (argv[i][0] == '-') {
|
|
508
|
+
union ieee754_double ud;
|
|
509
|
+
ud.d = u.val;
|
|
510
|
+
if (is_little_endian())
|
|
511
|
+
ud.ieee.ieee_little_endian.negative = 1;
|
|
512
|
+
else
|
|
513
|
+
ud.ieee.ieee_big_endian.negative = 1;
|
|
514
|
+
bh_memcpy_s(&u.val, sizeof(double), &ud.d,
|
|
515
|
+
sizeof(double));
|
|
516
|
+
}
|
|
517
|
+
if (endptr && endptr[0] == ':') {
|
|
518
|
+
uint64 sig;
|
|
519
|
+
union ieee754_double ud;
|
|
520
|
+
sig = strtoull(endptr + 1, &endptr, 0);
|
|
521
|
+
ud.d = u.val;
|
|
522
|
+
if (is_little_endian()) {
|
|
523
|
+
ud.ieee.ieee_little_endian.mantissa0 = sig >> 32;
|
|
524
|
+
ud.ieee.ieee_little_endian.mantissa1 = (uint32)sig;
|
|
525
|
+
}
|
|
526
|
+
else {
|
|
527
|
+
ud.ieee.ieee_big_endian.mantissa0 = sig >> 32;
|
|
528
|
+
ud.ieee.ieee_big_endian.mantissa1 = (uint32)sig;
|
|
529
|
+
}
|
|
530
|
+
bh_memcpy_s(&u.val, sizeof(double), &ud.d,
|
|
531
|
+
sizeof(double));
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
argv1[p++] = u.parts[0];
|
|
535
|
+
argv1[p++] = u.parts[1];
|
|
536
|
+
break;
|
|
537
|
+
}
|
|
538
|
+
#if WASM_ENABLE_SIMD != 0
|
|
539
|
+
case VALUE_TYPE_V128:
|
|
540
|
+
{
|
|
541
|
+
/* it likes 0x123\0x234 or 123\234 */
|
|
542
|
+
/* retrieve first i64 */
|
|
543
|
+
*(uint64 *)(argv1 + p) = strtoull(argv[i], &endptr, 0);
|
|
544
|
+
/* skip \ */
|
|
545
|
+
endptr++;
|
|
546
|
+
/* retrieve second i64 */
|
|
547
|
+
*(uint64 *)(argv1 + p + 2) = strtoull(endptr, &endptr, 0);
|
|
548
|
+
p += 4;
|
|
549
|
+
break;
|
|
550
|
+
}
|
|
551
|
+
#endif /* WASM_ENABLE_SIMD != 0 */
|
|
552
|
+
#if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
|
|
553
|
+
case VALUE_TYPE_FUNCREF:
|
|
554
|
+
#if UINTPTR_MAX == UINT32_MAX
|
|
555
|
+
case VALUE_TYPE_EXTERNREF:
|
|
556
|
+
#endif
|
|
557
|
+
{
|
|
558
|
+
if (strncasecmp(argv[i], "null", 4) == 0) {
|
|
559
|
+
argv1[p++] = (uint32)-1;
|
|
560
|
+
}
|
|
561
|
+
else {
|
|
562
|
+
argv1[p++] = (uint32)strtoul(argv[i], &endptr, 0);
|
|
563
|
+
}
|
|
564
|
+
break;
|
|
565
|
+
}
|
|
566
|
+
#if UINTPTR_MAX == UINT64_MAX
|
|
567
|
+
case VALUE_TYPE_EXTERNREF:
|
|
568
|
+
{
|
|
569
|
+
union {
|
|
570
|
+
uintptr_t val;
|
|
571
|
+
uint32 parts[2];
|
|
572
|
+
} u;
|
|
573
|
+
if (strncasecmp(argv[i], "null", 4) == 0) {
|
|
574
|
+
u.val = (uintptr_t)-1LL;
|
|
575
|
+
}
|
|
576
|
+
else {
|
|
577
|
+
u.val = strtoull(argv[i], &endptr, 0);
|
|
578
|
+
}
|
|
579
|
+
argv1[p++] = u.parts[0];
|
|
580
|
+
argv1[p++] = u.parts[1];
|
|
581
|
+
break;
|
|
582
|
+
}
|
|
583
|
+
#endif
|
|
584
|
+
#endif /* WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0 */
|
|
585
|
+
default:
|
|
586
|
+
{
|
|
587
|
+
#if WASM_ENABLE_GC != 0
|
|
588
|
+
bool is_extern_ref = false;
|
|
589
|
+
bool is_anyref = false;
|
|
590
|
+
|
|
591
|
+
if (wasm_is_type_reftype(type->types[i])) {
|
|
592
|
+
if (strncasecmp(argv[i], "null", 4) == 0) {
|
|
593
|
+
PUT_REF_TO_ADDR(argv1 + p, NULL_REF);
|
|
594
|
+
p += REF_CELL_NUM;
|
|
595
|
+
break;
|
|
596
|
+
}
|
|
597
|
+
else if (type->types[i] == VALUE_TYPE_EXTERNREF) {
|
|
598
|
+
is_extern_ref = true;
|
|
599
|
+
}
|
|
600
|
+
else if (type->types[i] == VALUE_TYPE_ANYREF) {
|
|
601
|
+
is_anyref = true;
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
if (wasm_is_type_multi_byte_type(type->types[i])) {
|
|
605
|
+
WASMRefType *ref_type = ref_type_map->ref_type;
|
|
606
|
+
if (wasm_is_refheaptype_common(
|
|
607
|
+
&ref_type->ref_ht_common)) {
|
|
608
|
+
int32 heap_type = ref_type->ref_ht_common.heap_type;
|
|
609
|
+
if (heap_type == HEAP_TYPE_EXTERN) {
|
|
610
|
+
is_extern_ref = true;
|
|
611
|
+
}
|
|
612
|
+
else if (heap_type == HEAP_TYPE_ANY) {
|
|
613
|
+
is_anyref = true;
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
ref_type_map++;
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
if (is_extern_ref) {
|
|
621
|
+
WASMExternrefObjectRef gc_obj;
|
|
622
|
+
void *extern_obj =
|
|
623
|
+
(void *)(uintptr_t)strtoull(argv[i], &endptr, 0);
|
|
624
|
+
gc_obj = wasm_externref_obj_new(exec_env, extern_obj);
|
|
625
|
+
if (!gc_obj) {
|
|
626
|
+
wasm_runtime_set_exception(
|
|
627
|
+
module_inst, "create extern object failed");
|
|
628
|
+
goto fail;
|
|
629
|
+
}
|
|
630
|
+
if (!(local_ref =
|
|
631
|
+
runtime_malloc(sizeof(WASMLocalObjectRef),
|
|
632
|
+
module_inst, NULL, 0))) {
|
|
633
|
+
goto fail;
|
|
634
|
+
}
|
|
635
|
+
wasm_runtime_push_local_obj_ref(exec_env, local_ref);
|
|
636
|
+
local_ref->val = (WASMObjectRef)gc_obj;
|
|
637
|
+
num_local_ref_pushed++;
|
|
638
|
+
PUT_REF_TO_ADDR(argv1 + p, gc_obj);
|
|
639
|
+
p += REF_CELL_NUM;
|
|
640
|
+
}
|
|
641
|
+
else if (is_anyref) {
|
|
642
|
+
/* If a parameter type is (ref null? any) and its value
|
|
643
|
+
* is not null, then we treat the value as host ptr */
|
|
644
|
+
WASMAnyrefObjectRef gc_obj;
|
|
645
|
+
void *host_obj =
|
|
646
|
+
(void *)(uintptr_t)strtoull(argv[i], &endptr, 0);
|
|
647
|
+
gc_obj = wasm_anyref_obj_new(exec_env, host_obj);
|
|
648
|
+
if (!gc_obj) {
|
|
649
|
+
wasm_runtime_set_exception(
|
|
650
|
+
module_inst, "create anyref object failed");
|
|
651
|
+
goto fail;
|
|
652
|
+
}
|
|
653
|
+
if (!(local_ref =
|
|
654
|
+
runtime_malloc(sizeof(WASMLocalObjectRef),
|
|
655
|
+
module_inst, NULL, 0))) {
|
|
656
|
+
goto fail;
|
|
657
|
+
}
|
|
658
|
+
wasm_runtime_push_local_obj_ref(exec_env, local_ref);
|
|
659
|
+
local_ref->val = (WASMObjectRef)gc_obj;
|
|
660
|
+
num_local_ref_pushed++;
|
|
661
|
+
PUT_REF_TO_ADDR(argv1 + p, gc_obj);
|
|
662
|
+
p += REF_CELL_NUM;
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
break;
|
|
666
|
+
}
|
|
667
|
+
#endif /* end of WASM_ENABLE_GC != 0 */
|
|
668
|
+
bh_assert(0);
|
|
669
|
+
break;
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
if (endptr && *endptr != '\0' && *endptr != '_') {
|
|
673
|
+
snprintf(buf, sizeof(buf), "invalid input argument %" PRId32 ": %s",
|
|
674
|
+
i, argv[i]);
|
|
675
|
+
wasm_runtime_set_exception(module_inst, buf);
|
|
676
|
+
goto fail;
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
wasm_runtime_set_exception(module_inst, NULL);
|
|
681
|
+
#if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
|
|
682
|
+
bh_assert(p == (int32)argc1);
|
|
683
|
+
#endif
|
|
684
|
+
|
|
685
|
+
if (!wasm_runtime_call_wasm(exec_env, target_func, argc1, argv1)) {
|
|
686
|
+
goto fail;
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
#if WASM_ENABLE_GC != 0
|
|
690
|
+
ref_type_map = type->result_ref_type_maps;
|
|
691
|
+
#endif
|
|
692
|
+
/* print return value */
|
|
693
|
+
for (j = 0; j < type->result_count; j++) {
|
|
694
|
+
switch (type->types[type->param_count + j]) {
|
|
695
|
+
case VALUE_TYPE_I32:
|
|
696
|
+
{
|
|
697
|
+
os_printf("0x%" PRIx32 ":i32", argv1[k]);
|
|
698
|
+
k++;
|
|
699
|
+
break;
|
|
700
|
+
}
|
|
701
|
+
case VALUE_TYPE_I64:
|
|
702
|
+
{
|
|
703
|
+
union {
|
|
704
|
+
uint64 val;
|
|
705
|
+
uint32 parts[2];
|
|
706
|
+
} u;
|
|
707
|
+
u.parts[0] = argv1[k];
|
|
708
|
+
u.parts[1] = argv1[k + 1];
|
|
709
|
+
k += 2;
|
|
710
|
+
os_printf("0x%" PRIx64 ":i64", u.val);
|
|
711
|
+
break;
|
|
712
|
+
}
|
|
713
|
+
case VALUE_TYPE_F32:
|
|
714
|
+
{
|
|
715
|
+
os_printf("%.7g:f32", *(float32 *)(argv1 + k));
|
|
716
|
+
k++;
|
|
717
|
+
break;
|
|
718
|
+
}
|
|
719
|
+
case VALUE_TYPE_F64:
|
|
720
|
+
{
|
|
721
|
+
union {
|
|
722
|
+
float64 val;
|
|
723
|
+
uint32 parts[2];
|
|
724
|
+
} u;
|
|
725
|
+
u.parts[0] = argv1[k];
|
|
726
|
+
u.parts[1] = argv1[k + 1];
|
|
727
|
+
k += 2;
|
|
728
|
+
os_printf("%.7g:f64", u.val);
|
|
729
|
+
break;
|
|
730
|
+
}
|
|
731
|
+
#if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
|
|
732
|
+
case VALUE_TYPE_FUNCREF:
|
|
733
|
+
{
|
|
734
|
+
if (argv1[k] != NULL_REF)
|
|
735
|
+
os_printf("%" PRIu32 ":ref.func", argv1[k]);
|
|
736
|
+
else
|
|
737
|
+
os_printf("func:ref.null");
|
|
738
|
+
k++;
|
|
739
|
+
break;
|
|
740
|
+
}
|
|
741
|
+
case VALUE_TYPE_EXTERNREF:
|
|
742
|
+
{
|
|
743
|
+
#if UINTPTR_MAX == UINT32_MAX
|
|
744
|
+
if (argv1[k] != 0 && argv1[k] != (uint32)-1)
|
|
745
|
+
os_printf("0x%" PRIxPTR ":ref.extern", (uintptr_t)argv1[k]);
|
|
746
|
+
else
|
|
747
|
+
os_printf("extern:ref.null");
|
|
748
|
+
k++;
|
|
749
|
+
#else
|
|
750
|
+
union {
|
|
751
|
+
uintptr_t val;
|
|
752
|
+
uint32 parts[2];
|
|
753
|
+
} u;
|
|
754
|
+
u.parts[0] = argv1[k];
|
|
755
|
+
u.parts[1] = argv1[k + 1];
|
|
756
|
+
k += 2;
|
|
757
|
+
if (u.val && u.val != (uintptr_t)-1LL)
|
|
758
|
+
os_printf("0x%" PRIxPTR ":ref.extern", u.val);
|
|
759
|
+
else
|
|
760
|
+
os_printf("extern:ref.null");
|
|
761
|
+
#endif
|
|
762
|
+
break;
|
|
763
|
+
}
|
|
764
|
+
#endif /* end of WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0 */
|
|
765
|
+
#if WASM_ENABLE_SIMD != 0
|
|
766
|
+
case VALUE_TYPE_V128:
|
|
767
|
+
{
|
|
768
|
+
uint64 *v = (uint64 *)(argv1 + k);
|
|
769
|
+
os_printf("<0x%016" PRIx64 " 0x%016" PRIx64 ">:v128", *v,
|
|
770
|
+
*(v + 1));
|
|
771
|
+
k += 4;
|
|
772
|
+
break;
|
|
773
|
+
}
|
|
774
|
+
#endif /* WASM_ENABLE_SIMD != 0 */
|
|
775
|
+
default:
|
|
776
|
+
{
|
|
777
|
+
#if WASM_ENABLE_GC != 0
|
|
778
|
+
if (wasm_is_type_reftype(type->types[type->param_count + j])) {
|
|
779
|
+
void *gc_obj = GET_REF_FROM_ADDR(argv1 + k);
|
|
780
|
+
k += REF_CELL_NUM;
|
|
781
|
+
if (!gc_obj) {
|
|
782
|
+
uint8 type1 = type->types[type->param_count + j];
|
|
783
|
+
WASMRefType *ref_type1 = NULL;
|
|
784
|
+
WASMType **types = NULL;
|
|
785
|
+
uint32 type_count = 0;
|
|
786
|
+
|
|
787
|
+
if (wasm_is_type_multi_byte_type(
|
|
788
|
+
type->types[type->param_count + j]))
|
|
789
|
+
ref_type1 = ref_type_map->ref_type;
|
|
790
|
+
|
|
791
|
+
#if WASM_ENABLE_INTERP != 0
|
|
792
|
+
if (module_inst->module_type == Wasm_Module_Bytecode) {
|
|
793
|
+
WASMModule *module =
|
|
794
|
+
((WASMModuleInstance *)module_inst)->module;
|
|
795
|
+
types = module->types;
|
|
796
|
+
type_count = module->type_count;
|
|
797
|
+
}
|
|
798
|
+
#endif
|
|
799
|
+
#if WASM_ENABLE_AOT != 0
|
|
800
|
+
if (module_inst->module_type == Wasm_Module_AoT) {
|
|
801
|
+
AOTModule *module =
|
|
802
|
+
(AOTModule *)((AOTModuleInstance *)module_inst)
|
|
803
|
+
->module;
|
|
804
|
+
types = module->types;
|
|
805
|
+
type_count = module->type_count;
|
|
806
|
+
}
|
|
807
|
+
#endif
|
|
808
|
+
bh_assert(type);
|
|
809
|
+
if (wasm_reftype_is_subtype_of(type1, ref_type1,
|
|
810
|
+
REF_TYPE_ANYREF, NULL,
|
|
811
|
+
types, type_count))
|
|
812
|
+
os_printf("any:");
|
|
813
|
+
else if (wasm_reftype_is_subtype_of(
|
|
814
|
+
type1, ref_type1, REF_TYPE_FUNCREF, NULL,
|
|
815
|
+
types, type_count))
|
|
816
|
+
os_printf("func:");
|
|
817
|
+
if (wasm_reftype_is_subtype_of(type1, ref_type1,
|
|
818
|
+
REF_TYPE_EXTERNREF, NULL,
|
|
819
|
+
types, type_count))
|
|
820
|
+
os_printf("extern:");
|
|
821
|
+
os_printf("ref.null");
|
|
822
|
+
}
|
|
823
|
+
else if (wasm_obj_is_func_obj(gc_obj))
|
|
824
|
+
os_printf("ref.func");
|
|
825
|
+
#if WASM_ENABLE_STRINGREF != 0
|
|
826
|
+
else if (wasm_obj_is_stringref_obj(gc_obj)
|
|
827
|
+
|| wasm_obj_is_stringview_wtf8_obj(gc_obj)) {
|
|
828
|
+
wasm_string_dump(
|
|
829
|
+
(WASMString)wasm_stringref_obj_get_value(gc_obj));
|
|
830
|
+
}
|
|
831
|
+
else if (wasm_obj_is_stringview_wtf16_obj(gc_obj)) {
|
|
832
|
+
wasm_string_dump(
|
|
833
|
+
(WASMString)wasm_stringview_wtf16_obj_get_value(
|
|
834
|
+
gc_obj));
|
|
835
|
+
}
|
|
836
|
+
#endif
|
|
837
|
+
else if (wasm_obj_is_externref_obj(gc_obj)) {
|
|
838
|
+
#if WASM_ENABLE_SPEC_TEST != 0
|
|
839
|
+
WASMObjectRef obj = wasm_externref_obj_to_internal_obj(
|
|
840
|
+
(WASMExternrefObjectRef)gc_obj);
|
|
841
|
+
if (wasm_obj_is_anyref_obj(obj))
|
|
842
|
+
os_printf("0x%" PRIxPTR ":ref.extern",
|
|
843
|
+
(uintptr_t)wasm_anyref_obj_get_value(
|
|
844
|
+
(WASMAnyrefObjectRef)obj));
|
|
845
|
+
else
|
|
846
|
+
#endif
|
|
847
|
+
os_printf("ref.extern");
|
|
848
|
+
}
|
|
849
|
+
else if (wasm_obj_is_i31_obj(gc_obj))
|
|
850
|
+
os_printf("ref.i31");
|
|
851
|
+
else if (wasm_obj_is_array_obj(gc_obj))
|
|
852
|
+
os_printf("ref.array");
|
|
853
|
+
else if (wasm_obj_is_struct_obj(gc_obj))
|
|
854
|
+
os_printf("ref.struct");
|
|
855
|
+
else if (wasm_obj_is_eq_obj(gc_obj))
|
|
856
|
+
os_printf("ref.eq");
|
|
857
|
+
else if (wasm_obj_is_anyref_obj(gc_obj))
|
|
858
|
+
os_printf("0x%" PRIxPTR ":ref.host",
|
|
859
|
+
(uintptr_t)wasm_anyref_obj_get_value(
|
|
860
|
+
(WASMAnyrefObjectRef)gc_obj));
|
|
861
|
+
else if (wasm_obj_is_internal_obj(gc_obj))
|
|
862
|
+
os_printf("ref.any");
|
|
863
|
+
|
|
864
|
+
if (wasm_is_type_multi_byte_type(
|
|
865
|
+
type->types[type->param_count + j]))
|
|
866
|
+
ref_type_map++;
|
|
867
|
+
|
|
868
|
+
break;
|
|
869
|
+
}
|
|
870
|
+
#endif /* endof WASM_ENABLE_GC != 0 */
|
|
871
|
+
bh_assert(0);
|
|
872
|
+
break;
|
|
873
|
+
}
|
|
874
|
+
}
|
|
875
|
+
if (j < (uint32)(type->result_count - 1))
|
|
876
|
+
os_printf(",");
|
|
877
|
+
}
|
|
878
|
+
os_printf("\n");
|
|
879
|
+
|
|
880
|
+
#if WASM_ENABLE_GC != 0
|
|
881
|
+
for (j = 0; j < num_local_ref_pushed; j++) {
|
|
882
|
+
local_ref = wasm_runtime_pop_local_obj_ref(exec_env);
|
|
883
|
+
wasm_runtime_free(local_ref);
|
|
884
|
+
}
|
|
885
|
+
#endif
|
|
886
|
+
|
|
887
|
+
wasm_runtime_free(argv1);
|
|
888
|
+
return true;
|
|
889
|
+
|
|
890
|
+
fail:
|
|
891
|
+
if (argv1)
|
|
892
|
+
wasm_runtime_free(argv1);
|
|
893
|
+
|
|
894
|
+
#if WASM_ENABLE_GC != 0
|
|
895
|
+
for (j = 0; j < num_local_ref_pushed; j++) {
|
|
896
|
+
local_ref = wasm_runtime_pop_local_obj_ref(exec_env);
|
|
897
|
+
wasm_runtime_free(local_ref);
|
|
898
|
+
}
|
|
899
|
+
#endif
|
|
900
|
+
|
|
901
|
+
bh_assert(wasm_runtime_get_exception(module_inst));
|
|
902
|
+
return false;
|
|
903
|
+
}
|
|
904
|
+
|
|
905
|
+
bool
|
|
906
|
+
wasm_application_execute_func(WASMModuleInstanceCommon *module_inst,
|
|
907
|
+
const char *name, int32 argc, char *argv[])
|
|
908
|
+
{
|
|
909
|
+
bool ret;
|
|
910
|
+
#if WASM_ENABLE_MEMORY_PROFILING != 0
|
|
911
|
+
WASMExecEnv *exec_env;
|
|
912
|
+
#endif
|
|
913
|
+
|
|
914
|
+
ret = execute_func(module_inst, name, argc, argv);
|
|
915
|
+
|
|
916
|
+
#if WASM_ENABLE_MEMORY_PROFILING != 0
|
|
917
|
+
exec_env = wasm_runtime_get_exec_env_singleton(module_inst);
|
|
918
|
+
if (exec_env) {
|
|
919
|
+
wasm_runtime_dump_mem_consumption(exec_env);
|
|
920
|
+
}
|
|
921
|
+
#endif
|
|
922
|
+
|
|
923
|
+
#if WASM_ENABLE_GC_PERF_PROFILING != 0
|
|
924
|
+
void *handle = wasm_runtime_get_gc_heap_handle(module_inst);
|
|
925
|
+
mem_allocator_dump_perf_profiling(handle);
|
|
926
|
+
#endif
|
|
927
|
+
|
|
928
|
+
#if WASM_ENABLE_PERF_PROFILING != 0
|
|
929
|
+
wasm_runtime_dump_perf_profiling(module_inst);
|
|
930
|
+
#endif
|
|
931
|
+
|
|
932
|
+
return (ret && !wasm_runtime_get_exception(module_inst)) ? true : false;
|
|
933
|
+
}
|