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