Coverage Report

Created: 2025-08-25 06:58

/src/WasmEdge/include/ast/component/instance.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/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
0
  std::string_view getName() const noexcept { return Name; }
Unexecuted instantiation: WasmEdge::AST::Component::InstantiateArg<unsigned int>::getName() const
Unexecuted instantiation: WasmEdge::AST::Component::InstantiateArg<WasmEdge::AST::Component::SortIndex>::getName() const
36
0
  std::string &getName() noexcept { return Name; }
Unexecuted instantiation: WasmEdge::AST::Component::InstantiateArg<unsigned int>::getName()
Unexecuted instantiation: WasmEdge::AST::Component::InstantiateArg<WasmEdge::AST::Component::SortIndex>::getName()
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
0
  IndexType &getIndex() noexcept { return Idx; }
Unexecuted instantiation: WasmEdge::AST::Component::InstantiateArg<unsigned int>::getIndex()
Unexecuted instantiation: WasmEdge::AST::Component::InstantiateArg<WasmEdge::AST::Component::SortIndex>::getIndex()
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
0
  std::string_view getName() const noexcept { return Name; }
54
0
  std::string &getName() noexcept { return Name; }
55
0
  const SortIndex &getSortIdx() const noexcept { return SortIdx; }
56
0
  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
0
                          InstantiateArgs &&Args) noexcept {
78
0
    Expr.emplace<std::pair<uint32_t, InstantiateArgs>>(ModIdx, std::move(Args));
79
0
  }
80
0
  uint32_t getModuleIndex() const noexcept {
81
0
    return std::get_if<std::pair<uint32_t, InstantiateArgs>>(&Expr)->first;
82
0
  }
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
0
  void setInlineExports(InlineExports &&Exports) noexcept {
88
0
    Expr.emplace<InlineExports>(std::move(Exports));
89
0
  }
90
0
  Span<const InlineExport> getInlineExports() const noexcept {
91
0
    return *std::get_if<InlineExports>(&Expr);
92
0
  }
93
94
0
  bool isInstantiateModule() const noexcept {
95
0
    return std::holds_alternative<std::pair<uint32_t, InstantiateArgs>>(Expr);
96
0
  }
97
98
0
  bool isInlineExport() const noexcept {
99
0
    return std::holds_alternative<InlineExports>(Expr);
100
0
  }
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
0
                          InstantiateArgs &&Args) noexcept {
121
0
    Expr.emplace<std::pair<uint32_t, InstantiateArgs>>(CompIdx,
122
0
                                                       std::move(Args));
123
0
  }
124
0
  uint32_t getComponentIndex() const noexcept {
125
0
    return std::get_if<std::pair<uint32_t, InstantiateArgs>>(&Expr)->first;
126
0
  }
127
0
  Span<const InstantiateArg<SortIndex>> getInstantiateArgs() const noexcept {
128
0
    return std::get_if<std::pair<uint32_t, InstantiateArgs>>(&Expr)->second;
129
0
  }
130
131
0
  void setInlineExports(InlineExports &&Exports) noexcept {
132
0
    Expr.emplace<InlineExports>(std::move(Exports));
133
0
  }
134
0
  Span<const InlineExport> getInlineExports() const noexcept {
135
0
    return *std::get_if<InlineExports>(&Expr);
136
0
  }
137
138
0
  bool isInstantiateModule() const noexcept {
139
0
    return std::holds_alternative<std::pair<uint32_t, InstantiateArgs>>(Expr);
140
0
  }
141
142
0
  bool isInlineExport() const noexcept {
143
0
    return std::holds_alternative<InlineExports>(Expr);
144
0
  }
145
146
private:
147
  std::variant<std::pair<uint32_t, InstantiateArgs>, InlineExports> Expr;
148
};
149
150
} // namespace Component
151
} // namespace AST
152
} // namespace WasmEdge