Coverage Report

Created: 2026-07-16 07:09

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
12
  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
12
  std::string_view getName() const noexcept { return Name; }
36
4.15k
  std::string &getName() noexcept { return Name; }
WasmEdge::AST::Component::InstantiateArg<unsigned int>::getName()
Line
Count
Source
36
435
  std::string &getName() noexcept { return Name; }
WasmEdge::AST::Component::InstantiateArg<WasmEdge::AST::Component::SortIndex>::getName()
Line
Count
Source
36
3.71k
  std::string &getName() noexcept { return Name; }
37
0
  const IndexType &getIndex() const noexcept { return Idx; }
Unexecuted instantiation: WasmEdge::AST::Component::InstantiateArg<WasmEdge::AST::Component::SortIndex>::getIndex() const
Unexecuted instantiation: WasmEdge::AST::Component::InstantiateArg<unsigned int>::getIndex() const
38
4.11k
  IndexType &getIndex() noexcept { return Idx; }
WasmEdge::AST::Component::InstantiateArg<unsigned int>::getIndex()
Line
Count
Source
38
397
  IndexType &getIndex() noexcept { return Idx; }
WasmEdge::AST::Component::InstantiateArg<WasmEdge::AST::Component::SortIndex>::getIndex()
Line
Count
Source
38
3.71k
  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
751
  std::string_view getName() const noexcept { return Name; }
54
39.8k
  std::string &getName() noexcept { return Name; }
55
712
  const SortIndex &getSortIdx() const noexcept { return SortIdx; }
56
39.7k
  SortIndex &getSortIdx() noexcept { return SortIdx; }
57
58
private:
59
  std::string Name;
60
  SortIndex SortIdx;
61
};
62
63
// core:instance       ::= ie:<core:instanceexpr>
64
//                       => (instance ie)
65
// core:instanceexpr   ::= 0x00 m:<moduleidx> arg*:vec(<core:instantiatearg>)
66
//                       => (instantiate m arg*)
67
//                       | 0x01 e*:vec(<core:inlineexport>)
68
//                       => e*
69
70
/// AST Component::CoreInstance node.
71
class CoreInstance {
72
public:
73
  using InstantiateArgs = std::vector<InstantiateArg<uint32_t>>;
74
  using InlineExports = std::vector<InlineExport>;
75
76
  void setInstantiateArgs(const uint32_t ModIdx,
77
903
                          InstantiateArgs &&Args) noexcept {
78
903
    Expr.emplace<std::pair<uint32_t, InstantiateArgs>>(ModIdx, std::move(Args));
79
903
  }
80
26
  uint32_t getModuleIndex() const noexcept {
81
26
    return std::get_if<std::pair<uint32_t, InstantiateArgs>>(&Expr)->first;
82
26
  }
83
0
  Span<const InstantiateArg<uint32_t>> getInstantiateArgs() const noexcept {
84
0
    return std::get_if<std::pair<uint32_t, InstantiateArgs>>(&Expr)->second;
85
0
  }
86
87
2.39k
  void setInlineExports(InlineExports &&Exports) noexcept {
88
2.39k
    Expr.emplace<InlineExports>(std::move(Exports));
89
2.39k
  }
90
1.62k
  Span<const InlineExport> getInlineExports() const noexcept {
91
1.62k
    return *std::get_if<InlineExports>(&Expr);
92
1.62k
  }
93
94
1.64k
  bool isInstantiateModule() const noexcept {
95
1.64k
    return std::holds_alternative<std::pair<uint32_t, InstantiateArgs>>(Expr);
96
1.64k
  }
97
98
1.62k
  bool isInlineExport() const noexcept {
99
1.62k
    return std::holds_alternative<InlineExports>(Expr);
100
1.62k
  }
101
102
private:
103
  std::variant<std::pair<uint32_t, InstantiateArgs>, InlineExports> Expr;
104
};
105
106
// instance            ::= ie:<instanceexpr>
107
//                       => (instance ie)
108
// instanceexpr        ::= 0x00 c:<componentidx> arg*:vec(<instantiatearg>)
109
//                       => (instantiate c arg*)
110
//                       | 0x01 e*:vec(<inlineexport>)
111
//                       => e*
112
113
/// AST Component::Instance node.
114
class Instance {
115
public:
116
  using InstantiateArgs = std::vector<InstantiateArg<SortIndex>>;
117
  using InlineExports = std::vector<InlineExport>;
118
119
  void setInstantiateArgs(const uint32_t CompIdx,
120
2.31k
                          InstantiateArgs &&Args) noexcept {
121
2.31k
    Expr.emplace<std::pair<uint32_t, InstantiateArgs>>(CompIdx,
122
2.31k
                                                       std::move(Args));
123
2.31k
  }
124
1.41k
  uint32_t getComponentIndex() const noexcept {
125
1.41k
    return std::get_if<std::pair<uint32_t, InstantiateArgs>>(&Expr)->first;
126
1.41k
  }
127
2.71k
  Span<const InstantiateArg<SortIndex>> getInstantiateArgs() const noexcept {
128
2.71k
    return std::get_if<std::pair<uint32_t, InstantiateArgs>>(&Expr)->second;
129
2.71k
  }
130
131
9.69k
  void setInlineExports(InlineExports &&Exports) noexcept {
132
9.69k
    Expr.emplace<InlineExports>(std::move(Exports));
133
9.69k
  }
134
7.82k
  Span<const InlineExport> getInlineExports() const noexcept {
135
7.82k
    return *std::get_if<InlineExports>(&Expr);
136
7.82k
  }
137
138
9.23k
  bool isInstantiateModule() const noexcept {
139
9.23k
    return std::holds_alternative<std::pair<uint32_t, InstantiateArgs>>(Expr);
140
9.23k
  }
141
142
7.82k
  bool isInlineExport() const noexcept {
143
7.82k
    return std::holds_alternative<InlineExports>(Expr);
144
7.82k
  }
145
146
private:
147
  std::variant<std::pair<uint32_t, InstantiateArgs>, InlineExports> Expr;
148
};
149
150
} // namespace Component
151
} // namespace AST
152
} // namespace WasmEdge