/src/WasmEdge/include/ast/component/instance.h
Line | Count | Source |
1 | | // SPDX-License-Identifier: Apache-2.0 |
2 | | // SPDX-FileCopyrightText: Copyright The WasmEdge Authors |
3 | | |
4 | | //===-- wasmedge/ast/component/instance.h - Instance class definitions ----===// |
5 | | // |
6 | | // Part of the WasmEdge Project. |
7 | | // |
8 | | //===----------------------------------------------------------------------===// |
9 | | /// |
10 | | /// \file |
11 | | /// This file contains the declaration of the Instance node related classes. |
12 | | /// |
13 | | //===----------------------------------------------------------------------===// |
14 | | #pragma once |
15 | | |
16 | | #include "ast/component/sort.h" |
17 | | #include "common/span.h" |
18 | | |
19 | | #include <string> |
20 | | #include <variant> |
21 | | #include <vector> |
22 | | |
23 | | namespace WasmEdge { |
24 | | namespace AST { |
25 | | namespace Component { |
26 | | |
27 | | // core:instantiatearg ::= n:<core:name> 0x12 i:<instanceidx> |
28 | | // => (with n (instance i)) |
29 | | // instantiatearg ::= n:<name> si:<sortidx> |
30 | | // => (with n si) |
31 | | |
32 | | /// AST Component::InstantiateArg class template. |
33 | | template <typename IndexType> class InstantiateArg { |
34 | | public: |
35 | 1.88k | std::string_view getName() const noexcept { return Name; }Unexecuted instantiation: WasmEdge::AST::Component::InstantiateArg<unsigned int>::getName() const WasmEdge::AST::Component::InstantiateArg<WasmEdge::AST::Component::SortIndex>::getName() const Line | Count | Source | 35 | 1.88k | std::string_view getName() const noexcept { return Name; } |
|
36 | 3.96k | std::string &getName() noexcept { return Name; }WasmEdge::AST::Component::InstantiateArg<unsigned int>::getName() Line | Count | Source | 36 | 483 | std::string &getName() noexcept { return Name; } |
WasmEdge::AST::Component::InstantiateArg<WasmEdge::AST::Component::SortIndex>::getName() Line | Count | Source | 36 | 3.48k | std::string &getName() noexcept { return Name; } |
|
37 | 5.67k | const IndexType &getIndex() const noexcept { return Idx; }Unexecuted instantiation: WasmEdge::AST::Component::InstantiateArg<unsigned int>::getIndex() const WasmEdge::AST::Component::InstantiateArg<WasmEdge::AST::Component::SortIndex>::getIndex() const Line | Count | Source | 37 | 5.67k | const IndexType &getIndex() const noexcept { return Idx; } |
|
38 | 3.92k | IndexType &getIndex() noexcept { return Idx; }WasmEdge::AST::Component::InstantiateArg<unsigned int>::getIndex() Line | Count | Source | 38 | 446 | IndexType &getIndex() noexcept { return Idx; } |
WasmEdge::AST::Component::InstantiateArg<WasmEdge::AST::Component::SortIndex>::getIndex() Line | Count | Source | 38 | 3.48k | IndexType &getIndex() noexcept { return Idx; } |
|
39 | | |
40 | | private: |
41 | | std::string Name; |
42 | | IndexType Idx; |
43 | | }; |
44 | | |
45 | | // core:inlineexport ::= n:<core:name> si:<core:sortidx> |
46 | | // => (export n si) |
47 | | // inlineexport ::= n:<exportname> si:<sortidx> |
48 | | // => (export n si) |
49 | | |
50 | | /// AST Component::InlineExport class. |
51 | | class InlineExport { |
52 | | public: |
53 | 4.82k | std::string_view getName() const noexcept { return Name; } |
54 | 8.88k | std::string &getName() noexcept { return Name; } |
55 | 5.59k | const SortIndex &getSortIdx() const noexcept { return SortIdx; } |
56 | 8.83k | SortIndex &getSortIdx() noexcept { return SortIdx; } |
57 | 2.19k | std::vector<std::string> &getImplements() noexcept { return Implements; } |
58 | 256 | Span<const std::string> getImplements() const noexcept { return Implements; } |
59 | 2.19k | std::vector<std::string> &getExternalIds() noexcept { return ExternalIds; } |
60 | 256 | Span<const std::string> getExternalIds() const noexcept { |
61 | 256 | return ExternalIds; |
62 | 256 | } |
63 | 2.19k | std::vector<std::string> &getVersionSuffixes() noexcept { |
64 | 2.19k | return VersionSuffixes; |
65 | 2.19k | } |
66 | 256 | Span<const std::string> getVersionSuffixes() const noexcept { |
67 | 256 | return VersionSuffixes; |
68 | 256 | } |
69 | | |
70 | | private: |
71 | | std::string Name; |
72 | | SortIndex SortIdx; |
73 | | std::vector<std::string> Implements; |
74 | | std::vector<std::string> ExternalIds; |
75 | | std::vector<std::string> VersionSuffixes; |
76 | | }; |
77 | | |
78 | | // core:instance ::= ie:<core:instanceexpr> |
79 | | // => (instance ie) |
80 | | // core:instanceexpr ::= 0x00 m:<moduleidx> arg*:vec(<core:instantiatearg>) |
81 | | // => (instantiate m arg*) |
82 | | // | 0x01 e*:vec(<core:inlineexport>) |
83 | | // => e* |
84 | | |
85 | | /// AST Component::CoreInstance node. |
86 | | class CoreInstance { |
87 | | public: |
88 | | using InstantiateArgs = std::vector<InstantiateArg<uint32_t>>; |
89 | | using InlineExports = std::vector<InlineExport>; |
90 | | |
91 | | void setInstantiateArgs(const uint32_t ModIdx, |
92 | 395k | InstantiateArgs &&Args) noexcept { |
93 | 395k | Expr.emplace<std::pair<uint32_t, InstantiateArgs>>(ModIdx, std::move(Args)); |
94 | 395k | } |
95 | 514 | uint32_t getModuleIndex() const noexcept { |
96 | 514 | return std::get_if<std::pair<uint32_t, InstantiateArgs>>(&Expr)->first; |
97 | 514 | } |
98 | 476 | Span<const InstantiateArg<uint32_t>> getInstantiateArgs() const noexcept { |
99 | 476 | return std::get_if<std::pair<uint32_t, InstantiateArgs>>(&Expr)->second; |
100 | 476 | } |
101 | | |
102 | 89.0k | void setInlineExports(InlineExports &&Exports) noexcept { |
103 | 89.0k | Expr.emplace<InlineExports>(std::move(Exports)); |
104 | 89.0k | } |
105 | 83.8k | Span<const InlineExport> getInlineExports() const noexcept { |
106 | 83.8k | return *std::get_if<InlineExports>(&Expr); |
107 | 83.8k | } |
108 | | |
109 | 84.3k | bool isInstantiateModule() const noexcept { |
110 | 84.3k | return std::holds_alternative<std::pair<uint32_t, InstantiateArgs>>(Expr); |
111 | 84.3k | } |
112 | | |
113 | 0 | bool isInlineExport() const noexcept { |
114 | 0 | return std::holds_alternative<InlineExports>(Expr); |
115 | 0 | } |
116 | | |
117 | | private: |
118 | | std::variant<std::pair<uint32_t, InstantiateArgs>, InlineExports> Expr; |
119 | | }; |
120 | | |
121 | | // instance ::= ie:<instanceexpr> |
122 | | // => (instance ie) |
123 | | // instanceexpr ::= 0x00 c:<componentidx> arg*:vec(<instantiatearg>) |
124 | | // => (instantiate c arg*) |
125 | | // | 0x01 e*:vec(<inlineexport>) |
126 | | // => e* |
127 | | |
128 | | /// AST Component::Instance node. |
129 | | class Instance { |
130 | | public: |
131 | | using InstantiateArgs = std::vector<InstantiateArg<SortIndex>>; |
132 | | using InlineExports = std::vector<InlineExport>; |
133 | | |
134 | | void setInstantiateArgs(const uint32_t CompIdx, |
135 | 300k | InstantiateArgs &&Args) noexcept { |
136 | 300k | Expr.emplace<std::pair<uint32_t, InstantiateArgs>>(CompIdx, |
137 | 300k | std::move(Args)); |
138 | 300k | } |
139 | 283k | uint32_t getComponentIndex() const noexcept { |
140 | 283k | return std::get_if<std::pair<uint32_t, InstantiateArgs>>(&Expr)->first; |
141 | 283k | } |
142 | 283k | Span<const InstantiateArg<SortIndex>> getInstantiateArgs() const noexcept { |
143 | 283k | return std::get_if<std::pair<uint32_t, InstantiateArgs>>(&Expr)->second; |
144 | 283k | } |
145 | | |
146 | 1.15M | void setInlineExports(InlineExports &&Exports) noexcept { |
147 | 1.15M | Expr.emplace<InlineExports>(std::move(Exports)); |
148 | 1.15M | } |
149 | 1.07M | Span<const InlineExport> getInlineExports() const noexcept { |
150 | 1.07M | return *std::get_if<InlineExports>(&Expr); |
151 | 1.07M | } |
152 | | |
153 | 1.36M | bool isInstantiateModule() const noexcept { |
154 | 1.36M | return std::holds_alternative<std::pair<uint32_t, InstantiateArgs>>(Expr); |
155 | 1.36M | } |
156 | | |
157 | 0 | bool isInlineExport() const noexcept { |
158 | 0 | return std::holds_alternative<InlineExports>(Expr); |
159 | 0 | } |
160 | | |
161 | | private: |
162 | | std::variant<std::pair<uint32_t, InstantiateArgs>, InlineExports> Expr; |
163 | | }; |
164 | | |
165 | | } // namespace Component |
166 | | } // namespace AST |
167 | | } // namespace WasmEdge |