Coverage Report

Created: 2026-11-09 06:49

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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
7.33k
  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
7.33k
  std::string_view getName() const noexcept { return Name; }
36
14.4k
  std::string &getName() noexcept { return Name; }
WasmEdge::AST::Component::InstantiateArg<unsigned int>::getName()
Line
Count
Source
36
745
  std::string &getName() noexcept { return Name; }
WasmEdge::AST::Component::InstantiateArg<WasmEdge::AST::Component::SortIndex>::getName()
Line
Count
Source
36
13.7k
  std::string &getName() noexcept { return Name; }
37
22.0k
  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
22.0k
  const IndexType &getIndex() const noexcept { return Idx; }
38
14.4k
  IndexType &getIndex() noexcept { return Idx; }
WasmEdge::AST::Component::InstantiateArg<unsigned int>::getIndex()
Line
Count
Source
38
710
  IndexType &getIndex() noexcept { return Idx; }
WasmEdge::AST::Component::InstantiateArg<WasmEdge::AST::Component::SortIndex>::getIndex()
Line
Count
Source
38
13.7k
  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
5.31k
  std::string_view getName() const noexcept { return Name; }
54
11.0k
  std::string &getName() noexcept { return Name; }
55
6.61k
  const SortIndex &getSortIdx() const noexcept { return SortIdx; }
56
11.0k
  SortIndex &getSortIdx() noexcept { return SortIdx; }
57
2.51k
  std::vector<std::string> &getImplements() noexcept { return Implements; }
58
493
  Span<const std::string> getImplements() const noexcept { return Implements; }
59
2.51k
  std::vector<std::string> &getExternalIds() noexcept { return ExternalIds; }
60
0
  Span<const std::string> getExternalIds() const noexcept {
61
0
    return ExternalIds;
62
0
  }
63
2.51k
  std::vector<std::string> &getVersionSuffixes() noexcept {
64
2.51k
    return VersionSuffixes;
65
2.51k
  }
66
493
  Span<const std::string> getVersionSuffixes() const noexcept {
67
493
    return VersionSuffixes;
68
493
  }
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
148k
                          InstantiateArgs &&Args) noexcept {
93
148k
    Expr.emplace<std::pair<uint32_t, InstantiateArgs>>(ModIdx, std::move(Args));
94
148k
  }
95
459
  uint32_t getModuleIndex() const noexcept {
96
459
    return std::get_if<std::pair<uint32_t, InstantiateArgs>>(&Expr)->first;
97
459
  }
98
425
  Span<const InstantiateArg<uint32_t>> getInstantiateArgs() const noexcept {
99
425
    return std::get_if<std::pair<uint32_t, InstantiateArgs>>(&Expr)->second;
100
425
  }
101
102
124k
  void setInlineExports(InlineExports &&Exports) noexcept {
103
124k
    Expr.emplace<InlineExports>(std::move(Exports));
104
124k
  }
105
117k
  Span<const InlineExport> getInlineExports() const noexcept {
106
117k
    return *std::get_if<InlineExports>(&Expr);
107
117k
  }
108
109
117k
  bool isInstantiateModule() const noexcept {
110
117k
    return std::holds_alternative<std::pair<uint32_t, InstantiateArgs>>(Expr);
111
117k
  }
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
1.02M
                          InstantiateArgs &&Args) noexcept {
136
1.02M
    Expr.emplace<std::pair<uint32_t, InstantiateArgs>>(CompIdx,
137
1.02M
                                                       std::move(Args));
138
1.02M
  }
139
975k
  uint32_t getComponentIndex() const noexcept {
140
975k
    return std::get_if<std::pair<uint32_t, InstantiateArgs>>(&Expr)->first;
141
975k
  }
142
975k
  Span<const InstantiateArg<SortIndex>> getInstantiateArgs() const noexcept {
143
975k
    return std::get_if<std::pair<uint32_t, InstantiateArgs>>(&Expr)->second;
144
975k
  }
145
146
1.19M
  void setInlineExports(InlineExports &&Exports) noexcept {
147
1.19M
    Expr.emplace<InlineExports>(std::move(Exports));
148
1.19M
  }
149
1.10M
  Span<const InlineExport> getInlineExports() const noexcept {
150
1.10M
    return *std::get_if<InlineExports>(&Expr);
151
1.10M
  }
152
153
2.07M
  bool isInstantiateModule() const noexcept {
154
2.07M
    return std::holds_alternative<std::pair<uint32_t, InstantiateArgs>>(Expr);
155
2.07M
  }
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