Coverage Report

Created: 2026-08-08 06:32

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
5.12k
  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
5.12k
  std::string_view getName() const noexcept { return Name; }
36
7.73k
  std::string &getName() noexcept { return Name; }
WasmEdge::AST::Component::InstantiateArg<unsigned int>::getName()
Line
Count
Source
36
381
  std::string &getName() noexcept { return Name; }
WasmEdge::AST::Component::InstantiateArg<WasmEdge::AST::Component::SortIndex>::getName()
Line
Count
Source
36
7.35k
  std::string &getName() noexcept { return Name; }
37
32
  const IndexType &getIndex() const noexcept { return Idx; }
WasmEdge::AST::Component::InstantiateArg<WasmEdge::AST::Component::SortIndex>::getIndex() const
Line
Count
Source
37
32
  const IndexType &getIndex() const noexcept { return Idx; }
Unexecuted instantiation: WasmEdge::AST::Component::InstantiateArg<unsigned int>::getIndex() const
38
7.70k
  IndexType &getIndex() noexcept { return Idx; }
WasmEdge::AST::Component::InstantiateArg<unsigned int>::getIndex()
Line
Count
Source
38
344
  IndexType &getIndex() noexcept { return Idx; }
WasmEdge::AST::Component::InstantiateArg<WasmEdge::AST::Component::SortIndex>::getIndex()
Line
Count
Source
38
7.35k
  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
12.6k
  std::string_view getName() const noexcept { return Name; }
54
10.9k
  std::string &getName() noexcept { return Name; }
55
12.2k
  const SortIndex &getSortIdx() const noexcept { return SortIdx; }
56
10.9k
  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
58.7k
                          InstantiateArgs &&Args) noexcept {
78
58.7k
    Expr.emplace<std::pair<uint32_t, InstantiateArgs>>(ModIdx, std::move(Args));
79
58.7k
  }
80
242
  uint32_t getModuleIndex() const noexcept {
81
242
    return std::get_if<std::pair<uint32_t, InstantiateArgs>>(&Expr)->first;
82
242
  }
83
398
  Span<const InstantiateArg<uint32_t>> getInstantiateArgs() const noexcept {
84
398
    return std::get_if<std::pair<uint32_t, InstantiateArgs>>(&Expr)->second;
85
398
  }
86
87
31.8k
  void setInlineExports(InlineExports &&Exports) noexcept {
88
31.8k
    Expr.emplace<InlineExports>(std::move(Exports));
89
31.8k
  }
90
30.8k
  Span<const InlineExport> getInlineExports() const noexcept {
91
30.8k
    return *std::get_if<InlineExports>(&Expr);
92
30.8k
  }
93
94
31.0k
  bool isInstantiateModule() const noexcept {
95
31.0k
    return std::holds_alternative<std::pair<uint32_t, InstantiateArgs>>(Expr);
96
31.0k
  }
97
98
30.8k
  bool isInlineExport() const noexcept {
99
30.8k
    return std::holds_alternative<InlineExports>(Expr);
100
30.8k
  }
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
7.75k
                          InstantiateArgs &&Args) noexcept {
121
7.75k
    Expr.emplace<std::pair<uint32_t, InstantiateArgs>>(CompIdx,
122
7.75k
                                                       std::move(Args));
123
7.75k
  }
124
5.85k
  uint32_t getComponentIndex() const noexcept {
125
5.85k
    return std::get_if<std::pair<uint32_t, InstantiateArgs>>(&Expr)->first;
126
5.85k
  }
127
11.4k
  Span<const InstantiateArg<SortIndex>> getInstantiateArgs() const noexcept {
128
11.4k
    return std::get_if<std::pair<uint32_t, InstantiateArgs>>(&Expr)->second;
129
11.4k
  }
130
131
95.1k
  void setInlineExports(InlineExports &&Exports) noexcept {
132
95.1k
    Expr.emplace<InlineExports>(std::move(Exports));
133
95.1k
  }
134
93.6k
  Span<const InlineExport> getInlineExports() const noexcept {
135
93.6k
    return *std::get_if<InlineExports>(&Expr);
136
93.6k
  }
137
138
99.5k
  bool isInstantiateModule() const noexcept {
139
99.5k
    return std::holds_alternative<std::pair<uint32_t, InstantiateArgs>>(Expr);
140
99.5k
  }
141
142
93.6k
  bool isInlineExport() const noexcept {
143
93.6k
    return std::holds_alternative<InlineExports>(Expr);
144
93.6k
  }
145
146
private:
147
  std::variant<std::pair<uint32_t, InstantiateArgs>, InlineExports> Expr;
148
};
149
150
} // namespace Component
151
} // namespace AST
152
} // namespace WasmEdge