Coverage Report

Created: 2025-09-27 06:27

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/WasmEdge/include/runtime/instance/module.h
Line
Count
Source
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::Instance::ComponentInstance&, WasmEdge::AST::Component::AliasSection const&)::$_0&>(WasmEdge::Executor::Executor::instantiate(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::Instance::ComponentInstance&, WasmEdge::AST::Component::AliasSection const&)::$_0&>(WasmEdge::Executor::Executor::instantiate(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::Instance::ComponentInstance&, WasmEdge::AST::Component::AliasSection const&)::$_0&>(WasmEdge::Executor::Executor::instantiate(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::Instance::ComponentInstance&, WasmEdge::AST::Component::AliasSection const&)::$_0&>(WasmEdge::Executor::Executor::instantiate(WasmEdge::Runtime::Instance::ComponentInstance&, WasmEdge::AST::Component::AliasSection const&)::$_0&) const
230
231
protected:
232
  friend class Executor::Executor;
233
  friend class ComponentInstance;
234
  friend class Runtime::CallingFrame;
235
236
  /// Create and copy the defined type to this module instance.
237
0
  void addDefinedType(const AST::SubType &SType) {
238
0
    std::unique_lock Lock(Mutex);
239
0
    OwnedTypes.push_back(std::make_unique<AST::SubType>(SType));
240
0
    Types.push_back(OwnedTypes.back().get());
241
0
  }
242
243
  /// Create and add instances into this module instance.
244
0
  template <typename... Args> void addFunc(Args &&...Values) {
245
0
    std::unique_lock Lock(Mutex);
246
0
    unsafeAddInstance(OwnedFuncInsts, FuncInsts, this,
247
0
                      std::forward<Args>(Values)...);
248
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>&&)
249
0
  template <typename... Args> void addTable(Args &&...Values) {
250
0
    std::unique_lock Lock(Mutex);
251
0
    unsafeAddInstance(OwnedTabInsts, TabInsts, std::forward<Args>(Values)...);
252
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&)
253
0
  template <typename... Args> void addMemory(Args &&...Values) {
254
0
    std::unique_lock Lock(Mutex);
255
0
    unsafeAddInstance(OwnedMemInsts, MemInsts, std::forward<Args>(Values)...);
256
0
  }
257
0
  template <typename... Args> void addTag(Args &&...Values) {
258
0
    std::unique_lock Lock(Mutex);
259
0
    unsafeAddInstance(OwnedTagInsts, TagInsts, std::forward<Args>(Values)...);
260
0
  }
261
0
  template <typename... Args> void addGlobal(Args &&...Values) {
262
0
    std::unique_lock Lock(Mutex);
263
0
    unsafeAddInstance(OwnedGlobInsts, GlobInsts, std::forward<Args>(Values)...);
264
0
  }
265
0
  template <typename... Args> void addElem(Args &&...Values) {
266
0
    std::unique_lock Lock(Mutex);
267
0
    unsafeAddInstance(OwnedElemInsts, ElemInsts, std::forward<Args>(Values)...);
268
0
  }
269
0
  template <typename... Args> void addData(Args &&...Values) {
270
0
    std::unique_lock Lock(Mutex);
271
0
    unsafeAddInstance(OwnedDataInsts, DataInsts, std::forward<Args>(Values)...);
272
0
  }
273
0
  template <typename... Args> ArrayInstance *newArray(Args &&...Values) {
274
0
    std::unique_lock Lock(Mutex);
275
0
    OwnedArrayInsts.push_back(
276
0
        std::make_unique<ArrayInstance>(this, std::forward<Args>(Values)...));
277
0
    return OwnedArrayInsts.back().get();
278
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> > >&&)
279
0
  template <typename... Args> StructInstance *newStruct(Args &&...Values) {
280
0
    std::unique_lock Lock(Mutex);
281
0
    OwnedStructInsts.push_back(
282
0
        std::make_unique<StructInstance>(this, std::forward<Args>(Values)...));
283
0
    return OwnedStructInsts.back().get();
284
0
  }
285
286
  /// Import instances into this module instance.
287
0
  void importFunction(FunctionInstance *Func) {
288
0
    std::unique_lock Lock(Mutex);
289
0
    unsafeImportInstance(FuncInsts, Func);
290
0
  }
291
0
  void importTable(TableInstance *Tab) {
292
0
    std::unique_lock Lock(Mutex);
293
0
    unsafeImportInstance(TabInsts, Tab);
294
0
  }
295
0
  void importMemory(MemoryInstance *Mem) {
296
0
    std::unique_lock Lock(Mutex);
297
0
    unsafeImportInstance(MemInsts, Mem);
298
0
  }
299
0
  void importTag(TagInstance *Tg) {
300
0
    std::unique_lock Lock(Mutex);
301
0
    unsafeImportInstance(TagInsts, Tg);
302
0
  }
303
0
  void importGlobal(GlobalInstance *Glob) {
304
0
    std::unique_lock Lock(Mutex);
305
0
    ImpGlobalNum++;
306
0
    unsafeImportInstance(GlobInsts, Glob);
307
0
  }
308
309
  /// Export instances with name from this module instance.
310
0
  void exportFunction(std::string_view Name, uint32_t Idx) {
311
0
    std::unique_lock Lock(Mutex);
312
0
    ExpFuncs.insert_or_assign(std::string(Name), FuncInsts[Idx]);
313
0
  }
314
0
  void exportTable(std::string_view Name, uint32_t Idx) {
315
0
    std::unique_lock Lock(Mutex);
316
0
    ExpTables.insert_or_assign(std::string(Name), TabInsts[Idx]);
317
0
  }
318
0
  void exportMemory(std::string_view Name, uint32_t Idx) {
319
0
    std::unique_lock Lock(Mutex);
320
0
    ExpMems.insert_or_assign(std::string(Name), MemInsts[Idx]);
321
0
  }
322
0
  void exportGlobal(std::string_view Name, uint32_t Idx) {
323
0
    std::unique_lock Lock(Mutex);
324
0
    ExpGlobals.insert_or_assign(std::string(Name), GlobInsts[Idx]);
325
0
  }
326
0
  void exportTag(std::string_view Name, uint32_t Idx) {
327
0
    std::unique_lock Lock(Mutex);
328
0
    ExpTags.insert_or_assign(std::string(Name), TagInsts[Idx]);
329
0
  }
330
331
  /// Get defined type list.
332
0
  Span<const AST::SubType *const> getTypeList() const noexcept { return Types; }
333
334
  /// Get instance pointer by index.
335
0
  Expect<const AST::SubType *> getType(uint32_t Idx) const noexcept {
336
0
    std::shared_lock Lock(Mutex);
337
0
    if (unlikely(Idx >= Types.size())) {
338
      // Error logging need to be handled in caller.
339
0
      return Unexpect(ErrCode::Value::WrongInstanceIndex);
340
0
    }
341
0
    return unsafeGetType(Idx);
342
0
  }
343
0
  const AST::SubType *unsafeGetType(uint32_t Idx) const noexcept {
344
0
    return Types[Idx];
345
0
  }
346
0
  Expect<FunctionInstance *> getFunc(uint32_t Idx) const noexcept {
347
0
    std::shared_lock Lock(Mutex);
348
0
    if (Idx >= FuncInsts.size()) {
349
0
      // Error logging need to be handled in caller.
350
0
      return Unexpect(ErrCode::Value::WrongInstanceIndex);
351
0
    }
352
0
    return unsafeGetFunction(Idx);
353
0
  }
354
0
  FunctionInstance *unsafeGetFunction(uint32_t Idx) const noexcept {
355
0
    return FuncInsts[Idx];
356
0
  }
357
0
  Expect<TableInstance *> getTable(uint32_t Idx) const noexcept {
358
0
    std::shared_lock Lock(Mutex);
359
0
    if (Idx >= TabInsts.size()) {
360
0
      // Error logging need to be handled in caller.
361
0
      return Unexpect(ErrCode::Value::WrongInstanceIndex);
362
0
    }
363
0
    return unsafeGetTable(Idx);
364
0
  }
365
0
  TableInstance *unsafeGetTable(uint32_t Idx) const noexcept {
366
0
    return TabInsts[Idx];
367
0
  }
368
0
  Expect<MemoryInstance *> getMemory(uint32_t Idx) const noexcept {
369
0
    std::shared_lock Lock(Mutex);
370
0
    if (Idx >= MemInsts.size()) {
371
      // Error logging need to be handled in caller.
372
0
      return Unexpect(ErrCode::Value::WrongInstanceIndex);
373
0
    }
374
0
    return unsafeGetMemory(Idx);
375
0
  }
376
0
  MemoryInstance *unsafeGetMemory(uint32_t Idx) const noexcept {
377
0
    return MemInsts[Idx];
378
0
  }
379
0
  TagInstance *unsafeGetTag(uint32_t Idx) const noexcept {
380
0
    return TagInsts[Idx];
381
0
  }
382
0
  Expect<GlobalInstance *> getGlobal(uint32_t Idx) const noexcept {
383
0
    std::shared_lock Lock(Mutex);
384
0
    if (Idx >= GlobInsts.size()) {
385
      // Error logging need to be handled in caller.
386
0
      return Unexpect(ErrCode::Value::WrongInstanceIndex);
387
0
    }
388
0
    return unsafeGetGlobal(Idx);
389
0
  }
390
0
  GlobalInstance *unsafeGetGlobal(uint32_t Idx) const noexcept {
391
0
    return GlobInsts[Idx];
392
0
  }
393
0
  Expect<ElementInstance *> getElem(uint32_t Idx) const noexcept {
394
0
    std::shared_lock Lock(Mutex);
395
0
    if (Idx >= ElemInsts.size()) {
396
0
      // Error logging need to be handled in caller.
397
0
      return Unexpect(ErrCode::Value::WrongInstanceIndex);
398
0
    }
399
0
    return unsafeGetElem(Idx);
400
0
  }
401
0
  ElementInstance *unsafeGetElem(uint32_t Idx) const noexcept {
402
0
    return ElemInsts[Idx];
403
0
  }
404
0
  Expect<DataInstance *> getData(uint32_t Idx) const noexcept {
405
0
    std::shared_lock Lock(Mutex);
406
0
    if (Idx >= DataInsts.size()) {
407
0
      // Error logging need to be handled in caller.
408
0
      return Unexpect(ErrCode::Value::WrongInstanceIndex);
409
0
    }
410
0
    return unsafeGetData(Idx);
411
0
  }
412
0
  DataInstance *unsafeGetData(uint32_t Idx) const noexcept {
413
0
    return DataInsts[Idx];
414
0
  }
415
416
  /// Get the instances count.
417
0
  uint32_t getFuncNum() const noexcept {
418
0
    std::shared_lock Lock(Mutex);
419
0
    return static_cast<uint32_t>(FuncInsts.size());
420
0
  }
421
0
  uint32_t getMemoryNum() const noexcept {
422
0
    std::shared_lock Lock(Mutex);
423
0
    return static_cast<uint32_t>(MemInsts.size());
424
0
  }
425
0
  uint32_t getGlobalNum() const noexcept {
426
0
    std::shared_lock Lock(Mutex);
427
0
    return static_cast<uint32_t>(GlobInsts.size());
428
0
  }
429
430
  /// Get imported global instances count.
431
0
  uint32_t getGlobalImportNum() const noexcept { return ImpGlobalNum; }
432
433
  /// Set the start function index and find the function instance.
434
0
  void setStartIdx(uint32_t Idx) noexcept {
435
0
    std::unique_lock Lock(Mutex);
436
0
    StartFunc = FuncInsts[Idx];
437
0
  }
438
439
  /// Get start function address in Store.
440
0
  const FunctionInstance *getStartFunc() const noexcept {
441
0
    std::shared_lock Lock(Mutex);
442
0
    return StartFunc;
443
0
  }
444
445
  /// Set the target imported WASI module when instantiation.
446
0
  void setWASIModule(const ModuleInstance *Mod) noexcept {
447
0
    std::unique_lock Lock(Mutex);
448
0
    WASIModInst = Mod;
449
0
  }
450
451
  /// Get the target imported WASI module when instantiation.
452
0
  const ModuleInstance *getWASIModule() const noexcept {
453
0
    std::shared_lock Lock(Mutex);
454
0
    return WASIModInst;
455
0
  }
456
457
  /// Unsafe import instance into this module.
458
  template <typename T>
459
  std::enable_if_t<IsEntityV<T>, void>
460
0
  unsafeImportInstance(std::vector<T *> &Vec, T *Ptr) {
461
0
    Vec.push_back(Ptr);
462
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*)
463
464
  /// Unsafe import defined type from host function into this module.
465
0
  void unsafeImportDefinedType(const AST::SubType &SType) {
466
0
    Types.push_back(&SType);
467
0
    const_cast<AST::SubType *>(Types.back())
468
0
        ->setTypeIndex(static_cast<uint32_t>(Types.size()) - 1);
469
0
  }
470
471
  /// Unsafe create and add the instance into this module.
472
  template <typename T, typename... Args>
473
  std::enable_if_t<IsInstanceV<T>, void>
474
  unsafeAddInstance(std::vector<std::unique_ptr<T>> &OwnedInstsVec,
475
0
                    std::vector<T *> &InstsVec, Args &&...Values) {
476
0
    OwnedInstsVec.push_back(std::make_unique<T>(std::forward<Args>(Values)...));
477
0
    InstsVec.push_back(OwnedInstsVec.back().get());
478
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*&)
479
480
  /// Unsafe add and export the existing instance into this module.
481
  template <typename T, typename... Args>
482
  std::enable_if_t<IsEntityV<T>, void>
483
  unsafeAddHostInstance(std::string_view Name,
484
                        std::vector<std::unique_ptr<T>> &OwnedInstsVec,
485
                        std::vector<T *> &InstsVec,
486
                        std::map<std::string, T *, std::less<>> &InstsMap,
487
0
                        std::unique_ptr<T> &&Inst) {
488
0
    OwnedInstsVec.push_back(std::move(Inst));
489
0
    InstsVec.push_back(OwnedInstsVec.back().get());
490
0
    InstsMap.insert_or_assign(std::string(Name), InstsVec.back());
491
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> >&&)
492
493
  /// Unsafe find and get the exported instance by name.
494
  template <typename T>
495
  std::enable_if_t<IsEntityV<T>, T *>
496
  unsafeFindExports(const std::map<std::string, T *, std::less<>> &Map,
497
0
                    std::string_view ExtName) const noexcept {
498
0
    auto Iter = Map.find(ExtName);
499
0
    if (likely(Iter != Map.cend())) {
500
0
      return Iter->second;
501
0
    }
502
0
    return nullptr;
503
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
504
505
  /// \name Data for compiled functions.
506
  /// @{
507
#if WASMEDGE_ALLOCATOR_IS_STABLE
508
  std::vector<uint8_t *> MemoryPtrs;
509
#else
510
  std::vector<uint8_t **> MemoryPtrs;
511
#endif
512
  std::vector<ValVariant *> GlobalPtrs;
513
  /// @}
514
515
  friend class Runtime::StoreManager;
516
  using BeforeModuleDestroyCallback = void(StoreManager *Store,
517
                                           const ModuleInstance *Mod);
518
0
  void linkStore(StoreManager *Store, BeforeModuleDestroyCallback Callback) {
519
    // Link to store when registration.
520
0
    std::unique_lock Lock(Mutex);
521
0
    LinkedStore.insert_or_assign(Store, Callback);
522
0
  }
523
524
0
  void unlinkStore(StoreManager *Store) {
525
    // Unlink to store. Call by the store manager when the store manager being
526
    // destroyed before this module instance.
527
0
    std::unique_lock Lock(Mutex);
528
0
    LinkedStore.erase(Store);
529
0
  }
530
531
  /// Mutex.
532
  mutable std::shared_mutex Mutex;
533
534
  /// Module name.
535
  const std::string ModName;
536
537
  /// Defined types.
538
  std::vector<const AST::SubType *> Types;
539
  std::vector<std::unique_ptr<const AST::SubType>> OwnedTypes;
540
541
  /// Owned instances in this module.
542
  std::vector<std::unique_ptr<FunctionInstance>> OwnedFuncInsts;
543
  std::vector<std::unique_ptr<TableInstance>> OwnedTabInsts;
544
  std::vector<std::unique_ptr<MemoryInstance>> OwnedMemInsts;
545
  std::vector<std::unique_ptr<TagInstance>> OwnedTagInsts;
546
  std::vector<std::unique_ptr<GlobalInstance>> OwnedGlobInsts;
547
  std::vector<std::unique_ptr<ElementInstance>> OwnedElemInsts;
548
  std::vector<std::unique_ptr<DataInstance>> OwnedDataInsts;
549
  std::vector<std::unique_ptr<ArrayInstance>> OwnedArrayInsts;
550
  std::vector<std::unique_ptr<StructInstance>> OwnedStructInsts;
551
552
  /// Imported and added instances in this module.
553
  std::vector<FunctionInstance *> FuncInsts;
554
  std::vector<TableInstance *> TabInsts;
555
  std::vector<MemoryInstance *> MemInsts;
556
  std::vector<TagInstance *> TagInsts;
557
  std::vector<GlobalInstance *> GlobInsts;
558
  std::vector<ElementInstance *> ElemInsts;
559
  std::vector<DataInstance *> DataInsts;
560
561
  /// Imported instances counts.
562
  uint32_t ImpGlobalNum = 0;
563
564
  /// Exported name maps.
565
  std::map<std::string, FunctionInstance *, std::less<>> ExpFuncs;
566
  std::map<std::string, TableInstance *, std::less<>> ExpTables;
567
  std::map<std::string, MemoryInstance *, std::less<>> ExpMems;
568
  std::map<std::string, TagInstance *, std::less<>> ExpTags;
569
  std::map<std::string, GlobalInstance *, std::less<>> ExpGlobals;
570
571
  /// Start function instance.
572
  const FunctionInstance *StartFunc = nullptr;
573
574
  /// Imported WASI module instance when instantiation.
575
  const ModuleInstance *WASIModInst = nullptr;
576
577
  /// Linked store.
578
  std::map<StoreManager *, std::function<BeforeModuleDestroyCallback>>
579
      LinkedStore;
580
581
  /// External data and its finalizer function pointer.
582
  void *HostData;
583
  std::function<void(void *)> HostDataFinalizer;
584
};
585
586
} // namespace Instance
587
} // namespace Runtime
588
} // namespace WasmEdge