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