/src/WasmEdge/lib/api/wasmedge.cpp
Line | Count | Source |
1 | | // SPDX-License-Identifier: Apache-2.0 |
2 | | // SPDX-FileCopyrightText: Copyright The WasmEdge Authors |
3 | | |
4 | | #include "wasmedge/wasmedge.h" |
5 | | |
6 | | #include "common/defines.h" |
7 | | #include "driver/compiler.h" |
8 | | #include "driver/tool.h" |
9 | | #include "driver/unitool.h" |
10 | | #include "host/wasi/wasimodule.h" |
11 | | #include "plugin/plugin.h" |
12 | | #include "system/winapi.h" |
13 | | #include "vm/vm.h" |
14 | | #include "llvm/codegen.h" |
15 | | #include "llvm/compiler.h" |
16 | | |
17 | | #ifdef WASMEDGE_BUILD_FUZZING |
18 | | #include "driver/fuzzPO.h" |
19 | | #include "driver/fuzzTool.h" |
20 | | #endif |
21 | | |
22 | | #ifdef WASMEDGE_BUILD_WASI_NN_RPC |
23 | | #include "driver/wasiNNRPCServerTool.h" |
24 | | #endif |
25 | | |
26 | | #include <algorithm> |
27 | | #include <chrono> |
28 | | #include <cstdint> |
29 | | #include <cstdlib> |
30 | | #include <cstring> |
31 | | #include <functional> |
32 | | #include <map> |
33 | | #include <memory> |
34 | | #include <new> |
35 | | #include <string> |
36 | | #include <string_view> |
37 | | #include <utility> |
38 | | #include <vector> |
39 | | |
40 | | // WasmEdge_ConfigureContext implementation. |
41 | | struct WasmEdge_ConfigureContext { |
42 | | WasmEdge::Configure Conf; |
43 | | }; |
44 | | |
45 | | // WasmEdge_StatisticsContext implementation. |
46 | | struct WasmEdge_StatisticsContext {}; |
47 | | |
48 | | // WasmEdge_ASTModuleContext implementation. |
49 | | struct WasmEdge_ASTModuleContext {}; |
50 | | |
51 | | // WasmEdge_FunctionTypeContext implementation. |
52 | | struct WasmEdge_FunctionTypeContext {}; |
53 | | |
54 | | // WasmEdge_TableTypeContext implementation. |
55 | | struct WasmEdge_TableTypeContext {}; |
56 | | |
57 | | // WasmEdge_MemoryTypeContext implementation. |
58 | | struct WasmEdge_MemoryTypeContext {}; |
59 | | |
60 | | // WasmEdge_TagTypeContext implementation. |
61 | | struct WasmEdge_TagTypeContext {}; |
62 | | |
63 | | // WasmEdge_GlobalTypeContext implementation. |
64 | | struct WasmEdge_GlobalTypeContext {}; |
65 | | |
66 | | // WasmEdge_ImportTypeContext implementation. |
67 | | struct WasmEdge_ImportTypeContext {}; |
68 | | |
69 | | // WasmEdge_ExportTypeContext implementation. |
70 | | struct WasmEdge_ExportTypeContext {}; |
71 | | |
72 | | // WasmEdge_CompilerContext implementation. |
73 | | struct WasmEdge_CompilerContext { |
74 | | #ifdef WASMEDGE_USE_LLVM |
75 | | WasmEdge_CompilerContext(const WasmEdge::Configure &Conf) noexcept |
76 | 0 | : Compiler(Conf), CodeGen(Conf), Load(Conf), Valid(Conf) {} |
77 | | WasmEdge::LLVM::Compiler Compiler; |
78 | | WasmEdge::LLVM::CodeGen CodeGen; |
79 | | WasmEdge::Loader::Loader Load; |
80 | | WasmEdge::Validator::Validator Valid; |
81 | | #endif |
82 | | }; |
83 | | |
84 | | // WasmEdge_LoaderContext implementation. |
85 | | struct WasmEdge_LoaderContext {}; |
86 | | |
87 | | // WasmEdge_ValidatorContext implementation. |
88 | | struct WasmEdge_ValidatorContext {}; |
89 | | |
90 | | // WasmEdge_ExecutorContext implementation. |
91 | | struct WasmEdge_ExecutorContext {}; |
92 | | |
93 | | // WasmEdge_StoreContext implementation. |
94 | | struct WasmEdge_StoreContext {}; |
95 | | |
96 | | // WasmEdge_ModuleInstanceContext implementation. |
97 | | struct WasmEdge_ModuleInstanceContext {}; |
98 | | |
99 | | // WasmEdge_FunctionInstanceContext implementation. |
100 | | struct WasmEdge_FunctionInstanceContext {}; |
101 | | |
102 | | // WasmEdge_TableInstanceContext implementation. |
103 | | struct WasmEdge_TableInstanceContext {}; |
104 | | |
105 | | // WasmEdge_MemoryInstanceContext implementation. |
106 | | struct WasmEdge_MemoryInstanceContext {}; |
107 | | |
108 | | // WasmEdge_TagInstanceContext implementation. |
109 | | struct WasmEdge_TagInstanceContext {}; |
110 | | |
111 | | // WasmEdge_GlobalInstanceContext implementation. |
112 | | struct WasmEdge_GlobalInstanceContext {}; |
113 | | |
114 | | // WasmEdge_CallingFrameContext implementation. |
115 | | struct WasmEdge_CallingFrameContext {}; |
116 | | |
117 | | // WasmEdge_Async implementation. |
118 | | struct WasmEdge_Async { |
119 | | template <typename... Args> |
120 | | WasmEdge_Async(Args &&...Vals) noexcept |
121 | 0 | : Async(std::forward<Args>(Vals)...) {} |
122 | | WasmEdge::Async<WasmEdge::Expect< |
123 | | std::vector<std::pair<WasmEdge::ValVariant, WasmEdge::ValType>>>> |
124 | | Async; |
125 | | }; |
126 | | |
127 | | // WasmEdge_VMContext implementation. |
128 | | struct WasmEdge_VMContext { |
129 | | template <typename... Args> |
130 | | WasmEdge_VMContext(Args &&...Vals) noexcept |
131 | 0 | : VM(std::forward<Args>(Vals)...) {}Unexecuted instantiation: WasmEdge_VMContext::WasmEdge_VMContext<WasmEdge::Configure const&, WasmEdge::Runtime::StoreManager&>(WasmEdge::Configure const&, WasmEdge::Runtime::StoreManager&) Unexecuted instantiation: WasmEdge_VMContext::WasmEdge_VMContext<WasmEdge::Configure const&>(WasmEdge::Configure const&) Unexecuted instantiation: WasmEdge_VMContext::WasmEdge_VMContext<WasmEdge::Configure, WasmEdge::Runtime::StoreManager&>(WasmEdge::Configure&&, WasmEdge::Runtime::StoreManager&) Unexecuted instantiation: WasmEdge_VMContext::WasmEdge_VMContext<WasmEdge::Configure>(WasmEdge::Configure&&) |
132 | | WasmEdge::VM::VM VM; |
133 | | }; |
134 | | |
135 | | // WasmEdge_PluginContext implementation. |
136 | | struct WasmEdge_PluginContext {}; |
137 | | |
138 | | namespace { |
139 | | |
140 | | using namespace WasmEdge; |
141 | | |
142 | | // Helper function for returning a WasmEdge_Result by error code. |
143 | | inline constexpr WasmEdge_Result |
144 | 0 | genWasmEdge_Result(const ErrCode::Value &Code) noexcept { |
145 | 0 | return WasmEdge_Result{/* Code */ static_cast<uint32_t>(Code) & 0x00FFFFFFU}; |
146 | 0 | } |
147 | | inline constexpr WasmEdge_Result |
148 | 0 | genWasmEdge_Result(const ErrCode &Code) noexcept { |
149 | 0 | return WasmEdge_Result{/* Code */ Code.operator uint32_t()}; |
150 | 0 | } |
151 | | |
152 | | // Helper function for returning a struct uint128_t / int128_t |
153 | | // from class WasmEdge::uint128_t / WasmEdge::int128_t. |
154 | | template <typename C> |
155 | 0 | inline constexpr ::uint128_t to_uint128_t(C Val) noexcept { |
156 | 0 | #if defined(__x86_64__) || defined(__aarch64__) || \ |
157 | 0 | (defined(__riscv) && __riscv_xlen == 64) || defined(__s390x__) |
158 | 0 | return Val; |
159 | | #else |
160 | | return {/* Low */ Val.low(), /* High */ static_cast<uint64_t>(Val.high())}; |
161 | | #endif |
162 | 0 | } |
163 | 0 | template <typename C> inline constexpr ::int128_t to_int128_t(C Val) noexcept { |
164 | 0 | #if defined(__x86_64__) || defined(__aarch64__) || \ |
165 | 0 | (defined(__riscv) && __riscv_xlen == 64) || defined(__s390x__) |
166 | 0 | return Val; |
167 | | #else |
168 | | return {/* Low */ Val.low(), /* High */ Val.high()}; |
169 | | #endif |
170 | 0 | } |
171 | | |
172 | | // Helper function for returning a class WasmEdge::uint128_t / |
173 | | // WasmEdge::int128_t from struct uint128_t / int128_t. |
174 | | template <typename C, typename T> |
175 | 0 | inline constexpr C to_WasmEdge_128_t(T Val) noexcept { |
176 | 0 | #if defined(__x86_64__) || defined(__aarch64__) || \ |
177 | 0 | (defined(__riscv) && __riscv_xlen == 64) || defined(__s390x__) |
178 | 0 | return Val; |
179 | | #else |
180 | | return C(Val.High, Val.Low); |
181 | | #endif |
182 | 0 | } Unexecuted instantiation: wasmedge.cpp:__int128 (anonymous namespace)::to_WasmEdge_128_t<__int128, __int128>(__int128) Unexecuted instantiation: wasmedge.cpp:unsigned __int128 (anonymous namespace)::to_WasmEdge_128_t<unsigned __int128, unsigned __int128>(unsigned __int128) |
183 | | |
184 | | // Helper functions for returning a WasmEdge::ValType by WasmEdge_ValType. |
185 | 0 | inline ValType genValType(const WasmEdge_ValType &T) noexcept { |
186 | 0 | std::array<uint8_t, 8> R; |
187 | 0 | std::copy_n(T.Data, 8, R.begin()); |
188 | 0 | return ValType(R); |
189 | 0 | } |
190 | | |
191 | 0 | inline std::filesystem::path genPath(const char *Path) { |
192 | 0 | return (Path && *Path) ? std::filesystem::absolute(Path) |
193 | 0 | : std::filesystem::path(); |
194 | 0 | } |
195 | | |
196 | | // Helper functions for returning a WasmEdge_ValType by WasmEdge::ValType. |
197 | 0 | inline WasmEdge_ValType genWasmEdge_ValType(const ValType &T) noexcept { |
198 | 0 | WasmEdge_ValType VT; |
199 | 0 | std::copy_n(T.getRawData().cbegin(), 8, VT.Data); |
200 | 0 | return VT; |
201 | 0 | } |
202 | | |
203 | | // Helper functions for returning a WasmEdge_Value by various values. |
204 | | template <typename T> |
205 | 0 | inline WasmEdge_Value genWasmEdge_Value(const T &Val) noexcept { |
206 | 0 | return WasmEdge_Value{ |
207 | 0 | /* Value */ to_uint128_t(ValVariant(Val).unwrap()), |
208 | 0 | /* Type */ genWasmEdge_ValType(WasmEdge::ValTypeFromType<T>())}; |
209 | 0 | } Unexecuted instantiation: wasmedge.cpp:WasmEdge_Value (anonymous namespace)::genWasmEdge_Value<int>(int const&) Unexecuted instantiation: wasmedge.cpp:WasmEdge_Value (anonymous namespace)::genWasmEdge_Value<long>(long const&) Unexecuted instantiation: wasmedge.cpp:WasmEdge_Value (anonymous namespace)::genWasmEdge_Value<float>(float const&) Unexecuted instantiation: wasmedge.cpp:WasmEdge_Value (anonymous namespace)::genWasmEdge_Value<double>(double const&) Unexecuted instantiation: wasmedge.cpp:WasmEdge_Value (anonymous namespace)::genWasmEdge_Value<__int128>(__int128 const&) |
210 | | inline WasmEdge_Value genWasmEdge_Value(const ValVariant &Val, |
211 | 0 | const ValType &T) noexcept { |
212 | 0 | return WasmEdge_Value{/* Value */ to_uint128_t(Val.unwrap()), |
213 | 0 | /* Type */ genWasmEdge_ValType(T)}; |
214 | 0 | } |
215 | | |
216 | | // Helper function for converting a WasmEdge_Value array to a ValVariant |
217 | | // vector. |
218 | | inline std::pair<std::vector<ValVariant>, std::vector<ValType>> |
219 | 0 | genParamPair(const WasmEdge_Value *Val, const uint32_t Len) { |
220 | | // Nullable-value handling for reference-type checking is delegated to the |
221 | | // executor. |
222 | 0 | std::vector<ValVariant> VVec; |
223 | 0 | std::vector<ValType> TVec; |
224 | 0 | if (Val == nullptr) { |
225 | 0 | return {VVec, TVec}; |
226 | 0 | } |
227 | 0 | VVec.resize(Len); |
228 | 0 | TVec.resize(Len); |
229 | 0 | for (uint32_t I = 0; I < Len; I++) { |
230 | 0 | TVec[I] = genValType(Val[I].Type); |
231 | 0 | switch (TVec[I].getCode()) { |
232 | 0 | case TypeCode::I32: |
233 | 0 | VVec[I] = ValVariant::wrap<uint32_t>( |
234 | 0 | to_WasmEdge_128_t<WasmEdge::uint128_t>(Val[I].Value)); |
235 | 0 | break; |
236 | 0 | case TypeCode::I64: |
237 | 0 | VVec[I] = ValVariant::wrap<uint64_t>( |
238 | 0 | to_WasmEdge_128_t<WasmEdge::uint128_t>(Val[I].Value)); |
239 | 0 | break; |
240 | 0 | case TypeCode::F32: |
241 | 0 | VVec[I] = ValVariant::wrap<float>( |
242 | 0 | to_WasmEdge_128_t<WasmEdge::uint128_t>(Val[I].Value)); |
243 | 0 | break; |
244 | 0 | case TypeCode::F64: |
245 | 0 | VVec[I] = ValVariant::wrap<double>( |
246 | 0 | to_WasmEdge_128_t<WasmEdge::uint128_t>(Val[I].Value)); |
247 | 0 | break; |
248 | 0 | case TypeCode::V128: |
249 | 0 | VVec[I] = ValVariant::wrap<WasmEdge::uint128_t>( |
250 | 0 | to_WasmEdge_128_t<WasmEdge::uint128_t>(Val[I].Value)); |
251 | 0 | break; |
252 | 0 | case TypeCode::Ref: |
253 | 0 | case TypeCode::RefNull: { |
254 | 0 | VVec[I] = ValVariant::wrap<RefVariant>( |
255 | 0 | to_WasmEdge_128_t<WasmEdge::uint128_t>(Val[I].Value)); |
256 | 0 | break; |
257 | 0 | } |
258 | 0 | default: |
259 | 0 | assumingUnreachable(); |
260 | 0 | } |
261 | 0 | } |
262 | 0 | return {VVec, TVec}; |
263 | 0 | } |
264 | | |
265 | | // Helper function for making a Span to a uint8_t array. |
266 | | template <typename T> |
267 | | inline constexpr Span<const T> genSpan(const T *Buf, |
268 | 0 | const uint64_t Len) noexcept { |
269 | 0 | if (Buf && Len > 0) { |
270 | 0 | return Span<const T>(Buf, Len); |
271 | 0 | } |
272 | 0 | return Span<const T>(); |
273 | 0 | } Unexecuted instantiation: wasmedge.cpp:cxx20::span<unsigned long const, 18446744073709551615ul> (anonymous namespace)::genSpan<unsigned long>(unsigned long const*, unsigned long) Unexecuted instantiation: wasmedge.cpp:cxx20::span<unsigned char const, 18446744073709551615ul> (anonymous namespace)::genSpan<unsigned char>(unsigned char const*, unsigned long) |
274 | | |
275 | | // Helper functions for converting WasmEdge_String to std::String. |
276 | 0 | inline std::string_view genStrView(const WasmEdge_String S) noexcept { |
277 | 0 | return std::string_view(S.Buf, S.Length); |
278 | 0 | } |
279 | | |
280 | | // Helper functions for converting a ValVariant vector to a WasmEdge_Value |
281 | | // array. |
282 | | inline constexpr void |
283 | | fillWasmEdge_ValueArr(Span<const std::pair<ValVariant, ValType>> Vec, |
284 | 0 | WasmEdge_Value *Val, const uint32_t Len) noexcept { |
285 | 0 | if (Val == nullptr) { |
286 | 0 | return; |
287 | 0 | } |
288 | 0 | for (uint32_t I = 0; I < Len && I < Vec.size(); I++) { |
289 | 0 | Val[I] = genWasmEdge_Value(Vec[I].first, Vec[I].second); |
290 | 0 | } |
291 | 0 | } |
292 | | |
293 | | // Helper template to run and return result. |
294 | 0 | auto EmptyThen = [](auto &&) noexcept {}; |
295 | 0 | template <typename T> inline bool isContext(T *Cxt) noexcept { |
296 | 0 | return (Cxt != nullptr); |
297 | 0 | } Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_CompilerContext>(WasmEdge_CompilerContext*) Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_LoaderContext>(WasmEdge_LoaderContext*) Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_ASTModuleContext*>(WasmEdge_ASTModuleContext**) Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_ASTModuleContext const>(WasmEdge_ASTModuleContext const*) Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_Bytes>(WasmEdge_Bytes*) Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_ValidatorContext>(WasmEdge_ValidatorContext*) Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_ExecutorContext>(WasmEdge_ExecutorContext*) Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_ModuleInstanceContext*>(WasmEdge_ModuleInstanceContext**) Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_StoreContext>(WasmEdge_StoreContext*) Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_ModuleInstanceContext const>(WasmEdge_ModuleInstanceContext const*) Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_FunctionInstanceContext const>(WasmEdge_FunctionInstanceContext const*) Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_ModuleInstanceContext>(WasmEdge_ModuleInstanceContext*) Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_FunctionInstanceContext>(WasmEdge_FunctionInstanceContext*) Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_TableInstanceContext>(WasmEdge_TableInstanceContext*) Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_MemoryInstanceContext>(WasmEdge_MemoryInstanceContext*) Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_GlobalInstanceContext>(WasmEdge_GlobalInstanceContext*) Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_TableInstanceContext const>(WasmEdge_TableInstanceContext const*) Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_Value>(WasmEdge_Value*) Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_MemoryInstanceContext const>(WasmEdge_MemoryInstanceContext const*) Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<unsigned char>(unsigned char*) Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<unsigned char const>(unsigned char const*) Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_Async const>(WasmEdge_Async const*) Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_VMContext>(WasmEdge_VMContext*) |
298 | | template <typename T, typename... Args> |
299 | 0 | inline bool isContext(T *Cxt, Args *...Cxts) noexcept { |
300 | 0 | return isContext(Cxt) && isContext(Cxts...); |
301 | 0 | } Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_LoaderContext, WasmEdge_ASTModuleContext*>(WasmEdge_LoaderContext*, WasmEdge_ASTModuleContext**) Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_LoaderContext, WasmEdge_ASTModuleContext const, WasmEdge_Bytes>(WasmEdge_LoaderContext*, WasmEdge_ASTModuleContext const*, WasmEdge_Bytes*) Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_ASTModuleContext const, WasmEdge_Bytes>(WasmEdge_ASTModuleContext const*, WasmEdge_Bytes*) Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_ValidatorContext, WasmEdge_ASTModuleContext const>(WasmEdge_ValidatorContext*, WasmEdge_ASTModuleContext const*) Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_ExecutorContext, WasmEdge_ModuleInstanceContext*, WasmEdge_StoreContext, WasmEdge_ASTModuleContext const>(WasmEdge_ExecutorContext*, WasmEdge_ModuleInstanceContext**, WasmEdge_StoreContext*, WasmEdge_ASTModuleContext const*) Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_ModuleInstanceContext*, WasmEdge_StoreContext, WasmEdge_ASTModuleContext const>(WasmEdge_ModuleInstanceContext**, WasmEdge_StoreContext*, WasmEdge_ASTModuleContext const*) Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_StoreContext, WasmEdge_ASTModuleContext const>(WasmEdge_StoreContext*, WasmEdge_ASTModuleContext const*) Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_ExecutorContext, WasmEdge_StoreContext, WasmEdge_ModuleInstanceContext const>(WasmEdge_ExecutorContext*, WasmEdge_StoreContext*, WasmEdge_ModuleInstanceContext const*) Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_StoreContext, WasmEdge_ModuleInstanceContext const>(WasmEdge_StoreContext*, WasmEdge_ModuleInstanceContext const*) Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_ExecutorContext, WasmEdge_FunctionInstanceContext const>(WasmEdge_ExecutorContext*, WasmEdge_FunctionInstanceContext const*) Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_ModuleInstanceContext, WasmEdge_FunctionInstanceContext>(WasmEdge_ModuleInstanceContext*, WasmEdge_FunctionInstanceContext*) Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_ModuleInstanceContext, WasmEdge_TableInstanceContext>(WasmEdge_ModuleInstanceContext*, WasmEdge_TableInstanceContext*) Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_ModuleInstanceContext, WasmEdge_MemoryInstanceContext>(WasmEdge_ModuleInstanceContext*, WasmEdge_MemoryInstanceContext*) Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_ModuleInstanceContext, WasmEdge_GlobalInstanceContext>(WasmEdge_ModuleInstanceContext*, WasmEdge_GlobalInstanceContext*) Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_TableInstanceContext const, WasmEdge_Value>(WasmEdge_TableInstanceContext const*, WasmEdge_Value*) Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_MemoryInstanceContext const, unsigned char>(WasmEdge_MemoryInstanceContext const*, unsigned char*) Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_MemoryInstanceContext, unsigned char const>(WasmEdge_MemoryInstanceContext*, unsigned char const*) Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_VMContext, WasmEdge_ASTModuleContext const>(WasmEdge_VMContext*, WasmEdge_ASTModuleContext const*) Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_VMContext, WasmEdge_ModuleInstanceContext const>(WasmEdge_VMContext*, WasmEdge_ModuleInstanceContext const*) |
302 | | // Helper to log and convert exceptions at the C API boundary. |
303 | 0 | inline WasmEdge_Result handleCAPIError() noexcept { |
304 | 0 | try { |
305 | 0 | throw; |
306 | 0 | } catch (const std::bad_alloc &) { |
307 | 0 | spdlog::error("C API failed: memory allocation failure"sv); |
308 | 0 | } catch (const std::exception &E) { |
309 | 0 | spdlog::error("C API failed: {}"sv, E.what()); |
310 | 0 | } catch (...) { |
311 | 0 | spdlog::error("C API failed: unknown exception"sv); |
312 | 0 | } |
313 | 0 | return genWasmEdge_Result(ErrCode::Value::RuntimeError); |
314 | 0 | } |
315 | | |
316 | | template <typename T, typename U, typename... CxtT> |
317 | 0 | inline WasmEdge_Result wrap(T &&Proc, U &&Then, CxtT *...Cxts) noexcept { |
318 | 0 | if (isContext(Cxts...)) { |
319 | 0 | try { |
320 | 0 | if (auto Res = Proc()) { |
321 | 0 | Then(Res); |
322 | 0 | return genWasmEdge_Result(ErrCode::Value::Success); |
323 | 0 | } else { |
324 | 0 | return genWasmEdge_Result(Res.error()); |
325 | 0 | } |
326 | 0 | } catch (...) { |
327 | 0 | return handleCAPIError(); |
328 | 0 | } |
329 | 0 | } else { |
330 | 0 | return genWasmEdge_Result(ErrCode::Value::WrongVMWorkflow); |
331 | 0 | } |
332 | 0 | } Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_CompilerCompile::$_0, (anonymous namespace)::$_11&, WasmEdge_CompilerContext>(WasmEdge_CompilerCompile::$_0&&, (anonymous namespace)::$_11&, WasmEdge_CompilerContext*) Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_CompilerCompileFromBytes::$_0, (anonymous namespace)::$_11&, WasmEdge_CompilerContext>(WasmEdge_CompilerCompileFromBytes::$_0&&, (anonymous namespace)::$_11&, WasmEdge_CompilerContext*) Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_LoaderParseFromFile::$_0, WasmEdge_LoaderParseFromFile::$_1, WasmEdge_LoaderContext, WasmEdge_ASTModuleContext*>(WasmEdge_LoaderParseFromFile::$_0&&, WasmEdge_LoaderParseFromFile::$_1&&, WasmEdge_LoaderContext*, WasmEdge_ASTModuleContext**) Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_LoaderParseFromBytes::$_0, WasmEdge_LoaderParseFromBytes::$_1, WasmEdge_LoaderContext, WasmEdge_ASTModuleContext*>(WasmEdge_LoaderParseFromBytes::$_0&&, WasmEdge_LoaderParseFromBytes::$_1&&, WasmEdge_LoaderContext*, WasmEdge_ASTModuleContext**) Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_LoaderSerializeASTModule::$_0, WasmEdge_LoaderSerializeASTModule::$_1, WasmEdge_LoaderContext, WasmEdge_ASTModuleContext const, WasmEdge_Bytes>(WasmEdge_LoaderSerializeASTModule::$_0&&, WasmEdge_LoaderSerializeASTModule::$_1&&, WasmEdge_LoaderContext*, WasmEdge_ASTModuleContext const*, WasmEdge_Bytes*) Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_ValidatorValidate::$_0, (anonymous namespace)::$_11&, WasmEdge_ValidatorContext, WasmEdge_ASTModuleContext const>(WasmEdge_ValidatorValidate::$_0&&, (anonymous namespace)::$_11&, WasmEdge_ValidatorContext*, WasmEdge_ASTModuleContext const*) Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_ExecutorInstantiate::$_0, WasmEdge_ExecutorInstantiate::$_1, WasmEdge_ExecutorContext, WasmEdge_ModuleInstanceContext*, WasmEdge_StoreContext, WasmEdge_ASTModuleContext const>(WasmEdge_ExecutorInstantiate::$_0&&, WasmEdge_ExecutorInstantiate::$_1&&, WasmEdge_ExecutorContext*, WasmEdge_ModuleInstanceContext**, WasmEdge_StoreContext*, WasmEdge_ASTModuleContext const*) Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_ExecutorRegister::$_0, WasmEdge_ExecutorRegister::$_1, WasmEdge_ExecutorContext, WasmEdge_ModuleInstanceContext*, WasmEdge_StoreContext, WasmEdge_ASTModuleContext const>(WasmEdge_ExecutorRegister::$_0&&, WasmEdge_ExecutorRegister::$_1&&, WasmEdge_ExecutorContext*, WasmEdge_ModuleInstanceContext**, WasmEdge_StoreContext*, WasmEdge_ASTModuleContext const*) Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_ExecutorRegisterImport::$_0, (anonymous namespace)::$_11&, WasmEdge_ExecutorContext, WasmEdge_StoreContext, WasmEdge_ModuleInstanceContext const>(WasmEdge_ExecutorRegisterImport::$_0&&, (anonymous namespace)::$_11&, WasmEdge_ExecutorContext*, WasmEdge_StoreContext*, WasmEdge_ModuleInstanceContext const*) Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_ExecutorRegisterImportWithAlias::$_0, (anonymous namespace)::$_11&, WasmEdge_ExecutorContext, WasmEdge_StoreContext, WasmEdge_ModuleInstanceContext const>(WasmEdge_ExecutorRegisterImportWithAlias::$_0&&, (anonymous namespace)::$_11&, WasmEdge_ExecutorContext*, WasmEdge_StoreContext*, WasmEdge_ModuleInstanceContext const*) Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_ExecutorInvoke::$_0, WasmEdge_ExecutorInvoke::$_1, WasmEdge_ExecutorContext, WasmEdge_FunctionInstanceContext const>(WasmEdge_ExecutorInvoke::$_0&&, WasmEdge_ExecutorInvoke::$_1&&, WasmEdge_ExecutorContext*, WasmEdge_FunctionInstanceContext const*) Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_ModuleInstanceAddFunction::$_0, (anonymous namespace)::$_11&, WasmEdge_ModuleInstanceContext, WasmEdge_FunctionInstanceContext>(WasmEdge_ModuleInstanceAddFunction::$_0&&, (anonymous namespace)::$_11&, WasmEdge_ModuleInstanceContext*, WasmEdge_FunctionInstanceContext*) Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_ModuleInstanceAddTable::$_0, (anonymous namespace)::$_11&, WasmEdge_ModuleInstanceContext, WasmEdge_TableInstanceContext>(WasmEdge_ModuleInstanceAddTable::$_0&&, (anonymous namespace)::$_11&, WasmEdge_ModuleInstanceContext*, WasmEdge_TableInstanceContext*) Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_ModuleInstanceAddMemory::$_0, (anonymous namespace)::$_11&, WasmEdge_ModuleInstanceContext, WasmEdge_MemoryInstanceContext>(WasmEdge_ModuleInstanceAddMemory::$_0&&, (anonymous namespace)::$_11&, WasmEdge_ModuleInstanceContext*, WasmEdge_MemoryInstanceContext*) Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_ModuleInstanceAddGlobal::$_0, (anonymous namespace)::$_11&, WasmEdge_ModuleInstanceContext, WasmEdge_GlobalInstanceContext>(WasmEdge_ModuleInstanceAddGlobal::$_0&&, (anonymous namespace)::$_11&, WasmEdge_ModuleInstanceContext*, WasmEdge_GlobalInstanceContext*) Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_TableInstanceGetData::$_0, WasmEdge_TableInstanceGetData::$_1, WasmEdge_TableInstanceContext const, WasmEdge_Value>(WasmEdge_TableInstanceGetData::$_0&&, WasmEdge_TableInstanceGetData::$_1&&, WasmEdge_TableInstanceContext const*, WasmEdge_Value*) Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_TableInstanceSetData::$_0, (anonymous namespace)::$_11&, WasmEdge_TableInstanceContext>(WasmEdge_TableInstanceSetData::$_0&&, (anonymous namespace)::$_11&, WasmEdge_TableInstanceContext*) Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_TableInstanceGrow::$_0, (anonymous namespace)::$_11&, WasmEdge_TableInstanceContext>(WasmEdge_TableInstanceGrow::$_0&&, (anonymous namespace)::$_11&, WasmEdge_TableInstanceContext*) Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_MemoryInstanceGetData::$_0, WasmEdge_MemoryInstanceGetData::$_1, WasmEdge_MemoryInstanceContext const, unsigned char>(WasmEdge_MemoryInstanceGetData::$_0&&, WasmEdge_MemoryInstanceGetData::$_1&&, WasmEdge_MemoryInstanceContext const*, unsigned char*) Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_MemoryInstanceSetData::$_0, (anonymous namespace)::$_11&, WasmEdge_MemoryInstanceContext, unsigned char const>(WasmEdge_MemoryInstanceSetData::$_0&&, (anonymous namespace)::$_11&, WasmEdge_MemoryInstanceContext*, unsigned char const*) Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_MemoryInstanceGrowPage::$_0, (anonymous namespace)::$_11&, WasmEdge_MemoryInstanceContext>(WasmEdge_MemoryInstanceGrowPage::$_0&&, (anonymous namespace)::$_11&, WasmEdge_MemoryInstanceContext*) Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_GlobalInstanceSetValue::$_0, (anonymous namespace)::$_11&, WasmEdge_GlobalInstanceContext>(WasmEdge_GlobalInstanceSetValue::$_0&&, (anonymous namespace)::$_11&, WasmEdge_GlobalInstanceContext*) Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_AsyncGet::$_0, WasmEdge_AsyncGet::$_1, WasmEdge_Async const>(WasmEdge_AsyncGet::$_0&&, WasmEdge_AsyncGet::$_1&&, WasmEdge_Async const*) Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_VMRegisterModuleFromFile::$_0, (anonymous namespace)::$_11&, WasmEdge_VMContext>(WasmEdge_VMRegisterModuleFromFile::$_0&&, (anonymous namespace)::$_11&, WasmEdge_VMContext*) Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_VMRegisterModuleFromBytes::$_0, (anonymous namespace)::$_11&, WasmEdge_VMContext>(WasmEdge_VMRegisterModuleFromBytes::$_0&&, (anonymous namespace)::$_11&, WasmEdge_VMContext*) Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_VMRegisterModuleFromASTModule::$_0, (anonymous namespace)::$_11&, WasmEdge_VMContext, WasmEdge_ASTModuleContext const>(WasmEdge_VMRegisterModuleFromASTModule::$_0&&, (anonymous namespace)::$_11&, WasmEdge_VMContext*, WasmEdge_ASTModuleContext const*) Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_VMRegisterModuleFromImport::$_0, (anonymous namespace)::$_11&, WasmEdge_VMContext, WasmEdge_ModuleInstanceContext const>(WasmEdge_VMRegisterModuleFromImport::$_0&&, (anonymous namespace)::$_11&, WasmEdge_VMContext*, WasmEdge_ModuleInstanceContext const*) Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_VMRegisterModuleFromImportWithAlias::$_0, (anonymous namespace)::$_11&, WasmEdge_VMContext, WasmEdge_ModuleInstanceContext const>(WasmEdge_VMRegisterModuleFromImportWithAlias::$_0&&, (anonymous namespace)::$_11&, WasmEdge_VMContext*, WasmEdge_ModuleInstanceContext const*) Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_VMRunWasmFromFile::$_0, WasmEdge_VMRunWasmFromFile::$_1, WasmEdge_VMContext>(WasmEdge_VMRunWasmFromFile::$_0&&, WasmEdge_VMRunWasmFromFile::$_1&&, WasmEdge_VMContext*) Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_VMRunWasmFromBytes::$_0, WasmEdge_VMRunWasmFromBytes::$_1, WasmEdge_VMContext>(WasmEdge_VMRunWasmFromBytes::$_0&&, WasmEdge_VMRunWasmFromBytes::$_1&&, WasmEdge_VMContext*) Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_VMRunWasmFromASTModule::$_0, WasmEdge_VMRunWasmFromASTModule::$_1, WasmEdge_VMContext, WasmEdge_ASTModuleContext const>(WasmEdge_VMRunWasmFromASTModule::$_0&&, WasmEdge_VMRunWasmFromASTModule::$_1&&, WasmEdge_VMContext*, WasmEdge_ASTModuleContext const*) Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_VMLoadWasmFromFile::$_0, (anonymous namespace)::$_11&, WasmEdge_VMContext>(WasmEdge_VMLoadWasmFromFile::$_0&&, (anonymous namespace)::$_11&, WasmEdge_VMContext*) Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_VMLoadWasmFromBytes::$_0, (anonymous namespace)::$_11&, WasmEdge_VMContext>(WasmEdge_VMLoadWasmFromBytes::$_0&&, (anonymous namespace)::$_11&, WasmEdge_VMContext*) Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_VMLoadWasmFromASTModule::$_0, (anonymous namespace)::$_11&, WasmEdge_VMContext, WasmEdge_ASTModuleContext const>(WasmEdge_VMLoadWasmFromASTModule::$_0&&, (anonymous namespace)::$_11&, WasmEdge_VMContext*, WasmEdge_ASTModuleContext const*) Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_VMValidate::$_0, (anonymous namespace)::$_11&, WasmEdge_VMContext>(WasmEdge_VMValidate::$_0&&, (anonymous namespace)::$_11&, WasmEdge_VMContext*) Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_VMInstantiate::$_0, (anonymous namespace)::$_11&, WasmEdge_VMContext>(WasmEdge_VMInstantiate::$_0&&, (anonymous namespace)::$_11&, WasmEdge_VMContext*) Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_VMExecute::$_0, WasmEdge_VMExecute::$_1, WasmEdge_VMContext>(WasmEdge_VMExecute::$_0&&, WasmEdge_VMExecute::$_1&&, WasmEdge_VMContext*) Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_VMExecuteRegistered::$_0, WasmEdge_VMExecuteRegistered::$_1, WasmEdge_VMContext>(WasmEdge_VMExecuteRegistered::$_0&&, WasmEdge_VMExecuteRegistered::$_1&&, WasmEdge_VMContext*) |
333 | | |
334 | | // Helper function for retrieving exported maps. |
335 | | template <typename T> |
336 | | inline uint32_t fillMap(const std::map<std::string, T *, std::less<>> &Map, |
337 | 0 | WasmEdge_String *Names, const uint32_t Len) noexcept { |
338 | 0 | uint32_t I = 0; |
339 | 0 | for (auto &&Pair : Map) { |
340 | 0 | if (I >= Len) { |
341 | 0 | break; |
342 | 0 | } |
343 | 0 | if (Names) { |
344 | 0 | Names[I] = WasmEdge_String{ |
345 | 0 | /* Length */ static_cast<uint32_t>(Pair.first.length()), |
346 | 0 | /* Buf */ Pair.first.data()}; |
347 | 0 | } |
348 | 0 | I++; |
349 | 0 | } |
350 | 0 | return static_cast<uint32_t>(Map.size()); |
351 | 0 | } Unexecuted instantiation: wasmedge.cpp:unsigned int (anonymous namespace)::fillMap<WasmEdge::Runtime::Instance::ModuleInstance const>(std::__1::map<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, WasmEdge::Runtime::Instance::ModuleInstance const*, std::__1::less<void>, std::__1::allocator<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const, WasmEdge::Runtime::Instance::ModuleInstance const*> > > const&, WasmEdge_String*, unsigned int) Unexecuted instantiation: wasmedge.cpp:unsigned int (anonymous namespace)::fillMap<WasmEdge::Runtime::Instance::FunctionInstance>(std::__1::map<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, WasmEdge::Runtime::Instance::FunctionInstance*, std::__1::less<void>, std::__1::allocator<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const, WasmEdge::Runtime::Instance::FunctionInstance*> > > const&, WasmEdge_String*, unsigned int) Unexecuted instantiation: wasmedge.cpp:unsigned int (anonymous namespace)::fillMap<WasmEdge::Runtime::Instance::TableInstance>(std::__1::map<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, WasmEdge::Runtime::Instance::TableInstance*, std::__1::less<void>, std::__1::allocator<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const, WasmEdge::Runtime::Instance::TableInstance*> > > const&, WasmEdge_String*, unsigned int) Unexecuted instantiation: wasmedge.cpp:unsigned int (anonymous namespace)::fillMap<WasmEdge::Runtime::Instance::MemoryInstance>(std::__1::map<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, WasmEdge::Runtime::Instance::MemoryInstance*, std::__1::less<void>, std::__1::allocator<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const, WasmEdge::Runtime::Instance::MemoryInstance*> > > const&, WasmEdge_String*, unsigned int) Unexecuted instantiation: wasmedge.cpp:unsigned int (anonymous namespace)::fillMap<WasmEdge::Runtime::Instance::TagInstance>(std::__1::map<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, WasmEdge::Runtime::Instance::TagInstance*, std::__1::less<void>, std::__1::allocator<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const, WasmEdge::Runtime::Instance::TagInstance*> > > const&, WasmEdge_String*, unsigned int) Unexecuted instantiation: wasmedge.cpp:unsigned int (anonymous namespace)::fillMap<WasmEdge::Runtime::Instance::GlobalInstance>(std::__1::map<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, WasmEdge::Runtime::Instance::GlobalInstance*, std::__1::less<void>, std::__1::allocator<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const, WasmEdge::Runtime::Instance::GlobalInstance*> > > const&, WasmEdge_String*, unsigned int) |
352 | | |
353 | | // Helper functions of context conversions. |
354 | | #define CONVTO(SIMP, INST, NAME, QUANT) \ |
355 | 0 | inline QUANT auto *to##SIMP##Cxt(QUANT INST *Cxt) noexcept { \ |
356 | 0 | return reinterpret_cast<QUANT WasmEdge_##NAME##Context *>(Cxt); \ |
357 | 0 | } Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::toFuncCxt(WasmEdge::Runtime::Instance::FunctionInstance const*) Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::toStatCxt(WasmEdge::Statistics::Statistics*) Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::toImpTypeCxt(WasmEdge::AST::ImportDesc const*) Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::toExpTypeCxt(WasmEdge::AST::ExportDesc const*) Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::toLimitCxt(WasmEdge::AST::Limit*) Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::toFuncTypeCxt(WasmEdge::AST::FunctionType*) Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::toTabTypeCxt(WasmEdge::AST::TableType*) Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::toLimitCxt(WasmEdge::AST::Limit const*) Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::toMemTypeCxt(WasmEdge::AST::MemoryType*) Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::toFuncTypeCxt(WasmEdge::AST::FunctionType const*) Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::toGlobTypeCxt(WasmEdge::AST::GlobalType*) Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::toTabTypeCxt(WasmEdge::AST::TableType const*) Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::toMemTypeCxt(WasmEdge::AST::MemoryType const*) Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::toTagTypeCxt(WasmEdge::AST::TagType const*) Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::toGlobTypeCxt(WasmEdge::AST::GlobalType const*) Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::toLoaderCxt(WasmEdge::Loader::Loader*) Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::toValidatorCxt(WasmEdge::Validator::Validator*) Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::toExecutorCxt(WasmEdge::Executor::Executor*) Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::toStoreCxt(WasmEdge::Runtime::StoreManager*) Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::toModCxt(WasmEdge::Runtime::Instance::ModuleInstance const*) Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::toModCxt(WasmEdge::Runtime::Instance::ModuleInstance*) Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::toFuncCxt(WasmEdge::Runtime::Instance::FunctionInstance*) Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::toTabCxt(WasmEdge::Runtime::Instance::TableInstance*) Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::toMemCxt(WasmEdge::Runtime::Instance::MemoryInstance*) Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::toTagCxt(WasmEdge::Runtime::Instance::TagInstance*) Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::toGlobCxt(WasmEdge::Runtime::Instance::GlobalInstance*) Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::toPluginCxt(WasmEdge::Plugin::Plugin const*) Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::toASTModCxt(WasmEdge::AST::Module*) Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::toCallFrameCxt(WasmEdge::Runtime::CallingFrame const*) |
358 | | CONVTO(Stat, Statistics::Statistics, Statistics, ) |
359 | | CONVTO(ASTMod, AST::Module, ASTModule, ) |
360 | | CONVTO(Limit, AST::Limit, Limit, ) |
361 | | CONVTO(Limit, AST::Limit, Limit, const) |
362 | | CONVTO(FuncType, AST::FunctionType, FunctionType, ) |
363 | | CONVTO(FuncType, AST::FunctionType, FunctionType, const) |
364 | | CONVTO(TabType, AST::TableType, TableType, ) |
365 | | CONVTO(TabType, AST::TableType, TableType, const) |
366 | | CONVTO(MemType, AST::MemoryType, MemoryType, ) |
367 | | CONVTO(MemType, AST::MemoryType, MemoryType, const) |
368 | | CONVTO(TagType, AST::TagType, TagType, const) |
369 | | CONVTO(GlobType, AST::GlobalType, GlobalType, ) |
370 | | CONVTO(GlobType, AST::GlobalType, GlobalType, const) |
371 | | CONVTO(ImpType, AST::ImportDesc, ImportType, const) |
372 | | CONVTO(ExpType, AST::ExportDesc, ExportType, const) |
373 | | CONVTO(Store, Runtime::StoreManager, Store, ) |
374 | | CONVTO(Loader, Loader::Loader, Loader, ) |
375 | | CONVTO(Validator, Validator::Validator, Validator, ) |
376 | | CONVTO(Executor, Executor::Executor, Executor, ) |
377 | | CONVTO(Mod, Runtime::Instance::ModuleInstance, ModuleInstance, ) |
378 | | CONVTO(Mod, Runtime::Instance::ModuleInstance, ModuleInstance, const) |
379 | | CONVTO(Func, Runtime::Instance::FunctionInstance, FunctionInstance, ) |
380 | | CONVTO(Func, Runtime::Instance::FunctionInstance, FunctionInstance, const) |
381 | | CONVTO(Tab, Runtime::Instance::TableInstance, TableInstance, ) |
382 | | CONVTO(Mem, Runtime::Instance::MemoryInstance, MemoryInstance, ) |
383 | | CONVTO(Tag, Runtime::Instance::TagInstance, TagInstance, ) |
384 | | CONVTO(Glob, Runtime::Instance::GlobalInstance, GlobalInstance, ) |
385 | | CONVTO(CallFrame, Runtime::CallingFrame, CallingFrame, const) |
386 | | CONVTO(Plugin, Plugin::Plugin, Plugin, const) |
387 | | #undef CONVTO |
388 | | |
389 | | #define CONVFROM(SIMP, INST, NAME, QUANT) \ |
390 | | inline QUANT auto *from##SIMP##Cxt( \ |
391 | 0 | QUANT WasmEdge_##NAME##Context *Cxt) noexcept { \ |
392 | 0 | return reinterpret_cast<QUANT INST *>(Cxt); \ |
393 | 0 | } Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::fromFuncCxt(WasmEdge_FunctionInstanceContext const*) Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::fromStatCxt(WasmEdge_StatisticsContext const*) Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::fromStatCxt(WasmEdge_StatisticsContext*) Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::fromASTModCxt(WasmEdge_ASTModuleContext const*) Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::fromASTModCxt(WasmEdge_ASTModuleContext*) Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::fromLimitCxt(WasmEdge_LimitContext const*) Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::fromLimitCxt(WasmEdge_LimitContext*) Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::fromFuncTypeCxt(WasmEdge_FunctionTypeContext const*) Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::fromFuncTypeCxt(WasmEdge_FunctionTypeContext*) Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::fromTabTypeCxt(WasmEdge_TableTypeContext const*) Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::fromTabTypeCxt(WasmEdge_TableTypeContext*) Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::fromMemTypeCxt(WasmEdge_MemoryTypeContext const*) Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::fromMemTypeCxt(WasmEdge_MemoryTypeContext*) Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::fromTagTypeCxt(WasmEdge_TagTypeContext const*) Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::fromGlobTypeCxt(WasmEdge_GlobalTypeContext const*) Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::fromGlobTypeCxt(WasmEdge_GlobalTypeContext*) Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::fromImpTypeCxt(WasmEdge_ImportTypeContext const*) Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::fromExpTypeCxt(WasmEdge_ExportTypeContext const*) Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::fromLoaderCxt(WasmEdge_LoaderContext*) Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::fromValidatorCxt(WasmEdge_ValidatorContext*) Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::fromExecutorCxt(WasmEdge_ExecutorContext*) Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::fromStoreCxt(WasmEdge_StoreContext const*) Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::fromStoreCxt(WasmEdge_StoreContext*) Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::fromModCxt(WasmEdge_ModuleInstanceContext*) Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::fromModCxt(WasmEdge_ModuleInstanceContext const*) Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::fromFuncCxt(WasmEdge_FunctionInstanceContext*) Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::fromTabCxt(WasmEdge_TableInstanceContext const*) Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::fromTabCxt(WasmEdge_TableInstanceContext*) Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::fromMemCxt(WasmEdge_MemoryInstanceContext const*) Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::fromMemCxt(WasmEdge_MemoryInstanceContext*) Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::fromTagCxt(WasmEdge_TagInstanceContext const*) Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::fromGlobCxt(WasmEdge_GlobalInstanceContext const*) Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::fromGlobCxt(WasmEdge_GlobalInstanceContext*) Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::fromCallFrameCxt(WasmEdge_CallingFrameContext const*) Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::fromPluginCxt(WasmEdge_PluginContext const*) |
394 | | CONVFROM(Stat, Statistics::Statistics, Statistics, ) |
395 | | CONVFROM(Stat, Statistics::Statistics, Statistics, const) |
396 | | CONVFROM(ASTMod, AST::Module, ASTModule, ) |
397 | | CONVFROM(ASTMod, AST::Module, ASTModule, const) |
398 | | CONVFROM(Limit, AST::Limit, Limit, ) |
399 | | CONVFROM(Limit, AST::Limit, Limit, const) |
400 | | CONVFROM(FuncType, AST::FunctionType, FunctionType, ) |
401 | | CONVFROM(FuncType, AST::FunctionType, FunctionType, const) |
402 | | CONVFROM(TabType, AST::TableType, TableType, ) |
403 | | CONVFROM(TabType, AST::TableType, TableType, const) |
404 | | CONVFROM(MemType, AST::MemoryType, MemoryType, ) |
405 | | CONVFROM(MemType, AST::MemoryType, MemoryType, const) |
406 | | CONVFROM(TagType, AST::TagType, TagType, const) |
407 | | CONVFROM(GlobType, AST::GlobalType, GlobalType, ) |
408 | | CONVFROM(GlobType, AST::GlobalType, GlobalType, const) |
409 | | CONVFROM(ImpType, AST::ImportDesc, ImportType, const) |
410 | | CONVFROM(ExpType, AST::ExportDesc, ExportType, const) |
411 | | CONVFROM(Store, Runtime::StoreManager, Store, ) |
412 | | CONVFROM(Store, Runtime::StoreManager, Store, const) |
413 | | CONVFROM(Loader, Loader::Loader, Loader, ) |
414 | | CONVFROM(Validator, Validator::Validator, Validator, ) |
415 | | CONVFROM(Executor, Executor::Executor, Executor, ) |
416 | | CONVFROM(Mod, Runtime::Instance::ModuleInstance, ModuleInstance, ) |
417 | | CONVFROM(Mod, Runtime::Instance::ModuleInstance, ModuleInstance, const) |
418 | | CONVFROM(Func, Runtime::Instance::FunctionInstance, FunctionInstance, ) |
419 | | CONVFROM(Func, Runtime::Instance::FunctionInstance, FunctionInstance, const) |
420 | | CONVFROM(Tab, Runtime::Instance::TableInstance, TableInstance, ) |
421 | | CONVFROM(Tab, Runtime::Instance::TableInstance, TableInstance, const) |
422 | | CONVFROM(Mem, Runtime::Instance::MemoryInstance, MemoryInstance, ) |
423 | | CONVFROM(Mem, Runtime::Instance::MemoryInstance, MemoryInstance, const) |
424 | | CONVFROM(Tag, Runtime::Instance::TagInstance, TagInstance, const) |
425 | | CONVFROM(Glob, Runtime::Instance::GlobalInstance, GlobalInstance, ) |
426 | | CONVFROM(Glob, Runtime::Instance::GlobalInstance, GlobalInstance, const) |
427 | | CONVFROM(CallFrame, Runtime::CallingFrame, CallingFrame, const) |
428 | | CONVFROM(Plugin, Plugin::Plugin, Plugin, const) |
429 | | #undef CONVFROM |
430 | | |
431 | | // C API Host function class |
432 | | class CAPIHostFunc : public Runtime::HostFunctionBase { |
433 | | public: |
434 | | CAPIHostFunc(const AST::FunctionType *Type, WasmEdge_HostFunc_t FuncPtr, |
435 | | void *ExtData, const uint64_t FuncCost = 0) noexcept |
436 | 0 | : Runtime::HostFunctionBase(FuncCost), Func(FuncPtr), Wrap(nullptr), |
437 | 0 | Binding(nullptr), Data(ExtData) { |
438 | 0 | DefType.getCompositeType().getFuncType() = *Type; |
439 | 0 | } |
440 | | CAPIHostFunc(const AST::FunctionType *Type, WasmEdge_WrapFunc_t WrapPtr, |
441 | | void *BindingPtr, void *ExtData, |
442 | | const uint64_t FuncCost = 0) noexcept |
443 | 0 | : Runtime::HostFunctionBase(FuncCost), Func(nullptr), Wrap(WrapPtr), |
444 | 0 | Binding(BindingPtr), Data(ExtData) { |
445 | 0 | DefType.getCompositeType().getFuncType() = *Type; |
446 | 0 | } |
447 | | ~CAPIHostFunc() noexcept override = default; |
448 | | |
449 | | Expect<void> run(const Runtime::CallingFrame &CallFrame, |
450 | | Span<const ValVariant> Args, |
451 | 0 | Span<ValVariant> Rets) override { |
452 | 0 | auto &FuncType = DefType.getCompositeType().getFuncType(); |
453 | 0 | std::vector<WasmEdge_Value> Params(FuncType.getParamTypes().size()), |
454 | 0 | Returns(FuncType.getReturnTypes().size()); |
455 | 0 | for (uint32_t I = 0; I < Args.size(); I++) { |
456 | 0 | Params[I] = genWasmEdge_Value(Args[I], FuncType.getParamTypes()[I]); |
457 | 0 | } |
458 | 0 | WasmEdge_Value *PPtr = Params.size() ? (&Params[0]) : nullptr; |
459 | 0 | WasmEdge_Value *RPtr = Returns.size() ? (&Returns[0]) : nullptr; |
460 | 0 | auto *CallFrameCxt = toCallFrameCxt(&CallFrame); |
461 | 0 | WasmEdge_Result Stat; |
462 | 0 | if (Func) { |
463 | 0 | Stat = Func(Data, CallFrameCxt, PPtr, RPtr); |
464 | 0 | } else { |
465 | 0 | Stat = Wrap(Binding, Data, CallFrameCxt, PPtr, |
466 | 0 | static_cast<uint32_t>(Params.size()), RPtr, |
467 | 0 | static_cast<uint32_t>(Returns.size())); |
468 | 0 | } |
469 | 0 | for (uint32_t I = 0; I < Rets.size(); I++) { |
470 | 0 | Rets[I] = to_WasmEdge_128_t<WasmEdge::uint128_t>(Returns[I].Value); |
471 | 0 | } |
472 | 0 | if (WasmEdge_ResultOK(Stat)) { |
473 | 0 | if (WasmEdge_ResultGetCode(Stat) == 0x01U) { |
474 | 0 | return Unexpect(ErrCode::Value::Terminated); |
475 | 0 | } |
476 | 0 | } else { |
477 | 0 | return Unexpect( |
478 | 0 | static_cast<ErrCategory>(WasmEdge_ResultGetCategory(Stat)), |
479 | 0 | WasmEdge_ResultGetCode(Stat)); |
480 | 0 | } |
481 | 0 | return {}; |
482 | 0 | } |
483 | 0 | void *getData() const noexcept { return Data; } |
484 | | |
485 | | private: |
486 | | WasmEdge_HostFunc_t Func; |
487 | | WasmEdge_WrapFunc_t Wrap; |
488 | | void *Binding; |
489 | | void *Data; |
490 | | }; |
491 | | |
492 | | } // namespace |
493 | | |
494 | | #ifdef __cplusplus |
495 | | extern "C" { |
496 | | #endif |
497 | | |
498 | | // >>>>>>>> WasmEdge version functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> |
499 | | |
500 | 0 | WASMEDGE_CAPI_EXPORT const char *WasmEdge_VersionGet(void) noexcept { |
501 | 0 | return WASMEDGE_VERSION; |
502 | 0 | } |
503 | | |
504 | 0 | WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_VersionGetMajor(void) noexcept { |
505 | 0 | return WASMEDGE_VERSION_MAJOR; |
506 | 0 | } |
507 | | |
508 | 0 | WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_VersionGetMinor(void) noexcept { |
509 | 0 | return WASMEDGE_VERSION_MINOR; |
510 | 0 | } |
511 | | |
512 | 0 | WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_VersionGetPatch(void) noexcept { |
513 | 0 | return WASMEDGE_VERSION_PATCH; |
514 | 0 | } |
515 | | |
516 | | // <<<<<<<< WasmEdge version functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
517 | | |
518 | | // >>>>>>>> WasmEdge logging functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> |
519 | | |
520 | 0 | WASMEDGE_CAPI_EXPORT void WasmEdge_LogSetErrorLevel(void) noexcept { |
521 | 0 | WasmEdge::Log::setErrorLoggingLevel(); |
522 | 0 | } |
523 | | |
524 | 0 | WASMEDGE_CAPI_EXPORT void WasmEdge_LogSetDebugLevel(void) noexcept { |
525 | 0 | WasmEdge::Log::setDebugLoggingLevel(); |
526 | 0 | } |
527 | | |
528 | 0 | WASMEDGE_CAPI_EXPORT void WasmEdge_LogOff(void) noexcept { |
529 | 0 | WasmEdge::Log::setLogOff(); |
530 | 0 | } |
531 | | |
532 | | WASMEDGE_CAPI_EXPORT void |
533 | 0 | WasmEdge_LogSetLevel(WasmEdge_LogLevel Level) noexcept { |
534 | 0 | switch (Level) { |
535 | 0 | case WasmEdge_LogLevel_Trace: |
536 | 0 | WasmEdge::Log::setTraceLoggingLevel(); |
537 | 0 | break; |
538 | 0 | case WasmEdge_LogLevel_Debug: |
539 | 0 | WasmEdge::Log::setDebugLoggingLevel(); |
540 | 0 | break; |
541 | 0 | case WasmEdge_LogLevel_Info: |
542 | 0 | WasmEdge::Log::setInfoLoggingLevel(); |
543 | 0 | break; |
544 | 0 | case WasmEdge_LogLevel_Warn: |
545 | 0 | WasmEdge::Log::setWarnLoggingLevel(); |
546 | 0 | break; |
547 | 0 | case WasmEdge_LogLevel_Error: |
548 | 0 | WasmEdge::Log::setErrorLoggingLevel(); |
549 | 0 | break; |
550 | 0 | case WasmEdge_LogLevel_Critical: |
551 | 0 | WasmEdge::Log::setCriticalLoggingLevel(); |
552 | 0 | break; |
553 | 0 | } |
554 | 0 | } |
555 | | |
556 | | WASMEDGE_CAPI_EXPORT void |
557 | 0 | WasmEdge_LogSetCallback(WasmEdge_LogCallback_t Callback) noexcept { |
558 | 0 | if (Callback) { |
559 | 0 | WasmEdge::Log::setLoggingCallback( |
560 | 0 | [Callback](const spdlog::details::log_msg &Msg) { |
561 | 0 | WasmEdge_LogMessage Message; |
562 | |
|
563 | 0 | Message.Message = WasmEdge_String{ |
564 | 0 | /* Length */ static_cast<uint32_t>(Msg.payload.size()), |
565 | 0 | /* Buf */ Msg.payload.data()}; |
566 | 0 | Message.LoggerName = WasmEdge_String{ |
567 | 0 | /* Length */ static_cast<uint32_t>(Msg.logger_name.size()), |
568 | 0 | /* Buf */ Msg.logger_name.data()}; |
569 | 0 | Message.Level = static_cast<WasmEdge_LogLevel>(Msg.level); |
570 | 0 | Message.Time = std::chrono::system_clock::to_time_t(Msg.time); |
571 | 0 | Message.ThreadId = static_cast<uint64_t>(Msg.thread_id); |
572 | |
|
573 | 0 | Callback(&Message); |
574 | 0 | }); |
575 | 0 | } else { |
576 | 0 | WasmEdge::Log::setLoggingCallback({}); |
577 | 0 | } |
578 | 0 | } |
579 | | |
580 | | // <<<<<<<< WasmEdge logging functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
581 | | |
582 | | // >>>>>>>> WasmEdge valtype functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> |
583 | | |
584 | 0 | WASMEDGE_CAPI_EXPORT WasmEdge_ValType WasmEdge_ValTypeGenI32(void) noexcept { |
585 | 0 | return genWasmEdge_ValType(ValType(TypeCode::I32)); |
586 | 0 | } |
587 | | |
588 | 0 | WASMEDGE_CAPI_EXPORT WasmEdge_ValType WasmEdge_ValTypeGenI64(void) noexcept { |
589 | 0 | return genWasmEdge_ValType(ValType(TypeCode::I64)); |
590 | 0 | } |
591 | | |
592 | 0 | WASMEDGE_CAPI_EXPORT WasmEdge_ValType WasmEdge_ValTypeGenF32(void) noexcept { |
593 | 0 | return genWasmEdge_ValType(ValType(TypeCode::F32)); |
594 | 0 | } |
595 | | |
596 | 0 | WASMEDGE_CAPI_EXPORT WasmEdge_ValType WasmEdge_ValTypeGenF64(void) noexcept { |
597 | 0 | return genWasmEdge_ValType(ValType(TypeCode::F64)); |
598 | 0 | } |
599 | | |
600 | 0 | WASMEDGE_CAPI_EXPORT WasmEdge_ValType WasmEdge_ValTypeGenV128(void) noexcept { |
601 | 0 | return genWasmEdge_ValType(ValType(TypeCode::V128)); |
602 | 0 | } |
603 | | |
604 | | WASMEDGE_CAPI_EXPORT WasmEdge_ValType |
605 | 0 | WasmEdge_ValTypeGenFuncRef(void) noexcept { |
606 | 0 | return genWasmEdge_ValType(ValType(TypeCode::FuncRef)); |
607 | 0 | } |
608 | | |
609 | | WASMEDGE_CAPI_EXPORT WasmEdge_ValType |
610 | 0 | WasmEdge_ValTypeGenExternRef(void) noexcept { |
611 | 0 | return genWasmEdge_ValType(ValType(TypeCode::ExternRef)); |
612 | 0 | } |
613 | | |
614 | | WASMEDGE_CAPI_EXPORT bool |
615 | | WasmEdge_ValTypeIsEqual(const WasmEdge_ValType ValType1, |
616 | 0 | const WasmEdge_ValType ValType2) noexcept { |
617 | 0 | return genValType(ValType1) == genValType(ValType2); |
618 | 0 | } |
619 | | |
620 | | WASMEDGE_CAPI_EXPORT bool |
621 | 0 | WasmEdge_ValTypeIsI32(const WasmEdge_ValType ValType) noexcept { |
622 | 0 | return genValType(ValType).getCode() == WasmEdge::TypeCode::I32; |
623 | 0 | } |
624 | | |
625 | | WASMEDGE_CAPI_EXPORT bool |
626 | 0 | WasmEdge_ValTypeIsI64(const WasmEdge_ValType ValType) noexcept { |
627 | 0 | return genValType(ValType).getCode() == WasmEdge::TypeCode::I64; |
628 | 0 | } |
629 | | |
630 | | WASMEDGE_CAPI_EXPORT bool |
631 | 0 | WasmEdge_ValTypeIsF32(const WasmEdge_ValType ValType) noexcept { |
632 | 0 | return genValType(ValType).getCode() == WasmEdge::TypeCode::F32; |
633 | 0 | } |
634 | | |
635 | | WASMEDGE_CAPI_EXPORT bool |
636 | 0 | WasmEdge_ValTypeIsF64(const WasmEdge_ValType ValType) noexcept { |
637 | 0 | return genValType(ValType).getCode() == WasmEdge::TypeCode::F64; |
638 | 0 | } |
639 | | |
640 | | WASMEDGE_CAPI_EXPORT bool |
641 | 0 | WasmEdge_ValTypeIsV128(const WasmEdge_ValType ValType) noexcept { |
642 | 0 | return genValType(ValType).getCode() == WasmEdge::TypeCode::V128; |
643 | 0 | } |
644 | | |
645 | | WASMEDGE_CAPI_EXPORT bool |
646 | 0 | WasmEdge_ValTypeIsFuncRef(const WasmEdge_ValType ValType) noexcept { |
647 | 0 | return genValType(ValType).isFuncRefType(); |
648 | 0 | } |
649 | | |
650 | | WASMEDGE_CAPI_EXPORT bool |
651 | 0 | WasmEdge_ValTypeIsExternRef(const WasmEdge_ValType ValType) noexcept { |
652 | 0 | return genValType(ValType).isExternRefType(); |
653 | 0 | } |
654 | | |
655 | | WASMEDGE_CAPI_EXPORT bool |
656 | 0 | WasmEdge_ValTypeIsRef(const WasmEdge_ValType ValType) noexcept { |
657 | 0 | return genValType(ValType).isRefType(); |
658 | 0 | } |
659 | | |
660 | | WASMEDGE_CAPI_EXPORT bool |
661 | 0 | WasmEdge_ValTypeIsRefNull(const WasmEdge_ValType ValType) noexcept { |
662 | 0 | return genValType(ValType).isNullableRefType(); |
663 | 0 | } |
664 | | |
665 | | // <<<<<<<< WasmEdge valtype functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
666 | | |
667 | | // >>>>>>>> WasmEdge value functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> |
668 | | |
669 | | WASMEDGE_CAPI_EXPORT WasmEdge_Value |
670 | 0 | WasmEdge_ValueGenI32(const int32_t Val) noexcept { |
671 | 0 | return genWasmEdge_Value(Val); |
672 | 0 | } |
673 | | |
674 | | WASMEDGE_CAPI_EXPORT WasmEdge_Value |
675 | 0 | WasmEdge_ValueGenI64(const int64_t Val) noexcept { |
676 | 0 | return genWasmEdge_Value(Val); |
677 | 0 | } |
678 | | |
679 | | WASMEDGE_CAPI_EXPORT WasmEdge_Value |
680 | 0 | WasmEdge_ValueGenF32(const float Val) noexcept { |
681 | 0 | return genWasmEdge_Value(Val); |
682 | 0 | } |
683 | | |
684 | | WASMEDGE_CAPI_EXPORT WasmEdge_Value |
685 | 0 | WasmEdge_ValueGenF64(const double Val) noexcept { |
686 | 0 | return genWasmEdge_Value(Val); |
687 | 0 | } |
688 | | |
689 | | WASMEDGE_CAPI_EXPORT WasmEdge_Value |
690 | 0 | WasmEdge_ValueGenV128(const ::int128_t Val) noexcept { |
691 | 0 | return genWasmEdge_Value(to_WasmEdge_128_t<WasmEdge::int128_t>(Val)); |
692 | 0 | } |
693 | | |
694 | | WASMEDGE_CAPI_EXPORT WasmEdge_Value |
695 | 0 | WasmEdge_ValueGenFuncRef(const WasmEdge_FunctionInstanceContext *Cxt) noexcept { |
696 | 0 | return genWasmEdge_Value(WasmEdge::RefVariant(fromFuncCxt(Cxt)), |
697 | 0 | TypeCode::FuncRef); |
698 | 0 | } |
699 | | |
700 | | WASMEDGE_CAPI_EXPORT WasmEdge_Value |
701 | 0 | WasmEdge_ValueGenExternRef(void *Ref) noexcept { |
702 | 0 | return genWasmEdge_Value(WasmEdge::RefVariant(Ref), TypeCode::ExternRef); |
703 | 0 | } |
704 | | |
705 | | WASMEDGE_CAPI_EXPORT int32_t |
706 | 0 | WasmEdge_ValueGetI32(const WasmEdge_Value Val) noexcept { |
707 | 0 | return WasmEdge::ValVariant::wrap<int32_t>( |
708 | 0 | to_WasmEdge_128_t<WasmEdge::uint128_t>(Val.Value)) |
709 | 0 | .get<int32_t>(); |
710 | 0 | } |
711 | | |
712 | | WASMEDGE_CAPI_EXPORT int64_t |
713 | 0 | WasmEdge_ValueGetI64(const WasmEdge_Value Val) noexcept { |
714 | 0 | return WasmEdge::ValVariant::wrap<int64_t>( |
715 | 0 | to_WasmEdge_128_t<WasmEdge::uint128_t>(Val.Value)) |
716 | 0 | .get<int64_t>(); |
717 | 0 | } |
718 | | |
719 | | WASMEDGE_CAPI_EXPORT float |
720 | 0 | WasmEdge_ValueGetF32(const WasmEdge_Value Val) noexcept { |
721 | 0 | return WasmEdge::ValVariant::wrap<float>( |
722 | 0 | to_WasmEdge_128_t<WasmEdge::uint128_t>(Val.Value)) |
723 | 0 | .get<float>(); |
724 | 0 | } |
725 | | |
726 | | WASMEDGE_CAPI_EXPORT double |
727 | 0 | WasmEdge_ValueGetF64(const WasmEdge_Value Val) noexcept { |
728 | 0 | return WasmEdge::ValVariant::wrap<double>( |
729 | 0 | to_WasmEdge_128_t<WasmEdge::uint128_t>(Val.Value)) |
730 | 0 | .get<double>(); |
731 | 0 | } |
732 | | |
733 | | WASMEDGE_CAPI_EXPORT ::int128_t |
734 | 0 | WasmEdge_ValueGetV128(const WasmEdge_Value Val) noexcept { |
735 | 0 | return to_int128_t(WasmEdge::ValVariant::wrap<WasmEdge::int128_t>( |
736 | 0 | to_WasmEdge_128_t<WasmEdge::uint128_t>(Val.Value)) |
737 | 0 | .get<WasmEdge::int128_t>()); |
738 | 0 | } |
739 | | |
740 | | WASMEDGE_CAPI_EXPORT bool |
741 | 0 | WasmEdge_ValueIsNullRef(const WasmEdge_Value Val) noexcept { |
742 | 0 | return WasmEdge::ValVariant::wrap<WasmEdge::RefVariant>( |
743 | 0 | to_WasmEdge_128_t<WasmEdge::uint128_t>(Val.Value)) |
744 | 0 | .get<WasmEdge::RefVariant>() |
745 | 0 | .isNull(); |
746 | 0 | } |
747 | | |
748 | | WASMEDGE_CAPI_EXPORT const WasmEdge_FunctionInstanceContext * |
749 | 0 | WasmEdge_ValueGetFuncRef(const WasmEdge_Value Val) noexcept { |
750 | 0 | return toFuncCxt(WasmEdge::retrieveFuncRef( |
751 | 0 | WasmEdge::ValVariant::wrap<WasmEdge::RefVariant>( |
752 | 0 | to_WasmEdge_128_t<WasmEdge::uint128_t>(Val.Value)) |
753 | 0 | .get<WasmEdge::RefVariant>() |
754 | 0 | .getPtr<WasmEdge::Runtime::Instance::FunctionInstance>())); |
755 | 0 | } |
756 | | |
757 | | WASMEDGE_CAPI_EXPORT void * |
758 | 0 | WasmEdge_ValueGetExternRef(const WasmEdge_Value Val) noexcept { |
759 | 0 | return &WasmEdge::retrieveExternRef<uint32_t>( |
760 | 0 | WasmEdge::ValVariant::wrap<WasmEdge::RefVariant>( |
761 | 0 | to_WasmEdge_128_t<WasmEdge::uint128_t>(Val.Value)) |
762 | 0 | .get<WasmEdge::RefVariant>()); |
763 | 0 | } |
764 | | |
765 | | // <<<<<<<< WasmEdge value functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
766 | | |
767 | | // >>>>>>>> WasmEdge string functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> |
768 | | |
769 | | WASMEDGE_CAPI_EXPORT WasmEdge_String |
770 | 0 | WasmEdge_StringCreateByCString(const char *Str) noexcept { |
771 | 0 | if (Str) { |
772 | 0 | return WasmEdge_StringCreateByBuffer( |
773 | 0 | Str, static_cast<uint32_t>(std::strlen(Str))); |
774 | 0 | } |
775 | 0 | return WasmEdge_String{/* Length */ 0, /* Buf */ nullptr}; |
776 | 0 | } |
777 | | |
778 | | WASMEDGE_CAPI_EXPORT WasmEdge_String |
779 | 0 | WasmEdge_StringCreateByBuffer(const char *Buf, const uint32_t Len) noexcept { |
780 | 0 | try { |
781 | 0 | if (Buf && Len) { |
782 | 0 | char *Str = new char[Len]; |
783 | 0 | std::copy_n(Buf, Len, Str); |
784 | 0 | return WasmEdge_String{/* Length */ Len, /* Buf */ Str}; |
785 | 0 | } |
786 | 0 | } catch (...) { |
787 | 0 | handleCAPIError(); |
788 | 0 | } |
789 | 0 | return WasmEdge_String{/* Length */ 0, /* Buf */ nullptr}; |
790 | 0 | } |
791 | | |
792 | | WASMEDGE_CAPI_EXPORT WasmEdge_String |
793 | 0 | WasmEdge_StringWrap(const char *Buf, const uint32_t Len) noexcept { |
794 | 0 | return WasmEdge_String{/* Length */ Len, /* Buf */ Buf}; |
795 | 0 | } |
796 | | |
797 | | WASMEDGE_CAPI_EXPORT bool |
798 | | WasmEdge_StringIsEqual(const WasmEdge_String Str1, |
799 | 0 | const WasmEdge_String Str2) noexcept { |
800 | 0 | if (Str1.Length != Str2.Length) { |
801 | 0 | return false; |
802 | 0 | } |
803 | 0 | return std::equal(Str1.Buf, Str1.Buf + Str1.Length, Str2.Buf); |
804 | 0 | } |
805 | | |
806 | | WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_StringCopy(const WasmEdge_String Str, |
807 | | char *Buf, |
808 | 0 | const uint32_t Len) noexcept { |
809 | 0 | uint32_t RealLength = std::min(Len, Str.Length); |
810 | 0 | if (Buf) { |
811 | 0 | std::memset(Buf, 0, Len); |
812 | 0 | if (RealLength > 0) { |
813 | 0 | std::copy_n(Str.Buf, RealLength, Buf); |
814 | 0 | } |
815 | 0 | } |
816 | 0 | return RealLength; |
817 | 0 | } |
818 | | |
819 | 0 | WASMEDGE_CAPI_EXPORT void WasmEdge_StringDelete(WasmEdge_String Str) noexcept { |
820 | 0 | if (Str.Buf) { |
821 | 0 | delete[] Str.Buf; |
822 | 0 | } |
823 | 0 | } |
824 | | |
825 | | // <<<<<<<< WasmEdge string functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
826 | | |
827 | | // >>>>>>>> WasmEdge bytes functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> |
828 | | |
829 | | WASMEDGE_CAPI_EXPORT WasmEdge_Bytes |
830 | 0 | WasmEdge_BytesCreate(const uint8_t *Buf, const uint32_t Len) noexcept { |
831 | 0 | try { |
832 | 0 | if (Buf && Len) { |
833 | 0 | uint8_t *Str = new uint8_t[Len]; |
834 | 0 | std::copy_n(Buf, Len, Str); |
835 | 0 | return WasmEdge_Bytes{/* Length */ Len, /* Buf */ Str}; |
836 | 0 | } |
837 | 0 | } catch (...) { |
838 | 0 | handleCAPIError(); |
839 | 0 | } |
840 | 0 | return WasmEdge_Bytes{/* Length */ 0, /* Buf */ nullptr}; |
841 | 0 | } |
842 | | |
843 | | WASMEDGE_CAPI_EXPORT WasmEdge_Bytes |
844 | 0 | WasmEdge_BytesWrap(const uint8_t *Buf, const uint32_t Len) noexcept { |
845 | 0 | return WasmEdge_Bytes{/* Length */ Len, /* Buf */ Buf}; |
846 | 0 | } |
847 | | |
848 | 0 | WASMEDGE_CAPI_EXPORT void WasmEdge_BytesDelete(WasmEdge_Bytes Bytes) noexcept { |
849 | 0 | if (Bytes.Buf) { |
850 | 0 | delete[] Bytes.Buf; |
851 | 0 | } |
852 | 0 | } |
853 | | |
854 | | // <<<<<<<< WasmEdge bytes functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
855 | | |
856 | | // >>>>>>>> WasmEdge result functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> |
857 | | |
858 | | WASMEDGE_CAPI_EXPORT bool |
859 | 0 | WasmEdge_ResultOK(const WasmEdge_Result Res) noexcept { |
860 | 0 | if (WasmEdge_ResultGetCategory(Res) == WasmEdge_ErrCategory_WASM && |
861 | 0 | (static_cast<WasmEdge::ErrCode::Value>(WasmEdge_ResultGetCode(Res)) == |
862 | 0 | WasmEdge::ErrCode::Value::Success || |
863 | 0 | static_cast<WasmEdge::ErrCode::Value>(WasmEdge_ResultGetCode(Res)) == |
864 | 0 | WasmEdge::ErrCode::Value::Terminated)) { |
865 | 0 | return true; |
866 | 0 | } else { |
867 | 0 | return false; |
868 | 0 | } |
869 | 0 | } |
870 | | |
871 | | WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_ResultGen( |
872 | 0 | const enum WasmEdge_ErrCategory Category, const uint32_t Code) noexcept { |
873 | 0 | return WasmEdge_Result{/* Code */ (static_cast<uint32_t>(Category) << 24) + |
874 | 0 | (Code & 0x00FFFFFFU)}; |
875 | 0 | } |
876 | | |
877 | | WASMEDGE_CAPI_EXPORT uint32_t |
878 | 0 | WasmEdge_ResultGetCode(const WasmEdge_Result Res) noexcept { |
879 | 0 | return Res.Code & 0x00FFFFFFU; |
880 | 0 | } |
881 | | |
882 | | WASMEDGE_CAPI_EXPORT WasmEdge_ErrCategory |
883 | 0 | WasmEdge_ResultGetCategory(const WasmEdge_Result Res) noexcept { |
884 | 0 | return static_cast<WasmEdge_ErrCategory>(Res.Code >> 24); |
885 | 0 | } |
886 | | |
887 | | WASMEDGE_CAPI_EXPORT const char * |
888 | 0 | WasmEdge_ResultGetMessage(const WasmEdge_Result Res) noexcept { |
889 | 0 | if (WasmEdge_ResultGetCategory(Res) != WasmEdge_ErrCategory_WASM) { |
890 | 0 | return WasmEdge::ErrCodeStr[WasmEdge::ErrCode::Value::UserDefError].data(); |
891 | 0 | } |
892 | 0 | return WasmEdge::ErrCodeStr[static_cast<WasmEdge::ErrCode::Value>( |
893 | 0 | WasmEdge_ResultGetCode(Res))] |
894 | 0 | .data(); |
895 | 0 | } |
896 | | |
897 | | // <<<<<<<< WasmEdge result functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
898 | | |
899 | | // >>>>>>>> WasmEdge configure functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> |
900 | | |
901 | | WASMEDGE_CAPI_EXPORT WasmEdge_ConfigureContext * |
902 | 0 | WasmEdge_ConfigureCreate(void) noexcept { |
903 | 0 | try { |
904 | 0 | return new WasmEdge_ConfigureContext; |
905 | 0 | } catch (...) { |
906 | 0 | handleCAPIError(); |
907 | 0 | return nullptr; |
908 | 0 | } |
909 | 0 | } |
910 | | |
911 | | WASMEDGE_CAPI_EXPORT void |
912 | | WasmEdge_ConfigureAddProposal(WasmEdge_ConfigureContext *Cxt, |
913 | 0 | const enum WasmEdge_Proposal Prop) noexcept { |
914 | 0 | if (Cxt) { |
915 | 0 | Cxt->Conf.addProposal(static_cast<WasmEdge::Proposal>(Prop)); |
916 | 0 | } |
917 | 0 | } |
918 | | |
919 | | WASMEDGE_CAPI_EXPORT void |
920 | | WasmEdge_ConfigureRemoveProposal(WasmEdge_ConfigureContext *Cxt, |
921 | 0 | const enum WasmEdge_Proposal Prop) noexcept { |
922 | 0 | if (Cxt) { |
923 | 0 | Cxt->Conf.removeProposal(static_cast<WasmEdge::Proposal>(Prop)); |
924 | 0 | } |
925 | 0 | } |
926 | | |
927 | | WASMEDGE_CAPI_EXPORT bool |
928 | | WasmEdge_ConfigureHasProposal(const WasmEdge_ConfigureContext *Cxt, |
929 | 0 | const enum WasmEdge_Proposal Prop) noexcept { |
930 | 0 | if (Cxt) { |
931 | 0 | return Cxt->Conf.hasProposal(static_cast<WasmEdge::Proposal>(Prop)); |
932 | 0 | } |
933 | 0 | return false; |
934 | 0 | } |
935 | | |
936 | | WASMEDGE_CAPI_EXPORT void |
937 | | WasmEdge_ConfigureSetWASMStandard(WasmEdge_ConfigureContext *Cxt, |
938 | 0 | const enum WasmEdge_Standard Std) noexcept { |
939 | 0 | if (Cxt) { |
940 | 0 | Cxt->Conf.setWASMStandard(static_cast<WasmEdge::Standard>(Std)); |
941 | 0 | } |
942 | 0 | } |
943 | | |
944 | | WASMEDGE_CAPI_EXPORT void WasmEdge_ConfigureAddHostRegistration( |
945 | | WasmEdge_ConfigureContext *Cxt, |
946 | 0 | const enum WasmEdge_HostRegistration Host) noexcept { |
947 | 0 | if (Cxt) { |
948 | 0 | Cxt->Conf.addHostRegistration( |
949 | 0 | static_cast<WasmEdge::HostRegistration>(Host)); |
950 | 0 | } |
951 | 0 | } |
952 | | |
953 | | WASMEDGE_CAPI_EXPORT void WasmEdge_ConfigureRemoveHostRegistration( |
954 | | WasmEdge_ConfigureContext *Cxt, |
955 | 0 | const enum WasmEdge_HostRegistration Host) noexcept { |
956 | 0 | if (Cxt) { |
957 | 0 | Cxt->Conf.removeHostRegistration( |
958 | 0 | static_cast<WasmEdge::HostRegistration>(Host)); |
959 | 0 | } |
960 | 0 | } |
961 | | |
962 | | WASMEDGE_CAPI_EXPORT bool WasmEdge_ConfigureHasHostRegistration( |
963 | | const WasmEdge_ConfigureContext *Cxt, |
964 | 0 | const enum WasmEdge_HostRegistration Host) noexcept { |
965 | 0 | if (Cxt) { |
966 | 0 | return Cxt->Conf.hasHostRegistration( |
967 | 0 | static_cast<WasmEdge::HostRegistration>(Host)); |
968 | 0 | } |
969 | 0 | return false; |
970 | 0 | } |
971 | | |
972 | | WASMEDGE_CAPI_EXPORT void |
973 | | WasmEdge_ConfigureSetMaxMemoryPage(WasmEdge_ConfigureContext *Cxt, |
974 | 0 | const uint64_t Page) noexcept { |
975 | 0 | if (Cxt) { |
976 | 0 | Cxt->Conf.getRuntimeConfigure().setMaxMemoryPage(Page); |
977 | 0 | } |
978 | 0 | } |
979 | | |
980 | | WASMEDGE_CAPI_EXPORT uint64_t WasmEdge_ConfigureGetMaxMemoryPage( |
981 | 0 | const WasmEdge_ConfigureContext *Cxt) noexcept { |
982 | 0 | if (Cxt) { |
983 | 0 | return Cxt->Conf.getRuntimeConfigure().getMaxMemoryPage(); |
984 | 0 | } |
985 | 0 | return 0; |
986 | 0 | } |
987 | | |
988 | | WASMEDGE_CAPI_EXPORT void |
989 | | WasmEdge_ConfigureSetRunMode(WasmEdge_ConfigureContext *Cxt, |
990 | 0 | const enum WasmEdge_RunMode Mode) noexcept { |
991 | 0 | if (Cxt) { |
992 | 0 | Cxt->Conf.getRuntimeConfigure().setRunMode( |
993 | 0 | static_cast<WasmEdge::RunMode>(Mode)); |
994 | 0 | } |
995 | 0 | } |
996 | | |
997 | | WASMEDGE_CAPI_EXPORT enum WasmEdge_RunMode |
998 | 0 | WasmEdge_ConfigureGetRunMode(const WasmEdge_ConfigureContext *Cxt) noexcept { |
999 | 0 | if (Cxt) { |
1000 | 0 | return static_cast<WasmEdge_RunMode>( |
1001 | 0 | Cxt->Conf.getRuntimeConfigure().getRunMode()); |
1002 | 0 | } |
1003 | 0 | return WasmEdge_RunMode_Interpreter; |
1004 | 0 | } |
1005 | | |
1006 | | WASMEDGE_CAPI_EXPORT void |
1007 | | WasmEdge_ConfigureSetForceInterpreter(WasmEdge_ConfigureContext *Cxt, |
1008 | 0 | const bool IsForceInterpreter) noexcept { |
1009 | 0 | if (Cxt) { |
1010 | 0 | if (IsForceInterpreter) { |
1011 | 0 | Cxt->Conf.getRuntimeConfigure().setRunMode( |
1012 | 0 | WasmEdge::RunMode::Interpreter); |
1013 | 0 | } |
1014 | | // Passing `false` is a no-op, preserving the historical "don't force" |
1015 | | // semantic. |
1016 | 0 | } |
1017 | 0 | } |
1018 | | |
1019 | | WASMEDGE_CAPI_EXPORT void |
1020 | | WasmEdge_ConfigureSetAllowAFUNIX(WasmEdge_ConfigureContext *Cxt, |
1021 | 0 | const bool EnableAFUNIX) noexcept { |
1022 | 0 | if (Cxt) { |
1023 | 0 | Cxt->Conf.getRuntimeConfigure().setAllowAFUNIX(EnableAFUNIX); |
1024 | 0 | } |
1025 | 0 | } |
1026 | | |
1027 | | WASMEDGE_CAPI_EXPORT bool |
1028 | 0 | WasmEdge_ConfigureIsAllowAFUNIX(const WasmEdge_ConfigureContext *Cxt) noexcept { |
1029 | 0 | if (Cxt) { |
1030 | 0 | return Cxt->Conf.getRuntimeConfigure().isAllowAFUNIX(); |
1031 | 0 | } |
1032 | 0 | return false; |
1033 | 0 | } |
1034 | | |
1035 | | WASMEDGE_CAPI_EXPORT bool WasmEdge_ConfigureIsForceInterpreter( |
1036 | 0 | const WasmEdge_ConfigureContext *Cxt) noexcept { |
1037 | 0 | if (Cxt) { |
1038 | 0 | return Cxt->Conf.getRuntimeConfigure().getRunMode() == |
1039 | 0 | WasmEdge::RunMode::Interpreter; |
1040 | 0 | } |
1041 | 0 | return false; |
1042 | 0 | } |
1043 | | |
1044 | | WASMEDGE_CAPI_EXPORT void WasmEdge_ConfigureCompilerSetOptimizationLevel( |
1045 | | WasmEdge_ConfigureContext *Cxt, |
1046 | 0 | const enum WasmEdge_CompilerOptimizationLevel Level) noexcept { |
1047 | 0 | if (Cxt) { |
1048 | 0 | Cxt->Conf.getCompilerConfigure().setOptimizationLevel( |
1049 | 0 | static_cast<WasmEdge::CompilerConfigure::OptimizationLevel>(Level)); |
1050 | 0 | } |
1051 | 0 | } |
1052 | | |
1053 | | WASMEDGE_CAPI_EXPORT enum WasmEdge_CompilerOptimizationLevel |
1054 | | WasmEdge_ConfigureCompilerGetOptimizationLevel( |
1055 | 0 | const WasmEdge_ConfigureContext *Cxt) noexcept { |
1056 | 0 | if (Cxt) { |
1057 | 0 | return static_cast<WasmEdge_CompilerOptimizationLevel>( |
1058 | 0 | Cxt->Conf.getCompilerConfigure().getOptimizationLevel()); |
1059 | 0 | } |
1060 | 0 | return WasmEdge_CompilerOptimizationLevel_O0; |
1061 | 0 | } |
1062 | | |
1063 | | WASMEDGE_CAPI_EXPORT void WasmEdge_ConfigureCompilerSetOutputFormat( |
1064 | | WasmEdge_ConfigureContext *Cxt, |
1065 | 0 | const enum WasmEdge_CompilerOutputFormat Format) noexcept { |
1066 | 0 | if (Cxt) { |
1067 | 0 | Cxt->Conf.getCompilerConfigure().setOutputFormat( |
1068 | 0 | static_cast<WasmEdge::CompilerConfigure::OutputFormat>(Format)); |
1069 | 0 | } |
1070 | 0 | } |
1071 | | |
1072 | | WASMEDGE_CAPI_EXPORT enum WasmEdge_CompilerOutputFormat |
1073 | | WasmEdge_ConfigureCompilerGetOutputFormat( |
1074 | 0 | const WasmEdge_ConfigureContext *Cxt) noexcept { |
1075 | 0 | if (Cxt) { |
1076 | 0 | return static_cast<WasmEdge_CompilerOutputFormat>( |
1077 | 0 | Cxt->Conf.getCompilerConfigure().getOutputFormat()); |
1078 | 0 | } |
1079 | 0 | return WasmEdge_CompilerOutputFormat_Wasm; |
1080 | 0 | } |
1081 | | |
1082 | | WASMEDGE_CAPI_EXPORT void |
1083 | | WasmEdge_ConfigureCompilerSetDumpIR(WasmEdge_ConfigureContext *Cxt, |
1084 | 0 | const bool IsDump) noexcept { |
1085 | 0 | if (Cxt) { |
1086 | 0 | Cxt->Conf.getCompilerConfigure().setDumpIR(IsDump); |
1087 | 0 | } |
1088 | 0 | } |
1089 | | |
1090 | | WASMEDGE_CAPI_EXPORT bool WasmEdge_ConfigureCompilerIsDumpIR( |
1091 | 0 | const WasmEdge_ConfigureContext *Cxt) noexcept { |
1092 | 0 | if (Cxt) { |
1093 | 0 | return Cxt->Conf.getCompilerConfigure().isDumpIR(); |
1094 | 0 | } |
1095 | 0 | return false; |
1096 | 0 | } |
1097 | | |
1098 | | WASMEDGE_CAPI_EXPORT void |
1099 | | WasmEdge_ConfigureCompilerSetGenericBinary(WasmEdge_ConfigureContext *Cxt, |
1100 | 0 | const bool IsGeneric) noexcept { |
1101 | 0 | if (Cxt) { |
1102 | 0 | Cxt->Conf.getCompilerConfigure().setGenericBinary(IsGeneric); |
1103 | 0 | } |
1104 | 0 | } |
1105 | | |
1106 | | WASMEDGE_CAPI_EXPORT bool WasmEdge_ConfigureCompilerIsGenericBinary( |
1107 | 0 | const WasmEdge_ConfigureContext *Cxt) noexcept { |
1108 | 0 | if (Cxt) { |
1109 | 0 | return Cxt->Conf.getCompilerConfigure().isGenericBinary(); |
1110 | 0 | } |
1111 | 0 | return false; |
1112 | 0 | } |
1113 | | |
1114 | | WASMEDGE_CAPI_EXPORT void WasmEdge_ConfigureCompilerSetInterruptible( |
1115 | 0 | WasmEdge_ConfigureContext *Cxt, const bool IsInterruptible) noexcept { |
1116 | 0 | if (Cxt) { |
1117 | 0 | Cxt->Conf.getCompilerConfigure().setInterruptible(IsInterruptible); |
1118 | 0 | } |
1119 | 0 | } |
1120 | | |
1121 | | WASMEDGE_CAPI_EXPORT bool WasmEdge_ConfigureCompilerIsInterruptible( |
1122 | 0 | const WasmEdge_ConfigureContext *Cxt) noexcept { |
1123 | 0 | if (Cxt) { |
1124 | 0 | return Cxt->Conf.getCompilerConfigure().isInterruptible(); |
1125 | 0 | } |
1126 | 0 | return false; |
1127 | 0 | } |
1128 | | |
1129 | | WASMEDGE_CAPI_EXPORT void WasmEdge_ConfigureStatisticsSetInstructionCounting( |
1130 | 0 | WasmEdge_ConfigureContext *Cxt, const bool IsCount) noexcept { |
1131 | 0 | if (Cxt) { |
1132 | 0 | Cxt->Conf.getStatisticsConfigure().setInstructionCounting(IsCount); |
1133 | 0 | } |
1134 | 0 | } |
1135 | | |
1136 | | WASMEDGE_CAPI_EXPORT bool WasmEdge_ConfigureStatisticsIsInstructionCounting( |
1137 | 0 | const WasmEdge_ConfigureContext *Cxt) noexcept { |
1138 | 0 | if (Cxt) { |
1139 | 0 | return Cxt->Conf.getStatisticsConfigure().isInstructionCounting(); |
1140 | 0 | } |
1141 | 0 | return false; |
1142 | 0 | } |
1143 | | |
1144 | | WASMEDGE_CAPI_EXPORT void |
1145 | | WasmEdge_ConfigureStatisticsSetCostMeasuring(WasmEdge_ConfigureContext *Cxt, |
1146 | 0 | const bool IsMeasure) noexcept { |
1147 | 0 | if (Cxt) { |
1148 | 0 | Cxt->Conf.getStatisticsConfigure().setCostMeasuring(IsMeasure); |
1149 | 0 | } |
1150 | 0 | } |
1151 | | |
1152 | | WASMEDGE_CAPI_EXPORT bool WasmEdge_ConfigureStatisticsIsCostMeasuring( |
1153 | 0 | const WasmEdge_ConfigureContext *Cxt) noexcept { |
1154 | 0 | if (Cxt) { |
1155 | 0 | return Cxt->Conf.getStatisticsConfigure().isCostMeasuring(); |
1156 | 0 | } |
1157 | 0 | return false; |
1158 | 0 | } |
1159 | | |
1160 | | WASMEDGE_CAPI_EXPORT void |
1161 | | WasmEdge_ConfigureStatisticsSetTimeMeasuring(WasmEdge_ConfigureContext *Cxt, |
1162 | 0 | const bool IsMeasure) noexcept { |
1163 | 0 | if (Cxt) { |
1164 | 0 | Cxt->Conf.getStatisticsConfigure().setTimeMeasuring(IsMeasure); |
1165 | 0 | } |
1166 | 0 | } |
1167 | | |
1168 | | WASMEDGE_CAPI_EXPORT bool WasmEdge_ConfigureStatisticsIsTimeMeasuring( |
1169 | 0 | const WasmEdge_ConfigureContext *Cxt) noexcept { |
1170 | 0 | if (Cxt) { |
1171 | 0 | return Cxt->Conf.getStatisticsConfigure().isTimeMeasuring(); |
1172 | 0 | } |
1173 | 0 | return false; |
1174 | 0 | } |
1175 | | |
1176 | | WASMEDGE_CAPI_EXPORT void |
1177 | 0 | WasmEdge_ConfigureDelete(WasmEdge_ConfigureContext *Cxt) noexcept { |
1178 | 0 | delete Cxt; |
1179 | 0 | } |
1180 | | |
1181 | | // <<<<<<<< WasmEdge configure functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
1182 | | |
1183 | | // >>>>>>>> WasmEdge statistics functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> |
1184 | | |
1185 | | WASMEDGE_CAPI_EXPORT WasmEdge_StatisticsContext * |
1186 | 0 | WasmEdge_StatisticsCreate(void) noexcept { |
1187 | 0 | try { |
1188 | 0 | return toStatCxt(new WasmEdge::Statistics::Statistics); |
1189 | 0 | } catch (...) { |
1190 | 0 | handleCAPIError(); |
1191 | 0 | return nullptr; |
1192 | 0 | } |
1193 | 0 | } |
1194 | | |
1195 | | WASMEDGE_CAPI_EXPORT uint64_t WasmEdge_StatisticsGetInstrCount( |
1196 | 0 | const WasmEdge_StatisticsContext *Cxt) noexcept { |
1197 | 0 | if (Cxt) { |
1198 | 0 | return fromStatCxt(Cxt)->getInstrCount(); |
1199 | 0 | } |
1200 | 0 | return 0; |
1201 | 0 | } |
1202 | | |
1203 | | WASMEDGE_CAPI_EXPORT double WasmEdge_StatisticsGetInstrPerSecond( |
1204 | 0 | const WasmEdge_StatisticsContext *Cxt) noexcept { |
1205 | 0 | if (Cxt) { |
1206 | 0 | return fromStatCxt(Cxt)->getInstrPerSecond(); |
1207 | 0 | } |
1208 | 0 | return 0.0; |
1209 | 0 | } |
1210 | | |
1211 | | WASMEDGE_CAPI_EXPORT uint64_t WasmEdge_StatisticsGetTotalCost( |
1212 | 0 | const WasmEdge_StatisticsContext *Cxt) noexcept { |
1213 | 0 | if (Cxt) { |
1214 | 0 | return fromStatCxt(Cxt)->getTotalCost(); |
1215 | 0 | } |
1216 | 0 | return 0; |
1217 | 0 | } |
1218 | | |
1219 | | WASMEDGE_CAPI_EXPORT void |
1220 | | WasmEdge_StatisticsSetCostTable(WasmEdge_StatisticsContext *Cxt, |
1221 | | uint64_t *CostArr, |
1222 | 0 | const uint32_t Len) noexcept { |
1223 | 0 | if (Cxt) { |
1224 | 0 | fromStatCxt(Cxt)->setCostTable(genSpan(CostArr, Len)); |
1225 | 0 | } |
1226 | 0 | } |
1227 | | |
1228 | | WASMEDGE_CAPI_EXPORT void |
1229 | | WasmEdge_StatisticsSetCostLimit(WasmEdge_StatisticsContext *Cxt, |
1230 | 0 | const uint64_t Limit) noexcept { |
1231 | 0 | if (Cxt) { |
1232 | 0 | fromStatCxt(Cxt)->setCostLimit(Limit); |
1233 | 0 | } |
1234 | 0 | } |
1235 | | |
1236 | | WASMEDGE_CAPI_EXPORT void |
1237 | 0 | WasmEdge_StatisticsClear(WasmEdge_StatisticsContext *Cxt) noexcept { |
1238 | 0 | if (Cxt) { |
1239 | 0 | fromStatCxt(Cxt)->clear(); |
1240 | 0 | } |
1241 | 0 | } |
1242 | | |
1243 | | WASMEDGE_CAPI_EXPORT void |
1244 | 0 | WasmEdge_StatisticsDelete(WasmEdge_StatisticsContext *Cxt) noexcept { |
1245 | 0 | delete fromStatCxt(Cxt); |
1246 | 0 | } |
1247 | | |
1248 | | // <<<<<<<< WasmEdge statistics functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
1249 | | |
1250 | | // >>>>>>>> WasmEdge AST module functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> |
1251 | | |
1252 | | WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_ASTModuleListImportsLength( |
1253 | 0 | const WasmEdge_ASTModuleContext *Cxt) noexcept { |
1254 | 0 | if (Cxt) { |
1255 | 0 | return static_cast<uint32_t>( |
1256 | 0 | fromASTModCxt(Cxt)->getImportSection().getContent().size()); |
1257 | 0 | } |
1258 | 0 | return 0; |
1259 | 0 | } |
1260 | | |
1261 | | WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_ASTModuleListImports( |
1262 | | const WasmEdge_ASTModuleContext *Cxt, |
1263 | 0 | const WasmEdge_ImportTypeContext **Imports, const uint32_t Len) noexcept { |
1264 | 0 | if (Cxt) { |
1265 | 0 | const auto &ImpSec = fromASTModCxt(Cxt)->getImportSection().getContent(); |
1266 | 0 | if (Imports) { |
1267 | 0 | for (uint32_t I = 0; I < Len && I < ImpSec.size(); I++) { |
1268 | 0 | Imports[I] = toImpTypeCxt(&ImpSec[I]); |
1269 | 0 | } |
1270 | 0 | } |
1271 | 0 | return static_cast<uint32_t>(ImpSec.size()); |
1272 | 0 | } |
1273 | 0 | return 0; |
1274 | 0 | } |
1275 | | |
1276 | | WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_ASTModuleListExportsLength( |
1277 | 0 | const WasmEdge_ASTModuleContext *Cxt) noexcept { |
1278 | 0 | if (Cxt) { |
1279 | 0 | return static_cast<uint32_t>( |
1280 | 0 | fromASTModCxt(Cxt)->getExportSection().getContent().size()); |
1281 | 0 | } |
1282 | 0 | return 0; |
1283 | 0 | } |
1284 | | |
1285 | | WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_ASTModuleListExports( |
1286 | | const WasmEdge_ASTModuleContext *Cxt, |
1287 | 0 | const WasmEdge_ExportTypeContext **Exports, const uint32_t Len) noexcept { |
1288 | 0 | if (Cxt) { |
1289 | 0 | const auto &ExpSec = fromASTModCxt(Cxt)->getExportSection().getContent(); |
1290 | 0 | if (Exports) { |
1291 | 0 | for (uint32_t I = 0; I < Len && I < ExpSec.size(); I++) { |
1292 | 0 | Exports[I] = toExpTypeCxt(&ExpSec[I]); |
1293 | 0 | } |
1294 | 0 | } |
1295 | 0 | return static_cast<uint32_t>(ExpSec.size()); |
1296 | 0 | } |
1297 | 0 | return 0; |
1298 | 0 | } |
1299 | | |
1300 | | WASMEDGE_CAPI_EXPORT void |
1301 | 0 | WasmEdge_ASTModuleDelete(WasmEdge_ASTModuleContext *Cxt) noexcept { |
1302 | 0 | std::unique_ptr<WasmEdge::AST::Module> Own(fromASTModCxt(Cxt)); |
1303 | 0 | } |
1304 | | |
1305 | | // <<<<<<<< WasmEdge AST module functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
1306 | | |
1307 | | // >>>>>>>> WasmEdge limit functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> |
1308 | | |
1309 | | WASMEDGE_CAPI_EXPORT WasmEdge_LimitContext * |
1310 | 0 | WasmEdge_LimitCreate(const uint64_t Min, const bool Is64Bit) noexcept { |
1311 | 0 | try { |
1312 | 0 | return toLimitCxt(new WasmEdge::AST::Limit(Min, Is64Bit)); |
1313 | 0 | } catch (...) { |
1314 | 0 | handleCAPIError(); |
1315 | 0 | return nullptr; |
1316 | 0 | } |
1317 | 0 | } |
1318 | | |
1319 | | WASMEDGE_CAPI_EXPORT WasmEdge_LimitContext * |
1320 | | WasmEdge_LimitCreateWithMax(const uint64_t Min, const uint64_t Max, |
1321 | 0 | const bool Is64Bit, const bool IsShared) noexcept { |
1322 | 0 | try { |
1323 | 0 | return toLimitCxt(new WasmEdge::AST::Limit(Min, Max, Is64Bit, IsShared)); |
1324 | 0 | } catch (...) { |
1325 | 0 | handleCAPIError(); |
1326 | 0 | return nullptr; |
1327 | 0 | } |
1328 | 0 | } |
1329 | | |
1330 | | WASMEDGE_CAPI_EXPORT uint64_t |
1331 | 0 | WasmEdge_LimitGetMin(const WasmEdge_LimitContext *Cxt) noexcept { |
1332 | 0 | if (Cxt) { |
1333 | 0 | return fromLimitCxt(Cxt)->getMin(); |
1334 | 0 | } |
1335 | 0 | return 0; |
1336 | 0 | } |
1337 | | |
1338 | | WASMEDGE_CAPI_EXPORT uint64_t |
1339 | 0 | WasmEdge_LimitGetMax(const WasmEdge_LimitContext *Cxt) noexcept { |
1340 | 0 | if (Cxt) { |
1341 | 0 | return fromLimitCxt(Cxt)->getMax(); |
1342 | 0 | } |
1343 | 0 | return 0; |
1344 | 0 | } |
1345 | | |
1346 | | WASMEDGE_CAPI_EXPORT bool |
1347 | 0 | WasmEdge_LimitHasMax(const WasmEdge_LimitContext *Cxt) noexcept { |
1348 | 0 | if (Cxt) { |
1349 | 0 | return fromLimitCxt(Cxt)->hasMax(); |
1350 | 0 | } |
1351 | 0 | return false; |
1352 | 0 | } |
1353 | | |
1354 | | WASMEDGE_CAPI_EXPORT bool |
1355 | 0 | WasmEdge_LimitIsShared(const WasmEdge_LimitContext *Cxt) noexcept { |
1356 | 0 | if (Cxt) { |
1357 | 0 | return fromLimitCxt(Cxt)->isShared(); |
1358 | 0 | } |
1359 | 0 | return false; |
1360 | 0 | } |
1361 | | |
1362 | | WASMEDGE_CAPI_EXPORT bool |
1363 | 0 | WasmEdge_LimitIs64Bit(const WasmEdge_LimitContext *Cxt) noexcept { |
1364 | 0 | if (Cxt) { |
1365 | 0 | return fromLimitCxt(Cxt)->is64(); |
1366 | 0 | } |
1367 | 0 | return false; |
1368 | 0 | } |
1369 | | |
1370 | | WASMEDGE_CAPI_EXPORT bool |
1371 | | WasmEdge_LimitIsEqual(const WasmEdge_LimitContext *Cxt1, |
1372 | 0 | const WasmEdge_LimitContext *Cxt2) noexcept { |
1373 | 0 | if (Cxt1 && Cxt2) { |
1374 | 0 | const auto *Lim1 = fromLimitCxt(Cxt1); |
1375 | 0 | const auto *Lim2 = fromLimitCxt(Cxt2); |
1376 | 0 | if (Lim1->hasMax() && Lim2->hasMax()) { |
1377 | 0 | return Lim1->getMin() == Lim2->getMin() && |
1378 | 0 | Lim1->getMax() == Lim2->getMax() && |
1379 | 0 | Lim1->getAddrType() == Lim2->getAddrType() && |
1380 | 0 | Lim1->isShared() == Lim2->isShared(); |
1381 | 0 | } else { |
1382 | 0 | return Lim1->hasMax() == Lim2->hasMax() && |
1383 | 0 | Lim1->getMin() == Lim2->getMin() && |
1384 | 0 | Lim1->getAddrType() == Lim2->getAddrType() && |
1385 | 0 | Lim1->isShared() == Lim2->isShared(); |
1386 | 0 | } |
1387 | 0 | } |
1388 | 0 | return false; |
1389 | 0 | } |
1390 | | |
1391 | | WASMEDGE_CAPI_EXPORT void |
1392 | 0 | WasmEdge_LimitDelete(WasmEdge_LimitContext *Cxt) noexcept { |
1393 | 0 | delete fromLimitCxt(Cxt); |
1394 | 0 | } |
1395 | | |
1396 | | // <<<<<<<< WasmEdge limit functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
1397 | | |
1398 | | // >>>>>>>> WasmEdge function type functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> |
1399 | | |
1400 | | WASMEDGE_CAPI_EXPORT WasmEdge_FunctionTypeContext *WasmEdge_FunctionTypeCreate( |
1401 | | const WasmEdge_ValType *ParamList, const uint32_t ParamLen, |
1402 | 0 | const WasmEdge_ValType *ReturnList, const uint32_t ReturnLen) noexcept { |
1403 | 0 | try { |
1404 | 0 | auto *Cxt = new WasmEdge::AST::FunctionType; |
1405 | 0 | if (ParamLen > 0) { |
1406 | 0 | Cxt->getParamTypes().resize(ParamLen); |
1407 | 0 | } |
1408 | 0 | for (uint32_t I = 0; I < ParamLen; I++) { |
1409 | 0 | Cxt->getParamTypes()[I] = genValType(ParamList[I]); |
1410 | 0 | } |
1411 | 0 | if (ReturnLen > 0) { |
1412 | 0 | Cxt->getReturnTypes().resize(ReturnLen); |
1413 | 0 | } |
1414 | 0 | for (uint32_t I = 0; I < ReturnLen; I++) { |
1415 | 0 | Cxt->getReturnTypes()[I] = genValType(ReturnList[I]); |
1416 | 0 | } |
1417 | 0 | return toFuncTypeCxt(Cxt); |
1418 | 0 | } catch (...) { |
1419 | 0 | handleCAPIError(); |
1420 | 0 | return nullptr; |
1421 | 0 | } |
1422 | 0 | } |
1423 | | |
1424 | | WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_FunctionTypeGetParametersLength( |
1425 | 0 | const WasmEdge_FunctionTypeContext *Cxt) noexcept { |
1426 | 0 | if (Cxt) { |
1427 | 0 | return static_cast<uint32_t>(fromFuncTypeCxt(Cxt)->getParamTypes().size()); |
1428 | 0 | } |
1429 | 0 | return 0; |
1430 | 0 | } |
1431 | | |
1432 | | WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_FunctionTypeGetParameters( |
1433 | | const WasmEdge_FunctionTypeContext *Cxt, WasmEdge_ValType *List, |
1434 | 0 | const uint32_t Len) noexcept { |
1435 | 0 | if (Cxt) { |
1436 | 0 | for (uint32_t I = 0; |
1437 | 0 | I < fromFuncTypeCxt(Cxt)->getParamTypes().size() && I < Len; I++) { |
1438 | 0 | List[I] = genWasmEdge_ValType(fromFuncTypeCxt(Cxt)->getParamTypes()[I]); |
1439 | 0 | } |
1440 | 0 | return static_cast<uint32_t>(fromFuncTypeCxt(Cxt)->getParamTypes().size()); |
1441 | 0 | } |
1442 | 0 | return 0; |
1443 | 0 | } |
1444 | | |
1445 | | WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_FunctionTypeGetReturnsLength( |
1446 | 0 | const WasmEdge_FunctionTypeContext *Cxt) noexcept { |
1447 | 0 | if (Cxt) { |
1448 | 0 | return static_cast<uint32_t>(fromFuncTypeCxt(Cxt)->getReturnTypes().size()); |
1449 | 0 | } |
1450 | 0 | return 0; |
1451 | 0 | } |
1452 | | |
1453 | | WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_FunctionTypeGetReturns( |
1454 | | const WasmEdge_FunctionTypeContext *Cxt, WasmEdge_ValType *List, |
1455 | 0 | const uint32_t Len) noexcept { |
1456 | 0 | if (Cxt) { |
1457 | 0 | for (uint32_t I = 0; |
1458 | 0 | I < fromFuncTypeCxt(Cxt)->getReturnTypes().size() && I < Len; I++) { |
1459 | 0 | List[I] = genWasmEdge_ValType(fromFuncTypeCxt(Cxt)->getReturnTypes()[I]); |
1460 | 0 | } |
1461 | 0 | return static_cast<uint32_t>(fromFuncTypeCxt(Cxt)->getReturnTypes().size()); |
1462 | 0 | } |
1463 | 0 | return 0; |
1464 | 0 | } |
1465 | | |
1466 | | WASMEDGE_CAPI_EXPORT void |
1467 | 0 | WasmEdge_FunctionTypeDelete(WasmEdge_FunctionTypeContext *Cxt) noexcept { |
1468 | 0 | delete fromFuncTypeCxt(Cxt); |
1469 | 0 | } |
1470 | | |
1471 | | // <<<<<<<< WasmEdge function type functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
1472 | | |
1473 | | // >>>>>>>> WasmEdge table type functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> |
1474 | | |
1475 | | WASMEDGE_CAPI_EXPORT WasmEdge_TableTypeContext * |
1476 | | WasmEdge_TableTypeCreate(const WasmEdge_ValType RefType, |
1477 | 0 | const WasmEdge_LimitContext *Limit) noexcept { |
1478 | 0 | try { |
1479 | 0 | WasmEdge::ValType RT = genValType(RefType); |
1480 | 0 | if (Limit && RT.isRefType()) { |
1481 | 0 | return toTabTypeCxt( |
1482 | 0 | new WasmEdge::AST::TableType(RT, *fromLimitCxt(Limit))); |
1483 | 0 | } |
1484 | 0 | } catch (...) { |
1485 | 0 | handleCAPIError(); |
1486 | 0 | } |
1487 | 0 | return nullptr; |
1488 | 0 | } |
1489 | | |
1490 | | WASMEDGE_CAPI_EXPORT WasmEdge_ValType |
1491 | 0 | WasmEdge_TableTypeGetRefType(const WasmEdge_TableTypeContext *Cxt) noexcept { |
1492 | 0 | if (Cxt) { |
1493 | 0 | return genWasmEdge_ValType(fromTabTypeCxt(Cxt)->getRefType()); |
1494 | 0 | } |
1495 | 0 | return WasmEdge_ValTypeGenFuncRef(); |
1496 | 0 | } |
1497 | | |
1498 | | WASMEDGE_CAPI_EXPORT const WasmEdge_LimitContext * |
1499 | 0 | WasmEdge_TableTypeGetLimit(const WasmEdge_TableTypeContext *Cxt) noexcept { |
1500 | 0 | if (Cxt) { |
1501 | 0 | return toLimitCxt(&fromTabTypeCxt(Cxt)->getLimit()); |
1502 | 0 | } |
1503 | 0 | return nullptr; |
1504 | 0 | } |
1505 | | |
1506 | | WASMEDGE_CAPI_EXPORT void |
1507 | 0 | WasmEdge_TableTypeDelete(WasmEdge_TableTypeContext *Cxt) noexcept { |
1508 | 0 | delete fromTabTypeCxt(Cxt); |
1509 | 0 | } |
1510 | | |
1511 | | // <<<<<<<< WasmEdge table type functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
1512 | | |
1513 | | // >>>>>>>> WasmEdge memory type functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> |
1514 | | |
1515 | | WASMEDGE_CAPI_EXPORT WasmEdge_MemoryTypeContext * |
1516 | 0 | WasmEdge_MemoryTypeCreate(const WasmEdge_LimitContext *Limit) noexcept { |
1517 | 0 | try { |
1518 | 0 | if (Limit) { |
1519 | 0 | return toMemTypeCxt(new WasmEdge::AST::MemoryType(*fromLimitCxt(Limit))); |
1520 | 0 | } |
1521 | 0 | } catch (...) { |
1522 | 0 | handleCAPIError(); |
1523 | 0 | } |
1524 | 0 | return nullptr; |
1525 | 0 | } |
1526 | | |
1527 | | WASMEDGE_CAPI_EXPORT const WasmEdge_LimitContext * |
1528 | 0 | WasmEdge_MemoryTypeGetLimit(const WasmEdge_MemoryTypeContext *Cxt) noexcept { |
1529 | 0 | if (Cxt) { |
1530 | 0 | return toLimitCxt(&fromMemTypeCxt(Cxt)->getLimit()); |
1531 | 0 | } |
1532 | 0 | return nullptr; |
1533 | 0 | } |
1534 | | |
1535 | | WASMEDGE_CAPI_EXPORT void |
1536 | 0 | WasmEdge_MemoryTypeDelete(WasmEdge_MemoryTypeContext *Cxt) noexcept { |
1537 | 0 | delete fromMemTypeCxt(Cxt); |
1538 | 0 | } |
1539 | | |
1540 | | // <<<<<<<< WasmEdge memory type functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
1541 | | |
1542 | | // >>>>>>>> WasmEdge tag type functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> |
1543 | | |
1544 | | WASMEDGE_CAPI_EXPORT const WasmEdge_FunctionTypeContext * |
1545 | 0 | WasmEdge_TagTypeGetFunctionType(const WasmEdge_TagTypeContext *Cxt) noexcept { |
1546 | 0 | if (Cxt) { |
1547 | 0 | const auto &CompType = fromTagTypeCxt(Cxt)->getDefType().getCompositeType(); |
1548 | 0 | if (CompType.isFunc()) { |
1549 | 0 | return toFuncTypeCxt(&CompType.getFuncType()); |
1550 | 0 | } |
1551 | 0 | } |
1552 | 0 | return nullptr; |
1553 | 0 | } |
1554 | | |
1555 | | // <<<<<<<< WasmEdge tag type functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
1556 | | |
1557 | | // >>>>>>>> WasmEdge global type functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> |
1558 | | |
1559 | | WASMEDGE_CAPI_EXPORT WasmEdge_GlobalTypeContext * |
1560 | | WasmEdge_GlobalTypeCreate(const WasmEdge_ValType ValType, |
1561 | 0 | const enum WasmEdge_Mutability Mut) noexcept { |
1562 | 0 | try { |
1563 | 0 | return toGlobTypeCxt(new WasmEdge::AST::GlobalType( |
1564 | 0 | genValType(ValType), static_cast<WasmEdge::ValMut>(Mut))); |
1565 | 0 | } catch (...) { |
1566 | 0 | handleCAPIError(); |
1567 | 0 | return nullptr; |
1568 | 0 | } |
1569 | 0 | } |
1570 | | |
1571 | | WASMEDGE_CAPI_EXPORT WasmEdge_ValType |
1572 | 0 | WasmEdge_GlobalTypeGetValType(const WasmEdge_GlobalTypeContext *Cxt) noexcept { |
1573 | 0 | if (Cxt) { |
1574 | 0 | return genWasmEdge_ValType(fromGlobTypeCxt(Cxt)->getValType()); |
1575 | 0 | } |
1576 | 0 | return WasmEdge_ValTypeGenI32(); |
1577 | 0 | } |
1578 | | |
1579 | | WASMEDGE_CAPI_EXPORT enum WasmEdge_Mutability WasmEdge_GlobalTypeGetMutability( |
1580 | 0 | const WasmEdge_GlobalTypeContext *Cxt) noexcept { |
1581 | 0 | if (Cxt) { |
1582 | 0 | return static_cast<WasmEdge_Mutability>(fromGlobTypeCxt(Cxt)->getValMut()); |
1583 | 0 | } |
1584 | 0 | return WasmEdge_Mutability_Const; |
1585 | 0 | } |
1586 | | |
1587 | | WASMEDGE_CAPI_EXPORT void |
1588 | 0 | WasmEdge_GlobalTypeDelete(WasmEdge_GlobalTypeContext *Cxt) noexcept { |
1589 | 0 | delete fromGlobTypeCxt(Cxt); |
1590 | 0 | } |
1591 | | |
1592 | | // <<<<<<<< WasmEdge global type functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
1593 | | |
1594 | | // >>>>>>>> WasmEdge import type functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> |
1595 | | |
1596 | | WASMEDGE_CAPI_EXPORT enum WasmEdge_ExternalType |
1597 | | WasmEdge_ImportTypeGetExternalType( |
1598 | 0 | const WasmEdge_ImportTypeContext *Cxt) noexcept { |
1599 | 0 | if (Cxt) { |
1600 | 0 | return static_cast<WasmEdge_ExternalType>( |
1601 | 0 | fromImpTypeCxt(Cxt)->getExternalType()); |
1602 | 0 | } |
1603 | 0 | return WasmEdge_ExternalType_Function; |
1604 | 0 | } |
1605 | | |
1606 | | WASMEDGE_CAPI_EXPORT WasmEdge_String WasmEdge_ImportTypeGetModuleName( |
1607 | 0 | const WasmEdge_ImportTypeContext *Cxt) noexcept { |
1608 | 0 | if (Cxt) { |
1609 | 0 | auto StrView = fromImpTypeCxt(Cxt)->getModuleName(); |
1610 | 0 | return WasmEdge_String{/* Length */ static_cast<uint32_t>(StrView.length()), |
1611 | 0 | /* Buf */ StrView.data()}; |
1612 | 0 | } |
1613 | 0 | return WasmEdge_String{/* Length */ 0, /* Buf */ nullptr}; |
1614 | 0 | } |
1615 | | |
1616 | | WASMEDGE_CAPI_EXPORT WasmEdge_String WasmEdge_ImportTypeGetExternalName( |
1617 | 0 | const WasmEdge_ImportTypeContext *Cxt) noexcept { |
1618 | 0 | if (Cxt) { |
1619 | 0 | auto StrView = fromImpTypeCxt(Cxt)->getExternalName(); |
1620 | 0 | return WasmEdge_String{/* Length */ static_cast<uint32_t>(StrView.length()), |
1621 | 0 | /* Buf */ StrView.data()}; |
1622 | 0 | } |
1623 | 0 | return WasmEdge_String{/* Length */ 0, /* Buf */ nullptr}; |
1624 | 0 | } |
1625 | | |
1626 | | WASMEDGE_CAPI_EXPORT const WasmEdge_FunctionTypeContext * |
1627 | | WasmEdge_ImportTypeGetFunctionType( |
1628 | | const WasmEdge_ASTModuleContext *ASTCxt, |
1629 | 0 | const WasmEdge_ImportTypeContext *Cxt) noexcept { |
1630 | 0 | if (ASTCxt && Cxt && |
1631 | 0 | fromImpTypeCxt(Cxt)->getExternalType() == |
1632 | 0 | WasmEdge::ExternalType::Function) { |
1633 | 0 | uint32_t Idx = fromImpTypeCxt(Cxt)->getExternalFuncTypeIdx(); |
1634 | 0 | auto SubTypes = fromASTModCxt(ASTCxt)->getTypeSection().getContent(); |
1635 | 0 | if (Idx < SubTypes.size() && SubTypes[Idx].getCompositeType().isFunc()) { |
1636 | 0 | return toFuncTypeCxt(&(SubTypes[Idx].getCompositeType().getFuncType())); |
1637 | 0 | } |
1638 | 0 | } |
1639 | 0 | return nullptr; |
1640 | 0 | } |
1641 | | |
1642 | | WASMEDGE_CAPI_EXPORT const WasmEdge_TableTypeContext * |
1643 | | WasmEdge_ImportTypeGetTableType( |
1644 | | const WasmEdge_ASTModuleContext *ASTCxt, |
1645 | 0 | const WasmEdge_ImportTypeContext *Cxt) noexcept { |
1646 | 0 | if (ASTCxt && Cxt && |
1647 | 0 | fromImpTypeCxt(Cxt)->getExternalType() == WasmEdge::ExternalType::Table) { |
1648 | 0 | return toTabTypeCxt(&fromImpTypeCxt(Cxt)->getExternalTableType()); |
1649 | 0 | } |
1650 | 0 | return nullptr; |
1651 | 0 | } |
1652 | | |
1653 | | WASMEDGE_CAPI_EXPORT const WasmEdge_MemoryTypeContext * |
1654 | | WasmEdge_ImportTypeGetMemoryType( |
1655 | | const WasmEdge_ASTModuleContext *ASTCxt, |
1656 | 0 | const WasmEdge_ImportTypeContext *Cxt) noexcept { |
1657 | 0 | if (ASTCxt && Cxt && |
1658 | 0 | fromImpTypeCxt(Cxt)->getExternalType() == |
1659 | 0 | WasmEdge::ExternalType::Memory) { |
1660 | 0 | return toMemTypeCxt(&fromImpTypeCxt(Cxt)->getExternalMemoryType()); |
1661 | 0 | } |
1662 | 0 | return nullptr; |
1663 | 0 | } |
1664 | | |
1665 | | WASMEDGE_CAPI_EXPORT const WasmEdge_TagTypeContext * |
1666 | | WasmEdge_ImportTypeGetTagType(const WasmEdge_ASTModuleContext *ASTCxt, |
1667 | 0 | const WasmEdge_ImportTypeContext *Cxt) noexcept { |
1668 | 0 | if (ASTCxt && Cxt && |
1669 | 0 | fromImpTypeCxt(Cxt)->getExternalType() == WasmEdge::ExternalType::Tag) { |
1670 | 0 | return toTagTypeCxt(&fromImpTypeCxt(Cxt)->getExternalTagType()); |
1671 | 0 | } |
1672 | 0 | return nullptr; |
1673 | 0 | } |
1674 | | |
1675 | | WASMEDGE_CAPI_EXPORT const WasmEdge_GlobalTypeContext * |
1676 | | WasmEdge_ImportTypeGetGlobalType( |
1677 | | const WasmEdge_ASTModuleContext *ASTCxt, |
1678 | 0 | const WasmEdge_ImportTypeContext *Cxt) noexcept { |
1679 | 0 | if (ASTCxt && Cxt && |
1680 | 0 | fromImpTypeCxt(Cxt)->getExternalType() == |
1681 | 0 | WasmEdge::ExternalType::Global) { |
1682 | 0 | return toGlobTypeCxt(&fromImpTypeCxt(Cxt)->getExternalGlobalType()); |
1683 | 0 | } |
1684 | 0 | return nullptr; |
1685 | 0 | } |
1686 | | |
1687 | | // <<<<<<<< WasmEdge import type functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
1688 | | |
1689 | | // >>>>>>>> WasmEdge export type functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> |
1690 | | |
1691 | | WASMEDGE_CAPI_EXPORT enum WasmEdge_ExternalType |
1692 | | WasmEdge_ExportTypeGetExternalType( |
1693 | 0 | const WasmEdge_ExportTypeContext *Cxt) noexcept { |
1694 | 0 | if (Cxt) { |
1695 | 0 | return static_cast<WasmEdge_ExternalType>( |
1696 | 0 | fromExpTypeCxt(Cxt)->getExternalType()); |
1697 | 0 | } |
1698 | 0 | return WasmEdge_ExternalType_Function; |
1699 | 0 | } |
1700 | | |
1701 | | WASMEDGE_CAPI_EXPORT WasmEdge_String WasmEdge_ExportTypeGetExternalName( |
1702 | 0 | const WasmEdge_ExportTypeContext *Cxt) noexcept { |
1703 | 0 | if (Cxt) { |
1704 | 0 | auto StrView = fromExpTypeCxt(Cxt)->getExternalName(); |
1705 | 0 | return WasmEdge_String{/* Length */ static_cast<uint32_t>(StrView.length()), |
1706 | 0 | /* Buf */ StrView.data()}; |
1707 | 0 | } |
1708 | 0 | return WasmEdge_String{/* Length */ 0, /* Buf */ nullptr}; |
1709 | 0 | } |
1710 | | |
1711 | | WASMEDGE_CAPI_EXPORT const WasmEdge_FunctionTypeContext * |
1712 | | WasmEdge_ExportTypeGetFunctionType( |
1713 | | const WasmEdge_ASTModuleContext *ASTCxt, |
1714 | 0 | const WasmEdge_ExportTypeContext *Cxt) noexcept { |
1715 | 0 | try { |
1716 | 0 | if (ASTCxt && Cxt && |
1717 | 0 | fromExpTypeCxt(Cxt)->getExternalType() == |
1718 | 0 | WasmEdge::ExternalType::Function) { |
1719 | 0 | auto ImpDescs = fromASTModCxt(ASTCxt)->getImportSection().getContent(); |
1720 | 0 | auto FuncIdxs = fromASTModCxt(ASTCxt)->getFunctionSection().getContent(); |
1721 | 0 | uint32_t ExtIdx = fromExpTypeCxt(Cxt)->getExternalIndex(); |
1722 | | |
1723 | | // Indexing the import descriptions. |
1724 | 0 | std::vector<uint32_t> ImpFuncs; |
1725 | 0 | ImpFuncs.reserve(ImpDescs.size()); |
1726 | 0 | for (uint32_t I = 0; I < ImpDescs.size(); I++) { |
1727 | 0 | if (ImpDescs[I].getExternalType() == WasmEdge::ExternalType::Function) { |
1728 | 0 | ImpFuncs.push_back(I); |
1729 | 0 | } |
1730 | 0 | } |
1731 | | // Get the function type index. |
1732 | 0 | uint32_t TypeIdx = 0; |
1733 | 0 | if (ExtIdx < ImpFuncs.size()) { |
1734 | | // Imported function. Get the function type index from the import desc. |
1735 | 0 | TypeIdx = ImpDescs[ImpFuncs[ExtIdx]].getExternalFuncTypeIdx(); |
1736 | 0 | } else if (ExtIdx < ImpFuncs.size() + FuncIdxs.size()) { |
1737 | | // Module owned function. Get the function type index from the section. |
1738 | 0 | TypeIdx = FuncIdxs[ExtIdx - ImpFuncs.size()]; |
1739 | 0 | } else { |
1740 | | // Invalid function index. |
1741 | 0 | return nullptr; |
1742 | 0 | } |
1743 | | // Get the function type. |
1744 | 0 | auto SubTypes = fromASTModCxt(ASTCxt)->getTypeSection().getContent(); |
1745 | 0 | if (TypeIdx < SubTypes.size() && |
1746 | 0 | SubTypes[TypeIdx].getCompositeType().isFunc()) { |
1747 | 0 | return toFuncTypeCxt( |
1748 | 0 | &(SubTypes[TypeIdx].getCompositeType().getFuncType())); |
1749 | 0 | } |
1750 | 0 | } |
1751 | 0 | } catch (...) { |
1752 | 0 | handleCAPIError(); |
1753 | 0 | } |
1754 | 0 | return nullptr; |
1755 | 0 | } |
1756 | | |
1757 | | WASMEDGE_CAPI_EXPORT const WasmEdge_TableTypeContext * |
1758 | | WasmEdge_ExportTypeGetTableType( |
1759 | | const WasmEdge_ASTModuleContext *ASTCxt, |
1760 | 0 | const WasmEdge_ExportTypeContext *Cxt) noexcept { |
1761 | 0 | try { |
1762 | 0 | if (ASTCxt && Cxt && |
1763 | 0 | fromExpTypeCxt(Cxt)->getExternalType() == |
1764 | 0 | WasmEdge::ExternalType::Table) { |
1765 | 0 | auto ImpDescs = fromASTModCxt(ASTCxt)->getImportSection().getContent(); |
1766 | 0 | auto TabDescs = fromASTModCxt(ASTCxt)->getTableSection().getContent(); |
1767 | 0 | uint32_t ExtIdx = fromExpTypeCxt(Cxt)->getExternalIndex(); |
1768 | | |
1769 | | // Indexing the import descriptions. |
1770 | 0 | std::vector<uint32_t> ImpTabs; |
1771 | 0 | ImpTabs.reserve(ImpDescs.size()); |
1772 | 0 | for (uint32_t I = 0; I < ImpDescs.size(); I++) { |
1773 | 0 | if (ImpDescs[I].getExternalType() == WasmEdge::ExternalType::Table) { |
1774 | 0 | ImpTabs.push_back(I); |
1775 | 0 | } |
1776 | 0 | } |
1777 | | // Get the table type. |
1778 | 0 | if (ExtIdx < ImpTabs.size()) { |
1779 | | // Imported table. Get the table type from the import desc. |
1780 | 0 | return toTabTypeCxt(&ImpDescs[ImpTabs[ExtIdx]].getExternalTableType()); |
1781 | 0 | } else if (ExtIdx < ImpTabs.size() + TabDescs.size()) { |
1782 | | // Module owned table. Get the table type from the section. |
1783 | 0 | return toTabTypeCxt(&TabDescs[ExtIdx - ImpTabs.size()].getTableType()); |
1784 | 0 | } else { |
1785 | | // Invalid table type index. |
1786 | 0 | return nullptr; |
1787 | 0 | } |
1788 | 0 | } |
1789 | 0 | } catch (...) { |
1790 | 0 | handleCAPIError(); |
1791 | 0 | } |
1792 | 0 | return nullptr; |
1793 | 0 | } |
1794 | | |
1795 | | WASMEDGE_CAPI_EXPORT const WasmEdge_MemoryTypeContext * |
1796 | | WasmEdge_ExportTypeGetMemoryType( |
1797 | | const WasmEdge_ASTModuleContext *ASTCxt, |
1798 | 0 | const WasmEdge_ExportTypeContext *Cxt) noexcept { |
1799 | 0 | try { |
1800 | 0 | if (ASTCxt && Cxt && |
1801 | 0 | fromExpTypeCxt(Cxt)->getExternalType() == |
1802 | 0 | WasmEdge::ExternalType::Memory) { |
1803 | 0 | auto ImpDescs = fromASTModCxt(ASTCxt)->getImportSection().getContent(); |
1804 | 0 | auto MemTypes = fromASTModCxt(ASTCxt)->getMemorySection().getContent(); |
1805 | 0 | uint32_t ExtIdx = fromExpTypeCxt(Cxt)->getExternalIndex(); |
1806 | | |
1807 | | // Indexing the import descriptions. |
1808 | 0 | std::vector<uint32_t> ImpMems; |
1809 | 0 | ImpMems.reserve(ImpDescs.size()); |
1810 | 0 | for (uint32_t I = 0; I < ImpDescs.size(); I++) { |
1811 | 0 | if (ImpDescs[I].getExternalType() == WasmEdge::ExternalType::Memory) { |
1812 | 0 | ImpMems.push_back(I); |
1813 | 0 | } |
1814 | 0 | } |
1815 | | // Get the memory type. |
1816 | 0 | if (ExtIdx < ImpMems.size()) { |
1817 | | // Imported memory. Get the memory type from the import desc. |
1818 | 0 | return toMemTypeCxt(&ImpDescs[ImpMems[ExtIdx]].getExternalMemoryType()); |
1819 | 0 | } else if (ExtIdx < ImpMems.size() + MemTypes.size()) { |
1820 | | // Module owned memory. Get the memory type from the section. |
1821 | 0 | return toMemTypeCxt(&MemTypes[ExtIdx - ImpMems.size()]); |
1822 | 0 | } else { |
1823 | | // Invalid memory type index. |
1824 | 0 | return nullptr; |
1825 | 0 | } |
1826 | 0 | } |
1827 | 0 | } catch (...) { |
1828 | 0 | handleCAPIError(); |
1829 | 0 | } |
1830 | 0 | return nullptr; |
1831 | 0 | } |
1832 | | |
1833 | | WASMEDGE_CAPI_EXPORT const WasmEdge_TagTypeContext * |
1834 | | WasmEdge_ExportTypeGetTagType(const WasmEdge_ASTModuleContext *ASTCxt, |
1835 | 0 | const WasmEdge_ExportTypeContext *Cxt) noexcept { |
1836 | 0 | if (ASTCxt && Cxt && |
1837 | 0 | fromExpTypeCxt(Cxt)->getExternalType() == WasmEdge::ExternalType::Tag) { |
1838 | | // `external_index` = `tag_type_index` + `import_tag_nums` |
1839 | 0 | uint32_t ExtIdx = fromExpTypeCxt(Cxt)->getExternalIndex(); |
1840 | 0 | const auto &ImpDescs = |
1841 | 0 | fromASTModCxt(ASTCxt)->getImportSection().getContent(); |
1842 | 0 | for (auto &&ImpDesc : ImpDescs) { |
1843 | 0 | if (ImpDesc.getExternalType() == WasmEdge::ExternalType::Tag) { |
1844 | 0 | ExtIdx--; |
1845 | 0 | } |
1846 | 0 | } |
1847 | | // Get the tag type. |
1848 | 0 | const auto &TagDescs = fromASTModCxt(ASTCxt)->getTagSection().getContent(); |
1849 | 0 | if (ExtIdx >= TagDescs.size()) { |
1850 | 0 | return nullptr; |
1851 | 0 | } |
1852 | 0 | return toTagTypeCxt(&TagDescs[ExtIdx]); |
1853 | 0 | } |
1854 | 0 | return nullptr; |
1855 | 0 | } |
1856 | | |
1857 | | WASMEDGE_CAPI_EXPORT const WasmEdge_GlobalTypeContext * |
1858 | | WasmEdge_ExportTypeGetGlobalType( |
1859 | | const WasmEdge_ASTModuleContext *ASTCxt, |
1860 | 0 | const WasmEdge_ExportTypeContext *Cxt) noexcept { |
1861 | 0 | try { |
1862 | 0 | if (ASTCxt && Cxt && |
1863 | 0 | fromExpTypeCxt(Cxt)->getExternalType() == |
1864 | 0 | WasmEdge::ExternalType::Global) { |
1865 | 0 | auto ImpDescs = fromASTModCxt(ASTCxt)->getImportSection().getContent(); |
1866 | 0 | auto GlobDescs = fromASTModCxt(ASTCxt)->getGlobalSection().getContent(); |
1867 | 0 | uint32_t ExtIdx = fromExpTypeCxt(Cxt)->getExternalIndex(); |
1868 | | |
1869 | | // Indexing the import descriptions. |
1870 | 0 | std::vector<uint32_t> ImpGlobs; |
1871 | 0 | ImpGlobs.reserve(ImpDescs.size()); |
1872 | 0 | for (uint32_t I = 0; I < ImpDescs.size(); I++) { |
1873 | 0 | if (ImpDescs[I].getExternalType() == WasmEdge::ExternalType::Global) { |
1874 | 0 | ImpGlobs.push_back(I); |
1875 | 0 | } |
1876 | 0 | } |
1877 | | // Get the global type. |
1878 | 0 | if (ExtIdx < ImpGlobs.size()) { |
1879 | | // Imported global. Get the global type from the import desc. |
1880 | 0 | return toGlobTypeCxt( |
1881 | 0 | &ImpDescs[ImpGlobs[ExtIdx]].getExternalGlobalType()); |
1882 | 0 | } else if (ExtIdx < ImpGlobs.size() + GlobDescs.size()) { |
1883 | | // Module owned global. Get the global type from the section. |
1884 | 0 | return toGlobTypeCxt( |
1885 | 0 | &GlobDescs[ExtIdx - ImpGlobs.size()].getGlobalType()); |
1886 | 0 | } else { |
1887 | | // Invalid global type index. |
1888 | 0 | return nullptr; |
1889 | 0 | } |
1890 | 0 | } |
1891 | 0 | } catch (...) { |
1892 | 0 | handleCAPIError(); |
1893 | 0 | } |
1894 | 0 | return nullptr; |
1895 | 0 | } |
1896 | | |
1897 | | // <<<<<<<< WasmEdge export type functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
1898 | | |
1899 | | // >>>>>>>> WasmEdge AOT compiler functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> |
1900 | | |
1901 | | WASMEDGE_CAPI_EXPORT WasmEdge_CompilerContext * |
1902 | | WasmEdge_CompilerCreate(const WasmEdge_ConfigureContext *ConfCxt |
1903 | 0 | [[maybe_unused]]) noexcept { |
1904 | 0 | #ifdef WASMEDGE_USE_LLVM |
1905 | 0 | try { |
1906 | | // Set force interpreter here to load instructions of function body |
1907 | | // forcibly. |
1908 | 0 | if (ConfCxt) { |
1909 | 0 | WasmEdge::Configure CopyConf(ConfCxt->Conf); |
1910 | 0 | CopyConf.getRuntimeConfigure().setRunMode(WasmEdge::RunMode::Interpreter); |
1911 | 0 | return new WasmEdge_CompilerContext(CopyConf); |
1912 | 0 | } else { |
1913 | 0 | WasmEdge::Configure CopyConf; |
1914 | 0 | CopyConf.getRuntimeConfigure().setRunMode(WasmEdge::RunMode::Interpreter); |
1915 | 0 | return new WasmEdge_CompilerContext(CopyConf); |
1916 | 0 | } |
1917 | 0 | } catch (...) { |
1918 | 0 | handleCAPIError(); |
1919 | 0 | return nullptr; |
1920 | 0 | } |
1921 | | #else |
1922 | | return nullptr; |
1923 | | #endif |
1924 | 0 | } |
1925 | | |
1926 | | WASMEDGE_CAPI_EXPORT WasmEdge_Result |
1927 | | WasmEdge_CompilerCompile(WasmEdge_CompilerContext *Cxt [[maybe_unused]], |
1928 | | const char *InPath [[maybe_unused]], |
1929 | 0 | const char *OutPath [[maybe_unused]]) noexcept { |
1930 | 0 | #ifdef WASMEDGE_USE_LLVM |
1931 | 0 | return wrap( |
1932 | 0 | [&]() -> WasmEdge::Expect<void> { |
1933 | 0 | std::filesystem::path InputPath = genPath(InPath); |
1934 | 0 | std::filesystem::path OutputPath = genPath(OutPath); |
1935 | 0 | std::vector<WasmEdge::Byte> Data; |
1936 | 0 | std::unique_ptr<WasmEdge::AST::Module> Module; |
1937 | 0 | return Cxt->Compiler.checkConfigure() |
1938 | 0 | .and_then([&]() noexcept { return Cxt->Load.loadFile(InputPath); }) |
1939 | 0 | .and_then([&](auto Result) noexcept { |
1940 | 0 | Data = std::move(Result); |
1941 | 0 | return Cxt->Load.parseModule(Data); |
1942 | 0 | }) |
1943 | 0 | .and_then([&](auto Result) noexcept { |
1944 | 0 | Module = std::move(Result); |
1945 | 0 | return Cxt->Valid.validate(*Module.get()); |
1946 | 0 | }) |
1947 | 0 | .and_then( |
1948 | 0 | [&]() noexcept { return Cxt->Compiler.compile(*Module.get()); }) |
1949 | 0 | .and_then([&](auto Result) noexcept { |
1950 | 0 | return Cxt->CodeGen.codegen(Data, std::move(Result), OutputPath); |
1951 | 0 | }); |
1952 | 0 | }, |
1953 | 0 | EmptyThen, Cxt); |
1954 | | #else |
1955 | | return genWasmEdge_Result(ErrCode::Value::AOTDisabled); |
1956 | | #endif |
1957 | 0 | } |
1958 | | |
1959 | | WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_CompilerCompileFromBuffer( |
1960 | | WasmEdge_CompilerContext *Cxt, const uint8_t *InBuffer, |
1961 | 0 | const uint64_t InBufferLen, const char *OutPath) noexcept { |
1962 | 0 | return WasmEdge_CompilerCompileFromBytes( |
1963 | 0 | Cxt, WasmEdge_BytesWrap(InBuffer, static_cast<uint32_t>(InBufferLen)), |
1964 | 0 | OutPath); |
1965 | 0 | } |
1966 | | |
1967 | | WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_CompilerCompileFromBytes( |
1968 | | WasmEdge_CompilerContext *Cxt [[maybe_unused]], |
1969 | | const WasmEdge_Bytes Bytes [[maybe_unused]], |
1970 | 0 | const char *OutPath [[maybe_unused]]) noexcept { |
1971 | 0 | #ifdef WASMEDGE_USE_LLVM |
1972 | 0 | return wrap( |
1973 | 0 | [&]() -> WasmEdge::Expect<void> { |
1974 | 0 | std::filesystem::path OutputPath = genPath(OutPath); |
1975 | 0 | auto Data = genSpan(Bytes.Buf, Bytes.Length); |
1976 | 0 | std::unique_ptr<WasmEdge::AST::Module> Module; |
1977 | 0 | return Cxt->Load.parseModule(Data) |
1978 | 0 | .and_then([&](auto Result) noexcept { |
1979 | 0 | Module = std::move(Result); |
1980 | 0 | return Cxt->Valid.validate(*Module); |
1981 | 0 | }) |
1982 | 0 | .and_then([&]() noexcept { return Cxt->Compiler.compile(*Module); }) |
1983 | 0 | .and_then([&](auto Result) noexcept { |
1984 | 0 | return Cxt->CodeGen.codegen(Data, std::move(Result), OutputPath); |
1985 | 0 | }); |
1986 | 0 | }, |
1987 | 0 | EmptyThen, Cxt); |
1988 | | #else |
1989 | | return genWasmEdge_Result(ErrCode::Value::AOTDisabled); |
1990 | | #endif |
1991 | 0 | } |
1992 | | |
1993 | | WASMEDGE_CAPI_EXPORT void |
1994 | 0 | WasmEdge_CompilerDelete(WasmEdge_CompilerContext *Cxt) noexcept { |
1995 | 0 | delete Cxt; |
1996 | 0 | } |
1997 | | |
1998 | | // <<<<<<<< WasmEdge AOT compiler functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
1999 | | |
2000 | | // >>>>>>>> WasmEdge loader functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> |
2001 | | |
2002 | | WASMEDGE_CAPI_EXPORT WasmEdge_LoaderContext * |
2003 | 0 | WasmEdge_LoaderCreate(const WasmEdge_ConfigureContext *ConfCxt) noexcept { |
2004 | 0 | try { |
2005 | 0 | if (ConfCxt) { |
2006 | 0 | return toLoaderCxt(new WasmEdge::Loader::Loader( |
2007 | 0 | ConfCxt->Conf, &WasmEdge::Executor::Executor::Intrinsics)); |
2008 | 0 | } else { |
2009 | 0 | return toLoaderCxt(new WasmEdge::Loader::Loader( |
2010 | 0 | WasmEdge::Configure(), &WasmEdge::Executor::Executor::Intrinsics)); |
2011 | 0 | } |
2012 | 0 | } catch (...) { |
2013 | 0 | handleCAPIError(); |
2014 | 0 | return nullptr; |
2015 | 0 | } |
2016 | 0 | } |
2017 | | |
2018 | | WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_LoaderParseFromFile( |
2019 | | WasmEdge_LoaderContext *Cxt, WasmEdge_ASTModuleContext **Module, |
2020 | 0 | const char *Path) noexcept { |
2021 | 0 | return wrap([&]() { return fromLoaderCxt(Cxt)->parseModule(genPath(Path)); }, |
2022 | 0 | [&](auto &&Res) { *Module = toASTModCxt((*Res).release()); }, Cxt, |
2023 | 0 | Module); |
2024 | 0 | } |
2025 | | |
2026 | | WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_LoaderParseFromBuffer( |
2027 | | WasmEdge_LoaderContext *Cxt, WasmEdge_ASTModuleContext **Module, |
2028 | 0 | const uint8_t *Buf, const uint32_t BufLen) noexcept { |
2029 | 0 | return WasmEdge_LoaderParseFromBytes(Cxt, Module, |
2030 | 0 | WasmEdge_BytesWrap(Buf, BufLen)); |
2031 | 0 | } |
2032 | | |
2033 | | WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_LoaderParseFromBytes( |
2034 | | WasmEdge_LoaderContext *Cxt, WasmEdge_ASTModuleContext **Module, |
2035 | 0 | const WasmEdge_Bytes Bytes) noexcept { |
2036 | 0 | return wrap( |
2037 | 0 | [&]() { |
2038 | 0 | return fromLoaderCxt(Cxt)->parseModule( |
2039 | 0 | genSpan(Bytes.Buf, Bytes.Length)); |
2040 | 0 | }, |
2041 | 0 | [&](auto &&Res) { *Module = toASTModCxt((*Res).release()); }, Cxt, |
2042 | 0 | Module); |
2043 | 0 | } |
2044 | | |
2045 | | WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_LoaderSerializeASTModule( |
2046 | | WasmEdge_LoaderContext *Cxt, const WasmEdge_ASTModuleContext *ASTCxt, |
2047 | 0 | WasmEdge_Bytes *Buf) noexcept { |
2048 | 0 | return wrap( |
2049 | 0 | [&]() { |
2050 | 0 | return fromLoaderCxt(Cxt)->serializeModule(*fromASTModCxt(ASTCxt)); |
2051 | 0 | }, |
2052 | 0 | [&](auto &&Res) { |
2053 | 0 | uint32_t Size = static_cast<uint32_t>((*Res).size()); |
2054 | 0 | uint8_t *Bytes = new uint8_t[Size]; |
2055 | 0 | std::copy_n((*Res).begin(), Size, Bytes); |
2056 | 0 | Buf->Length = Size; |
2057 | 0 | Buf->Buf = Bytes; |
2058 | 0 | }, |
2059 | 0 | Cxt, ASTCxt, Buf); |
2060 | 0 | } |
2061 | | |
2062 | | WASMEDGE_CAPI_EXPORT void |
2063 | 0 | WasmEdge_LoaderDelete(WasmEdge_LoaderContext *Cxt) noexcept { |
2064 | 0 | delete fromLoaderCxt(Cxt); |
2065 | 0 | } |
2066 | | |
2067 | | // <<<<<<<< WasmEdge loader functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
2068 | | |
2069 | | // >>>>>>>> WasmEdge validator functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> |
2070 | | |
2071 | | WASMEDGE_CAPI_EXPORT WasmEdge_ValidatorContext * |
2072 | 0 | WasmEdge_ValidatorCreate(const WasmEdge_ConfigureContext *ConfCxt) noexcept { |
2073 | 0 | try { |
2074 | 0 | if (ConfCxt) { |
2075 | 0 | return toValidatorCxt(new WasmEdge::Validator::Validator(ConfCxt->Conf)); |
2076 | 0 | } else { |
2077 | 0 | return toValidatorCxt( |
2078 | 0 | new WasmEdge::Validator::Validator(WasmEdge::Configure())); |
2079 | 0 | } |
2080 | 0 | } catch (...) { |
2081 | 0 | handleCAPIError(); |
2082 | 0 | return nullptr; |
2083 | 0 | } |
2084 | 0 | } |
2085 | | |
2086 | | WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_ValidatorValidate( |
2087 | | WasmEdge_ValidatorContext *Cxt, |
2088 | 0 | const WasmEdge_ASTModuleContext *ModuleCxt) noexcept { |
2089 | 0 | return wrap( |
2090 | 0 | [&]() { |
2091 | 0 | return fromValidatorCxt(Cxt)->validate(*fromASTModCxt(ModuleCxt)); |
2092 | 0 | }, |
2093 | 0 | EmptyThen, Cxt, ModuleCxt); |
2094 | 0 | } |
2095 | | |
2096 | | WASMEDGE_CAPI_EXPORT void |
2097 | 0 | WasmEdge_ValidatorDelete(WasmEdge_ValidatorContext *Cxt) noexcept { |
2098 | 0 | delete fromValidatorCxt(Cxt); |
2099 | 0 | } |
2100 | | |
2101 | | // <<<<<<<< WasmEdge validator functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
2102 | | |
2103 | | // >>>>>>>> WasmEdge executor functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> |
2104 | | |
2105 | | WASMEDGE_CAPI_EXPORT WasmEdge_ExecutorContext * |
2106 | | WasmEdge_ExecutorCreate(const WasmEdge_ConfigureContext *ConfCxt, |
2107 | 0 | WasmEdge_StatisticsContext *StatCxt) noexcept { |
2108 | 0 | try { |
2109 | 0 | if (ConfCxt) { |
2110 | 0 | if (StatCxt) { |
2111 | 0 | return toExecutorCxt(new WasmEdge::Executor::Executor( |
2112 | 0 | ConfCxt->Conf, fromStatCxt(StatCxt))); |
2113 | 0 | } else { |
2114 | 0 | return toExecutorCxt(new WasmEdge::Executor::Executor(ConfCxt->Conf)); |
2115 | 0 | } |
2116 | 0 | } else { |
2117 | 0 | if (StatCxt) { |
2118 | 0 | return toExecutorCxt(new WasmEdge::Executor::Executor( |
2119 | 0 | WasmEdge::Configure(), fromStatCxt(StatCxt))); |
2120 | 0 | } else { |
2121 | 0 | return toExecutorCxt( |
2122 | 0 | new WasmEdge::Executor::Executor(WasmEdge::Configure())); |
2123 | 0 | } |
2124 | 0 | } |
2125 | 0 | } catch (...) { |
2126 | 0 | handleCAPIError(); |
2127 | 0 | return nullptr; |
2128 | 0 | } |
2129 | 0 | } |
2130 | | |
2131 | | WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_ExecutorInstantiate( |
2132 | | WasmEdge_ExecutorContext *Cxt, WasmEdge_ModuleInstanceContext **ModuleCxt, |
2133 | | WasmEdge_StoreContext *StoreCxt, |
2134 | 0 | const WasmEdge_ASTModuleContext *ASTCxt) noexcept { |
2135 | 0 | return wrap( |
2136 | 0 | [&]() { |
2137 | 0 | return fromExecutorCxt(Cxt)->instantiateModule(*fromStoreCxt(StoreCxt), |
2138 | 0 | *fromASTModCxt(ASTCxt)); |
2139 | 0 | }, |
2140 | 0 | [&](auto &&Res) { *ModuleCxt = toModCxt((*Res).release()); }, Cxt, |
2141 | 0 | ModuleCxt, StoreCxt, ASTCxt); |
2142 | 0 | } |
2143 | | |
2144 | | WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_ExecutorRegister( |
2145 | | WasmEdge_ExecutorContext *Cxt, WasmEdge_ModuleInstanceContext **ModuleCxt, |
2146 | | WasmEdge_StoreContext *StoreCxt, const WasmEdge_ASTModuleContext *ASTCxt, |
2147 | 0 | const WasmEdge_String ModuleName) noexcept { |
2148 | 0 | return wrap( |
2149 | 0 | [&]() { |
2150 | 0 | return fromExecutorCxt(Cxt)->registerModule(*fromStoreCxt(StoreCxt), |
2151 | 0 | *fromASTModCxt(ASTCxt), |
2152 | 0 | genStrView(ModuleName)); |
2153 | 0 | }, |
2154 | 0 | [&](auto &&Res) { *ModuleCxt = toModCxt((*Res).release()); }, Cxt, |
2155 | 0 | ModuleCxt, StoreCxt, ASTCxt); |
2156 | 0 | } |
2157 | | |
2158 | | WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_ExecutorRegisterImport( |
2159 | | WasmEdge_ExecutorContext *Cxt, WasmEdge_StoreContext *StoreCxt, |
2160 | 0 | const WasmEdge_ModuleInstanceContext *ImportCxt) noexcept { |
2161 | 0 | return wrap( |
2162 | 0 | [&]() { |
2163 | 0 | return fromExecutorCxt(Cxt)->registerModule(*fromStoreCxt(StoreCxt), |
2164 | 0 | *fromModCxt(ImportCxt)); |
2165 | 0 | }, |
2166 | 0 | EmptyThen, Cxt, StoreCxt, ImportCxt); |
2167 | 0 | } |
2168 | | |
2169 | | WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_ExecutorRegisterImportWithAlias( |
2170 | | WasmEdge_ExecutorContext *Cxt, WasmEdge_StoreContext *StoreCxt, |
2171 | | const WasmEdge_ModuleInstanceContext *ImportCxt, |
2172 | 0 | const WasmEdge_String ModuleName) noexcept { |
2173 | 0 | return wrap( |
2174 | 0 | [&]() { |
2175 | 0 | return fromExecutorCxt(Cxt)->registerModule(*fromStoreCxt(StoreCxt), |
2176 | 0 | *fromModCxt(ImportCxt), |
2177 | 0 | genStrView(ModuleName)); |
2178 | 0 | }, |
2179 | 0 | EmptyThen, Cxt, StoreCxt, ImportCxt); |
2180 | 0 | } |
2181 | | |
2182 | | WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_ExecutorInvoke( |
2183 | | WasmEdge_ExecutorContext *Cxt, |
2184 | | const WasmEdge_FunctionInstanceContext *FuncCxt, |
2185 | | const WasmEdge_Value *Params, const uint32_t ParamLen, |
2186 | 0 | WasmEdge_Value *Returns, const uint32_t ReturnLen) noexcept { |
2187 | 0 | try { |
2188 | 0 | auto ParamPair = genParamPair(Params, ParamLen); |
2189 | 0 | return wrap( |
2190 | 0 | [&]() -> WasmEdge::Expect<std::vector< |
2191 | 0 | std::pair<WasmEdge::ValVariant, WasmEdge::ValType>>> { |
2192 | 0 | return fromExecutorCxt(Cxt)->invoke( |
2193 | 0 | fromFuncCxt(FuncCxt), ParamPair.first, ParamPair.second); |
2194 | 0 | }, |
2195 | 0 | [&](auto &&Res) { fillWasmEdge_ValueArr(*Res, Returns, ReturnLen); }, |
2196 | 0 | Cxt, FuncCxt); |
2197 | 0 | } catch (...) { |
2198 | 0 | return handleCAPIError(); |
2199 | 0 | } |
2200 | 0 | } |
2201 | | |
2202 | | WASMEDGE_CAPI_EXPORT WasmEdge_Async * |
2203 | | WasmEdge_ExecutorAsyncInvoke(WasmEdge_ExecutorContext *Cxt, |
2204 | | const WasmEdge_FunctionInstanceContext *FuncCxt, |
2205 | | const WasmEdge_Value *Params, |
2206 | 0 | const uint32_t ParamLen) noexcept { |
2207 | 0 | try { |
2208 | 0 | if (Cxt && FuncCxt) { |
2209 | 0 | auto ParamPair = genParamPair(Params, ParamLen); |
2210 | 0 | return new WasmEdge_Async(fromExecutorCxt(Cxt)->asyncInvoke( |
2211 | 0 | fromFuncCxt(FuncCxt), ParamPair.first, ParamPair.second)); |
2212 | 0 | } |
2213 | 0 | } catch (...) { |
2214 | 0 | handleCAPIError(); |
2215 | 0 | } |
2216 | 0 | return nullptr; |
2217 | 0 | } |
2218 | | |
2219 | | WASMEDGE_CAPI_EXPORT void |
2220 | 0 | WasmEdge_ExecutorDelete(WasmEdge_ExecutorContext *Cxt) noexcept { |
2221 | 0 | delete fromExecutorCxt(Cxt); |
2222 | 0 | } |
2223 | | |
2224 | | // <<<<<<<< WasmEdge executor functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
2225 | | |
2226 | | // >>>>>>>> WasmEdge store functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> |
2227 | | |
2228 | | WASMEDGE_CAPI_EXPORT WasmEdge_StoreContext * |
2229 | 0 | WasmEdge_StoreCreate(void) noexcept { |
2230 | 0 | try { |
2231 | 0 | return toStoreCxt(new WasmEdge::Runtime::StoreManager); |
2232 | 0 | } catch (...) { |
2233 | 0 | handleCAPIError(); |
2234 | 0 | return nullptr; |
2235 | 0 | } |
2236 | 0 | } |
2237 | | |
2238 | | WASMEDGE_CAPI_EXPORT const WasmEdge_ModuleInstanceContext * |
2239 | | WasmEdge_StoreFindModule(const WasmEdge_StoreContext *Cxt, |
2240 | 0 | const WasmEdge_String Name) noexcept { |
2241 | 0 | if (Cxt) { |
2242 | 0 | return toModCxt(fromStoreCxt(Cxt)->findModule(genStrView(Name))); |
2243 | 0 | } |
2244 | 0 | return nullptr; |
2245 | 0 | } |
2246 | | |
2247 | | WASMEDGE_CAPI_EXPORT uint32_t |
2248 | 0 | WasmEdge_StoreListModuleLength(const WasmEdge_StoreContext *Cxt) noexcept { |
2249 | 0 | if (Cxt) { |
2250 | 0 | return fromStoreCxt(Cxt)->getModuleListSize(); |
2251 | 0 | } |
2252 | 0 | return 0; |
2253 | 0 | } |
2254 | | |
2255 | | WASMEDGE_CAPI_EXPORT uint32_t |
2256 | | WasmEdge_StoreListModule(const WasmEdge_StoreContext *Cxt, |
2257 | 0 | WasmEdge_String *Names, const uint32_t Len) noexcept { |
2258 | 0 | if (Cxt) { |
2259 | 0 | return fromStoreCxt(Cxt)->getModuleList( |
2260 | 0 | [&](auto &Map) { return fillMap(Map, Names, Len); }); |
2261 | 0 | } |
2262 | 0 | return 0; |
2263 | 0 | } |
2264 | | |
2265 | | WASMEDGE_CAPI_EXPORT void |
2266 | 0 | WasmEdge_StoreDelete(WasmEdge_StoreContext *Cxt) noexcept { |
2267 | 0 | delete fromStoreCxt(Cxt); |
2268 | 0 | } |
2269 | | |
2270 | | // <<<<<<<< WasmEdge store functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
2271 | | |
2272 | | // >>>>>>>> WasmEdge module instance functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> |
2273 | | |
2274 | | WASMEDGE_CAPI_EXPORT WasmEdge_ModuleInstanceContext * |
2275 | 0 | WasmEdge_ModuleInstanceCreate(const WasmEdge_String ModuleName) noexcept { |
2276 | 0 | try { |
2277 | 0 | return toModCxt(new WasmEdge::Runtime::Instance::ModuleInstance( |
2278 | 0 | genStrView(ModuleName))); |
2279 | 0 | } catch (...) { |
2280 | 0 | handleCAPIError(); |
2281 | 0 | return nullptr; |
2282 | 0 | } |
2283 | 0 | } |
2284 | | |
2285 | | WASMEDGE_CAPI_EXPORT WasmEdge_ModuleInstanceContext * |
2286 | | WasmEdge_ModuleInstanceCreateWASIWithFds( |
2287 | | const char *const *Args, const uint32_t ArgLen, const char *const *Envs, |
2288 | | const uint32_t EnvLen, const char *const *Preopens, |
2289 | | const uint32_t PreopenLen, const int32_t StdInFd, const int32_t StdOutFd, |
2290 | 0 | const int32_t StdErrFd) noexcept { |
2291 | 0 | try { |
2292 | 0 | auto WasiMod = std::make_unique<WasmEdge::Host::WasiModule>(); |
2293 | 0 | WasmEdge_ModuleInstanceInitWASIWithFds( |
2294 | 0 | toModCxt(WasiMod.get()), Args, ArgLen, Envs, EnvLen, Preopens, |
2295 | 0 | PreopenLen, StdInFd, StdOutFd, StdErrFd); |
2296 | 0 | return toModCxt(WasiMod.release()); |
2297 | 0 | } catch (...) { |
2298 | 0 | handleCAPIError(); |
2299 | 0 | return nullptr; |
2300 | 0 | } |
2301 | 0 | } |
2302 | | |
2303 | | WASMEDGE_CAPI_EXPORT WasmEdge_ModuleInstanceContext * |
2304 | | WasmEdge_ModuleInstanceCreateWASI(const char *const *Args, |
2305 | | const uint32_t ArgLen, |
2306 | | const char *const *Envs, |
2307 | | const uint32_t EnvLen, |
2308 | | const char *const *Preopens, |
2309 | 0 | const uint32_t PreopenLen) noexcept { |
2310 | 0 | try { |
2311 | 0 | auto WasiMod = std::make_unique<WasmEdge::Host::WasiModule>(); |
2312 | 0 | WasmEdge_ModuleInstanceInitWASI(toModCxt(WasiMod.get()), Args, ArgLen, Envs, |
2313 | 0 | EnvLen, Preopens, PreopenLen); |
2314 | 0 | return toModCxt(WasiMod.release()); |
2315 | 0 | } catch (...) { |
2316 | 0 | handleCAPIError(); |
2317 | 0 | return nullptr; |
2318 | 0 | } |
2319 | 0 | } |
2320 | | |
2321 | | WASMEDGE_CAPI_EXPORT extern WasmEdge_ModuleInstanceContext * |
2322 | | WasmEdge_ModuleInstanceCreateWithData(const WasmEdge_String ModuleName, |
2323 | | void *HostData, |
2324 | 0 | void (*Finalizer)(void *)) noexcept { |
2325 | 0 | try { |
2326 | 0 | return toModCxt(new WasmEdge::Runtime::Instance::ModuleInstance( |
2327 | 0 | genStrView(ModuleName), HostData, Finalizer)); |
2328 | 0 | } catch (...) { |
2329 | 0 | handleCAPIError(); |
2330 | 0 | return nullptr; |
2331 | 0 | } |
2332 | 0 | } |
2333 | | |
2334 | | WASMEDGE_CAPI_EXPORT void WasmEdge_ModuleInstanceInitWASI( |
2335 | | WasmEdge_ModuleInstanceContext *Cxt, const char *const *Args, |
2336 | | const uint32_t ArgLen, const char *const *Envs, const uint32_t EnvLen, |
2337 | 0 | const char *const *Preopens, const uint32_t PreopenLen) noexcept { |
2338 | 0 | if (!Cxt) { |
2339 | 0 | return; |
2340 | 0 | } |
2341 | 0 | auto *WasiMod = dynamic_cast<WasmEdge::Host::WasiModule *>(fromModCxt(Cxt)); |
2342 | 0 | if (!WasiMod) { |
2343 | 0 | return; |
2344 | 0 | } |
2345 | 0 | try { |
2346 | 0 | std::vector<std::string> ArgVec, EnvVec, DirVec; |
2347 | 0 | std::string ProgName; |
2348 | 0 | if (Args) { |
2349 | 0 | if (ArgLen > 0) { |
2350 | 0 | ProgName = Args[0]; |
2351 | 0 | } |
2352 | 0 | for (uint32_t I = 1; I < ArgLen; I++) { |
2353 | 0 | ArgVec.emplace_back(Args[I]); |
2354 | 0 | } |
2355 | 0 | } |
2356 | 0 | if (Envs) { |
2357 | 0 | for (uint32_t I = 0; I < EnvLen; I++) { |
2358 | 0 | EnvVec.emplace_back(Envs[I]); |
2359 | 0 | } |
2360 | 0 | } |
2361 | 0 | if (Preopens) { |
2362 | 0 | for (uint32_t I = 0; I < PreopenLen; I++) { |
2363 | 0 | DirVec.emplace_back(Preopens[I]); |
2364 | 0 | } |
2365 | 0 | } |
2366 | 0 | WasiMod->init(DirVec, ProgName, ArgVec, EnvVec); |
2367 | 0 | } catch (...) { |
2368 | 0 | handleCAPIError(); |
2369 | 0 | } |
2370 | 0 | } |
2371 | | |
2372 | | WASMEDGE_CAPI_EXPORT void WasmEdge_ModuleInstanceInitWASIWithFds( |
2373 | | WasmEdge_ModuleInstanceContext *Cxt, const char *const *Args, |
2374 | | const uint32_t ArgLen, const char *const *Envs, const uint32_t EnvLen, |
2375 | | const char *const *Preopens, const uint32_t PreopenLen, |
2376 | | const int32_t StdInFd, const int32_t StdOutFd, |
2377 | 0 | const int32_t StdErrFd) noexcept { |
2378 | 0 | if (!Cxt) { |
2379 | 0 | return; |
2380 | 0 | } |
2381 | 0 | auto *WasiMod = dynamic_cast<WasmEdge::Host::WasiModule *>(fromModCxt(Cxt)); |
2382 | 0 | if (!WasiMod) { |
2383 | 0 | return; |
2384 | 0 | } |
2385 | 0 | try { |
2386 | 0 | std::vector<std::string> ArgVec, EnvVec, DirVec; |
2387 | 0 | std::string ProgName; |
2388 | 0 | if (Args) { |
2389 | 0 | if (ArgLen > 0) { |
2390 | 0 | ProgName = Args[0]; |
2391 | 0 | } |
2392 | 0 | for (uint32_t I = 1; I < ArgLen; I++) { |
2393 | 0 | ArgVec.emplace_back(Args[I]); |
2394 | 0 | } |
2395 | 0 | } |
2396 | 0 | if (Envs) { |
2397 | 0 | for (uint32_t I = 0; I < EnvLen; I++) { |
2398 | 0 | EnvVec.emplace_back(Envs[I]); |
2399 | 0 | } |
2400 | 0 | } |
2401 | 0 | if (Preopens) { |
2402 | 0 | for (uint32_t I = 0; I < PreopenLen; I++) { |
2403 | 0 | DirVec.emplace_back(Preopens[I]); |
2404 | 0 | } |
2405 | 0 | } |
2406 | 0 | auto Result = WasiMod->initWithFds(DirVec, ProgName, ArgVec, EnvVec, |
2407 | 0 | StdInFd, StdOutFd, StdErrFd); |
2408 | 0 | if (!Result) { |
2409 | 0 | spdlog::error(" Failed to initialize WASI environment: {}"sv, |
2410 | 0 | Result.error()); |
2411 | 0 | } |
2412 | 0 | } catch (...) { |
2413 | 0 | handleCAPIError(); |
2414 | 0 | } |
2415 | 0 | } |
2416 | | |
2417 | | WASMEDGE_CAPI_EXPORT extern uint32_t |
2418 | | WasmEdge_ModuleInstanceWASIGetNativeHandler( |
2419 | | const WasmEdge_ModuleInstanceContext *Cxt, int32_t Fd, |
2420 | 0 | uint64_t *NativeHandler) noexcept { |
2421 | 0 | if (!Cxt) { |
2422 | 0 | return 1; |
2423 | 0 | } |
2424 | 0 | auto *WasiMod = |
2425 | 0 | dynamic_cast<const WasmEdge::Host::WasiModule *>(fromModCxt(Cxt)); |
2426 | 0 | if (!WasiMod) { |
2427 | 0 | return 2; |
2428 | 0 | } |
2429 | 0 | auto Handler = WasiMod->getNativeHandler(Fd); |
2430 | 0 | if (!Handler) { |
2431 | 0 | return 2; |
2432 | 0 | } |
2433 | 0 | *NativeHandler = *Handler; |
2434 | 0 | return 0; |
2435 | 0 | } |
2436 | | |
2437 | | WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_ModuleInstanceWASIGetExitCode( |
2438 | 0 | const WasmEdge_ModuleInstanceContext *Cxt) noexcept { |
2439 | 0 | if (!Cxt) { |
2440 | 0 | return EXIT_FAILURE; |
2441 | 0 | } |
2442 | 0 | auto *WasiMod = |
2443 | 0 | dynamic_cast<const WasmEdge::Host::WasiModule *>(fromModCxt(Cxt)); |
2444 | 0 | if (!WasiMod) { |
2445 | 0 | return EXIT_FAILURE; |
2446 | 0 | } |
2447 | 0 | return WasiMod->getExitCode(); |
2448 | 0 | } |
2449 | | |
2450 | | WASMEDGE_CAPI_EXPORT void |
2451 | | WasmEdge_ModuleInstanceInitWasmEdgeProcess(const char *const *AllowedCmds, |
2452 | | const uint32_t CmdsLen, |
2453 | 0 | const bool AllowAll) noexcept { |
2454 | 0 | using namespace std::literals::string_view_literals; |
2455 | 0 | if (const auto *Plugin = |
2456 | 0 | WasmEdge::Plugin::Plugin::find("wasmedge_process"sv)) { |
2457 | 0 | PO::ArgumentParser Parser; |
2458 | 0 | Plugin->registerOptions(Parser); |
2459 | 0 | Parser.set_raw_value<std::vector<std::string>>( |
2460 | 0 | "allow-command"sv, |
2461 | 0 | std::vector<std::string>(AllowedCmds, AllowedCmds + CmdsLen)); |
2462 | 0 | if (AllowAll) { |
2463 | 0 | Parser.set_raw_value("allow-command-all"sv); |
2464 | 0 | } |
2465 | 0 | } |
2466 | 0 | } |
2467 | | |
2468 | | WASMEDGE_CAPI_EXPORT WasmEdge_String WasmEdge_ModuleInstanceGetModuleName( |
2469 | 0 | const WasmEdge_ModuleInstanceContext *Cxt) noexcept { |
2470 | 0 | if (Cxt) { |
2471 | 0 | auto StrView = fromModCxt(Cxt)->getModuleName(); |
2472 | 0 | return WasmEdge_String{/* Length */ static_cast<uint32_t>(StrView.length()), |
2473 | 0 | /* Buf */ StrView.data()}; |
2474 | 0 | } |
2475 | 0 | return WasmEdge_String{/* Length */ 0, /* Buf */ nullptr}; |
2476 | 0 | } |
2477 | | |
2478 | | WASMEDGE_CAPI_EXPORT void *WasmEdge_ModuleInstanceGetHostData( |
2479 | 0 | const WasmEdge_ModuleInstanceContext *Cxt) noexcept { |
2480 | 0 | if (Cxt) { |
2481 | 0 | return fromModCxt(Cxt)->getHostData(); |
2482 | 0 | } |
2483 | 0 | return nullptr; |
2484 | 0 | } |
2485 | | |
2486 | | WASMEDGE_CAPI_EXPORT WasmEdge_FunctionInstanceContext * |
2487 | | WasmEdge_ModuleInstanceFindFunction(const WasmEdge_ModuleInstanceContext *Cxt, |
2488 | 0 | const WasmEdge_String Name) noexcept { |
2489 | 0 | if (Cxt) { |
2490 | 0 | return toFuncCxt(fromModCxt(Cxt)->findFuncExports(genStrView(Name))); |
2491 | 0 | } |
2492 | 0 | return nullptr; |
2493 | 0 | } |
2494 | | |
2495 | | WASMEDGE_CAPI_EXPORT WasmEdge_TableInstanceContext * |
2496 | | WasmEdge_ModuleInstanceFindTable(const WasmEdge_ModuleInstanceContext *Cxt, |
2497 | 0 | const WasmEdge_String Name) noexcept { |
2498 | 0 | if (Cxt) { |
2499 | 0 | return toTabCxt(fromModCxt(Cxt)->findTableExports(genStrView(Name))); |
2500 | 0 | } |
2501 | 0 | return nullptr; |
2502 | 0 | } |
2503 | | |
2504 | | WASMEDGE_CAPI_EXPORT WasmEdge_MemoryInstanceContext * |
2505 | | WasmEdge_ModuleInstanceFindMemory(const WasmEdge_ModuleInstanceContext *Cxt, |
2506 | 0 | const WasmEdge_String Name) noexcept { |
2507 | 0 | if (Cxt) { |
2508 | 0 | return toMemCxt(fromModCxt(Cxt)->findMemoryExports(genStrView(Name))); |
2509 | 0 | } |
2510 | 0 | return nullptr; |
2511 | 0 | } |
2512 | | |
2513 | | WASMEDGE_CAPI_EXPORT WasmEdge_TagInstanceContext * |
2514 | | WasmEdge_ModuleInstanceFindTag(const WasmEdge_ModuleInstanceContext *Cxt, |
2515 | 0 | const WasmEdge_String Name) noexcept { |
2516 | 0 | if (Cxt) { |
2517 | 0 | return toTagCxt(fromModCxt(Cxt)->findTagExports(genStrView(Name))); |
2518 | 0 | } |
2519 | 0 | return nullptr; |
2520 | 0 | } |
2521 | | |
2522 | | WASMEDGE_CAPI_EXPORT WasmEdge_GlobalInstanceContext * |
2523 | | WasmEdge_ModuleInstanceFindGlobal(const WasmEdge_ModuleInstanceContext *Cxt, |
2524 | 0 | const WasmEdge_String Name) noexcept { |
2525 | 0 | if (Cxt) { |
2526 | 0 | return toGlobCxt(fromModCxt(Cxt)->findGlobalExports(genStrView(Name))); |
2527 | 0 | } |
2528 | 0 | return nullptr; |
2529 | 0 | } |
2530 | | |
2531 | | WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_ModuleInstanceListFunctionLength( |
2532 | 0 | const WasmEdge_ModuleInstanceContext *Cxt) noexcept { |
2533 | 0 | if (Cxt) { |
2534 | 0 | return fromModCxt(Cxt)->getFuncExportNum(); |
2535 | 0 | } |
2536 | 0 | return 0; |
2537 | 0 | } |
2538 | | |
2539 | | WASMEDGE_CAPI_EXPORT extern uint32_t |
2540 | | WasmEdge_ModuleInstanceListFunction(const WasmEdge_ModuleInstanceContext *Cxt, |
2541 | | WasmEdge_String *Names, |
2542 | 0 | const uint32_t Len) noexcept { |
2543 | 0 | if (Cxt) { |
2544 | 0 | return fromModCxt(Cxt)->getFuncExports( |
2545 | 0 | [&](auto &Map) { return fillMap(Map, Names, Len); }); |
2546 | 0 | } |
2547 | 0 | return 0; |
2548 | 0 | } |
2549 | | |
2550 | | WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_ModuleInstanceListTableLength( |
2551 | 0 | const WasmEdge_ModuleInstanceContext *Cxt) noexcept { |
2552 | 0 | if (Cxt) { |
2553 | 0 | return fromModCxt(Cxt)->getTableExportNum(); |
2554 | 0 | } |
2555 | 0 | return 0; |
2556 | 0 | } |
2557 | | |
2558 | | WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_ModuleInstanceListTable( |
2559 | | const WasmEdge_ModuleInstanceContext *Cxt, WasmEdge_String *Names, |
2560 | 0 | const uint32_t Len) noexcept { |
2561 | 0 | if (Cxt) { |
2562 | 0 | return fromModCxt(Cxt)->getTableExports( |
2563 | 0 | [&](auto &Map) { return fillMap(Map, Names, Len); }); |
2564 | 0 | } |
2565 | 0 | return 0; |
2566 | 0 | } |
2567 | | |
2568 | | WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_ModuleInstanceListMemoryLength( |
2569 | 0 | const WasmEdge_ModuleInstanceContext *Cxt) noexcept { |
2570 | 0 | if (Cxt) { |
2571 | 0 | return fromModCxt(Cxt)->getMemoryExportNum(); |
2572 | 0 | } |
2573 | 0 | return 0; |
2574 | 0 | } |
2575 | | |
2576 | | WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_ModuleInstanceListMemory( |
2577 | | const WasmEdge_ModuleInstanceContext *Cxt, WasmEdge_String *Names, |
2578 | 0 | const uint32_t Len) noexcept { |
2579 | 0 | if (Cxt) { |
2580 | 0 | return fromModCxt(Cxt)->getMemoryExports( |
2581 | 0 | [&](auto &Map) { return fillMap(Map, Names, Len); }); |
2582 | 0 | } |
2583 | 0 | return 0; |
2584 | 0 | } |
2585 | | |
2586 | | WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_ModuleInstanceListTagLength( |
2587 | 0 | const WasmEdge_ModuleInstanceContext *Cxt) noexcept { |
2588 | 0 | if (Cxt) { |
2589 | 0 | return fromModCxt(Cxt)->getTagExportNum(); |
2590 | 0 | } |
2591 | 0 | return 0; |
2592 | 0 | } |
2593 | | |
2594 | | WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_ModuleInstanceListTag( |
2595 | | const WasmEdge_ModuleInstanceContext *Cxt, WasmEdge_String *Names, |
2596 | 0 | const uint32_t Len) noexcept { |
2597 | 0 | if (Cxt) { |
2598 | 0 | return fromModCxt(Cxt)->getTagExports( |
2599 | 0 | [&](auto &Map) { return fillMap(Map, Names, Len); }); |
2600 | 0 | } |
2601 | 0 | return 0; |
2602 | 0 | } |
2603 | | |
2604 | | WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_ModuleInstanceListGlobalLength( |
2605 | 0 | const WasmEdge_ModuleInstanceContext *Cxt) noexcept { |
2606 | 0 | if (Cxt) { |
2607 | 0 | return fromModCxt(Cxt)->getGlobalExportNum(); |
2608 | 0 | } |
2609 | 0 | return 0; |
2610 | 0 | } |
2611 | | |
2612 | | WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_ModuleInstanceListGlobal( |
2613 | | const WasmEdge_ModuleInstanceContext *Cxt, WasmEdge_String *Names, |
2614 | 0 | const uint32_t Len) noexcept { |
2615 | 0 | if (Cxt) { |
2616 | 0 | return fromModCxt(Cxt)->getGlobalExports( |
2617 | 0 | [&](auto &Map) { return fillMap(Map, Names, Len); }); |
2618 | 0 | } |
2619 | 0 | return 0; |
2620 | 0 | } |
2621 | | |
2622 | | WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_ModuleInstanceAddFunction( |
2623 | | WasmEdge_ModuleInstanceContext *Cxt, const WasmEdge_String Name, |
2624 | 0 | WasmEdge_FunctionInstanceContext *FuncCxt) noexcept { |
2625 | 0 | return wrap( |
2626 | 0 | [&]() -> WasmEdge::Expect<void> { |
2627 | 0 | std::unique_ptr<WasmEdge::Runtime::Instance::FunctionInstance> Inst( |
2628 | 0 | fromFuncCxt(FuncCxt)); |
2629 | 0 | auto Res = |
2630 | 0 | fromModCxt(Cxt)->addHostFunc(genStrView(Name), std::move(Inst)); |
2631 | 0 | if (!Res) { |
2632 | 0 | static_cast<void>(Inst.release()); |
2633 | 0 | } |
2634 | 0 | return Res; |
2635 | 0 | }, |
2636 | 0 | EmptyThen, Cxt, FuncCxt); |
2637 | 0 | } |
2638 | | |
2639 | | WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_ModuleInstanceAddTable( |
2640 | | WasmEdge_ModuleInstanceContext *Cxt, const WasmEdge_String Name, |
2641 | 0 | WasmEdge_TableInstanceContext *TableCxt) noexcept { |
2642 | 0 | return wrap( |
2643 | 0 | [&]() -> WasmEdge::Expect<void> { |
2644 | 0 | std::unique_ptr<WasmEdge::Runtime::Instance::TableInstance> Inst( |
2645 | 0 | fromTabCxt(TableCxt)); |
2646 | 0 | auto Res = |
2647 | 0 | fromModCxt(Cxt)->addHostTable(genStrView(Name), std::move(Inst)); |
2648 | 0 | if (!Res) { |
2649 | 0 | static_cast<void>(Inst.release()); |
2650 | 0 | } |
2651 | 0 | return Res; |
2652 | 0 | }, |
2653 | 0 | EmptyThen, Cxt, TableCxt); |
2654 | 0 | } |
2655 | | |
2656 | | WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_ModuleInstanceAddMemory( |
2657 | | WasmEdge_ModuleInstanceContext *Cxt, const WasmEdge_String Name, |
2658 | 0 | WasmEdge_MemoryInstanceContext *MemoryCxt) noexcept { |
2659 | 0 | return wrap( |
2660 | 0 | [&]() -> WasmEdge::Expect<void> { |
2661 | 0 | std::unique_ptr<WasmEdge::Runtime::Instance::MemoryInstance> Inst( |
2662 | 0 | fromMemCxt(MemoryCxt)); |
2663 | 0 | auto Res = |
2664 | 0 | fromModCxt(Cxt)->addHostMemory(genStrView(Name), std::move(Inst)); |
2665 | 0 | if (!Res) { |
2666 | 0 | static_cast<void>(Inst.release()); |
2667 | 0 | } |
2668 | 0 | return Res; |
2669 | 0 | }, |
2670 | 0 | EmptyThen, Cxt, MemoryCxt); |
2671 | 0 | } |
2672 | | |
2673 | | WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_ModuleInstanceAddGlobal( |
2674 | | WasmEdge_ModuleInstanceContext *Cxt, const WasmEdge_String Name, |
2675 | 0 | WasmEdge_GlobalInstanceContext *GlobalCxt) noexcept { |
2676 | 0 | return wrap( |
2677 | 0 | [&]() -> WasmEdge::Expect<void> { |
2678 | 0 | std::unique_ptr<WasmEdge::Runtime::Instance::GlobalInstance> Inst( |
2679 | 0 | fromGlobCxt(GlobalCxt)); |
2680 | 0 | auto Res = |
2681 | 0 | fromModCxt(Cxt)->addHostGlobal(genStrView(Name), std::move(Inst)); |
2682 | 0 | if (!Res) { |
2683 | 0 | static_cast<void>(Inst.release()); |
2684 | 0 | } |
2685 | 0 | return Res; |
2686 | 0 | }, |
2687 | 0 | EmptyThen, Cxt, GlobalCxt); |
2688 | 0 | } |
2689 | | |
2690 | | WASMEDGE_CAPI_EXPORT void |
2691 | 0 | WasmEdge_ModuleInstanceDelete(WasmEdge_ModuleInstanceContext *Cxt) noexcept { |
2692 | 0 | auto *Mod = fromModCxt(Cxt); |
2693 | 0 | if (Mod) { |
2694 | 0 | Mod->terminate(); |
2695 | 0 | } |
2696 | 0 | } |
2697 | | |
2698 | | // <<<<<<<< WasmEdge module instance functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
2699 | | |
2700 | | // >>>>>>>> WasmEdge function instance functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> |
2701 | | |
2702 | | WASMEDGE_CAPI_EXPORT WasmEdge_FunctionInstanceContext * |
2703 | | WasmEdge_FunctionInstanceCreate(const WasmEdge_FunctionTypeContext *Type, |
2704 | | WasmEdge_HostFunc_t HostFunc, void *Data, |
2705 | 0 | const uint64_t Cost) noexcept { |
2706 | 0 | try { |
2707 | 0 | if (Type && HostFunc) { |
2708 | 0 | return toFuncCxt(new WasmEdge::Runtime::Instance::FunctionInstance( |
2709 | 0 | std::make_unique<CAPIHostFunc>(fromFuncTypeCxt(Type), HostFunc, Data, |
2710 | 0 | Cost))); |
2711 | 0 | } |
2712 | 0 | } catch (...) { |
2713 | 0 | handleCAPIError(); |
2714 | 0 | } |
2715 | 0 | return nullptr; |
2716 | 0 | } |
2717 | | |
2718 | | WASMEDGE_CAPI_EXPORT WasmEdge_FunctionInstanceContext * |
2719 | | WasmEdge_FunctionInstanceCreateBinding(const WasmEdge_FunctionTypeContext *Type, |
2720 | | WasmEdge_WrapFunc_t WrapFunc, |
2721 | | void *Binding, void *Data, |
2722 | 0 | const uint64_t Cost) noexcept { |
2723 | 0 | try { |
2724 | 0 | if (Type && WrapFunc) { |
2725 | 0 | return toFuncCxt(new WasmEdge::Runtime::Instance::FunctionInstance( |
2726 | 0 | std::make_unique<CAPIHostFunc>(fromFuncTypeCxt(Type), WrapFunc, |
2727 | 0 | Binding, Data, Cost))); |
2728 | 0 | } |
2729 | 0 | } catch (...) { |
2730 | 0 | handleCAPIError(); |
2731 | 0 | } |
2732 | 0 | return nullptr; |
2733 | 0 | } |
2734 | | |
2735 | | WASMEDGE_CAPI_EXPORT const WasmEdge_FunctionTypeContext * |
2736 | | WasmEdge_FunctionInstanceGetFunctionType( |
2737 | 0 | const WasmEdge_FunctionInstanceContext *Cxt) noexcept { |
2738 | 0 | if (Cxt) { |
2739 | 0 | return toFuncTypeCxt(&fromFuncCxt(Cxt)->getFuncType()); |
2740 | 0 | } |
2741 | 0 | return nullptr; |
2742 | 0 | } |
2743 | | |
2744 | | WASMEDGE_CAPI_EXPORT extern const void *WasmEdge_FunctionInstanceGetData( |
2745 | 0 | const WasmEdge_FunctionInstanceContext *Cxt) noexcept { |
2746 | 0 | if (Cxt) { |
2747 | 0 | return reinterpret_cast<CAPIHostFunc *>(&fromFuncCxt(Cxt)->getHostFunc()) |
2748 | 0 | ->getData(); |
2749 | 0 | } |
2750 | 0 | return nullptr; |
2751 | 0 | } |
2752 | | |
2753 | | WASMEDGE_CAPI_EXPORT void WasmEdge_FunctionInstanceDelete( |
2754 | 0 | WasmEdge_FunctionInstanceContext *Cxt) noexcept { |
2755 | 0 | delete fromFuncCxt(Cxt); |
2756 | 0 | } |
2757 | | |
2758 | | // <<<<<<<< WasmEdge function instance functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
2759 | | |
2760 | | // >>>>>>>> WasmEdge table instance functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> |
2761 | | |
2762 | | WASMEDGE_CAPI_EXPORT WasmEdge_TableInstanceContext * |
2763 | | WasmEdge_TableInstanceCreate( |
2764 | 0 | const WasmEdge_TableTypeContext *TabType) noexcept { |
2765 | 0 | try { |
2766 | 0 | if (TabType) { |
2767 | 0 | const AST::TableType &TType = *fromTabTypeCxt(TabType); |
2768 | 0 | if (!TType.getRefType().isNullableRefType()) { |
2769 | 0 | spdlog::error(WasmEdge::ErrCode::Value::NonNullRequired); |
2770 | 0 | return nullptr; |
2771 | 0 | } |
2772 | 0 | return toTabCxt(new WasmEdge::Runtime::Instance::TableInstance(TType)); |
2773 | 0 | } |
2774 | 0 | } catch (...) { |
2775 | 0 | handleCAPIError(); |
2776 | 0 | } |
2777 | 0 | return nullptr; |
2778 | 0 | } |
2779 | | |
2780 | | WASMEDGE_CAPI_EXPORT extern WasmEdge_TableInstanceContext * |
2781 | | WasmEdge_TableInstanceCreateWithInit(const WasmEdge_TableTypeContext *TabType, |
2782 | 0 | const WasmEdge_Value Value) noexcept { |
2783 | 0 | try { |
2784 | 0 | if (TabType) { |
2785 | | // Comparison of the value types needs the module instance to retrieve the |
2786 | | // function type index after applying the typed function reference |
2787 | | // proposal. It's impossible to do this without refactoring. Therefore |
2788 | | // simply match the FuncRef and ExternRef here. |
2789 | 0 | const AST::TableType &TType = *fromTabTypeCxt(TabType); |
2790 | 0 | WasmEdge::ValType GotType = genValType(Value.Type); |
2791 | 0 | if (TType.getRefType().isFuncRefType() != GotType.isFuncRefType()) { |
2792 | 0 | spdlog::error(WasmEdge::ErrCode::Value::RefTypeMismatch); |
2793 | 0 | spdlog::error( |
2794 | 0 | WasmEdge::ErrInfo::InfoMismatch(TType.getRefType(), GotType)); |
2795 | 0 | return nullptr; |
2796 | 0 | } |
2797 | 0 | auto Val = WasmEdge::ValVariant( |
2798 | 0 | to_WasmEdge_128_t<WasmEdge::uint128_t>(Value.Value)) |
2799 | 0 | .get<WasmEdge::RefVariant>(); |
2800 | 0 | if (!TType.getRefType().isNullableRefType() && Val.isNull()) { |
2801 | 0 | spdlog::error(WasmEdge::ErrCode::Value::NonNullRequired); |
2802 | 0 | return nullptr; |
2803 | 0 | } |
2804 | 0 | return toTabCxt( |
2805 | 0 | new WasmEdge::Runtime::Instance::TableInstance(TType, Val)); |
2806 | 0 | } |
2807 | 0 | } catch (...) { |
2808 | 0 | handleCAPIError(); |
2809 | 0 | } |
2810 | 0 | return nullptr; |
2811 | 0 | } |
2812 | | |
2813 | | WASMEDGE_CAPI_EXPORT const WasmEdge_TableTypeContext * |
2814 | | WasmEdge_TableInstanceGetTableType( |
2815 | 0 | const WasmEdge_TableInstanceContext *Cxt) noexcept { |
2816 | 0 | if (Cxt) { |
2817 | 0 | return toTabTypeCxt(&fromTabCxt(Cxt)->getTableType()); |
2818 | 0 | } |
2819 | 0 | return nullptr; |
2820 | 0 | } |
2821 | | |
2822 | | WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_TableInstanceGetData( |
2823 | | const WasmEdge_TableInstanceContext *Cxt, WasmEdge_Value *Data, |
2824 | 0 | const uint64_t Offset) noexcept { |
2825 | 0 | return wrap([&]() { return fromTabCxt(Cxt)->getRefAddr(Offset); }, |
2826 | 0 | [&Data, &Cxt](auto &&Res) { |
2827 | 0 | *Data = genWasmEdge_Value( |
2828 | 0 | *Res, fromTabCxt(Cxt)->getTableType().getRefType()); |
2829 | 0 | }, |
2830 | 0 | Cxt, Data); |
2831 | 0 | } |
2832 | | |
2833 | | WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_TableInstanceSetData( |
2834 | | WasmEdge_TableInstanceContext *Cxt, WasmEdge_Value Data, |
2835 | 0 | const uint64_t Offset) noexcept { |
2836 | 0 | return wrap( |
2837 | 0 | [&]() -> WasmEdge::Expect<void> { |
2838 | | // Comparison of the value types needs the module instance to retrieve |
2839 | | // the function type index after applying the typed function reference |
2840 | | // proposal. It's impossible to do this without refactoring. Therefore |
2841 | | // simply match the FuncRef and ExternRef here. |
2842 | 0 | WasmEdge::ValType ExpType = |
2843 | 0 | fromTabCxt(Cxt)->getTableType().getRefType(); |
2844 | 0 | WasmEdge::ValType GotType = genValType(Data.Type); |
2845 | 0 | if (!GotType.isRefType() || |
2846 | 0 | ExpType.isFuncRefType() != GotType.isFuncRefType()) { |
2847 | 0 | spdlog::error(WasmEdge::ErrCode::Value::RefTypeMismatch); |
2848 | 0 | spdlog::error(WasmEdge::ErrInfo::InfoMismatch(ExpType, GotType)); |
2849 | 0 | return Unexpect(WasmEdge::ErrCode::Value::RefTypeMismatch); |
2850 | 0 | } |
2851 | 0 | auto Val = WasmEdge::ValVariant( |
2852 | 0 | to_WasmEdge_128_t<WasmEdge::uint128_t>(Data.Value)) |
2853 | 0 | .get<WasmEdge::RefVariant>(); |
2854 | 0 | if (!ExpType.isNullableRefType() && Val.isNull()) { |
2855 | | // If this table is not a nullable ref type, the data should not be |
2856 | | // null. |
2857 | 0 | spdlog::error(WasmEdge::ErrCode::Value::NonNullRequired); |
2858 | 0 | return Unexpect(WasmEdge::ErrCode::Value::NonNullRequired); |
2859 | 0 | } |
2860 | 0 | return fromTabCxt(Cxt)->setRefAddr(Offset, Val); |
2861 | 0 | }, |
2862 | 0 | EmptyThen, Cxt); |
2863 | 0 | } |
2864 | | |
2865 | | WASMEDGE_CAPI_EXPORT uint64_t WasmEdge_TableInstanceGetSize( |
2866 | 0 | const WasmEdge_TableInstanceContext *Cxt) noexcept { |
2867 | 0 | if (Cxt) { |
2868 | 0 | return fromTabCxt(Cxt)->getSize(); |
2869 | 0 | } |
2870 | 0 | return 0; |
2871 | 0 | } |
2872 | | |
2873 | | WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_TableInstanceGrow( |
2874 | 0 | WasmEdge_TableInstanceContext *Cxt, const uint64_t Size) noexcept { |
2875 | 0 | return wrap( |
2876 | 0 | [&]() -> WasmEdge::Expect<void> { |
2877 | 0 | if (fromTabCxt(Cxt)->growTable(Size)) { |
2878 | 0 | return {}; |
2879 | 0 | } else { |
2880 | 0 | spdlog::error(WasmEdge::ErrCode::Value::TableOutOfBounds); |
2881 | 0 | return WasmEdge::Unexpect(WasmEdge::ErrCode::Value::TableOutOfBounds); |
2882 | 0 | } |
2883 | 0 | }, |
2884 | 0 | EmptyThen, Cxt); |
2885 | 0 | } |
2886 | | |
2887 | | WASMEDGE_CAPI_EXPORT void |
2888 | 0 | WasmEdge_TableInstanceDelete(WasmEdge_TableInstanceContext *Cxt) noexcept { |
2889 | 0 | delete fromTabCxt(Cxt); |
2890 | 0 | } |
2891 | | |
2892 | | // <<<<<<<< WasmEdge table instance functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
2893 | | |
2894 | | // >>>>>>>> WasmEdge memory instance functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> |
2895 | | |
2896 | | WASMEDGE_CAPI_EXPORT WasmEdge_MemoryInstanceContext * |
2897 | | WasmEdge_MemoryInstanceCreate( |
2898 | 0 | const WasmEdge_MemoryTypeContext *MemType) noexcept { |
2899 | 0 | try { |
2900 | 0 | if (MemType) { |
2901 | 0 | return toMemCxt(new WasmEdge::Runtime::Instance::MemoryInstance( |
2902 | 0 | *fromMemTypeCxt(MemType))); |
2903 | 0 | } |
2904 | 0 | } catch (...) { |
2905 | 0 | handleCAPIError(); |
2906 | 0 | } |
2907 | 0 | return nullptr; |
2908 | 0 | } |
2909 | | |
2910 | | WASMEDGE_CAPI_EXPORT const WasmEdge_MemoryTypeContext * |
2911 | | WasmEdge_MemoryInstanceGetMemoryType( |
2912 | 0 | const WasmEdge_MemoryInstanceContext *Cxt) noexcept { |
2913 | 0 | if (Cxt) { |
2914 | 0 | return toMemTypeCxt(&fromMemCxt(Cxt)->getMemoryType()); |
2915 | 0 | } |
2916 | 0 | return nullptr; |
2917 | 0 | } |
2918 | | |
2919 | | WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_MemoryInstanceGetData( |
2920 | | const WasmEdge_MemoryInstanceContext *Cxt, uint8_t *Data, |
2921 | 0 | const uint64_t Offset, const uint64_t Length) noexcept { |
2922 | 0 | return wrap([&]() { return fromMemCxt(Cxt)->getBytes(Offset, Length); }, |
2923 | 0 | [&](auto &&Res) { std::copy_n((*Res).begin(), Length, Data); }, |
2924 | 0 | Cxt, Data); |
2925 | 0 | } |
2926 | | |
2927 | | WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_MemoryInstanceSetData( |
2928 | | WasmEdge_MemoryInstanceContext *Cxt, const uint8_t *Data, |
2929 | 0 | const uint64_t Offset, const uint64_t Length) noexcept { |
2930 | 0 | return wrap( |
2931 | 0 | [&]() { |
2932 | 0 | return fromMemCxt(Cxt)->setBytes(genSpan(Data, Length), Offset, 0, |
2933 | 0 | Length); |
2934 | 0 | }, |
2935 | 0 | EmptyThen, Cxt, Data); |
2936 | 0 | } |
2937 | | |
2938 | | WASMEDGE_CAPI_EXPORT uint8_t * |
2939 | | WasmEdge_MemoryInstanceGetPointer(WasmEdge_MemoryInstanceContext *Cxt, |
2940 | | const uint64_t Offset, |
2941 | 0 | const uint64_t Length) noexcept { |
2942 | 0 | if (Cxt) { |
2943 | 0 | const auto S = fromMemCxt(Cxt)->getSpan<uint8_t>(Offset, Length); |
2944 | 0 | if (S.size() == Length) { |
2945 | 0 | return S.data(); |
2946 | 0 | } |
2947 | 0 | } |
2948 | 0 | return nullptr; |
2949 | 0 | } |
2950 | | |
2951 | | WASMEDGE_CAPI_EXPORT const uint8_t *WasmEdge_MemoryInstanceGetPointerConst( |
2952 | | const WasmEdge_MemoryInstanceContext *Cxt, const uint64_t Offset, |
2953 | 0 | const uint64_t Length) noexcept { |
2954 | 0 | if (Cxt) { |
2955 | 0 | const auto S = fromMemCxt(Cxt)->getSpan<const uint8_t>(Offset, Length); |
2956 | 0 | if (S.size() == Length) { |
2957 | 0 | return S.data(); |
2958 | 0 | } |
2959 | 0 | } |
2960 | 0 | return nullptr; |
2961 | 0 | } |
2962 | | |
2963 | | WASMEDGE_CAPI_EXPORT uint64_t WasmEdge_MemoryInstanceGetPageSize( |
2964 | 0 | const WasmEdge_MemoryInstanceContext *Cxt) noexcept { |
2965 | 0 | if (Cxt) { |
2966 | 0 | return fromMemCxt(Cxt)->getPageSize(); |
2967 | 0 | } |
2968 | 0 | return 0; |
2969 | 0 | } |
2970 | | |
2971 | | WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_MemoryInstanceGrowPage( |
2972 | 0 | WasmEdge_MemoryInstanceContext *Cxt, const uint64_t Page) noexcept { |
2973 | 0 | return wrap( |
2974 | 0 | [&]() -> WasmEdge::Expect<void> { |
2975 | 0 | if (fromMemCxt(Cxt)->growPage(Page)) { |
2976 | 0 | return {}; |
2977 | 0 | } else { |
2978 | 0 | spdlog::error(WasmEdge::ErrCode::Value::MemoryOutOfBounds); |
2979 | 0 | return WasmEdge::Unexpect( |
2980 | 0 | WasmEdge::ErrCode::Value::MemoryOutOfBounds); |
2981 | 0 | } |
2982 | 0 | }, |
2983 | 0 | EmptyThen, Cxt); |
2984 | 0 | } |
2985 | | |
2986 | | WASMEDGE_CAPI_EXPORT void |
2987 | 0 | WasmEdge_MemoryInstanceDelete(WasmEdge_MemoryInstanceContext *Cxt) noexcept { |
2988 | 0 | delete fromMemCxt(Cxt); |
2989 | 0 | } |
2990 | | |
2991 | | // <<<<<<<< WasmEdge memory instance functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
2992 | | |
2993 | | // >>>>>>>> WasmEdge tag instance functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> |
2994 | | |
2995 | | WASMEDGE_CAPI_EXPORT const WasmEdge_TagTypeContext * |
2996 | | WasmEdge_TagInstanceGetTagType( |
2997 | 0 | const WasmEdge_TagInstanceContext *Cxt) noexcept { |
2998 | 0 | if (Cxt) { |
2999 | 0 | return toTagTypeCxt(&fromTagCxt(Cxt)->getTagType()); |
3000 | 0 | } |
3001 | 0 | return nullptr; |
3002 | 0 | } |
3003 | | |
3004 | | // <<<<<<<< WasmEdge tag instance functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
3005 | | |
3006 | | // >>>>>>>> WasmEdge global instance functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> |
3007 | | |
3008 | | WASMEDGE_CAPI_EXPORT WasmEdge_GlobalInstanceContext * |
3009 | | WasmEdge_GlobalInstanceCreate(const WasmEdge_GlobalTypeContext *GlobType, |
3010 | 0 | const WasmEdge_Value Value) noexcept { |
3011 | 0 | try { |
3012 | 0 | if (GlobType) { |
3013 | | // Comparison of the value types needs the module instance to retrieve the |
3014 | | // function type index after applying the typed function reference |
3015 | | // proposal. It's impossible to do this without refactoring. Therefore |
3016 | | // simply match the FuncRef and ExternRef here. |
3017 | 0 | const AST::GlobalType >ype = *fromGlobTypeCxt(GlobType); |
3018 | 0 | WasmEdge::ValType ExpType = GType.getValType(); |
3019 | 0 | WasmEdge::ValType GotType = genValType(Value.Type); |
3020 | 0 | if (ExpType.isFuncRefType() != GotType.isFuncRefType()) { |
3021 | 0 | spdlog::error(WasmEdge::ErrCode::Value::SetValueErrorType); |
3022 | 0 | spdlog::error(WasmEdge::ErrInfo::InfoMismatch(ExpType, GotType)); |
3023 | 0 | return nullptr; |
3024 | 0 | } |
3025 | | |
3026 | 0 | WasmEdge::ValVariant Val = |
3027 | 0 | to_WasmEdge_128_t<WasmEdge::uint128_t>(Value.Value); |
3028 | 0 | if (ExpType.isRefType()) { |
3029 | | // Reference type case. |
3030 | 0 | if (!ExpType.isNullableRefType() && |
3031 | 0 | Val.get<WasmEdge::RefVariant>().isNull()) { |
3032 | | // If this global is not a nullable ref type, the data should not be |
3033 | | // null. |
3034 | 0 | spdlog::error(WasmEdge::ErrCode::Value::NonNullRequired); |
3035 | 0 | return nullptr; |
3036 | 0 | } |
3037 | 0 | } else { |
3038 | | // Number type case. |
3039 | 0 | if (ExpType != GotType) { |
3040 | 0 | spdlog::error(WasmEdge::ErrCode::Value::SetValueErrorType); |
3041 | 0 | return nullptr; |
3042 | 0 | } |
3043 | 0 | } |
3044 | 0 | return toGlobCxt( |
3045 | 0 | new WasmEdge::Runtime::Instance::GlobalInstance(GType, Val)); |
3046 | 0 | } |
3047 | 0 | } catch (...) { |
3048 | 0 | handleCAPIError(); |
3049 | 0 | } |
3050 | 0 | return nullptr; |
3051 | 0 | } |
3052 | | |
3053 | | WASMEDGE_CAPI_EXPORT const WasmEdge_GlobalTypeContext * |
3054 | | WasmEdge_GlobalInstanceGetGlobalType( |
3055 | 0 | const WasmEdge_GlobalInstanceContext *Cxt) noexcept { |
3056 | 0 | if (Cxt) { |
3057 | 0 | return toGlobTypeCxt(&fromGlobCxt(Cxt)->getGlobalType()); |
3058 | 0 | } |
3059 | 0 | return nullptr; |
3060 | 0 | } |
3061 | | |
3062 | | WASMEDGE_CAPI_EXPORT WasmEdge_Value WasmEdge_GlobalInstanceGetValue( |
3063 | 0 | const WasmEdge_GlobalInstanceContext *Cxt) noexcept { |
3064 | 0 | if (Cxt) { |
3065 | 0 | return genWasmEdge_Value(fromGlobCxt(Cxt)->getValue(), |
3066 | 0 | fromGlobCxt(Cxt)->getGlobalType().getValType()); |
3067 | 0 | } |
3068 | 0 | return genWasmEdge_Value( |
3069 | 0 | WasmEdge::ValVariant(static_cast<WasmEdge::uint128_t>(0U)), |
3070 | 0 | TypeCode::I32); |
3071 | 0 | } |
3072 | | |
3073 | | WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_GlobalInstanceSetValue( |
3074 | 0 | WasmEdge_GlobalInstanceContext *Cxt, const WasmEdge_Value Value) noexcept { |
3075 | 0 | return wrap( |
3076 | 0 | [&]() -> WasmEdge::Expect<void> { |
3077 | 0 | const auto &GlobType = fromGlobCxt(Cxt)->getGlobalType(); |
3078 | 0 | if (GlobType.getValMut() != WasmEdge::ValMut::Var) { |
3079 | 0 | spdlog::error(WasmEdge::ErrCode::Value::SetValueToConst); |
3080 | 0 | return Unexpect(WasmEdge::ErrCode::Value::SetValueToConst); |
3081 | 0 | } |
3082 | | |
3083 | | // Comparison of the value types needs the module instance to retrieve |
3084 | | // the function type index after applying the typed function reference |
3085 | | // proposal. It's impossible to do this without refactoring. Therefore |
3086 | | // simply match the FuncRef and ExternRef here. |
3087 | 0 | WasmEdge::ValType ExpType = GlobType.getValType(); |
3088 | 0 | WasmEdge::ValType GotType = genValType(Value.Type); |
3089 | 0 | if (ExpType.isRefType() && |
3090 | 0 | ExpType.isFuncRefType() != GotType.isFuncRefType()) { |
3091 | 0 | spdlog::error(WasmEdge::ErrCode::Value::RefTypeMismatch); |
3092 | 0 | spdlog::error(WasmEdge::ErrInfo::InfoMismatch(ExpType, GotType)); |
3093 | 0 | return Unexpect(WasmEdge::ErrCode::Value::RefTypeMismatch); |
3094 | 0 | } |
3095 | | |
3096 | 0 | WasmEdge::ValVariant Val = |
3097 | 0 | to_WasmEdge_128_t<WasmEdge::uint128_t>(Value.Value); |
3098 | 0 | if (ExpType.isRefType()) { |
3099 | | // Reference type case. |
3100 | 0 | if (!ExpType.isNullableRefType() && |
3101 | 0 | Val.get<WasmEdge::RefVariant>().isNull()) { |
3102 | | // If this global is not a nullable ref type, the data should not be |
3103 | | // null. |
3104 | 0 | spdlog::error(WasmEdge::ErrCode::Value::NonNullRequired); |
3105 | 0 | return Unexpect(WasmEdge::ErrCode::Value::NonNullRequired); |
3106 | 0 | } |
3107 | 0 | } else { |
3108 | | // Number type case. |
3109 | 0 | if (ExpType != GotType) { |
3110 | 0 | spdlog::error(WasmEdge::ErrCode::Value::SetValueErrorType); |
3111 | 0 | return Unexpect(WasmEdge::ErrCode::Value::SetValueErrorType); |
3112 | 0 | } |
3113 | 0 | } |
3114 | 0 | fromGlobCxt(Cxt)->setValue(Val); |
3115 | 0 | return {}; |
3116 | 0 | }, |
3117 | 0 | EmptyThen, Cxt); |
3118 | 0 | } |
3119 | | |
3120 | | WASMEDGE_CAPI_EXPORT void |
3121 | 0 | WasmEdge_GlobalInstanceDelete(WasmEdge_GlobalInstanceContext *Cxt) noexcept { |
3122 | 0 | delete fromGlobCxt(Cxt); |
3123 | 0 | } |
3124 | | |
3125 | | // <<<<<<<< WasmEdge global instance functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
3126 | | |
3127 | | // >>>>>>>> WasmEdge calling frame functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> |
3128 | | |
3129 | | WASMEDGE_CAPI_EXPORT WasmEdge_ExecutorContext *WasmEdge_CallingFrameGetExecutor( |
3130 | 0 | const WasmEdge_CallingFrameContext *Cxt) noexcept { |
3131 | 0 | if (Cxt) { |
3132 | 0 | return toExecutorCxt(fromCallFrameCxt(Cxt)->getExecutor()); |
3133 | 0 | } |
3134 | 0 | return nullptr; |
3135 | 0 | } |
3136 | | |
3137 | | WASMEDGE_CAPI_EXPORT const WasmEdge_ModuleInstanceContext * |
3138 | | WasmEdge_CallingFrameGetModuleInstance( |
3139 | 0 | const WasmEdge_CallingFrameContext *Cxt) noexcept { |
3140 | 0 | if (Cxt) { |
3141 | 0 | return toModCxt(fromCallFrameCxt(Cxt)->getModule()); |
3142 | 0 | } |
3143 | 0 | return nullptr; |
3144 | 0 | } |
3145 | | |
3146 | | WASMEDGE_CAPI_EXPORT WasmEdge_MemoryInstanceContext * |
3147 | | WasmEdge_CallingFrameGetMemoryInstance(const WasmEdge_CallingFrameContext *Cxt, |
3148 | 0 | const uint32_t Idx) noexcept { |
3149 | 0 | if (Cxt) { |
3150 | 0 | return toMemCxt(fromCallFrameCxt(Cxt)->getMemoryByIndex(Idx)); |
3151 | 0 | } |
3152 | 0 | return nullptr; |
3153 | 0 | } |
3154 | | |
3155 | | // <<<<<<<< WasmEdge calling frame functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
3156 | | |
3157 | | // >>>>>>>> WasmEdge Async functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> |
3158 | | |
3159 | | WASMEDGE_CAPI_EXPORT void |
3160 | 0 | WasmEdge_AsyncWait(const WasmEdge_Async *Cxt) noexcept { |
3161 | 0 | if (Cxt) { |
3162 | 0 | Cxt->Async.wait(); |
3163 | 0 | } |
3164 | 0 | } |
3165 | | |
3166 | | WASMEDGE_CAPI_EXPORT bool |
3167 | | WasmEdge_AsyncWaitFor(const WasmEdge_Async *Cxt, |
3168 | 0 | uint64_t Milliseconds) noexcept { |
3169 | 0 | if (Cxt) { |
3170 | 0 | return Cxt->Async.waitFor(std::chrono::milliseconds(Milliseconds)); |
3171 | 0 | } |
3172 | 0 | return false; |
3173 | 0 | } |
3174 | | |
3175 | 0 | WASMEDGE_CAPI_EXPORT void WasmEdge_AsyncCancel(WasmEdge_Async *Cxt) noexcept { |
3176 | 0 | if (Cxt) { |
3177 | 0 | Cxt->Async.cancel(); |
3178 | 0 | } |
3179 | 0 | } |
3180 | | |
3181 | | WASMEDGE_CAPI_EXPORT uint32_t |
3182 | 0 | WasmEdge_AsyncGetReturnsLength(const WasmEdge_Async *Cxt) noexcept { |
3183 | 0 | if (Cxt) { |
3184 | 0 | if (auto Res = Cxt->Async.get()) { |
3185 | 0 | return static_cast<uint32_t>((*Res).size()); |
3186 | 0 | } |
3187 | 0 | } |
3188 | 0 | return 0; |
3189 | 0 | } |
3190 | | |
3191 | | WASMEDGE_CAPI_EXPORT WasmEdge_Result |
3192 | | WasmEdge_AsyncGet(const WasmEdge_Async *Cxt, WasmEdge_Value *Returns, |
3193 | 0 | const uint32_t ReturnLen) noexcept { |
3194 | 0 | return wrap( |
3195 | 0 | [&]() { return Cxt->Async.get(); }, |
3196 | 0 | [&](auto Res) { fillWasmEdge_ValueArr(*Res, Returns, ReturnLen); }, Cxt); |
3197 | 0 | } |
3198 | | |
3199 | 0 | WASMEDGE_CAPI_EXPORT void WasmEdge_AsyncDelete(WasmEdge_Async *Cxt) noexcept { |
3200 | 0 | delete Cxt; |
3201 | 0 | } |
3202 | | |
3203 | | // <<<<<<<< WasmEdge Async functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
3204 | | |
3205 | | // >>>>>>>> WasmEdge VM functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> |
3206 | | |
3207 | | WASMEDGE_CAPI_EXPORT WasmEdge_VMContext * |
3208 | | WasmEdge_VMCreate(const WasmEdge_ConfigureContext *ConfCxt, |
3209 | 0 | WasmEdge_StoreContext *StoreCxt) noexcept { |
3210 | 0 | try { |
3211 | 0 | if (ConfCxt) { |
3212 | 0 | if (StoreCxt) { |
3213 | 0 | return new WasmEdge_VMContext(ConfCxt->Conf, *fromStoreCxt(StoreCxt)); |
3214 | 0 | } else { |
3215 | 0 | return new WasmEdge_VMContext(ConfCxt->Conf); |
3216 | 0 | } |
3217 | 0 | } else { |
3218 | 0 | if (StoreCxt) { |
3219 | 0 | return new WasmEdge_VMContext(WasmEdge::Configure(), |
3220 | 0 | *fromStoreCxt(StoreCxt)); |
3221 | 0 | } else { |
3222 | 0 | return new WasmEdge_VMContext(WasmEdge::Configure()); |
3223 | 0 | } |
3224 | 0 | } |
3225 | 0 | } catch (...) { |
3226 | 0 | handleCAPIError(); |
3227 | 0 | return nullptr; |
3228 | 0 | } |
3229 | 0 | } |
3230 | | |
3231 | | WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_VMRegisterModuleFromFile( |
3232 | | WasmEdge_VMContext *Cxt, const WasmEdge_String ModuleName, |
3233 | 0 | const char *Path) noexcept { |
3234 | 0 | return wrap( |
3235 | 0 | [&]() { |
3236 | 0 | return Cxt->VM.registerModule(genStrView(ModuleName), genPath(Path)); |
3237 | 0 | }, |
3238 | 0 | EmptyThen, Cxt); |
3239 | 0 | } |
3240 | | |
3241 | | WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_VMRegisterModuleFromBuffer( |
3242 | | WasmEdge_VMContext *Cxt, const WasmEdge_String ModuleName, |
3243 | 0 | const uint8_t *Buf, const uint32_t BufLen) noexcept { |
3244 | 0 | return WasmEdge_VMRegisterModuleFromBytes(Cxt, ModuleName, |
3245 | 0 | WasmEdge_BytesWrap(Buf, BufLen)); |
3246 | 0 | } |
3247 | | |
3248 | | WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_VMRegisterModuleFromBytes( |
3249 | | WasmEdge_VMContext *Cxt, const WasmEdge_String ModuleName, |
3250 | 0 | const WasmEdge_Bytes Bytes) noexcept { |
3251 | 0 | return wrap( |
3252 | 0 | [&]() { |
3253 | 0 | return Cxt->VM.registerModule(genStrView(ModuleName), |
3254 | 0 | genSpan(Bytes.Buf, Bytes.Length)); |
3255 | 0 | }, |
3256 | 0 | EmptyThen, Cxt); |
3257 | 0 | } |
3258 | | |
3259 | | WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_VMRegisterModuleFromASTModule( |
3260 | | WasmEdge_VMContext *Cxt, const WasmEdge_String ModuleName, |
3261 | 0 | const WasmEdge_ASTModuleContext *ASTCxt) noexcept { |
3262 | 0 | return wrap( |
3263 | 0 | [&]() { |
3264 | 0 | return Cxt->VM.registerModule(genStrView(ModuleName), |
3265 | 0 | *fromASTModCxt(ASTCxt)); |
3266 | 0 | }, |
3267 | 0 | EmptyThen, Cxt, ASTCxt); |
3268 | 0 | } |
3269 | | |
3270 | | WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_VMRegisterModuleFromImport( |
3271 | | WasmEdge_VMContext *Cxt, |
3272 | 0 | const WasmEdge_ModuleInstanceContext *ImportCxt) noexcept { |
3273 | 0 | return wrap([&]() { return Cxt->VM.registerModule(*fromModCxt(ImportCxt)); }, |
3274 | 0 | EmptyThen, Cxt, ImportCxt); |
3275 | 0 | } |
3276 | | |
3277 | | WASMEDGE_CAPI_EXPORT WasmEdge_Result |
3278 | | WasmEdge_VMRegisterModuleFromImportWithAlias( |
3279 | | WasmEdge_VMContext *Cxt, const WasmEdge_String ModuleName, |
3280 | 0 | const WasmEdge_ModuleInstanceContext *ImportCxt) noexcept { |
3281 | 0 | return wrap( |
3282 | 0 | [&]() { |
3283 | 0 | return Cxt->VM.registerModule(genStrView(ModuleName), |
3284 | 0 | *fromModCxt(ImportCxt)); |
3285 | 0 | }, |
3286 | 0 | EmptyThen, Cxt, ImportCxt); |
3287 | 0 | } |
3288 | | |
3289 | | WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_VMRunWasmFromFile( |
3290 | | WasmEdge_VMContext *Cxt, const char *Path, const WasmEdge_String FuncName, |
3291 | | const WasmEdge_Value *Params, const uint32_t ParamLen, |
3292 | 0 | WasmEdge_Value *Returns, const uint32_t ReturnLen) noexcept { |
3293 | 0 | try { |
3294 | 0 | auto ParamPair = genParamPair(Params, ParamLen); |
3295 | 0 | return wrap( |
3296 | 0 | [&]() { |
3297 | 0 | return Cxt->VM.runWasmFile(genPath(Path), genStrView(FuncName), |
3298 | 0 | ParamPair.first, ParamPair.second); |
3299 | 0 | }, |
3300 | 0 | [&](auto Res) { fillWasmEdge_ValueArr(*Res, Returns, ReturnLen); }, |
3301 | 0 | Cxt); |
3302 | 0 | } catch (...) { |
3303 | 0 | return handleCAPIError(); |
3304 | 0 | } |
3305 | 0 | } |
3306 | | |
3307 | | WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_VMRunWasmFromBuffer( |
3308 | | WasmEdge_VMContext *Cxt, const uint8_t *Buf, const uint32_t BufLen, |
3309 | | const WasmEdge_String FuncName, const WasmEdge_Value *Params, |
3310 | | const uint32_t ParamLen, WasmEdge_Value *Returns, |
3311 | 0 | const uint32_t ReturnLen) noexcept { |
3312 | 0 | return WasmEdge_VMRunWasmFromBytes(Cxt, WasmEdge_BytesWrap(Buf, BufLen), |
3313 | 0 | FuncName, Params, ParamLen, Returns, |
3314 | 0 | ReturnLen); |
3315 | 0 | } |
3316 | | |
3317 | | WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_VMRunWasmFromBytes( |
3318 | | WasmEdge_VMContext *Cxt, const WasmEdge_Bytes Bytes, |
3319 | | const WasmEdge_String FuncName, const WasmEdge_Value *Params, |
3320 | | const uint32_t ParamLen, WasmEdge_Value *Returns, |
3321 | 0 | const uint32_t ReturnLen) noexcept { |
3322 | 0 | try { |
3323 | 0 | auto ParamPair = genParamPair(Params, ParamLen); |
3324 | 0 | return wrap( |
3325 | 0 | [&]() { |
3326 | 0 | return Cxt->VM.runWasmFile(genSpan(Bytes.Buf, Bytes.Length), |
3327 | 0 | genStrView(FuncName), ParamPair.first, |
3328 | 0 | ParamPair.second); |
3329 | 0 | }, |
3330 | 0 | [&](auto &&Res) { fillWasmEdge_ValueArr(*Res, Returns, ReturnLen); }, |
3331 | 0 | Cxt); |
3332 | 0 | } catch (...) { |
3333 | 0 | return handleCAPIError(); |
3334 | 0 | } |
3335 | 0 | } |
3336 | | |
3337 | | WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_VMRunWasmFromASTModule( |
3338 | | WasmEdge_VMContext *Cxt, const WasmEdge_ASTModuleContext *ASTCxt, |
3339 | | const WasmEdge_String FuncName, const WasmEdge_Value *Params, |
3340 | | const uint32_t ParamLen, WasmEdge_Value *Returns, |
3341 | 0 | const uint32_t ReturnLen) noexcept { |
3342 | 0 | try { |
3343 | 0 | auto ParamPair = genParamPair(Params, ParamLen); |
3344 | 0 | return wrap( |
3345 | 0 | [&]() { |
3346 | 0 | return Cxt->VM.runWasmFile(*fromASTModCxt(ASTCxt), |
3347 | 0 | genStrView(FuncName), ParamPair.first, |
3348 | 0 | ParamPair.second); |
3349 | 0 | }, |
3350 | 0 | [&](auto &&Res) { fillWasmEdge_ValueArr(*Res, Returns, ReturnLen); }, |
3351 | 0 | Cxt, ASTCxt); |
3352 | 0 | } catch (...) { |
3353 | 0 | return handleCAPIError(); |
3354 | 0 | } |
3355 | 0 | } |
3356 | | |
3357 | | WASMEDGE_CAPI_EXPORT WasmEdge_Async *WasmEdge_VMAsyncRunWasmFromFile( |
3358 | | WasmEdge_VMContext *Cxt, const char *Path, const WasmEdge_String FuncName, |
3359 | 0 | const WasmEdge_Value *Params, const uint32_t ParamLen) noexcept { |
3360 | 0 | try { |
3361 | 0 | auto ParamPair = genParamPair(Params, ParamLen); |
3362 | 0 | if (Cxt) { |
3363 | 0 | return new WasmEdge_Async( |
3364 | 0 | Cxt->VM.asyncRunWasmFile(genPath(Path), genStrView(FuncName), |
3365 | 0 | ParamPair.first, ParamPair.second)); |
3366 | 0 | } |
3367 | 0 | } catch (...) { |
3368 | 0 | handleCAPIError(); |
3369 | 0 | } |
3370 | 0 | return nullptr; |
3371 | 0 | } |
3372 | | |
3373 | | WASMEDGE_CAPI_EXPORT WasmEdge_Async *WasmEdge_VMAsyncRunWasmFromBuffer( |
3374 | | WasmEdge_VMContext *Cxt, const uint8_t *Buf, const uint32_t BufLen, |
3375 | | const WasmEdge_String FuncName, const WasmEdge_Value *Params, |
3376 | 0 | const uint32_t ParamLen) noexcept { |
3377 | 0 | return WasmEdge_VMAsyncRunWasmFromBytes(Cxt, WasmEdge_BytesWrap(Buf, BufLen), |
3378 | 0 | FuncName, Params, ParamLen); |
3379 | 0 | } |
3380 | | |
3381 | | WASMEDGE_CAPI_EXPORT WasmEdge_Async *WasmEdge_VMAsyncRunWasmFromBytes( |
3382 | | WasmEdge_VMContext *Cxt, const WasmEdge_Bytes Bytes, |
3383 | | const WasmEdge_String FuncName, const WasmEdge_Value *Params, |
3384 | 0 | const uint32_t ParamLen) noexcept { |
3385 | 0 | try { |
3386 | 0 | auto ParamPair = genParamPair(Params, ParamLen); |
3387 | 0 | if (Cxt) { |
3388 | 0 | return new WasmEdge_Async(Cxt->VM.asyncRunWasmFile( |
3389 | 0 | genSpan(Bytes.Buf, Bytes.Length), genStrView(FuncName), |
3390 | 0 | ParamPair.first, ParamPair.second)); |
3391 | 0 | } |
3392 | 0 | } catch (...) { |
3393 | 0 | handleCAPIError(); |
3394 | 0 | } |
3395 | 0 | return nullptr; |
3396 | 0 | } |
3397 | | |
3398 | | WASMEDGE_CAPI_EXPORT WasmEdge_Async *WasmEdge_VMAsyncRunWasmFromASTModule( |
3399 | | WasmEdge_VMContext *Cxt, const WasmEdge_ASTModuleContext *ASTCxt, |
3400 | | const WasmEdge_String FuncName, const WasmEdge_Value *Params, |
3401 | 0 | const uint32_t ParamLen) noexcept { |
3402 | 0 | try { |
3403 | 0 | auto ParamPair = genParamPair(Params, ParamLen); |
3404 | 0 | if (Cxt && ASTCxt) { |
3405 | 0 | return new WasmEdge_Async( |
3406 | 0 | Cxt->VM.asyncRunWasmFile(*fromASTModCxt(ASTCxt), genStrView(FuncName), |
3407 | 0 | ParamPair.first, ParamPair.second)); |
3408 | 0 | } |
3409 | 0 | } catch (...) { |
3410 | 0 | handleCAPIError(); |
3411 | 0 | } |
3412 | 0 | return nullptr; |
3413 | 0 | } |
3414 | | |
3415 | | WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_VMLoadWasmFromFile( |
3416 | 0 | WasmEdge_VMContext *Cxt, const char *Path) noexcept { |
3417 | 0 | return wrap([&]() { return Cxt->VM.loadWasm(genPath(Path)); }, EmptyThen, |
3418 | 0 | Cxt); |
3419 | 0 | } |
3420 | | |
3421 | | WASMEDGE_CAPI_EXPORT WasmEdge_Result |
3422 | | WasmEdge_VMLoadWasmFromBuffer(WasmEdge_VMContext *Cxt, const uint8_t *Buf, |
3423 | 0 | const uint32_t BufLen) noexcept { |
3424 | 0 | return WasmEdge_VMLoadWasmFromBytes(Cxt, WasmEdge_BytesWrap(Buf, BufLen)); |
3425 | 0 | } |
3426 | | |
3427 | | WASMEDGE_CAPI_EXPORT extern WasmEdge_Result |
3428 | | WasmEdge_VMLoadWasmFromBytes(WasmEdge_VMContext *Cxt, |
3429 | 0 | const WasmEdge_Bytes Bytes) noexcept { |
3430 | 0 | return wrap( |
3431 | 0 | [&]() { return Cxt->VM.loadWasm(genSpan(Bytes.Buf, Bytes.Length)); }, |
3432 | 0 | EmptyThen, Cxt); |
3433 | 0 | } |
3434 | | |
3435 | | WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_VMLoadWasmFromASTModule( |
3436 | 0 | WasmEdge_VMContext *Cxt, const WasmEdge_ASTModuleContext *ASTCxt) noexcept { |
3437 | 0 | return wrap([&]() { return Cxt->VM.loadWasm(*fromASTModCxt(ASTCxt)); }, |
3438 | 0 | EmptyThen, Cxt, ASTCxt); |
3439 | 0 | } |
3440 | | |
3441 | | WASMEDGE_CAPI_EXPORT WasmEdge_Result |
3442 | 0 | WasmEdge_VMValidate(WasmEdge_VMContext *Cxt) noexcept { |
3443 | 0 | return wrap([&]() { return Cxt->VM.validate(); }, EmptyThen, Cxt); |
3444 | 0 | } |
3445 | | |
3446 | | WASMEDGE_CAPI_EXPORT WasmEdge_Result |
3447 | 0 | WasmEdge_VMInstantiate(WasmEdge_VMContext *Cxt) noexcept { |
3448 | 0 | return wrap([&]() { return Cxt->VM.instantiate(); }, EmptyThen, Cxt); |
3449 | 0 | } |
3450 | | |
3451 | | WASMEDGE_CAPI_EXPORT WasmEdge_Result |
3452 | | WasmEdge_VMExecute(WasmEdge_VMContext *Cxt, const WasmEdge_String FuncName, |
3453 | | const WasmEdge_Value *Params, const uint32_t ParamLen, |
3454 | 0 | WasmEdge_Value *Returns, const uint32_t ReturnLen) noexcept { |
3455 | 0 | try { |
3456 | 0 | auto ParamPair = genParamPair(Params, ParamLen); |
3457 | 0 | return wrap( |
3458 | 0 | [&]() { |
3459 | 0 | return Cxt->VM.execute(genStrView(FuncName), ParamPair.first, |
3460 | 0 | ParamPair.second); |
3461 | 0 | }, |
3462 | 0 | [&](auto &&Res) { fillWasmEdge_ValueArr(*Res, Returns, ReturnLen); }, |
3463 | 0 | Cxt); |
3464 | 0 | } catch (...) { |
3465 | 0 | return handleCAPIError(); |
3466 | 0 | } |
3467 | 0 | } |
3468 | | |
3469 | | WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_VMExecuteRegistered( |
3470 | | WasmEdge_VMContext *Cxt, const WasmEdge_String ModuleName, |
3471 | | const WasmEdge_String FuncName, const WasmEdge_Value *Params, |
3472 | | const uint32_t ParamLen, WasmEdge_Value *Returns, |
3473 | 0 | const uint32_t ReturnLen) noexcept { |
3474 | 0 | try { |
3475 | 0 | auto ParamPair = genParamPair(Params, ParamLen); |
3476 | 0 | return wrap( |
3477 | 0 | [&]() { |
3478 | 0 | return Cxt->VM.execute(genStrView(ModuleName), genStrView(FuncName), |
3479 | 0 | ParamPair.first, ParamPair.second); |
3480 | 0 | }, |
3481 | 0 | [&](auto &&Res) { fillWasmEdge_ValueArr(*Res, Returns, ReturnLen); }, |
3482 | 0 | Cxt); |
3483 | 0 | } catch (...) { |
3484 | 0 | return handleCAPIError(); |
3485 | 0 | } |
3486 | 0 | } |
3487 | | |
3488 | | WASMEDGE_CAPI_EXPORT WasmEdge_Async * |
3489 | | WasmEdge_VMAsyncExecute(WasmEdge_VMContext *Cxt, const WasmEdge_String FuncName, |
3490 | | const WasmEdge_Value *Params, |
3491 | 0 | const uint32_t ParamLen) noexcept { |
3492 | 0 | try { |
3493 | 0 | auto ParamPair = genParamPair(Params, ParamLen); |
3494 | 0 | if (Cxt) { |
3495 | 0 | return new WasmEdge_Async(Cxt->VM.asyncExecute( |
3496 | 0 | genStrView(FuncName), ParamPair.first, ParamPair.second)); |
3497 | 0 | } |
3498 | 0 | } catch (...) { |
3499 | 0 | handleCAPIError(); |
3500 | 0 | } |
3501 | 0 | return nullptr; |
3502 | 0 | } |
3503 | | |
3504 | | WASMEDGE_CAPI_EXPORT WasmEdge_Async *WasmEdge_VMAsyncExecuteRegistered( |
3505 | | WasmEdge_VMContext *Cxt, const WasmEdge_String ModuleName, |
3506 | | const WasmEdge_String FuncName, const WasmEdge_Value *Params, |
3507 | 0 | const uint32_t ParamLen) noexcept { |
3508 | 0 | try { |
3509 | 0 | auto ParamPair = genParamPair(Params, ParamLen); |
3510 | 0 | if (Cxt) { |
3511 | 0 | return new WasmEdge_Async( |
3512 | 0 | Cxt->VM.asyncExecute(genStrView(ModuleName), genStrView(FuncName), |
3513 | 0 | ParamPair.first, ParamPair.second)); |
3514 | 0 | } |
3515 | 0 | } catch (...) { |
3516 | 0 | handleCAPIError(); |
3517 | 0 | } |
3518 | 0 | return nullptr; |
3519 | 0 | } |
3520 | | |
3521 | | WASMEDGE_CAPI_EXPORT const WasmEdge_FunctionTypeContext * |
3522 | | WasmEdge_VMGetFunctionType(const WasmEdge_VMContext *Cxt, |
3523 | 0 | const WasmEdge_String FuncName) noexcept { |
3524 | 0 | try { |
3525 | 0 | if (Cxt) { |
3526 | 0 | const auto FuncList = Cxt->VM.getFunctionList(); |
3527 | 0 | for (const auto &It : FuncList) { |
3528 | 0 | if (It.first == genStrView(FuncName)) { |
3529 | 0 | return toFuncTypeCxt(&It.second); |
3530 | 0 | } |
3531 | 0 | } |
3532 | 0 | } |
3533 | 0 | } catch (...) { |
3534 | 0 | handleCAPIError(); |
3535 | 0 | } |
3536 | 0 | return nullptr; |
3537 | 0 | } |
3538 | | |
3539 | | WASMEDGE_CAPI_EXPORT const WasmEdge_FunctionTypeContext * |
3540 | | WasmEdge_VMGetFunctionTypeRegistered(const WasmEdge_VMContext *Cxt, |
3541 | | const WasmEdge_String ModuleName, |
3542 | 0 | const WasmEdge_String FuncName) noexcept { |
3543 | 0 | if (Cxt) { |
3544 | 0 | const auto *ModInst = |
3545 | 0 | Cxt->VM.getStoreManager().findModule(genStrView(ModuleName)); |
3546 | 0 | if (ModInst != nullptr) { |
3547 | 0 | const auto *FuncInst = ModInst->findFuncExports(genStrView(FuncName)); |
3548 | 0 | if (FuncInst != nullptr) { |
3549 | 0 | return toFuncTypeCxt(&FuncInst->getFuncType()); |
3550 | 0 | } |
3551 | 0 | } |
3552 | 0 | } |
3553 | 0 | return nullptr; |
3554 | 0 | } |
3555 | | |
3556 | 0 | WASMEDGE_CAPI_EXPORT void WasmEdge_VMCleanup(WasmEdge_VMContext *Cxt) noexcept { |
3557 | 0 | if (Cxt) { |
3558 | 0 | Cxt->VM.cleanup(); |
3559 | 0 | } |
3560 | 0 | } |
3561 | | |
3562 | | WASMEDGE_CAPI_EXPORT void |
3563 | | WasmEdge_VMDeleteRegisteredModule(const WasmEdge_VMContext *Cxt, |
3564 | 0 | const WasmEdge_String ModuleName) noexcept { |
3565 | 0 | if (!Cxt || !ModuleName.Buf) { |
3566 | 0 | return; // Invalid input |
3567 | 0 | } |
3568 | | |
3569 | 0 | auto &VM = const_cast<WasmEdge_VMContext *>(Cxt)->VM; |
3570 | 0 | auto &StoreMgr = VM.getStoreManager(); |
3571 | 0 | auto NameStr = genStrView(ModuleName); |
3572 | | |
3573 | | // Unregister from VM |
3574 | 0 | VM.unregisterModule(NameStr); |
3575 | |
|
3576 | 0 | if (StoreMgr.findModule(NameStr)) { |
3577 | | // Unregister from StoreManager |
3578 | 0 | StoreMgr.unregisterModule(NameStr); |
3579 | 0 | } |
3580 | 0 | } |
3581 | | |
3582 | | WASMEDGE_CAPI_EXPORT void WasmEdge_VMForceDeleteRegisteredModule( |
3583 | 0 | const WasmEdge_VMContext *Cxt, const WasmEdge_String ModuleName) noexcept { |
3584 | 0 | WasmEdge_VMDeleteRegisteredModule(Cxt, ModuleName); |
3585 | 0 | } |
3586 | | |
3587 | | WASMEDGE_CAPI_EXPORT uint32_t |
3588 | 0 | WasmEdge_VMGetFunctionListLength(const WasmEdge_VMContext *Cxt) noexcept { |
3589 | 0 | try { |
3590 | 0 | if (Cxt) { |
3591 | 0 | return static_cast<uint32_t>(Cxt->VM.getFunctionList().size()); |
3592 | 0 | } |
3593 | 0 | } catch (...) { |
3594 | 0 | handleCAPIError(); |
3595 | 0 | } |
3596 | 0 | return 0; |
3597 | 0 | } |
3598 | | |
3599 | | WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_VMGetFunctionList( |
3600 | | const WasmEdge_VMContext *Cxt, WasmEdge_String *Names, |
3601 | | const WasmEdge_FunctionTypeContext **FuncTypes, |
3602 | 0 | const uint32_t Len) noexcept { |
3603 | 0 | if (Cxt) { |
3604 | | // Do not use VM::getFunctionList() here because it would allocate the |
3605 | | // returned function name strings. |
3606 | 0 | const auto *ModInst = Cxt->VM.getActiveModule(); |
3607 | 0 | if (ModInst != nullptr) { |
3608 | 0 | return ModInst->getFuncExports([&](const auto &FuncExp) { |
3609 | 0 | uint32_t I = 0; |
3610 | 0 | for (auto It = FuncExp.cbegin(); It != FuncExp.cend() && I < Len; |
3611 | 0 | It++, I++) { |
3612 | 0 | const auto *FuncInst = It->second; |
3613 | 0 | const auto &FuncType = FuncInst->getFuncType(); |
3614 | 0 | if (Names) { |
3615 | 0 | Names[I] = WasmEdge_String{ |
3616 | 0 | /* Length */ static_cast<uint32_t>(It->first.length()), |
3617 | 0 | /* Buf */ It->first.data()}; |
3618 | 0 | } |
3619 | 0 | if (FuncTypes) { |
3620 | 0 | FuncTypes[I] = toFuncTypeCxt(&FuncType); |
3621 | 0 | } |
3622 | 0 | } |
3623 | 0 | return static_cast<uint32_t>(FuncExp.size()); |
3624 | 0 | }); |
3625 | 0 | } |
3626 | 0 | } |
3627 | 0 | return 0; |
3628 | 0 | } |
3629 | | |
3630 | | WASMEDGE_CAPI_EXPORT WasmEdge_ModuleInstanceContext * |
3631 | | WasmEdge_VMGetImportModuleContext( |
3632 | | const WasmEdge_VMContext *Cxt, |
3633 | 0 | const enum WasmEdge_HostRegistration Reg) noexcept { |
3634 | 0 | if (Cxt) { |
3635 | 0 | return toModCxt( |
3636 | 0 | Cxt->VM.getImportModule(static_cast<WasmEdge::HostRegistration>(Reg))); |
3637 | 0 | } |
3638 | 0 | return nullptr; |
3639 | 0 | } |
3640 | | |
3641 | | WASMEDGE_CAPI_EXPORT uint32_t |
3642 | 0 | WasmEdge_VMListRegisteredModuleLength(const WasmEdge_VMContext *Cxt) noexcept { |
3643 | 0 | if (Cxt) { |
3644 | 0 | return Cxt->VM.getStoreManager().getModuleListSize(); |
3645 | 0 | } |
3646 | 0 | return 0; |
3647 | 0 | } |
3648 | | |
3649 | | WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_VMListRegisteredModule( |
3650 | | const WasmEdge_VMContext *Cxt, WasmEdge_String *Names, |
3651 | 0 | const uint32_t Len) noexcept { |
3652 | 0 | if (Cxt) { |
3653 | 0 | return Cxt->VM.getStoreManager().getModuleList( |
3654 | 0 | [&](auto &Map) { return fillMap(Map, Names, Len); }); |
3655 | 0 | } |
3656 | 0 | return 0; |
3657 | 0 | } |
3658 | | |
3659 | | WASMEDGE_CAPI_EXPORT const WasmEdge_ModuleInstanceContext * |
3660 | | WasmEdge_VMGetRegisteredModule(const WasmEdge_VMContext *Cxt, |
3661 | 0 | const WasmEdge_String ModuleName) noexcept { |
3662 | 0 | if (Cxt) { |
3663 | 0 | return toModCxt( |
3664 | 0 | Cxt->VM.getStoreManager().findModule(genStrView(ModuleName))); |
3665 | 0 | } |
3666 | 0 | return nullptr; |
3667 | 0 | } |
3668 | | |
3669 | | WASMEDGE_CAPI_EXPORT const WasmEdge_ModuleInstanceContext * |
3670 | 0 | WasmEdge_VMGetActiveModule(const WasmEdge_VMContext *Cxt) noexcept { |
3671 | 0 | if (Cxt) { |
3672 | 0 | return toModCxt(Cxt->VM.getActiveModule()); |
3673 | 0 | } |
3674 | 0 | return nullptr; |
3675 | 0 | } |
3676 | | |
3677 | | WASMEDGE_CAPI_EXPORT WasmEdge_StoreContext * |
3678 | 0 | WasmEdge_VMGetStoreContext(WasmEdge_VMContext *Cxt) noexcept { |
3679 | 0 | if (Cxt) { |
3680 | 0 | return toStoreCxt(&Cxt->VM.getStoreManager()); |
3681 | 0 | } |
3682 | 0 | return nullptr; |
3683 | 0 | } |
3684 | | |
3685 | | WASMEDGE_CAPI_EXPORT WasmEdge_LoaderContext * |
3686 | 0 | WasmEdge_VMGetLoaderContext(WasmEdge_VMContext *Cxt) noexcept { |
3687 | 0 | if (Cxt) { |
3688 | 0 | return toLoaderCxt(&Cxt->VM.getLoader()); |
3689 | 0 | } |
3690 | 0 | return nullptr; |
3691 | 0 | } |
3692 | | |
3693 | | WASMEDGE_CAPI_EXPORT WasmEdge_ValidatorContext * |
3694 | 0 | WasmEdge_VMGetValidatorContext(WasmEdge_VMContext *Cxt) noexcept { |
3695 | 0 | if (Cxt) { |
3696 | 0 | return toValidatorCxt(&Cxt->VM.getValidator()); |
3697 | 0 | } |
3698 | 0 | return nullptr; |
3699 | 0 | } |
3700 | | |
3701 | | WASMEDGE_CAPI_EXPORT WasmEdge_ExecutorContext * |
3702 | 0 | WasmEdge_VMGetExecutorContext(WasmEdge_VMContext *Cxt) noexcept { |
3703 | 0 | if (Cxt) { |
3704 | 0 | return toExecutorCxt(&Cxt->VM.getExecutor()); |
3705 | 0 | } |
3706 | 0 | return nullptr; |
3707 | 0 | } |
3708 | | |
3709 | | WASMEDGE_CAPI_EXPORT WasmEdge_StatisticsContext * |
3710 | 0 | WasmEdge_VMGetStatisticsContext(WasmEdge_VMContext *Cxt) noexcept { |
3711 | 0 | if (Cxt) { |
3712 | 0 | return toStatCxt(&Cxt->VM.getStatistics()); |
3713 | 0 | } |
3714 | 0 | return nullptr; |
3715 | 0 | } |
3716 | | |
3717 | 0 | WASMEDGE_CAPI_EXPORT void WasmEdge_VMDelete(WasmEdge_VMContext *Cxt) noexcept { |
3718 | 0 | delete Cxt; |
3719 | 0 | } |
3720 | | |
3721 | | // <<<<<<<< WasmEdge VM functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
3722 | | |
3723 | | // >>>>>>>> WasmEdge Driver functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> |
3724 | | |
3725 | | #if WASMEDGE_OS_WINDOWS |
3726 | | WASMEDGE_CAPI_EXPORT const char ** |
3727 | | WasmEdge_Driver_ArgvCreate(int Argc, const wchar_t *Argv[]) noexcept { |
3728 | | try { |
3729 | | const Span<const wchar_t *> Args(Argv, static_cast<size_t>(Argc)); |
3730 | | const size_t PointerArraySize = static_cast<size_t>(Argc) * sizeof(char *); |
3731 | | size_t StringBufferSize = 0; |
3732 | | for (auto Arg : Args) { |
3733 | | const auto Res = std::max<size_t>( |
3734 | | static_cast<size_t>(winapi::WideCharToMultiByte( |
3735 | | winapi::CP_UTF8_, 0, Arg, -1, nullptr, 0, nullptr, nullptr)), |
3736 | | 1u); |
3737 | | StringBufferSize += Res; |
3738 | | } |
3739 | | auto Buffer = std::make_unique<char[]>(PointerArraySize + StringBufferSize); |
3740 | | Span<char *> PointerArray(reinterpret_cast<char **>(Buffer.get()), |
3741 | | static_cast<size_t>(Argc)); |
3742 | | Span<char> StringBuffer(Buffer.get() + PointerArraySize, StringBufferSize); |
3743 | | for (auto Arg : Args) { |
3744 | | PointerArray[0] = StringBuffer.data(); |
3745 | | PointerArray = PointerArray.subspan(1); |
3746 | | const auto Res = std::max<size_t>( |
3747 | | static_cast<size_t>(winapi::WideCharToMultiByte( |
3748 | | winapi::CP_UTF8_, 0, Arg, -1, StringBuffer.data(), |
3749 | | static_cast<int>(StringBuffer.size()), nullptr, nullptr)), |
3750 | | 1); |
3751 | | StringBuffer = StringBuffer.subspan(Res); |
3752 | | } |
3753 | | |
3754 | | return reinterpret_cast<const char **>(Buffer.release()); |
3755 | | } catch (...) { |
3756 | | handleCAPIError(); |
3757 | | return nullptr; |
3758 | | } |
3759 | | } |
3760 | | |
3761 | | WASMEDGE_CAPI_EXPORT void |
3762 | | WasmEdge_Driver_ArgvDelete(const char *Argv[]) noexcept { |
3763 | | std::unique_ptr<char[]> Buffer(reinterpret_cast<char *>(Argv)); |
3764 | | Buffer.reset(); |
3765 | | } |
3766 | | |
3767 | | WASMEDGE_CAPI_EXPORT void |
3768 | | WasmEdge_Driver_SetConsoleOutputCPtoUTF8(void) noexcept { |
3769 | | #if WINAPI_PARTITION_DESKTOP |
3770 | | winapi::SetConsoleOutputCP(winapi::CP_UTF8_); |
3771 | | #endif |
3772 | | } |
3773 | | #endif |
3774 | | |
3775 | | WASMEDGE_CAPI_EXPORT int WasmEdge_Driver_Compiler(int Argc, |
3776 | 0 | const char *Argv[]) noexcept { |
3777 | 0 | try { |
3778 | 0 | return WasmEdge::Driver::UniTool(Argc, Argv, |
3779 | 0 | WasmEdge::Driver::ToolType::Compiler); |
3780 | 0 | } catch (...) { |
3781 | 0 | handleCAPIError(); |
3782 | 0 | return EXIT_FAILURE; |
3783 | 0 | } |
3784 | 0 | } |
3785 | | |
3786 | | WASMEDGE_CAPI_EXPORT int WasmEdge_Driver_Tool(int Argc, |
3787 | 0 | const char *Argv[]) noexcept { |
3788 | 0 | try { |
3789 | 0 | return WasmEdge::Driver::UniTool(Argc, Argv, |
3790 | 0 | WasmEdge::Driver::ToolType::Tool); |
3791 | 0 | } catch (...) { |
3792 | 0 | handleCAPIError(); |
3793 | 0 | return EXIT_FAILURE; |
3794 | 0 | } |
3795 | 0 | } |
3796 | | |
3797 | | #ifdef WASMEDGE_BUILD_WASI_NN_RPC |
3798 | | WASMEDGE_CAPI_EXPORT int |
3799 | | WasmEdge_Driver_WasiNNRPCServer(int Argc, const char *Argv[]) noexcept { |
3800 | | try { |
3801 | | return WasmEdge::Driver::WasiNNRPCServer(Argc, Argv); |
3802 | | } catch (...) { |
3803 | | handleCAPIError(); |
3804 | | return EXIT_FAILURE; |
3805 | | } |
3806 | | } |
3807 | | #endif |
3808 | | |
3809 | | WASMEDGE_CAPI_EXPORT int WasmEdge_Driver_UniTool(int Argc, |
3810 | 0 | const char *Argv[]) noexcept { |
3811 | 0 | try { |
3812 | 0 | return WasmEdge::Driver::UniTool(Argc, Argv, |
3813 | 0 | WasmEdge::Driver::ToolType::All); |
3814 | 0 | } catch (...) { |
3815 | 0 | handleCAPIError(); |
3816 | 0 | return EXIT_FAILURE; |
3817 | 0 | } |
3818 | 0 | } |
3819 | | |
3820 | | #ifdef WASMEDGE_BUILD_FUZZING |
3821 | | WASMEDGE_CAPI_EXPORT extern "C" int |
3822 | 9.46k | WasmEdge_Driver_FuzzTool(const uint8_t *Data, size_t Size) noexcept { |
3823 | 9.46k | try { |
3824 | 9.46k | return WasmEdge::Driver::FuzzTool(Data, Size); |
3825 | 9.46k | } catch (...) { |
3826 | 0 | handleCAPIError(); |
3827 | 0 | return EXIT_FAILURE; |
3828 | 0 | } |
3829 | 9.46k | } |
3830 | | |
3831 | | WASMEDGE_CAPI_EXPORT extern "C" int |
3832 | 1.61k | WasmEdge_Driver_FuzzPO(const uint8_t *Data, size_t Size) noexcept { |
3833 | 1.61k | try { |
3834 | 1.61k | return WasmEdge::Driver::FuzzPO(Data, Size); |
3835 | 1.61k | } catch (...) { |
3836 | 0 | handleCAPIError(); |
3837 | 0 | return EXIT_FAILURE; |
3838 | 0 | } |
3839 | 1.61k | } |
3840 | | #endif |
3841 | | |
3842 | | // <<<<<<<< WasmEdge Driver functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
3843 | | |
3844 | | // >>>>>>>> WasmEdge Plugin functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> |
3845 | | |
3846 | 0 | WASMEDGE_CAPI_EXPORT void WasmEdge_PluginLoadWithDefaultPaths(void) noexcept { |
3847 | 0 | WasmEdge::Plugin::Plugin::loadFromDefaultPaths(); |
3848 | 0 | } |
3849 | | |
3850 | | WASMEDGE_CAPI_EXPORT void |
3851 | 0 | WasmEdge_PluginLoadFromPath(const char *Path) noexcept { |
3852 | | // Not genPath(): this is noexcept and absolute() can throw. |
3853 | 0 | WasmEdge::Plugin::Plugin::load(std::filesystem::path(Path ? Path : "")); |
3854 | 0 | } |
3855 | | |
3856 | 0 | WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_PluginListPluginsLength(void) noexcept { |
3857 | 0 | return static_cast<uint32_t>(WasmEdge::Plugin::Plugin::plugins().size()); |
3858 | 0 | } |
3859 | | |
3860 | | WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_PluginListPlugins( |
3861 | 0 | WasmEdge_String *Names, const uint32_t Len) noexcept { |
3862 | 0 | const auto &PList = WasmEdge::Plugin::Plugin::plugins(); |
3863 | 0 | if (Names) { |
3864 | 0 | for (uint32_t I = 0; I < Len && I < PList.size(); I++) { |
3865 | 0 | Names[I] = WasmEdge_String{ |
3866 | 0 | /* Length */ static_cast<uint32_t>(std::strlen(PList[I].name())), |
3867 | 0 | /* Buf */ PList[I].name()}; |
3868 | 0 | } |
3869 | 0 | } |
3870 | 0 | return static_cast<uint32_t>(PList.size()); |
3871 | 0 | } |
3872 | | |
3873 | | WASMEDGE_CAPI_EXPORT const WasmEdge_PluginContext * |
3874 | 0 | WasmEdge_PluginFind(const WasmEdge_String Name) noexcept { |
3875 | 0 | return toPluginCxt(WasmEdge::Plugin::Plugin::find(genStrView(Name))); |
3876 | 0 | } |
3877 | | |
3878 | | WASMEDGE_CAPI_EXPORT WasmEdge_String |
3879 | 0 | WasmEdge_PluginGetPluginName(const WasmEdge_PluginContext *Cxt) noexcept { |
3880 | 0 | if (Cxt) { |
3881 | 0 | const char *Name = fromPluginCxt(Cxt)->name(); |
3882 | 0 | return WasmEdge_String{ |
3883 | 0 | /* Length */ static_cast<uint32_t>(std::strlen(Name)), |
3884 | 0 | /* Buf */ Name}; |
3885 | 0 | } |
3886 | 0 | return WasmEdge_String{/* Length */ 0, /* Buf */ nullptr}; |
3887 | 0 | } |
3888 | | |
3889 | | WASMEDGE_CAPI_EXPORT uint32_t |
3890 | 0 | WasmEdge_PluginListModuleLength(const WasmEdge_PluginContext *Cxt) noexcept { |
3891 | 0 | if (Cxt) { |
3892 | 0 | return static_cast<uint32_t>(fromPluginCxt(Cxt)->modules().size()); |
3893 | 0 | } |
3894 | 0 | return 0; |
3895 | 0 | } |
3896 | | |
3897 | | WASMEDGE_CAPI_EXPORT uint32_t |
3898 | | WasmEdge_PluginListModule(const WasmEdge_PluginContext *Cxt, |
3899 | 0 | WasmEdge_String *Names, const uint32_t Len) noexcept { |
3900 | 0 | if (Cxt) { |
3901 | 0 | auto MList = fromPluginCxt(Cxt)->modules(); |
3902 | 0 | if (Names) { |
3903 | 0 | for (uint32_t I = 0; I < Len && I < MList.size(); I++) { |
3904 | 0 | Names[I] = WasmEdge_String{ |
3905 | 0 | /* Length */ static_cast<uint32_t>(std::strlen(MList[I].name())), |
3906 | 0 | /* Buf */ MList[I].name()}; |
3907 | 0 | } |
3908 | 0 | } |
3909 | 0 | return static_cast<uint32_t>(MList.size()); |
3910 | 0 | } |
3911 | 0 | return 0; |
3912 | 0 | } |
3913 | | |
3914 | | WASMEDGE_CAPI_EXPORT WasmEdge_ModuleInstanceContext * |
3915 | | WasmEdge_PluginCreateModule(const WasmEdge_PluginContext *Cxt, |
3916 | 0 | const WasmEdge_String ModuleName) noexcept { |
3917 | 0 | try { |
3918 | 0 | if (Cxt) { |
3919 | 0 | if (const auto *PMod = |
3920 | 0 | fromPluginCxt(Cxt)->findModule(genStrView(ModuleName)); |
3921 | 0 | PMod) { |
3922 | 0 | return toModCxt(PMod->create().release()); |
3923 | 0 | } |
3924 | 0 | } |
3925 | 0 | } catch (...) { |
3926 | 0 | handleCAPIError(); |
3927 | 0 | } |
3928 | 0 | return nullptr; |
3929 | 0 | } |
3930 | | |
3931 | | WASMEDGE_CAPI_EXPORT void |
3932 | | WasmEdge_PluginInitWASINN(const char *const *NNPreloads, |
3933 | 0 | const uint32_t PreloadsLen) noexcept { |
3934 | 0 | try { |
3935 | 0 | using namespace std::literals::string_view_literals; |
3936 | 0 | if (const auto *Plugin = WasmEdge::Plugin::Plugin::find("wasi_nn"sv)) { |
3937 | 0 | PO::ArgumentParser Parser; |
3938 | 0 | Plugin->registerOptions(Parser); |
3939 | 0 | Parser.set_raw_value<std::vector<std::string>>( |
3940 | 0 | "nn-preload"sv, |
3941 | 0 | std::vector<std::string>(NNPreloads, NNPreloads + PreloadsLen)); |
3942 | 0 | } |
3943 | 0 | } catch (...) { |
3944 | 0 | handleCAPIError(); |
3945 | 0 | } |
3946 | 0 | } |
3947 | | |
3948 | | // <<<<<<<< WasmEdge Plugin functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
3949 | | |
3950 | | // >>>>>>>> WasmEdge Experimental functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> |
3951 | | |
3952 | | WASMEDGE_CAPI_EXPORT void WasmEdge_ExecutorExperimentalRegisterPreHostFunction( |
3953 | 0 | WasmEdge_ExecutorContext *Cxt, void *Data, void (*Func)(void *)) noexcept { |
3954 | 0 | if (!Cxt) { |
3955 | 0 | return; |
3956 | 0 | } |
3957 | 0 | fromExecutorCxt(Cxt)->registerPreHostFunction(Data, Func); |
3958 | 0 | } |
3959 | | |
3960 | | WASMEDGE_CAPI_EXPORT void WasmEdge_ExecutorExperimentalRegisterPostHostFunction( |
3961 | 0 | WasmEdge_ExecutorContext *Cxt, void *Data, void (*Func)(void *)) noexcept { |
3962 | 0 | if (!Cxt) { |
3963 | 0 | return; |
3964 | 0 | } |
3965 | 0 | fromExecutorCxt(Cxt)->registerPostHostFunction(Data, Func); |
3966 | 0 | } |
3967 | | |
3968 | | // <<<<<<<< WasmEdge Experimental Functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
3969 | | #ifdef __cplusplus |
3970 | | } // extern "C" |
3971 | | #endif |