/src/WasmEdge/include/runtime/instance/component.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/component.h - Component Instance definition // |
5 | | // |
6 | | // Part of the WasmEdge Project. |
7 | | // |
8 | | //===----------------------------------------------------------------------===// |
9 | | /// |
10 | | /// \file |
11 | | /// This file contains the component instance definition. |
12 | | /// |
13 | | //===----------------------------------------------------------------------===// |
14 | | #pragma once |
15 | | |
16 | | #include "ast/component/component.h" |
17 | | #include "ast/module.h" |
18 | | #include "ast/type.h" |
19 | | #include "common/errcode.h" |
20 | | #include "common/types.h" |
21 | | #include "runtime/component/hostfunc.h" |
22 | | #include "runtime/instance/component/function.h" |
23 | | #include "runtime/instance/module.h" |
24 | | |
25 | | #include <atomic> |
26 | | #include <functional> |
27 | | #include <map> |
28 | | #include <memory> |
29 | | #include <mutex> |
30 | | #include <set> |
31 | | #include <shared_mutex> |
32 | | #include <string> |
33 | | #include <type_traits> |
34 | | #include <vector> |
35 | | |
36 | | namespace WasmEdge { |
37 | | |
38 | | namespace Runtime { |
39 | | |
40 | | namespace Instance { |
41 | | |
42 | | using namespace AST::Component; |
43 | | |
44 | | namespace { |
45 | 0 | void typeConvert(ValueType &VT, const ValType &Ty) noexcept { |
46 | 0 | switch (Ty.getCode()) { |
47 | 0 | case TypeCode::I8: |
48 | 0 | VT.emplace<PrimValType>(PrimValType::S8); |
49 | 0 | break; |
50 | 0 | case TypeCode::I16: |
51 | 0 | VT.emplace<PrimValType>(PrimValType::S16); |
52 | 0 | break; |
53 | 0 | case TypeCode::I32: |
54 | 0 | VT.emplace<PrimValType>(PrimValType::S32); |
55 | 0 | break; |
56 | 0 | case TypeCode::I64: |
57 | 0 | VT.emplace<PrimValType>(PrimValType::S64); |
58 | 0 | break; |
59 | 0 | case TypeCode::F32: |
60 | 0 | VT.emplace<PrimValType>(PrimValType::F32); |
61 | 0 | break; |
62 | 0 | case TypeCode::F64: |
63 | 0 | VT.emplace<PrimValType>(PrimValType::F64); |
64 | 0 | break; |
65 | 0 |
|
66 | 0 | default: |
67 | 0 | break; |
68 | 0 | } |
69 | 0 | } Unexecuted instantiation: wasmedge.cpp:WasmEdge::Runtime::Instance::(anonymous namespace)::typeConvert(std::__1::variant<unsigned int, WasmEdge::AST::Component::PrimValType>&, WasmEdge::ValType const&) Unexecuted instantiation: uniTool.cpp:WasmEdge::Runtime::Instance::(anonymous namespace)::typeConvert(std::__1::variant<unsigned int, WasmEdge::AST::Component::PrimValType>&, WasmEdge::ValType const&) Unexecuted instantiation: runtimeTool.cpp:WasmEdge::Runtime::Instance::(anonymous namespace)::typeConvert(std::__1::variant<unsigned int, WasmEdge::AST::Component::PrimValType>&, WasmEdge::ValType const&) Unexecuted instantiation: vm.cpp:WasmEdge::Runtime::Instance::(anonymous namespace)::typeConvert(std::__1::variant<unsigned int, WasmEdge::AST::Component::PrimValType>&, WasmEdge::ValType const&) Unexecuted instantiation: plugin.cpp:WasmEdge::Runtime::Instance::(anonymous namespace)::typeConvert(std::__1::variant<unsigned int, WasmEdge::AST::Component::PrimValType>&, WasmEdge::ValType const&) Unexecuted instantiation: module.cpp:WasmEdge::Runtime::Instance::(anonymous namespace)::typeConvert(std::__1::variant<unsigned int, WasmEdge::AST::Component::PrimValType>&, WasmEdge::ValType const&) Unexecuted instantiation: proxy.cpp:WasmEdge::Runtime::Instance::(anonymous namespace)::typeConvert(std::__1::variant<unsigned int, WasmEdge::AST::Component::PrimValType>&, WasmEdge::ValType const&) Unexecuted instantiation: threadInstr.cpp:WasmEdge::Runtime::Instance::(anonymous namespace)::typeConvert(std::__1::variant<unsigned int, WasmEdge::AST::Component::PrimValType>&, WasmEdge::ValType const&) Unexecuted instantiation: refInstr.cpp:WasmEdge::Runtime::Instance::(anonymous namespace)::typeConvert(std::__1::variant<unsigned int, WasmEdge::AST::Component::PrimValType>&, WasmEdge::ValType const&) Unexecuted instantiation: engine.cpp:WasmEdge::Runtime::Instance::(anonymous namespace)::typeConvert(std::__1::variant<unsigned int, WasmEdge::AST::Component::PrimValType>&, WasmEdge::ValType const&) Unexecuted instantiation: helper.cpp:WasmEdge::Runtime::Instance::(anonymous namespace)::typeConvert(std::__1::variant<unsigned int, WasmEdge::AST::Component::PrimValType>&, WasmEdge::ValType const&) Unexecuted instantiation: executor.cpp:WasmEdge::Runtime::Instance::(anonymous namespace)::typeConvert(std::__1::variant<unsigned int, WasmEdge::AST::Component::PrimValType>&, WasmEdge::ValType const&) Unexecuted instantiation: component.cpp:WasmEdge::Runtime::Instance::(anonymous namespace)::typeConvert(std::__1::variant<unsigned int, WasmEdge::AST::Component::PrimValType>&, WasmEdge::ValType const&) Unexecuted instantiation: instantiate_component_alias.cpp:WasmEdge::Runtime::Instance::(anonymous namespace)::typeConvert(std::__1::variant<unsigned int, WasmEdge::AST::Component::PrimValType>&, WasmEdge::ValType const&) Unexecuted instantiation: instantiate_component_canon.cpp:WasmEdge::Runtime::Instance::(anonymous namespace)::typeConvert(std::__1::variant<unsigned int, WasmEdge::AST::Component::PrimValType>&, WasmEdge::ValType const&) Unexecuted instantiation: instantiate_component_export.cpp:WasmEdge::Runtime::Instance::(anonymous namespace)::typeConvert(std::__1::variant<unsigned int, WasmEdge::AST::Component::PrimValType>&, WasmEdge::ValType const&) Unexecuted instantiation: instantiate_component_import.cpp:WasmEdge::Runtime::Instance::(anonymous namespace)::typeConvert(std::__1::variant<unsigned int, WasmEdge::AST::Component::PrimValType>&, WasmEdge::ValType const&) Unexecuted instantiation: instantiate_component_instance.cpp:WasmEdge::Runtime::Instance::(anonymous namespace)::typeConvert(std::__1::variant<unsigned int, WasmEdge::AST::Component::PrimValType>&, WasmEdge::ValType const&) Unexecuted instantiation: instantiate_component_start.cpp:WasmEdge::Runtime::Instance::(anonymous namespace)::typeConvert(std::__1::variant<unsigned int, WasmEdge::AST::Component::PrimValType>&, WasmEdge::ValType const&) Unexecuted instantiation: instantiate_component_type.cpp:WasmEdge::Runtime::Instance::(anonymous namespace)::typeConvert(std::__1::variant<unsigned int, WasmEdge::AST::Component::PrimValType>&, WasmEdge::ValType const&) Unexecuted instantiation: tag.cpp:WasmEdge::Runtime::Instance::(anonymous namespace)::typeConvert(std::__1::variant<unsigned int, WasmEdge::AST::Component::PrimValType>&, WasmEdge::ValType const&) Unexecuted instantiation: controlInstr.cpp:WasmEdge::Runtime::Instance::(anonymous namespace)::typeConvert(std::__1::variant<unsigned int, WasmEdge::AST::Component::PrimValType>&, WasmEdge::ValType const&) Unexecuted instantiation: tableInstr.cpp:WasmEdge::Runtime::Instance::(anonymous namespace)::typeConvert(std::__1::variant<unsigned int, WasmEdge::AST::Component::PrimValType>&, WasmEdge::ValType const&) Unexecuted instantiation: memoryInstr.cpp:WasmEdge::Runtime::Instance::(anonymous namespace)::typeConvert(std::__1::variant<unsigned int, WasmEdge::AST::Component::PrimValType>&, WasmEdge::ValType const&) Unexecuted instantiation: variableInstr.cpp:WasmEdge::Runtime::Instance::(anonymous namespace)::typeConvert(std::__1::variant<unsigned int, WasmEdge::AST::Component::PrimValType>&, WasmEdge::ValType const&) Unexecuted instantiation: import.cpp:WasmEdge::Runtime::Instance::(anonymous namespace)::typeConvert(std::__1::variant<unsigned int, WasmEdge::AST::Component::PrimValType>&, WasmEdge::ValType const&) Unexecuted instantiation: function.cpp:WasmEdge::Runtime::Instance::(anonymous namespace)::typeConvert(std::__1::variant<unsigned int, WasmEdge::AST::Component::PrimValType>&, WasmEdge::ValType const&) Unexecuted instantiation: global.cpp:WasmEdge::Runtime::Instance::(anonymous namespace)::typeConvert(std::__1::variant<unsigned int, WasmEdge::AST::Component::PrimValType>&, WasmEdge::ValType const&) Unexecuted instantiation: table.cpp:WasmEdge::Runtime::Instance::(anonymous namespace)::typeConvert(std::__1::variant<unsigned int, WasmEdge::AST::Component::PrimValType>&, WasmEdge::ValType const&) Unexecuted instantiation: memory.cpp:WasmEdge::Runtime::Instance::(anonymous namespace)::typeConvert(std::__1::variant<unsigned int, WasmEdge::AST::Component::PrimValType>&, WasmEdge::ValType const&) Unexecuted instantiation: elem.cpp:WasmEdge::Runtime::Instance::(anonymous namespace)::typeConvert(std::__1::variant<unsigned int, WasmEdge::AST::Component::PrimValType>&, WasmEdge::ValType const&) Unexecuted instantiation: data.cpp:WasmEdge::Runtime::Instance::(anonymous namespace)::typeConvert(std::__1::variant<unsigned int, WasmEdge::AST::Component::PrimValType>&, WasmEdge::ValType const&) Unexecuted instantiation: export.cpp:WasmEdge::Runtime::Instance::(anonymous namespace)::typeConvert(std::__1::variant<unsigned int, WasmEdge::AST::Component::PrimValType>&, WasmEdge::ValType const&) Unexecuted instantiation: wasifunc.cpp:WasmEdge::Runtime::Instance::(anonymous namespace)::typeConvert(std::__1::variant<unsigned int, WasmEdge::AST::Component::PrimValType>&, WasmEdge::ValType const&) |
70 | | |
71 | 0 | void typeConvert(FuncType &FT, const AST::FunctionType &Ty) noexcept { |
72 | 0 | auto &PL = FT.getParamList(); |
73 | 0 | for (auto const &PT : Ty.getParamTypes()) { |
74 | 0 | LabelValType L{}; |
75 | 0 | typeConvert(L.getValType(), PT); |
76 | 0 | PL.emplace_back(L); |
77 | 0 | } |
78 | 0 | auto &RL = FT.getResultList(); |
79 | 0 | if (Ty.getReturnTypes().size() == 1) { |
80 | 0 | typeConvert(RL.emplace<ValueType>(), Ty.getReturnTypes()[0]); |
81 | 0 | } else { |
82 | 0 | auto &LL = RL.emplace<std::vector<LabelValType>>(); |
83 | 0 | for (auto const &RT : Ty.getReturnTypes()) { |
84 | 0 | LabelValType L{}; |
85 | 0 | typeConvert(L.getValType(), RT); |
86 | 0 | LL.emplace_back(L); |
87 | 0 | } |
88 | 0 | } |
89 | 0 | } Unexecuted instantiation: wasmedge.cpp:WasmEdge::Runtime::Instance::(anonymous namespace)::typeConvert(WasmEdge::AST::Component::FuncType&, WasmEdge::AST::FunctionType const&) Unexecuted instantiation: uniTool.cpp:WasmEdge::Runtime::Instance::(anonymous namespace)::typeConvert(WasmEdge::AST::Component::FuncType&, WasmEdge::AST::FunctionType const&) Unexecuted instantiation: runtimeTool.cpp:WasmEdge::Runtime::Instance::(anonymous namespace)::typeConvert(WasmEdge::AST::Component::FuncType&, WasmEdge::AST::FunctionType const&) Unexecuted instantiation: vm.cpp:WasmEdge::Runtime::Instance::(anonymous namespace)::typeConvert(WasmEdge::AST::Component::FuncType&, WasmEdge::AST::FunctionType const&) Unexecuted instantiation: plugin.cpp:WasmEdge::Runtime::Instance::(anonymous namespace)::typeConvert(WasmEdge::AST::Component::FuncType&, WasmEdge::AST::FunctionType const&) Unexecuted instantiation: module.cpp:WasmEdge::Runtime::Instance::(anonymous namespace)::typeConvert(WasmEdge::AST::Component::FuncType&, WasmEdge::AST::FunctionType const&) Unexecuted instantiation: proxy.cpp:WasmEdge::Runtime::Instance::(anonymous namespace)::typeConvert(WasmEdge::AST::Component::FuncType&, WasmEdge::AST::FunctionType const&) Unexecuted instantiation: threadInstr.cpp:WasmEdge::Runtime::Instance::(anonymous namespace)::typeConvert(WasmEdge::AST::Component::FuncType&, WasmEdge::AST::FunctionType const&) Unexecuted instantiation: refInstr.cpp:WasmEdge::Runtime::Instance::(anonymous namespace)::typeConvert(WasmEdge::AST::Component::FuncType&, WasmEdge::AST::FunctionType const&) Unexecuted instantiation: engine.cpp:WasmEdge::Runtime::Instance::(anonymous namespace)::typeConvert(WasmEdge::AST::Component::FuncType&, WasmEdge::AST::FunctionType const&) Unexecuted instantiation: helper.cpp:WasmEdge::Runtime::Instance::(anonymous namespace)::typeConvert(WasmEdge::AST::Component::FuncType&, WasmEdge::AST::FunctionType const&) Unexecuted instantiation: executor.cpp:WasmEdge::Runtime::Instance::(anonymous namespace)::typeConvert(WasmEdge::AST::Component::FuncType&, WasmEdge::AST::FunctionType const&) Unexecuted instantiation: component.cpp:WasmEdge::Runtime::Instance::(anonymous namespace)::typeConvert(WasmEdge::AST::Component::FuncType&, WasmEdge::AST::FunctionType const&) Unexecuted instantiation: instantiate_component_alias.cpp:WasmEdge::Runtime::Instance::(anonymous namespace)::typeConvert(WasmEdge::AST::Component::FuncType&, WasmEdge::AST::FunctionType const&) Unexecuted instantiation: instantiate_component_canon.cpp:WasmEdge::Runtime::Instance::(anonymous namespace)::typeConvert(WasmEdge::AST::Component::FuncType&, WasmEdge::AST::FunctionType const&) Unexecuted instantiation: instantiate_component_export.cpp:WasmEdge::Runtime::Instance::(anonymous namespace)::typeConvert(WasmEdge::AST::Component::FuncType&, WasmEdge::AST::FunctionType const&) Unexecuted instantiation: instantiate_component_import.cpp:WasmEdge::Runtime::Instance::(anonymous namespace)::typeConvert(WasmEdge::AST::Component::FuncType&, WasmEdge::AST::FunctionType const&) Unexecuted instantiation: instantiate_component_instance.cpp:WasmEdge::Runtime::Instance::(anonymous namespace)::typeConvert(WasmEdge::AST::Component::FuncType&, WasmEdge::AST::FunctionType const&) Unexecuted instantiation: instantiate_component_start.cpp:WasmEdge::Runtime::Instance::(anonymous namespace)::typeConvert(WasmEdge::AST::Component::FuncType&, WasmEdge::AST::FunctionType const&) Unexecuted instantiation: instantiate_component_type.cpp:WasmEdge::Runtime::Instance::(anonymous namespace)::typeConvert(WasmEdge::AST::Component::FuncType&, WasmEdge::AST::FunctionType const&) Unexecuted instantiation: tag.cpp:WasmEdge::Runtime::Instance::(anonymous namespace)::typeConvert(WasmEdge::AST::Component::FuncType&, WasmEdge::AST::FunctionType const&) Unexecuted instantiation: controlInstr.cpp:WasmEdge::Runtime::Instance::(anonymous namespace)::typeConvert(WasmEdge::AST::Component::FuncType&, WasmEdge::AST::FunctionType const&) Unexecuted instantiation: tableInstr.cpp:WasmEdge::Runtime::Instance::(anonymous namespace)::typeConvert(WasmEdge::AST::Component::FuncType&, WasmEdge::AST::FunctionType const&) Unexecuted instantiation: memoryInstr.cpp:WasmEdge::Runtime::Instance::(anonymous namespace)::typeConvert(WasmEdge::AST::Component::FuncType&, WasmEdge::AST::FunctionType const&) Unexecuted instantiation: variableInstr.cpp:WasmEdge::Runtime::Instance::(anonymous namespace)::typeConvert(WasmEdge::AST::Component::FuncType&, WasmEdge::AST::FunctionType const&) Unexecuted instantiation: import.cpp:WasmEdge::Runtime::Instance::(anonymous namespace)::typeConvert(WasmEdge::AST::Component::FuncType&, WasmEdge::AST::FunctionType const&) Unexecuted instantiation: function.cpp:WasmEdge::Runtime::Instance::(anonymous namespace)::typeConvert(WasmEdge::AST::Component::FuncType&, WasmEdge::AST::FunctionType const&) Unexecuted instantiation: global.cpp:WasmEdge::Runtime::Instance::(anonymous namespace)::typeConvert(WasmEdge::AST::Component::FuncType&, WasmEdge::AST::FunctionType const&) Unexecuted instantiation: table.cpp:WasmEdge::Runtime::Instance::(anonymous namespace)::typeConvert(WasmEdge::AST::Component::FuncType&, WasmEdge::AST::FunctionType const&) Unexecuted instantiation: memory.cpp:WasmEdge::Runtime::Instance::(anonymous namespace)::typeConvert(WasmEdge::AST::Component::FuncType&, WasmEdge::AST::FunctionType const&) Unexecuted instantiation: elem.cpp:WasmEdge::Runtime::Instance::(anonymous namespace)::typeConvert(WasmEdge::AST::Component::FuncType&, WasmEdge::AST::FunctionType const&) Unexecuted instantiation: data.cpp:WasmEdge::Runtime::Instance::(anonymous namespace)::typeConvert(WasmEdge::AST::Component::FuncType&, WasmEdge::AST::FunctionType const&) Unexecuted instantiation: export.cpp:WasmEdge::Runtime::Instance::(anonymous namespace)::typeConvert(WasmEdge::AST::Component::FuncType&, WasmEdge::AST::FunctionType const&) Unexecuted instantiation: wasifunc.cpp:WasmEdge::Runtime::Instance::(anonymous namespace)::typeConvert(WasmEdge::AST::Component::FuncType&, WasmEdge::AST::FunctionType const&) |
90 | | } // namespace |
91 | | |
92 | | class ComponentInstance { |
93 | | public: |
94 | 0 | ComponentInstance(std::string_view Name) : CompName(Name) {} |
95 | | |
96 | 0 | std::string_view getComponentName() const noexcept { return CompName; } |
97 | | |
98 | 0 | void addModule(const AST::Module &M) noexcept { ModList.emplace_back(M); } |
99 | 0 | const AST::Module &getModule(uint32_t Index) const noexcept { |
100 | 0 | return ModList[Index]; |
101 | 0 | } |
102 | | |
103 | 0 | void addComponent(const AST::Component::Component &C) noexcept { |
104 | 0 | CompList.emplace_back(C); |
105 | 0 | } |
106 | 0 | const AST::Component::Component &getComponent(uint32_t Index) const noexcept { |
107 | 0 | return CompList[Index]; |
108 | 0 | } |
109 | | |
110 | 0 | void addModuleInstance(ModuleInstance *Inst) noexcept { |
111 | 0 | ModInstList.push_back(std::move(Inst)); |
112 | 0 | } |
113 | 0 | void addModuleInstance(std::unique_ptr<ModuleInstance> Inst) noexcept { |
114 | 0 | ModInstList.push_back(Inst.get()); |
115 | 0 | OwnedModInstList.push_back(std::move(Inst)); |
116 | 0 | } |
117 | 0 | const ModuleInstance *getModuleInstance(uint32_t Index) const noexcept { |
118 | 0 | return ModInstList[Index]; |
119 | 0 | } |
120 | | |
121 | 0 | void addComponentInstance(const ComponentInstance *Inst) noexcept { |
122 | 0 | CompInstList.push_back(Inst); |
123 | 0 | } |
124 | 0 | void addComponentInstance(std::unique_ptr<ComponentInstance> Inst) noexcept { |
125 | 0 | CompInstList.push_back(Inst.get()); |
126 | 0 | OwnedCompInstList.push_back(std::move(Inst)); |
127 | 0 | } |
128 | 0 | const ComponentInstance *getComponentInstance(uint32_t Index) const noexcept { |
129 | 0 | return CompInstList[Index]; |
130 | 0 | } |
131 | | |
132 | | void addHostFunc( |
133 | | std::string_view Name, |
134 | 0 | std::unique_ptr<WasmEdge::Runtime::Component::HostFunctionBase> &&Func) { |
135 | 0 | addType(Func->getFuncType()); |
136 | 0 | auto FuncInst = std::make_unique<Instance::Component::FunctionInstance>( |
137 | 0 | std::move(Func)); |
138 | 0 | unsafeAddHostFunc(Name, std::move(FuncInst)); |
139 | 0 | } |
140 | | void addHostFunc(std::string_view Name, |
141 | 0 | std::unique_ptr<Component::FunctionInstance> &&Func) { |
142 | 0 | addType(Func->getFuncType()); |
143 | 0 | unsafeAddHostFunc(Name, std::move(Func)); |
144 | 0 | } |
145 | | |
146 | | void |
147 | 0 | addCoreFunctionInstance(std::unique_ptr<FunctionInstance> &&Inst) noexcept { |
148 | 0 | addCoreFunctionInstance(Inst.get()); |
149 | 0 | OwnedCoreFuncInstList.emplace_back(std::move(Inst)); |
150 | 0 | } |
151 | 0 | void addCoreFunctionInstance(FunctionInstance *Inst) noexcept { |
152 | 0 | CoreFuncInstList.push_back(Inst); |
153 | 0 | } |
154 | 0 | FunctionInstance *getCoreFunctionInstance(uint32_t Index) const noexcept { |
155 | 0 | return CoreFuncInstList[Index]; |
156 | 0 | } |
157 | | |
158 | | void addFunctionInstance( |
159 | 0 | std::unique_ptr<Component::FunctionInstance> Inst) noexcept { |
160 | 0 | addFunctionInstance(Inst.get()); |
161 | 0 | OwnedFuncInstList.emplace_back(std::move(Inst)); |
162 | 0 | } |
163 | 0 | void addFunctionInstance(Component::FunctionInstance *Inst) noexcept { |
164 | 0 | FuncInstList.push_back(Inst); |
165 | 0 | } |
166 | | Component::FunctionInstance * |
167 | 0 | getFunctionInstance(uint32_t Index) const noexcept { |
168 | 0 | return FuncInstList[Index]; |
169 | 0 | } |
170 | | |
171 | | // values stored in component instance |
172 | 0 | ValInterface getValue(uint32_t Index) const noexcept { |
173 | 0 | return ValueList[Index]; |
174 | 0 | } |
175 | 0 | void setValue(uint32_t Index, ValInterface V) noexcept { |
176 | 0 | ValueList[Index] = V; |
177 | 0 | } |
178 | | |
179 | 0 | void addExport(std::string_view Name, const ModuleInstance *Inst) { |
180 | 0 | ExportModuleMap.emplace(Name, Inst); |
181 | 0 | } |
182 | | const ModuleInstance * |
183 | 0 | findModuleExports(std::string_view Name) const noexcept { |
184 | 0 | return ExportModuleMap.at(std::string(Name)); |
185 | 0 | } |
186 | 0 | void addExport(std::string_view Name, Component::FunctionInstance *Inst) { |
187 | 0 | ExportFuncMap.insert_or_assign(std::string(Name), Inst); |
188 | 0 | } |
189 | | Component::FunctionInstance * |
190 | 0 | findFuncExports(std::string_view Name) const noexcept { |
191 | 0 | return ExportFuncMap.at(std::string(Name)); |
192 | 0 | } |
193 | | std::vector<std::pair<std::string, const AST::FunctionType &>> |
194 | 0 | getFuncExports() { |
195 | 0 | std::vector<std::pair<std::string, const AST::FunctionType &>> R; |
196 | 0 | R.reserve(ExportFuncMap.size()); |
197 | 0 | for (auto &&[Name, Func] : ExportFuncMap) { |
198 | 0 | const auto &FuncType = Func->getFuncType(); |
199 | 0 | R.emplace_back(Name, FuncType); |
200 | 0 | } |
201 | 0 | return R; |
202 | 0 | } |
203 | | |
204 | 0 | void addCoreTableInstance(TableInstance *Inst) noexcept { |
205 | 0 | CoreTabInstList.push_back(Inst); |
206 | 0 | } |
207 | 0 | TableInstance *getCoreTableInstance(uint32_t Index) const noexcept { |
208 | 0 | return CoreTabInstList[Index]; |
209 | 0 | } |
210 | 0 | void addCoreMemoryInstance(MemoryInstance *Inst) noexcept { |
211 | 0 | CoreMemInstList.push_back(Inst); |
212 | 0 | } |
213 | 0 | MemoryInstance *getCoreMemoryInstance(uint32_t Index) const noexcept { |
214 | 0 | return CoreMemInstList[Index]; |
215 | 0 | } |
216 | 0 | void addCoreGlobalInstance(GlobalInstance *Inst) noexcept { |
217 | 0 | CoreGlobInstList.push_back(Inst); |
218 | 0 | } |
219 | 0 | GlobalInstance *getCoreGlobalInstance(uint32_t Index) const noexcept { |
220 | 0 | return CoreGlobInstList[Index]; |
221 | 0 | } |
222 | | |
223 | 0 | void addCoreType(const CoreDefType &Ty) noexcept { |
224 | 0 | CoreTypes.emplace_back(Ty); |
225 | 0 | } |
226 | 0 | const CoreDefType &getCoreType(uint32_t Idx) const noexcept { |
227 | 0 | return CoreTypes[Idx]; |
228 | 0 | } |
229 | | |
230 | 0 | void addType(const AST::FunctionType &Ty) noexcept { |
231 | 0 | FuncType FT{}; |
232 | 0 | typeConvert(FT, Ty); |
233 | 0 | addType(FT); |
234 | 0 | } |
235 | 0 | void addType(const DefType &Ty) noexcept { Types.emplace_back(Ty); } |
236 | 0 | const DefType &getType(uint32_t Idx) const noexcept { return Types[Idx]; } |
237 | | |
238 | | private: |
239 | | void unsafeAddHostFunc( |
240 | | std::string_view Name, |
241 | 0 | std::unique_ptr<Component::FunctionInstance> Inst) noexcept { |
242 | 0 | addFunctionInstance(Inst.get()); |
243 | 0 | ExportFuncMap.insert_or_assign(std::string(Name), FuncInstList.back()); |
244 | 0 | OwnedFuncInstList.push_back(std::move(Inst)); |
245 | 0 | } |
246 | | |
247 | | private: |
248 | | std::string CompName; |
249 | | |
250 | | std::vector<AST::Module> ModList; |
251 | | std::vector<AST::Component::Component> CompList; |
252 | | |
253 | | std::vector<std::unique_ptr<ModuleInstance>> OwnedModInstList; |
254 | | std::vector<const ModuleInstance *> ModInstList; |
255 | | std::vector<std::unique_ptr<ComponentInstance>> OwnedCompInstList; |
256 | | std::vector<const ComponentInstance *> CompInstList; |
257 | | |
258 | | // value |
259 | | std::vector<ValInterface> ValueList; |
260 | | |
261 | | // core function |
262 | | // |
263 | | // The owned core functions are created by lowering process |
264 | | std::vector<std::unique_ptr<FunctionInstance>> OwnedCoreFuncInstList; |
265 | | std::vector<FunctionInstance *> CoreFuncInstList; |
266 | | |
267 | | // component function |
268 | | std::vector<std::unique_ptr<Component::FunctionInstance>> OwnedFuncInstList; |
269 | | std::vector<Component::FunctionInstance *> FuncInstList; |
270 | | std::map<std::string, Component::FunctionInstance *, std::less<>> |
271 | | ExportFuncMap; |
272 | | |
273 | | std::map<std::string, const ModuleInstance *, std::less<>> ExportModuleMap; |
274 | | |
275 | | // core memory, this is prepared for canonical ABI |
276 | | // |
277 | | // when a function is lowering or lifting, it can have options |
278 | | // 1. memory of a module instance |
279 | | // 2. realloc of the same module instance |
280 | | // these data will help a component function encode its high-level data to |
281 | | // core module data, for example, `string` converted to `(i32, i32)`. Which is |
282 | | // a pair of pointer and length, where pointer stored in the memory |
283 | | // instance from option. |
284 | | std::vector<TableInstance *> CoreTabInstList; |
285 | | std::vector<MemoryInstance *> CoreMemInstList; |
286 | | std::vector<GlobalInstance *> CoreGlobInstList; |
287 | | |
288 | | std::vector<AST::Component::CoreDefType> CoreTypes; |
289 | | std::vector<AST::Component::DefType> Types; |
290 | | }; |
291 | | |
292 | | } // namespace Instance |
293 | | } // namespace Runtime |
294 | | } // namespace WasmEdge |