/src/WasmEdge/include/runtime/instance/module.h
Line | Count | Source (jump to first uncovered line) |
1 | | // SPDX-License-Identifier: Apache-2.0 |
2 | | // SPDX-FileCopyrightText: 2019-2024 Second State INC |
3 | | |
4 | | //===-- wasmedge/runtime/instance/module.h - Module Instance definition ---===// |
5 | | // |
6 | | // Part of the WasmEdge Project. |
7 | | // |
8 | | //===----------------------------------------------------------------------===// |
9 | | /// |
10 | | /// \file |
11 | | /// This file contains the module instance definition in store manager. |
12 | | /// |
13 | | //===----------------------------------------------------------------------===// |
14 | | #pragma once |
15 | | |
16 | | #include "ast/component/component.h" |
17 | | #include "ast/module.h" |
18 | | #include "common/errcode.h" |
19 | | #include "runtime/hostfunc.h" |
20 | | #include "runtime/instance/array.h" |
21 | | #include "runtime/instance/data.h" |
22 | | #include "runtime/instance/elem.h" |
23 | | #include "runtime/instance/function.h" |
24 | | #include "runtime/instance/global.h" |
25 | | #include "runtime/instance/memory.h" |
26 | | #include "runtime/instance/struct.h" |
27 | | #include "runtime/instance/table.h" |
28 | | #include "runtime/instance/tag.h" |
29 | | |
30 | | #include <atomic> |
31 | | #include <functional> |
32 | | #include <map> |
33 | | #include <memory> |
34 | | #include <mutex> |
35 | | #include <set> |
36 | | #include <shared_mutex> |
37 | | #include <string> |
38 | | #include <type_traits> |
39 | | #include <vector> |
40 | | |
41 | | namespace WasmEdge { |
42 | | |
43 | | namespace Executor { |
44 | | class Executor; |
45 | | } |
46 | | |
47 | | namespace Runtime { |
48 | | |
49 | | class StoreManager; |
50 | | class CallingFrame; |
51 | | |
52 | | namespace Instance { |
53 | | |
54 | | namespace { |
55 | | /// Return true if T is an entity which can be exported or imported. |
56 | | template <typename T> |
57 | | inline constexpr const bool IsEntityV = |
58 | | std::is_same_v<T, Instance::FunctionInstance> || |
59 | | std::is_same_v<T, Instance::TableInstance> || |
60 | | std::is_same_v<T, Instance::MemoryInstance> || |
61 | | std::is_same_v<T, Instance::GlobalInstance> || |
62 | | std::is_same_v<T, Instance::TagInstance>; |
63 | | |
64 | | /// Return true if T is an instance. |
65 | | template <typename T> |
66 | | inline constexpr const bool IsInstanceV = |
67 | | IsEntityV<T> || std::is_same_v<T, Instance::ElementInstance> || |
68 | | std::is_same_v<T, Instance::DataInstance>; |
69 | | } // namespace |
70 | | |
71 | | class ComponentInstance; |
72 | | |
73 | | class ModuleInstance { |
74 | | public: |
75 | | ModuleInstance(std::string_view Name, void *Data = nullptr, |
76 | | std::function<void(void *)> Finalizer = nullptr) |
77 | 0 | : ModName(Name), HostData(Data), HostDataFinalizer(Finalizer) {} |
78 | 0 | virtual ~ModuleInstance() noexcept { |
79 | | // When destroying this module instance, call the callbacks to unlink to the |
80 | | // store managers. |
81 | 0 | for (auto &&Pair : LinkedStore) { |
82 | 0 | assuming(Pair.second); |
83 | 0 | Pair.second(Pair.first, this); |
84 | 0 | } |
85 | 0 | if (HostDataFinalizer.operator bool()) { |
86 | 0 | HostDataFinalizer(HostData); |
87 | 0 | } |
88 | 0 | } |
89 | | |
90 | 0 | std::string_view getModuleName() const noexcept { |
91 | 0 | std::shared_lock Lock(Mutex); |
92 | 0 | return ModName; |
93 | 0 | } |
94 | | |
95 | 0 | void *getHostData() const noexcept { return HostData; } |
96 | | |
97 | 0 | Span<const FunctionInstance *const> getFunctionInstances() const noexcept { |
98 | 0 | return Span<const FunctionInstance *const>( |
99 | 0 | const_cast<const FunctionInstance *const *>(FuncInsts.data()), |
100 | 0 | FuncInsts.size()); |
101 | 0 | } |
102 | | |
103 | 0 | Span<const MemoryInstance *const> getMemoryInstances() const noexcept { |
104 | 0 | return Span<const MemoryInstance *const>( |
105 | 0 | const_cast<const MemoryInstance *const *>(MemInsts.data()), |
106 | 0 | MemInsts.size()); |
107 | 0 | } |
108 | | |
109 | 0 | Span<const GlobalInstance *const> getGlobalInstances() const noexcept { |
110 | 0 | return Span<const GlobalInstance *const>( |
111 | 0 | const_cast<const GlobalInstance *const *>(GlobInsts.data()), |
112 | 0 | GlobInsts.size()); |
113 | 0 | } |
114 | | |
115 | 0 | Span<const DataInstance *const> getOwnedDataInstances() const noexcept { |
116 | 0 | return Span<const DataInstance *const>( |
117 | 0 | const_cast<const DataInstance *const *>(DataInsts.data()), |
118 | 0 | DataInsts.size()); |
119 | 0 | } |
120 | | |
121 | | /// Add exist instances and move ownership with exporting name. |
122 | | void addHostFunc(std::string_view Name, |
123 | 0 | std::unique_ptr<HostFunctionBase> &&Func) { |
124 | 0 | std::unique_lock Lock(Mutex); |
125 | 0 | unsafeImportDefinedType(Func->getDefinedType()); |
126 | 0 | unsafeAddHostInstance( |
127 | 0 | Name, OwnedFuncInsts, FuncInsts, ExpFuncs, |
128 | 0 | std::make_unique<FunctionInstance>( |
129 | 0 | this, static_cast<uint32_t>(Types.size()) - 1, std::move(Func))); |
130 | 0 | } |
131 | | void addHostFunc(std::string_view Name, |
132 | 0 | std::unique_ptr<FunctionInstance> &&Func) { |
133 | 0 | std::unique_lock Lock(Mutex); |
134 | 0 | assuming(Func->isHostFunction()); |
135 | 0 | unsafeImportDefinedType(Func->getHostFunc().getDefinedType()); |
136 | 0 | Func->linkDefinedType(this, static_cast<uint32_t>(Types.size()) - 1); |
137 | 0 | unsafeAddHostInstance(Name, OwnedFuncInsts, FuncInsts, ExpFuncs, |
138 | 0 | std::move(Func)); |
139 | 0 | } |
140 | | |
141 | | void addHostTable(std::string_view Name, |
142 | 0 | std::unique_ptr<TableInstance> &&Tab) { |
143 | 0 | std::unique_lock Lock(Mutex); |
144 | 0 | unsafeAddHostInstance(Name, OwnedTabInsts, TabInsts, ExpTables, |
145 | 0 | std::move(Tab)); |
146 | 0 | } |
147 | | void addHostMemory(std::string_view Name, |
148 | 0 | std::unique_ptr<MemoryInstance> &&Mem) { |
149 | 0 | std::unique_lock Lock(Mutex); |
150 | 0 | unsafeAddHostInstance(Name, OwnedMemInsts, MemInsts, ExpMems, |
151 | 0 | std::move(Mem)); |
152 | 0 | } |
153 | | void addHostGlobal(std::string_view Name, |
154 | 0 | std::unique_ptr<GlobalInstance> &&Glob) { |
155 | 0 | std::unique_lock Lock(Mutex); |
156 | 0 | unsafeAddHostInstance(Name, OwnedGlobInsts, GlobInsts, ExpGlobals, |
157 | 0 | std::move(Glob)); |
158 | 0 | } |
159 | | |
160 | | /// Find and get the exported instance by name. |
161 | 0 | FunctionInstance *findFuncExports(std::string_view ExtName) const noexcept { |
162 | 0 | std::shared_lock Lock(Mutex); |
163 | 0 | return unsafeFindExports(ExpFuncs, ExtName); |
164 | 0 | } |
165 | 0 | TableInstance *findTableExports(std::string_view ExtName) const noexcept { |
166 | 0 | std::shared_lock Lock(Mutex); |
167 | 0 | return unsafeFindExports(ExpTables, ExtName); |
168 | 0 | } |
169 | 0 | MemoryInstance *findMemoryExports(std::string_view ExtName) const noexcept { |
170 | 0 | std::shared_lock Lock(Mutex); |
171 | 0 | return unsafeFindExports(ExpMems, ExtName); |
172 | 0 | } |
173 | 0 | TagInstance *findTagExports(std::string_view ExtName) const noexcept { |
174 | 0 | std::shared_lock Lock(Mutex); |
175 | 0 | return unsafeFindExports(ExpTags, ExtName); |
176 | 0 | } |
177 | 0 | GlobalInstance *findGlobalExports(std::string_view ExtName) const noexcept { |
178 | 0 | std::shared_lock Lock(Mutex); |
179 | 0 | return unsafeFindExports(ExpGlobals, ExtName); |
180 | 0 | } |
181 | | |
182 | | /// Get the exported instances count. |
183 | 0 | uint32_t getFuncExportNum() const noexcept { |
184 | 0 | std::shared_lock Lock(Mutex); |
185 | 0 | return static_cast<uint32_t>(ExpFuncs.size()); |
186 | 0 | } |
187 | 0 | uint32_t getTableExportNum() const noexcept { |
188 | 0 | std::shared_lock Lock(Mutex); |
189 | 0 | return static_cast<uint32_t>(ExpTables.size()); |
190 | 0 | } |
191 | 0 | uint32_t getMemoryExportNum() const noexcept { |
192 | 0 | std::shared_lock Lock(Mutex); |
193 | 0 | return static_cast<uint32_t>(ExpMems.size()); |
194 | 0 | } |
195 | 0 | uint32_t getTagExportNum() const noexcept { |
196 | 0 | std::shared_lock Lock(Mutex); |
197 | 0 | return static_cast<uint32_t>(ExpTags.size()); |
198 | 0 | } |
199 | 0 | uint32_t getGlobalExportNum() const noexcept { |
200 | 0 | std::shared_lock Lock(Mutex); |
201 | 0 | return static_cast<uint32_t>(ExpGlobals.size()); |
202 | 0 | } |
203 | | |
204 | | /// Get the exported instances maps. |
205 | | template <typename CallbackT> |
206 | 0 | auto getFuncExports(CallbackT &&CallBack) const noexcept { |
207 | 0 | std::shared_lock Lock(Mutex); |
208 | 0 | return std::forward<CallbackT>(CallBack)(ExpFuncs); |
209 | 0 | } Unexecuted instantiation: wasmedge.cpp:auto WasmEdge::Runtime::Instance::ModuleInstance::getFuncExports<WasmEdge_ModuleInstanceListFunction::$_0>(WasmEdge_ModuleInstanceListFunction::$_0&&) const Unexecuted instantiation: wasmedge.cpp:auto WasmEdge::Runtime::Instance::ModuleInstance::getFuncExports<WasmEdge_VMGetFunctionList::$_0>(WasmEdge_VMGetFunctionList::$_0&&) const Unexecuted instantiation: vm.cpp:auto WasmEdge::Runtime::Instance::ModuleInstance::getFuncExports<WasmEdge::VM::VM::unsafeGetFunctionList() const::$_0>(WasmEdge::VM::VM::unsafeGetFunctionList() const::$_0&&) const Unexecuted instantiation: component_alias.cpp:auto WasmEdge::Runtime::Instance::ModuleInstance::getFuncExports<WasmEdge::Executor::Executor::instantiate(WasmEdge::Runtime::StoreManager&, WasmEdge::Runtime::Instance::ComponentInstance&, WasmEdge::AST::Component::AliasSection const&)::$_0&>(WasmEdge::Executor::Executor::instantiate(WasmEdge::Runtime::StoreManager&, WasmEdge::Runtime::Instance::ComponentInstance&, WasmEdge::AST::Component::AliasSection const&)::$_0&) const |
210 | | template <typename CallbackT> |
211 | 0 | auto getTableExports(CallbackT &&CallBack) const noexcept { |
212 | 0 | std::shared_lock Lock(Mutex); |
213 | 0 | return std::forward<CallbackT>(CallBack)(ExpTables); |
214 | 0 | } Unexecuted instantiation: wasmedge.cpp:auto WasmEdge::Runtime::Instance::ModuleInstance::getTableExports<WasmEdge_ModuleInstanceListTable::$_0>(WasmEdge_ModuleInstanceListTable::$_0&&) const Unexecuted instantiation: component_alias.cpp:auto WasmEdge::Runtime::Instance::ModuleInstance::getTableExports<WasmEdge::Executor::Executor::instantiate(WasmEdge::Runtime::StoreManager&, WasmEdge::Runtime::Instance::ComponentInstance&, WasmEdge::AST::Component::AliasSection const&)::$_0&>(WasmEdge::Executor::Executor::instantiate(WasmEdge::Runtime::StoreManager&, WasmEdge::Runtime::Instance::ComponentInstance&, WasmEdge::AST::Component::AliasSection const&)::$_0&) const |
215 | | template <typename CallbackT> |
216 | 0 | auto getMemoryExports(CallbackT &&CallBack) const noexcept { |
217 | 0 | std::shared_lock Lock(Mutex); |
218 | 0 | return std::forward<CallbackT>(CallBack)(ExpMems); |
219 | 0 | } Unexecuted instantiation: wasmedge.cpp:auto WasmEdge::Runtime::Instance::ModuleInstance::getMemoryExports<WasmEdge_ModuleInstanceListMemory::$_0>(WasmEdge_ModuleInstanceListMemory::$_0&&) const Unexecuted instantiation: component_alias.cpp:auto WasmEdge::Runtime::Instance::ModuleInstance::getMemoryExports<WasmEdge::Executor::Executor::instantiate(WasmEdge::Runtime::StoreManager&, WasmEdge::Runtime::Instance::ComponentInstance&, WasmEdge::AST::Component::AliasSection const&)::$_0&>(WasmEdge::Executor::Executor::instantiate(WasmEdge::Runtime::StoreManager&, WasmEdge::Runtime::Instance::ComponentInstance&, WasmEdge::AST::Component::AliasSection const&)::$_0&) const |
220 | | template <typename CallbackT> |
221 | 0 | auto getTagExports(CallbackT &&CallBack) const noexcept { |
222 | 0 | std::shared_lock Lock(Mutex); |
223 | 0 | return std::forward<CallbackT>(CallBack)(ExpTags); |
224 | 0 | } |
225 | | template <typename CallbackT> |
226 | 0 | auto getGlobalExports(CallbackT &&CallBack) const noexcept { |
227 | 0 | std::shared_lock Lock(Mutex); |
228 | 0 | return std::forward<CallbackT>(CallBack)(ExpGlobals); |
229 | 0 | } Unexecuted instantiation: wasmedge.cpp:auto WasmEdge::Runtime::Instance::ModuleInstance::getGlobalExports<WasmEdge_ModuleInstanceListGlobal::$_0>(WasmEdge_ModuleInstanceListGlobal::$_0&&) const Unexecuted instantiation: component_alias.cpp:auto WasmEdge::Runtime::Instance::ModuleInstance::getGlobalExports<WasmEdge::Executor::Executor::instantiate(WasmEdge::Runtime::StoreManager&, WasmEdge::Runtime::Instance::ComponentInstance&, WasmEdge::AST::Component::AliasSection const&)::$_0&>(WasmEdge::Executor::Executor::instantiate(WasmEdge::Runtime::StoreManager&, WasmEdge::Runtime::Instance::ComponentInstance&, WasmEdge::AST::Component::AliasSection const&)::$_0&) const |
230 | | |
231 | | /// Component model concepts |
232 | | /// |
233 | | /// Export functions with name, these functions are suppose not owned by this |
234 | | /// module, because the module is just a wrapper for component functions. |
235 | | /// |
236 | | /// See the example below, with statement below shows why we need this kind of |
237 | | /// exporting |
238 | | /// |
239 | | /// (component |
240 | | /// (core module $A |
241 | | /// (func (export "one") (result i32) (i32.const 1)) |
242 | | /// ) |
243 | | /// (core module $B |
244 | | /// (func (import "a" "one") (result i32)) |
245 | | /// ) |
246 | | /// (core instance $a (instantiate $A)) |
247 | | /// (core instance $b (instantiate $B (with "a" (instance $a)))) |
248 | | /// ) |
249 | 0 | void exportFunction(std::string_view Name, FunctionInstance *Func) { |
250 | 0 | std::unique_lock Lock(Mutex); |
251 | 0 | assuming(Func->isHostFunction()); |
252 | 0 | unsafeImportDefinedType(Func->getHostFunc().getDefinedType()); |
253 | 0 | Func->linkDefinedType(this, static_cast<uint32_t>(Types.size()) - 1); |
254 | 0 | FuncInsts.push_back(Func); |
255 | 0 | ExpFuncs.insert_or_assign(std::string(Name), FuncInsts.back()); |
256 | 0 | } |
257 | 0 | void exportTable(std::string_view Name, TableInstance *Tab) { |
258 | 0 | std::unique_lock Lock(Mutex); |
259 | 0 | TabInsts.push_back(Tab); |
260 | 0 | ExpTables.insert_or_assign(std::string(Name), TabInsts.back()); |
261 | 0 | } |
262 | 0 | void exportMemory(std::string_view Name, MemoryInstance *Mem) { |
263 | 0 | std::unique_lock Lock(Mutex); |
264 | 0 | MemInsts.push_back(Mem); |
265 | 0 | ExpMems.insert_or_assign(std::string(Name), MemInsts.back()); |
266 | 0 | } |
267 | 0 | void exportGlobal(std::string_view Name, GlobalInstance *Glob) { |
268 | 0 | std::unique_lock Lock(Mutex); |
269 | 0 | GlobInsts.push_back(Glob); |
270 | 0 | ExpGlobals.insert_or_assign(std::string(Name), GlobInsts.back()); |
271 | 0 | } |
272 | | |
273 | | protected: |
274 | | friend class Executor::Executor; |
275 | | friend class ComponentInstance; |
276 | | friend class Runtime::CallingFrame; |
277 | | |
278 | | /// Create and copy the defined type to this module instance. |
279 | 0 | void addDefinedType(const AST::SubType &SType) { |
280 | 0 | std::unique_lock Lock(Mutex); |
281 | 0 | OwnedTypes.push_back(std::make_unique<AST::SubType>(SType)); |
282 | 0 | Types.push_back(OwnedTypes.back().get()); |
283 | 0 | } |
284 | | |
285 | | /// Create and add instances into this module instance. |
286 | 0 | template <typename... Args> void addFunc(Args &&...Values) { |
287 | 0 | std::unique_lock Lock(Mutex); |
288 | 0 | unsafeAddInstance(OwnedFuncInsts, FuncInsts, this, |
289 | 0 | std::forward<Args>(Values)...); |
290 | 0 | } Unexecuted instantiation: void WasmEdge::Runtime::Instance::ModuleInstance::addFunc<unsigned int const&, WasmEdge::AST::FunctionType const&, WasmEdge::Symbol<void> >(unsigned int const&, WasmEdge::AST::FunctionType const&, WasmEdge::Symbol<void>&&) Unexecuted instantiation: void WasmEdge::Runtime::Instance::ModuleInstance::addFunc<unsigned int const&, WasmEdge::AST::FunctionType const&, cxx20::span<std::__1::pair<unsigned int, WasmEdge::ValType> const, 18446744073709551615ul>, cxx20::span<WasmEdge::AST::Instruction const, 18446744073709551615ul> >(unsigned int const&, WasmEdge::AST::FunctionType const&, cxx20::span<std::__1::pair<unsigned int, WasmEdge::ValType> const, 18446744073709551615ul>&&, cxx20::span<WasmEdge::AST::Instruction const, 18446744073709551615ul>&&) |
291 | 0 | template <typename... Args> void addTable(Args &&...Values) { |
292 | 0 | std::unique_lock Lock(Mutex); |
293 | 0 | unsafeAddInstance(OwnedTabInsts, TabInsts, std::forward<Args>(Values)...); |
294 | 0 | } Unexecuted instantiation: void WasmEdge::Runtime::Instance::ModuleInstance::addTable<WasmEdge::AST::TableType const&, WasmEdge::RefVariant&>(WasmEdge::AST::TableType const&, WasmEdge::RefVariant&) Unexecuted instantiation: void WasmEdge::Runtime::Instance::ModuleInstance::addTable<WasmEdge::AST::TableType const&>(WasmEdge::AST::TableType const&) |
295 | 0 | template <typename... Args> void addMemory(Args &&...Values) { |
296 | 0 | std::unique_lock Lock(Mutex); |
297 | 0 | unsafeAddInstance(OwnedMemInsts, MemInsts, std::forward<Args>(Values)...); |
298 | 0 | } |
299 | 0 | template <typename... Args> void addTag(Args &&...Values) { |
300 | 0 | std::unique_lock Lock(Mutex); |
301 | 0 | unsafeAddInstance(OwnedTagInsts, TagInsts, std::forward<Args>(Values)...); |
302 | 0 | } |
303 | 0 | template <typename... Args> void addGlobal(Args &&...Values) { |
304 | 0 | std::unique_lock Lock(Mutex); |
305 | 0 | unsafeAddInstance(OwnedGlobInsts, GlobInsts, std::forward<Args>(Values)...); |
306 | 0 | } |
307 | 0 | template <typename... Args> void addElem(Args &&...Values) { |
308 | 0 | std::unique_lock Lock(Mutex); |
309 | 0 | unsafeAddInstance(OwnedElemInsts, ElemInsts, std::forward<Args>(Values)...); |
310 | 0 | } |
311 | 0 | template <typename... Args> void addData(Args &&...Values) { |
312 | 0 | std::unique_lock Lock(Mutex); |
313 | 0 | unsafeAddInstance(OwnedDataInsts, DataInsts, std::forward<Args>(Values)...); |
314 | 0 | } |
315 | 0 | template <typename... Args> ArrayInstance *newArray(Args &&...Values) { |
316 | 0 | std::unique_lock Lock(Mutex); |
317 | 0 | OwnedArrayInsts.push_back( |
318 | 0 | std::make_unique<ArrayInstance>(this, std::forward<Args>(Values)...)); |
319 | 0 | return OwnedArrayInsts.back().get(); |
320 | 0 | } Unexecuted instantiation: WasmEdge::Runtime::Instance::ArrayInstance* WasmEdge::Runtime::Instance::ModuleInstance::newArray<unsigned int const&, unsigned int const&, WasmEdge::Variant<unsigned int, int, unsigned long, long, float, double, unsigned __int128, __int128, unsigned long __vector(2), long __vector(2), unsigned int __vector(4), int __vector(4), unsigned short __vector(8), short __vector(8), unsigned char __vector(16), signed char __vector(16), float __vector(4), double __vector(2), WasmEdge::RefVariant>&>(unsigned int const&, unsigned int const&, WasmEdge::Variant<unsigned int, int, unsigned long, long, float, double, unsigned __int128, __int128, unsigned long __vector(2), long __vector(2), unsigned int __vector(4), int __vector(4), unsigned short __vector(8), short __vector(8), unsigned char __vector(16), signed char __vector(16), float __vector(4), double __vector(2), WasmEdge::RefVariant>&) Unexecuted instantiation: WasmEdge::Runtime::Instance::ArrayInstance* WasmEdge::Runtime::Instance::ModuleInstance::newArray<unsigned int const&, unsigned int const&, WasmEdge::Variant<unsigned int, int, unsigned long, long, float, double, unsigned __int128, __int128, unsigned long __vector(2), long __vector(2), unsigned int __vector(4), int __vector(4), unsigned short __vector(8), short __vector(8), unsigned char __vector(16), signed char __vector(16), float __vector(4), double __vector(2), WasmEdge::RefVariant> >(unsigned int const&, unsigned int const&, WasmEdge::Variant<unsigned int, int, unsigned long, long, float, double, unsigned __int128, __int128, unsigned long __vector(2), long __vector(2), unsigned int __vector(4), int __vector(4), unsigned short __vector(8), short __vector(8), unsigned char __vector(16), signed char __vector(16), float __vector(4), double __vector(2), WasmEdge::RefVariant>&&) Unexecuted instantiation: WasmEdge::Runtime::Instance::ArrayInstance* WasmEdge::Runtime::Instance::ModuleInstance::newArray<unsigned int const&, std::__1::vector<WasmEdge::Variant<unsigned int, int, unsigned long, long, float, double, unsigned __int128, __int128, unsigned long __vector(2), long __vector(2), unsigned int __vector(4), int __vector(4), unsigned short __vector(8), short __vector(8), unsigned char __vector(16), signed char __vector(16), float __vector(4), double __vector(2), WasmEdge::RefVariant>, std::__1::allocator<WasmEdge::Variant<unsigned int, int, unsigned long, long, float, double, unsigned __int128, __int128, unsigned long __vector(2), long __vector(2), unsigned int __vector(4), int __vector(4), unsigned short __vector(8), short __vector(8), unsigned char __vector(16), signed char __vector(16), float __vector(4), double __vector(2), WasmEdge::RefVariant> > > >(unsigned int const&, std::__1::vector<WasmEdge::Variant<unsigned int, int, unsigned long, long, float, double, unsigned __int128, __int128, unsigned long __vector(2), long __vector(2), unsigned int __vector(4), int __vector(4), unsigned short __vector(8), short __vector(8), unsigned char __vector(16), signed char __vector(16), float __vector(4), double __vector(2), WasmEdge::RefVariant>, std::__1::allocator<WasmEdge::Variant<unsigned int, int, unsigned long, long, float, double, unsigned __int128, __int128, unsigned long __vector(2), long __vector(2), unsigned int __vector(4), int __vector(4), unsigned short __vector(8), short __vector(8), unsigned char __vector(16), signed char __vector(16), float __vector(4), double __vector(2), WasmEdge::RefVariant> > >&&) |
321 | 0 | template <typename... Args> StructInstance *newStruct(Args &&...Values) { |
322 | 0 | std::unique_lock Lock(Mutex); |
323 | 0 | OwnedStructInsts.push_back( |
324 | 0 | std::make_unique<StructInstance>(this, std::forward<Args>(Values)...)); |
325 | 0 | return OwnedStructInsts.back().get(); |
326 | 0 | } |
327 | | |
328 | | /// Import instances into this module instance. |
329 | 0 | void importFunction(FunctionInstance *Func) { |
330 | 0 | std::unique_lock Lock(Mutex); |
331 | 0 | unsafeImportInstance(FuncInsts, Func); |
332 | 0 | } |
333 | 0 | void importTable(TableInstance *Tab) { |
334 | 0 | std::unique_lock Lock(Mutex); |
335 | 0 | unsafeImportInstance(TabInsts, Tab); |
336 | 0 | } |
337 | 0 | void importMemory(MemoryInstance *Mem) { |
338 | 0 | std::unique_lock Lock(Mutex); |
339 | 0 | unsafeImportInstance(MemInsts, Mem); |
340 | 0 | } |
341 | 0 | void importTag(TagInstance *Tg) { |
342 | 0 | std::unique_lock Lock(Mutex); |
343 | 0 | unsafeImportInstance(TagInsts, Tg); |
344 | 0 | } |
345 | 0 | void importGlobal(GlobalInstance *Glob) { |
346 | 0 | std::unique_lock Lock(Mutex); |
347 | 0 | ImpGlobalNum++; |
348 | 0 | unsafeImportInstance(GlobInsts, Glob); |
349 | 0 | } |
350 | | |
351 | | /// Export instances with name from this module instance. |
352 | 0 | void exportFunction(std::string_view Name, uint32_t Idx) { |
353 | 0 | std::unique_lock Lock(Mutex); |
354 | 0 | ExpFuncs.insert_or_assign(std::string(Name), FuncInsts[Idx]); |
355 | 0 | } |
356 | 0 | void exportTable(std::string_view Name, uint32_t Idx) { |
357 | 0 | std::unique_lock Lock(Mutex); |
358 | 0 | ExpTables.insert_or_assign(std::string(Name), TabInsts[Idx]); |
359 | 0 | } |
360 | 0 | void exportMemory(std::string_view Name, uint32_t Idx) { |
361 | 0 | std::unique_lock Lock(Mutex); |
362 | 0 | ExpMems.insert_or_assign(std::string(Name), MemInsts[Idx]); |
363 | 0 | } |
364 | 0 | void exportGlobal(std::string_view Name, uint32_t Idx) { |
365 | 0 | std::unique_lock Lock(Mutex); |
366 | 0 | ExpGlobals.insert_or_assign(std::string(Name), GlobInsts[Idx]); |
367 | 0 | } |
368 | 0 | void exportTag(std::string_view Name, uint32_t Idx) { |
369 | 0 | std::unique_lock Lock(Mutex); |
370 | 0 | ExpTags.insert_or_assign(std::string(Name), TagInsts[Idx]); |
371 | 0 | } |
372 | | |
373 | | /// Get defined type list. |
374 | 0 | Span<const AST::SubType *const> getTypeList() const noexcept { return Types; } |
375 | | |
376 | | /// Get instance pointer by index. |
377 | 0 | Expect<const AST::SubType *> getType(uint32_t Idx) const noexcept { |
378 | 0 | std::shared_lock Lock(Mutex); |
379 | 0 | if (unlikely(Idx >= Types.size())) { |
380 | | // Error logging need to be handled in caller. |
381 | 0 | return Unexpect(ErrCode::Value::WrongInstanceIndex); |
382 | 0 | } |
383 | 0 | return unsafeGetType(Idx); |
384 | 0 | } |
385 | 0 | const AST::SubType *unsafeGetType(uint32_t Idx) const noexcept { |
386 | 0 | return Types[Idx]; |
387 | 0 | } |
388 | 0 | Expect<FunctionInstance *> getFunc(uint32_t Idx) const noexcept { |
389 | 0 | std::shared_lock Lock(Mutex); |
390 | 0 | if (Idx >= FuncInsts.size()) { |
391 | 0 | // Error logging need to be handled in caller. |
392 | 0 | return Unexpect(ErrCode::Value::WrongInstanceIndex); |
393 | 0 | } |
394 | 0 | return unsafeGetFunction(Idx); |
395 | 0 | } |
396 | 0 | FunctionInstance *unsafeGetFunction(uint32_t Idx) const noexcept { |
397 | 0 | return FuncInsts[Idx]; |
398 | 0 | } |
399 | 0 | Expect<TableInstance *> getTable(uint32_t Idx) const noexcept { |
400 | 0 | std::shared_lock Lock(Mutex); |
401 | 0 | if (Idx >= TabInsts.size()) { |
402 | 0 | // Error logging need to be handled in caller. |
403 | 0 | return Unexpect(ErrCode::Value::WrongInstanceIndex); |
404 | 0 | } |
405 | 0 | return unsafeGetTable(Idx); |
406 | 0 | } |
407 | 0 | TableInstance *unsafeGetTable(uint32_t Idx) const noexcept { |
408 | 0 | return TabInsts[Idx]; |
409 | 0 | } |
410 | 0 | Expect<MemoryInstance *> getMemory(uint32_t Idx) const noexcept { |
411 | 0 | std::shared_lock Lock(Mutex); |
412 | 0 | if (Idx >= MemInsts.size()) { |
413 | | // Error logging need to be handled in caller. |
414 | 0 | return Unexpect(ErrCode::Value::WrongInstanceIndex); |
415 | 0 | } |
416 | 0 | return unsafeGetMemory(Idx); |
417 | 0 | } |
418 | 0 | MemoryInstance *unsafeGetMemory(uint32_t Idx) const noexcept { |
419 | 0 | return MemInsts[Idx]; |
420 | 0 | } |
421 | 0 | TagInstance *unsafeGetTag(uint32_t Idx) const noexcept { |
422 | 0 | return TagInsts[Idx]; |
423 | 0 | } |
424 | 0 | Expect<GlobalInstance *> getGlobal(uint32_t Idx) const noexcept { |
425 | 0 | std::shared_lock Lock(Mutex); |
426 | 0 | if (Idx >= GlobInsts.size()) { |
427 | | // Error logging need to be handled in caller. |
428 | 0 | return Unexpect(ErrCode::Value::WrongInstanceIndex); |
429 | 0 | } |
430 | 0 | return unsafeGetGlobal(Idx); |
431 | 0 | } |
432 | 0 | GlobalInstance *unsafeGetGlobal(uint32_t Idx) const noexcept { |
433 | 0 | return GlobInsts[Idx]; |
434 | 0 | } |
435 | 0 | Expect<ElementInstance *> getElem(uint32_t Idx) const noexcept { |
436 | 0 | std::shared_lock Lock(Mutex); |
437 | 0 | if (Idx >= ElemInsts.size()) { |
438 | 0 | // Error logging need to be handled in caller. |
439 | 0 | return Unexpect(ErrCode::Value::WrongInstanceIndex); |
440 | 0 | } |
441 | 0 | return unsafeGetElem(Idx); |
442 | 0 | } |
443 | 0 | ElementInstance *unsafeGetElem(uint32_t Idx) const noexcept { |
444 | 0 | return ElemInsts[Idx]; |
445 | 0 | } |
446 | 0 | Expect<DataInstance *> getData(uint32_t Idx) const noexcept { |
447 | 0 | std::shared_lock Lock(Mutex); |
448 | 0 | if (Idx >= DataInsts.size()) { |
449 | 0 | // Error logging need to be handled in caller. |
450 | 0 | return Unexpect(ErrCode::Value::WrongInstanceIndex); |
451 | 0 | } |
452 | 0 | return unsafeGetData(Idx); |
453 | 0 | } |
454 | 0 | DataInstance *unsafeGetData(uint32_t Idx) const noexcept { |
455 | 0 | return DataInsts[Idx]; |
456 | 0 | } |
457 | | |
458 | | /// Get the instances count. |
459 | 0 | uint32_t getFuncNum() const noexcept { |
460 | 0 | std::shared_lock Lock(Mutex); |
461 | 0 | return static_cast<uint32_t>(FuncInsts.size()); |
462 | 0 | } |
463 | 0 | uint32_t getMemoryNum() const noexcept { |
464 | 0 | std::shared_lock Lock(Mutex); |
465 | 0 | return static_cast<uint32_t>(MemInsts.size()); |
466 | 0 | } |
467 | 0 | uint32_t getGlobalNum() const noexcept { |
468 | 0 | std::shared_lock Lock(Mutex); |
469 | 0 | return static_cast<uint32_t>(GlobInsts.size()); |
470 | 0 | } |
471 | | |
472 | | /// Get imported global instances count. |
473 | 0 | uint32_t getGlobalImportNum() const noexcept { return ImpGlobalNum; } |
474 | | |
475 | | /// Set the start function index and find the function instance. |
476 | 0 | void setStartIdx(uint32_t Idx) noexcept { |
477 | 0 | std::unique_lock Lock(Mutex); |
478 | 0 | StartFunc = FuncInsts[Idx]; |
479 | 0 | } |
480 | | |
481 | | /// Get start function address in Store. |
482 | 0 | FunctionInstance *getStartFunc() const noexcept { |
483 | 0 | std::shared_lock Lock(Mutex); |
484 | 0 | return StartFunc; |
485 | 0 | } |
486 | | |
487 | | /// Unsafe import instance into this module. |
488 | | template <typename T> |
489 | | std::enable_if_t<IsEntityV<T>, void> |
490 | 0 | unsafeImportInstance(std::vector<T *> &Vec, T *Ptr) { |
491 | 0 | Vec.push_back(Ptr); |
492 | 0 | } Unexecuted instantiation: std::__1::enable_if<IsEntityV<WasmEdge::Runtime::Instance::FunctionInstance>, void>::type WasmEdge::Runtime::Instance::ModuleInstance::unsafeImportInstance<WasmEdge::Runtime::Instance::FunctionInstance>(std::__1::vector<WasmEdge::Runtime::Instance::FunctionInstance*, std::__1::allocator<WasmEdge::Runtime::Instance::FunctionInstance*> >&, WasmEdge::Runtime::Instance::FunctionInstance*) Unexecuted instantiation: std::__1::enable_if<IsEntityV<WasmEdge::Runtime::Instance::TableInstance>, void>::type WasmEdge::Runtime::Instance::ModuleInstance::unsafeImportInstance<WasmEdge::Runtime::Instance::TableInstance>(std::__1::vector<WasmEdge::Runtime::Instance::TableInstance*, std::__1::allocator<WasmEdge::Runtime::Instance::TableInstance*> >&, WasmEdge::Runtime::Instance::TableInstance*) Unexecuted instantiation: std::__1::enable_if<IsEntityV<WasmEdge::Runtime::Instance::MemoryInstance>, void>::type WasmEdge::Runtime::Instance::ModuleInstance::unsafeImportInstance<WasmEdge::Runtime::Instance::MemoryInstance>(std::__1::vector<WasmEdge::Runtime::Instance::MemoryInstance*, std::__1::allocator<WasmEdge::Runtime::Instance::MemoryInstance*> >&, WasmEdge::Runtime::Instance::MemoryInstance*) Unexecuted instantiation: std::__1::enable_if<IsEntityV<WasmEdge::Runtime::Instance::TagInstance>, void>::type WasmEdge::Runtime::Instance::ModuleInstance::unsafeImportInstance<WasmEdge::Runtime::Instance::TagInstance>(std::__1::vector<WasmEdge::Runtime::Instance::TagInstance*, std::__1::allocator<WasmEdge::Runtime::Instance::TagInstance*> >&, WasmEdge::Runtime::Instance::TagInstance*) Unexecuted instantiation: std::__1::enable_if<IsEntityV<WasmEdge::Runtime::Instance::GlobalInstance>, void>::type WasmEdge::Runtime::Instance::ModuleInstance::unsafeImportInstance<WasmEdge::Runtime::Instance::GlobalInstance>(std::__1::vector<WasmEdge::Runtime::Instance::GlobalInstance*, std::__1::allocator<WasmEdge::Runtime::Instance::GlobalInstance*> >&, WasmEdge::Runtime::Instance::GlobalInstance*) |
493 | | |
494 | | /// Unsafe import defined type from host function into this module. |
495 | 0 | void unsafeImportDefinedType(const AST::SubType &SType) { |
496 | 0 | Types.push_back(&SType); |
497 | 0 | const_cast<AST::SubType *>(Types.back()) |
498 | 0 | ->setTypeIndex(static_cast<uint32_t>(Types.size()) - 1); |
499 | 0 | } |
500 | | |
501 | | /// Unsafe create and add the instance into this module. |
502 | | template <typename T, typename... Args> |
503 | | std::enable_if_t<IsInstanceV<T>, void> |
504 | | unsafeAddInstance(std::vector<std::unique_ptr<T>> &OwnedInstsVec, |
505 | 0 | std::vector<T *> &InstsVec, Args &&...Values) { |
506 | 0 | OwnedInstsVec.push_back(std::make_unique<T>(std::forward<Args>(Values)...)); |
507 | 0 | InstsVec.push_back(OwnedInstsVec.back().get()); |
508 | 0 | } Unexecuted instantiation: std::__1::enable_if<IsInstanceV<WasmEdge::Runtime::Instance::FunctionInstance>, void>::type WasmEdge::Runtime::Instance::ModuleInstance::unsafeAddInstance<WasmEdge::Runtime::Instance::FunctionInstance, WasmEdge::Runtime::Instance::ModuleInstance*, unsigned int const&, WasmEdge::AST::FunctionType const&, WasmEdge::Symbol<void> >(std::__1::vector<std::__1::unique_ptr<WasmEdge::Runtime::Instance::FunctionInstance, std::__1::default_delete<WasmEdge::Runtime::Instance::FunctionInstance> >, std::__1::allocator<std::__1::unique_ptr<WasmEdge::Runtime::Instance::FunctionInstance, std::__1::default_delete<WasmEdge::Runtime::Instance::FunctionInstance> > > >&, std::__1::vector<WasmEdge::Runtime::Instance::FunctionInstance*, std::__1::allocator<WasmEdge::Runtime::Instance::FunctionInstance*> >&, WasmEdge::Runtime::Instance::ModuleInstance*&&, unsigned int const&, WasmEdge::AST::FunctionType const&, WasmEdge::Symbol<void>&&) Unexecuted instantiation: std::__1::enable_if<IsInstanceV<WasmEdge::Runtime::Instance::FunctionInstance>, void>::type WasmEdge::Runtime::Instance::ModuleInstance::unsafeAddInstance<WasmEdge::Runtime::Instance::FunctionInstance, WasmEdge::Runtime::Instance::ModuleInstance*, unsigned int const&, WasmEdge::AST::FunctionType const&, cxx20::span<std::__1::pair<unsigned int, WasmEdge::ValType> const, 18446744073709551615ul>, cxx20::span<WasmEdge::AST::Instruction const, 18446744073709551615ul> >(std::__1::vector<std::__1::unique_ptr<WasmEdge::Runtime::Instance::FunctionInstance, std::__1::default_delete<WasmEdge::Runtime::Instance::FunctionInstance> >, std::__1::allocator<std::__1::unique_ptr<WasmEdge::Runtime::Instance::FunctionInstance, std::__1::default_delete<WasmEdge::Runtime::Instance::FunctionInstance> > > >&, std::__1::vector<WasmEdge::Runtime::Instance::FunctionInstance*, std::__1::allocator<WasmEdge::Runtime::Instance::FunctionInstance*> >&, WasmEdge::Runtime::Instance::ModuleInstance*&&, unsigned int const&, WasmEdge::AST::FunctionType const&, cxx20::span<std::__1::pair<unsigned int, WasmEdge::ValType> const, 18446744073709551615ul>&&, cxx20::span<WasmEdge::AST::Instruction const, 18446744073709551615ul>&&) Unexecuted instantiation: std::__1::enable_if<IsInstanceV<WasmEdge::Runtime::Instance::GlobalInstance>, void>::type WasmEdge::Runtime::Instance::ModuleInstance::unsafeAddInstance<WasmEdge::Runtime::Instance::GlobalInstance, WasmEdge::AST::GlobalType const&, WasmEdge::Variant<unsigned int, int, unsigned long, long, float, double, unsigned __int128, __int128, unsigned long __vector(2), long __vector(2), unsigned int __vector(4), int __vector(4), unsigned short __vector(8), short __vector(8), unsigned char __vector(16), signed char __vector(16), float __vector(4), double __vector(2), WasmEdge::RefVariant>&>(std::__1::vector<std::__1::unique_ptr<WasmEdge::Runtime::Instance::GlobalInstance, std::__1::default_delete<WasmEdge::Runtime::Instance::GlobalInstance> >, std::__1::allocator<std::__1::unique_ptr<WasmEdge::Runtime::Instance::GlobalInstance, std::__1::default_delete<WasmEdge::Runtime::Instance::GlobalInstance> > > >&, std::__1::vector<WasmEdge::Runtime::Instance::GlobalInstance*, std::__1::allocator<WasmEdge::Runtime::Instance::GlobalInstance*> >&, WasmEdge::AST::GlobalType const&, WasmEdge::Variant<unsigned int, int, unsigned long, long, float, double, unsigned __int128, __int128, unsigned long __vector(2), long __vector(2), unsigned int __vector(4), int __vector(4), unsigned short __vector(8), short __vector(8), unsigned char __vector(16), signed char __vector(16), float __vector(4), double __vector(2), WasmEdge::RefVariant>&) Unexecuted instantiation: std::__1::enable_if<IsInstanceV<WasmEdge::Runtime::Instance::TableInstance>, void>::type WasmEdge::Runtime::Instance::ModuleInstance::unsafeAddInstance<WasmEdge::Runtime::Instance::TableInstance, WasmEdge::AST::TableType const&, WasmEdge::RefVariant&>(std::__1::vector<std::__1::unique_ptr<WasmEdge::Runtime::Instance::TableInstance, std::__1::default_delete<WasmEdge::Runtime::Instance::TableInstance> >, std::__1::allocator<std::__1::unique_ptr<WasmEdge::Runtime::Instance::TableInstance, std::__1::default_delete<WasmEdge::Runtime::Instance::TableInstance> > > >&, std::__1::vector<WasmEdge::Runtime::Instance::TableInstance*, std::__1::allocator<WasmEdge::Runtime::Instance::TableInstance*> >&, WasmEdge::AST::TableType const&, WasmEdge::RefVariant&) Unexecuted instantiation: std::__1::enable_if<IsInstanceV<WasmEdge::Runtime::Instance::TableInstance>, void>::type WasmEdge::Runtime::Instance::ModuleInstance::unsafeAddInstance<WasmEdge::Runtime::Instance::TableInstance, WasmEdge::AST::TableType const&>(std::__1::vector<std::__1::unique_ptr<WasmEdge::Runtime::Instance::TableInstance, std::__1::default_delete<WasmEdge::Runtime::Instance::TableInstance> >, std::__1::allocator<std::__1::unique_ptr<WasmEdge::Runtime::Instance::TableInstance, std::__1::default_delete<WasmEdge::Runtime::Instance::TableInstance> > > >&, std::__1::vector<WasmEdge::Runtime::Instance::TableInstance*, std::__1::allocator<WasmEdge::Runtime::Instance::TableInstance*> >&, WasmEdge::AST::TableType const&) Unexecuted instantiation: std::__1::enable_if<IsInstanceV<WasmEdge::Runtime::Instance::MemoryInstance>, void>::type WasmEdge::Runtime::Instance::ModuleInstance::unsafeAddInstance<WasmEdge::Runtime::Instance::MemoryInstance, WasmEdge::AST::MemoryType const&, unsigned int>(std::__1::vector<std::__1::unique_ptr<WasmEdge::Runtime::Instance::MemoryInstance, std::__1::default_delete<WasmEdge::Runtime::Instance::MemoryInstance> >, std::__1::allocator<std::__1::unique_ptr<WasmEdge::Runtime::Instance::MemoryInstance, std::__1::default_delete<WasmEdge::Runtime::Instance::MemoryInstance> > > >&, std::__1::vector<WasmEdge::Runtime::Instance::MemoryInstance*, std::__1::allocator<WasmEdge::Runtime::Instance::MemoryInstance*> >&, WasmEdge::AST::MemoryType const&, unsigned int&&) Unexecuted instantiation: std::__1::enable_if<IsInstanceV<WasmEdge::Runtime::Instance::ElementInstance>, void>::type WasmEdge::Runtime::Instance::ModuleInstance::unsafeAddInstance<WasmEdge::Runtime::Instance::ElementInstance, unsigned int&, WasmEdge::ValType const&, std::__1::vector<WasmEdge::RefVariant, std::__1::allocator<WasmEdge::RefVariant> >&>(std::__1::vector<std::__1::unique_ptr<WasmEdge::Runtime::Instance::ElementInstance, std::__1::default_delete<WasmEdge::Runtime::Instance::ElementInstance> >, std::__1::allocator<std::__1::unique_ptr<WasmEdge::Runtime::Instance::ElementInstance, std::__1::default_delete<WasmEdge::Runtime::Instance::ElementInstance> > > >&, std::__1::vector<WasmEdge::Runtime::Instance::ElementInstance*, std::__1::allocator<WasmEdge::Runtime::Instance::ElementInstance*> >&, unsigned int&, WasmEdge::ValType const&, std::__1::vector<WasmEdge::RefVariant, std::__1::allocator<WasmEdge::RefVariant> >&) Unexecuted instantiation: std::__1::enable_if<IsInstanceV<WasmEdge::Runtime::Instance::DataInstance>, void>::type WasmEdge::Runtime::Instance::ModuleInstance::unsafeAddInstance<WasmEdge::Runtime::Instance::DataInstance, unsigned int&, cxx20::span<unsigned char const, 18446744073709551615ul> >(std::__1::vector<std::__1::unique_ptr<WasmEdge::Runtime::Instance::DataInstance, std::__1::default_delete<WasmEdge::Runtime::Instance::DataInstance> >, std::__1::allocator<std::__1::unique_ptr<WasmEdge::Runtime::Instance::DataInstance, std::__1::default_delete<WasmEdge::Runtime::Instance::DataInstance> > > >&, std::__1::vector<WasmEdge::Runtime::Instance::DataInstance*, std::__1::allocator<WasmEdge::Runtime::Instance::DataInstance*> >&, unsigned int&, cxx20::span<unsigned char const, 18446744073709551615ul>&&) Unexecuted instantiation: std::__1::enable_if<IsInstanceV<WasmEdge::Runtime::Instance::TagInstance>, void>::type WasmEdge::Runtime::Instance::ModuleInstance::unsafeAddInstance<WasmEdge::Runtime::Instance::TagInstance, WasmEdge::AST::TagType const&, WasmEdge::AST::SubType const*&>(std::__1::vector<std::__1::unique_ptr<WasmEdge::Runtime::Instance::TagInstance, std::__1::default_delete<WasmEdge::Runtime::Instance::TagInstance> >, std::__1::allocator<std::__1::unique_ptr<WasmEdge::Runtime::Instance::TagInstance, std::__1::default_delete<WasmEdge::Runtime::Instance::TagInstance> > > >&, std::__1::vector<WasmEdge::Runtime::Instance::TagInstance*, std::__1::allocator<WasmEdge::Runtime::Instance::TagInstance*> >&, WasmEdge::AST::TagType const&, WasmEdge::AST::SubType const*&) |
509 | | |
510 | | /// Unsafe add and export the existing instance into this module. |
511 | | template <typename T, typename... Args> |
512 | | std::enable_if_t<IsEntityV<T>, void> |
513 | | unsafeAddHostInstance(std::string_view Name, |
514 | | std::vector<std::unique_ptr<T>> &OwnedInstsVec, |
515 | | std::vector<T *> &InstsVec, |
516 | | std::map<std::string, T *, std::less<>> &InstsMap, |
517 | 0 | std::unique_ptr<T> &&Inst) { |
518 | 0 | OwnedInstsVec.push_back(std::move(Inst)); |
519 | 0 | InstsVec.push_back(OwnedInstsVec.back().get()); |
520 | 0 | InstsMap.insert_or_assign(std::string(Name), InstsVec.back()); |
521 | 0 | } Unexecuted instantiation: std::__1::enable_if<IsEntityV<WasmEdge::Runtime::Instance::FunctionInstance>, void>::type WasmEdge::Runtime::Instance::ModuleInstance::unsafeAddHostInstance<WasmEdge::Runtime::Instance::FunctionInstance>(std::__1::basic_string_view<char, std::__1::char_traits<char> >, std::__1::vector<std::__1::unique_ptr<WasmEdge::Runtime::Instance::FunctionInstance, std::__1::default_delete<WasmEdge::Runtime::Instance::FunctionInstance> >, std::__1::allocator<std::__1::unique_ptr<WasmEdge::Runtime::Instance::FunctionInstance, std::__1::default_delete<WasmEdge::Runtime::Instance::FunctionInstance> > > >&, std::__1::vector<WasmEdge::Runtime::Instance::FunctionInstance*, std::__1::allocator<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*> > >&, std::__1::unique_ptr<WasmEdge::Runtime::Instance::FunctionInstance, std::__1::default_delete<WasmEdge::Runtime::Instance::FunctionInstance> >&&) Unexecuted instantiation: std::__1::enable_if<IsEntityV<WasmEdge::Runtime::Instance::TableInstance>, void>::type WasmEdge::Runtime::Instance::ModuleInstance::unsafeAddHostInstance<WasmEdge::Runtime::Instance::TableInstance>(std::__1::basic_string_view<char, std::__1::char_traits<char> >, std::__1::vector<std::__1::unique_ptr<WasmEdge::Runtime::Instance::TableInstance, std::__1::default_delete<WasmEdge::Runtime::Instance::TableInstance> >, std::__1::allocator<std::__1::unique_ptr<WasmEdge::Runtime::Instance::TableInstance, std::__1::default_delete<WasmEdge::Runtime::Instance::TableInstance> > > >&, std::__1::vector<WasmEdge::Runtime::Instance::TableInstance*, std::__1::allocator<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*> > >&, std::__1::unique_ptr<WasmEdge::Runtime::Instance::TableInstance, std::__1::default_delete<WasmEdge::Runtime::Instance::TableInstance> >&&) Unexecuted instantiation: std::__1::enable_if<IsEntityV<WasmEdge::Runtime::Instance::MemoryInstance>, void>::type WasmEdge::Runtime::Instance::ModuleInstance::unsafeAddHostInstance<WasmEdge::Runtime::Instance::MemoryInstance>(std::__1::basic_string_view<char, std::__1::char_traits<char> >, std::__1::vector<std::__1::unique_ptr<WasmEdge::Runtime::Instance::MemoryInstance, std::__1::default_delete<WasmEdge::Runtime::Instance::MemoryInstance> >, std::__1::allocator<std::__1::unique_ptr<WasmEdge::Runtime::Instance::MemoryInstance, std::__1::default_delete<WasmEdge::Runtime::Instance::MemoryInstance> > > >&, std::__1::vector<WasmEdge::Runtime::Instance::MemoryInstance*, std::__1::allocator<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*> > >&, std::__1::unique_ptr<WasmEdge::Runtime::Instance::MemoryInstance, std::__1::default_delete<WasmEdge::Runtime::Instance::MemoryInstance> >&&) Unexecuted instantiation: std::__1::enable_if<IsEntityV<WasmEdge::Runtime::Instance::GlobalInstance>, void>::type WasmEdge::Runtime::Instance::ModuleInstance::unsafeAddHostInstance<WasmEdge::Runtime::Instance::GlobalInstance>(std::__1::basic_string_view<char, std::__1::char_traits<char> >, std::__1::vector<std::__1::unique_ptr<WasmEdge::Runtime::Instance::GlobalInstance, std::__1::default_delete<WasmEdge::Runtime::Instance::GlobalInstance> >, std::__1::allocator<std::__1::unique_ptr<WasmEdge::Runtime::Instance::GlobalInstance, std::__1::default_delete<WasmEdge::Runtime::Instance::GlobalInstance> > > >&, std::__1::vector<WasmEdge::Runtime::Instance::GlobalInstance*, std::__1::allocator<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*> > >&, std::__1::unique_ptr<WasmEdge::Runtime::Instance::GlobalInstance, std::__1::default_delete<WasmEdge::Runtime::Instance::GlobalInstance> >&&) |
522 | | |
523 | | /// Unsafe find and get the exported instance by name. |
524 | | template <typename T> |
525 | | std::enable_if_t<IsEntityV<T>, T *> |
526 | | unsafeFindExports(const std::map<std::string, T *, std::less<>> &Map, |
527 | 0 | std::string_view ExtName) const noexcept { |
528 | 0 | auto Iter = Map.find(ExtName); |
529 | 0 | if (likely(Iter != Map.cend())) { |
530 | 0 | return Iter->second; |
531 | 0 | } |
532 | 0 | return nullptr; |
533 | 0 | } Unexecuted instantiation: std::__1::enable_if<IsEntityV<WasmEdge::Runtime::Instance::FunctionInstance>, WasmEdge::Runtime::Instance::FunctionInstance*>::type WasmEdge::Runtime::Instance::ModuleInstance::unsafeFindExports<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&, std::__1::basic_string_view<char, std::__1::char_traits<char> >) const Unexecuted instantiation: std::__1::enable_if<IsEntityV<WasmEdge::Runtime::Instance::TableInstance>, WasmEdge::Runtime::Instance::TableInstance*>::type WasmEdge::Runtime::Instance::ModuleInstance::unsafeFindExports<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&, std::__1::basic_string_view<char, std::__1::char_traits<char> >) const Unexecuted instantiation: std::__1::enable_if<IsEntityV<WasmEdge::Runtime::Instance::MemoryInstance>, WasmEdge::Runtime::Instance::MemoryInstance*>::type WasmEdge::Runtime::Instance::ModuleInstance::unsafeFindExports<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&, std::__1::basic_string_view<char, std::__1::char_traits<char> >) const Unexecuted instantiation: std::__1::enable_if<IsEntityV<WasmEdge::Runtime::Instance::TagInstance>, WasmEdge::Runtime::Instance::TagInstance*>::type WasmEdge::Runtime::Instance::ModuleInstance::unsafeFindExports<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&, std::__1::basic_string_view<char, std::__1::char_traits<char> >) const Unexecuted instantiation: std::__1::enable_if<IsEntityV<WasmEdge::Runtime::Instance::GlobalInstance>, WasmEdge::Runtime::Instance::GlobalInstance*>::type WasmEdge::Runtime::Instance::ModuleInstance::unsafeFindExports<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&, std::__1::basic_string_view<char, std::__1::char_traits<char> >) const |
534 | | |
535 | | /// \name Data for compiled functions. |
536 | | /// @{ |
537 | | #if WASMEDGE_ALLOCATOR_IS_STABLE |
538 | | std::vector<uint8_t *> MemoryPtrs; |
539 | | #else |
540 | | std::vector<uint8_t **> MemoryPtrs; |
541 | | #endif |
542 | | std::vector<ValVariant *> GlobalPtrs; |
543 | | /// @} |
544 | | |
545 | | friend class Runtime::StoreManager; |
546 | | using BeforeModuleDestroyCallback = void(StoreManager *Store, |
547 | | const ModuleInstance *Mod); |
548 | 0 | void linkStore(StoreManager *Store, BeforeModuleDestroyCallback Callback) { |
549 | | // Link to store when registration. |
550 | 0 | std::unique_lock Lock(Mutex); |
551 | 0 | LinkedStore.insert_or_assign(Store, Callback); |
552 | 0 | } |
553 | | |
554 | 0 | void unlinkStore(StoreManager *Store) { |
555 | | // Unlink to store. Call by the store manager when the store manager being |
556 | | // destroyed before this module instance. |
557 | 0 | std::unique_lock Lock(Mutex); |
558 | 0 | LinkedStore.erase(Store); |
559 | 0 | } |
560 | | |
561 | | /// Mutex. |
562 | | mutable std::shared_mutex Mutex; |
563 | | |
564 | | /// Module name. |
565 | | const std::string ModName; |
566 | | |
567 | | /// Defined types. |
568 | | std::vector<const AST::SubType *> Types; |
569 | | std::vector<std::unique_ptr<const AST::SubType>> OwnedTypes; |
570 | | |
571 | | /// Owned instances in this module. |
572 | | std::vector<std::unique_ptr<FunctionInstance>> OwnedFuncInsts; |
573 | | std::vector<std::unique_ptr<TableInstance>> OwnedTabInsts; |
574 | | std::vector<std::unique_ptr<MemoryInstance>> OwnedMemInsts; |
575 | | std::vector<std::unique_ptr<TagInstance>> OwnedTagInsts; |
576 | | std::vector<std::unique_ptr<GlobalInstance>> OwnedGlobInsts; |
577 | | std::vector<std::unique_ptr<ElementInstance>> OwnedElemInsts; |
578 | | std::vector<std::unique_ptr<DataInstance>> OwnedDataInsts; |
579 | | std::vector<std::unique_ptr<ArrayInstance>> OwnedArrayInsts; |
580 | | std::vector<std::unique_ptr<StructInstance>> OwnedStructInsts; |
581 | | |
582 | | /// Imported and added instances in this module. |
583 | | std::vector<FunctionInstance *> FuncInsts; |
584 | | std::vector<TableInstance *> TabInsts; |
585 | | std::vector<MemoryInstance *> MemInsts; |
586 | | std::vector<TagInstance *> TagInsts; |
587 | | std::vector<GlobalInstance *> GlobInsts; |
588 | | std::vector<ElementInstance *> ElemInsts; |
589 | | std::vector<DataInstance *> DataInsts; |
590 | | |
591 | | /// Imported instances counts. |
592 | | uint32_t ImpGlobalNum = 0; |
593 | | |
594 | | /// Exported name maps. |
595 | | std::map<std::string, FunctionInstance *, std::less<>> ExpFuncs; |
596 | | std::map<std::string, TableInstance *, std::less<>> ExpTables; |
597 | | std::map<std::string, MemoryInstance *, std::less<>> ExpMems; |
598 | | std::map<std::string, TagInstance *, std::less<>> ExpTags; |
599 | | std::map<std::string, GlobalInstance *, std::less<>> ExpGlobals; |
600 | | |
601 | | /// Start function instance. |
602 | | FunctionInstance *StartFunc = nullptr; |
603 | | |
604 | | /// Linked store. |
605 | | std::map<StoreManager *, std::function<BeforeModuleDestroyCallback>> |
606 | | LinkedStore; |
607 | | |
608 | | /// External data and its finalizer function pointer. |
609 | | void *HostData; |
610 | | std::function<void(void *)> HostDataFinalizer; |
611 | | }; |
612 | | |
613 | | } // namespace Instance |
614 | | } // namespace Runtime |
615 | | } // namespace WasmEdge |