Coverage Report

Created: 2025-08-25 06:58

/src/WasmEdge/lib/loader/ast/component/component_instance.cpp
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
#include "loader/loader.h"
5
6
namespace WasmEdge {
7
namespace Loader {
8
9
0
Expect<void> Loader::loadCoreInstance(AST::Component::CoreInstance &Instance) {
10
0
  auto ReportError = [this](auto E) {
11
0
    return logLoadError(E, FMgr.getLastOffset(),
12
0
                        ASTNodeAttr::Comp_CoreInstance);
13
0
  };
14
  // core:instance       ::= ie:<core:instanceexpr>
15
  //                       => (instance ie)
16
  // core:instanceexpr   ::= 0x00 m:<moduleidx> arg*:vec(<core:instantiatearg>)
17
  //                       => (instantiate m arg*)
18
  //                       | 0x01 e*:vec(<core:inlineexport>)
19
  //                       => e*
20
21
0
  auto LoadInstArg =
22
0
      [this](AST::Component::InstantiateArg<uint32_t> &Arg) -> Expect<void> {
23
0
    auto ReportArgError = [this](auto E) {
24
0
      return logLoadError(E, FMgr.getLastOffset(),
25
0
                          ASTNodeAttr::Comp_CoreInstanceArg);
26
0
    };
Unexecuted instantiation: component_instance.cpp:auto WasmEdge::Loader::Loader::loadCoreInstance(WasmEdge::AST::Component::CoreInstance&)::$_0::operator()(WasmEdge::AST::Component::InstantiateArg<unsigned int>&) const::{lambda(auto:1)#1}::operator()<WasmEdge::ErrCode>(WasmEdge::ErrCode) const
Unexecuted instantiation: component_instance.cpp:auto WasmEdge::Loader::Loader::loadCoreInstance(WasmEdge::AST::Component::CoreInstance&)::$_0::operator()(WasmEdge::AST::Component::InstantiateArg<unsigned int>&) const::{lambda(auto:1)#1}::operator()<WasmEdge::ErrCode::Value>(WasmEdge::ErrCode::Value) const
27
    // core:instantiatearg ::= n:<core:name> 0x12 i:<instanceidx>
28
    //                       => (with n (instance i))
29
0
    EXPECTED_TRY(Arg.getName(), FMgr.readName().map_error(ReportArgError));
30
0
    EXPECTED_TRY(uint8_t B, FMgr.readByte().map_error(ReportArgError));
31
0
    if (B != 0x12U) {
32
0
      return ReportArgError(ErrCode::Value::MalformedCoreInstance);
33
0
    }
34
0
    EXPECTED_TRY(Arg.getIndex(), FMgr.readU32().map_error(ReportArgError));
35
0
    return {};
36
0
  };
37
38
0
  auto LoadInlineExp =
39
0
      [this](AST::Component::InlineExport &Exp) -> Expect<void> {
40
    // core:inlineexport ::= n:<core:name> si:<core:sortidx> => (export n si)
41
0
    EXPECTED_TRY(Exp.getName(), FMgr.readName().map_error([this](auto E) {
42
0
      return logLoadError(E, FMgr.getLastOffset(),
43
0
                          ASTNodeAttr::Comp_CoreInlineExport);
44
0
    }));
45
0
    return loadSortIndex(Exp.getSortIdx(), true).map_error([](auto E) {
46
0
      spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Comp_CoreInlineExport));
47
0
      return E;
48
0
    });
49
0
  };
50
51
0
  EXPECTED_TRY(uint8_t Flag, FMgr.readByte().map_error(ReportError));
52
0
  switch (Flag) {
53
0
  case 0x00: {
54
0
    EXPECTED_TRY(uint32_t Idx, FMgr.readU32().map_error(ReportError));
55
0
    std::vector<AST::Component::InstantiateArg<uint32_t>> Args;
56
0
    EXPECTED_TRY(loadVec<AST::Component::CoreInstance>(Args, LoadInstArg));
57
0
    Instance.setInstantiateArgs(Idx, std::move(Args));
58
0
    return {};
59
0
  }
60
0
  case 0x01: {
61
0
    std::vector<AST::Component::InlineExport> Exports;
62
0
    EXPECTED_TRY(loadVec<AST::Component::CoreInstance>(Exports, LoadInlineExp));
63
0
    Instance.setInlineExports(std::move(Exports));
64
0
    return {};
65
0
  }
66
0
  default:
67
0
    return logLoadError(ErrCode::Value::MalformedCoreInstance,
68
0
                        FMgr.getLastOffset(), ASTNodeAttr::Comp_CoreInstance);
69
0
  }
70
0
}
71
72
0
Expect<void> Loader::loadInstance(AST::Component::Instance &Instance) {
73
0
  auto ReportError = [this](auto E) {
74
0
    return logLoadError(E, FMgr.getLastOffset(), ASTNodeAttr::Comp_Instance);
75
0
  };
76
  // instance     ::= ie:<instanceexpr>
77
  //                => (instance ie)
78
  // instanceexpr ::= 0x00 c:<componentidx> arg*:vec(<instantiatearg>)
79
  //                => (instantiate c arg*)
80
  //                | 0x01 e*:vec(<inlineexport>)
81
  //                => e*
82
83
0
  auto LoadInstArg =
84
0
      [this](AST::Component::InstantiateArg<AST::Component::SortIndex> &Arg)
85
0
      -> Expect<void> {
86
    // instantiatearg ::= n:<string> si:<sortidx> => (with n si)
87
0
    EXPECTED_TRY(Arg.getName(), FMgr.readName().map_error([this](auto E) {
88
0
      return logLoadError(E, FMgr.getLastOffset(),
89
0
                          ASTNodeAttr::Comp_InstanceArg);
90
0
    }));
91
0
    return loadSortIndex(Arg.getIndex()).map_error([](auto E) {
92
0
      spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Comp_InstanceArg));
93
0
      return E;
94
0
    });
95
0
  };
96
97
0
  auto LoadInlineExp =
98
0
      [this](AST::Component::InlineExport &Exp) -> Expect<void> {
99
    // inlineexport ::= n:<exportname> si:<sortidx> => (export n si)
100
0
    EXPECTED_TRY(Exp.getName(), FMgr.readName().map_error([this](auto E) {
101
0
      return logLoadError(E, FMgr.getLastOffset(),
102
0
                          ASTNodeAttr::Comp_InlineExport);
103
0
    }));
104
0
    return loadSortIndex(Exp.getSortIdx()).map_error([](auto E) {
105
0
      spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Comp_InlineExport));
106
0
      return E;
107
0
    });
108
0
  };
109
110
0
  EXPECTED_TRY(uint8_t Flag, FMgr.readByte().map_error(ReportError));
111
0
  switch (Flag) {
112
0
  case 0x00: {
113
0
    EXPECTED_TRY(uint32_t Idx, FMgr.readU32().map_error(ReportError));
114
0
    std::vector<AST::Component::InstantiateArg<AST::Component::SortIndex>> Args;
115
0
    EXPECTED_TRY(loadVec<AST::Component::Instance>(Args, LoadInstArg));
116
0
    Instance.setInstantiateArgs(Idx, std::move(Args));
117
0
    return {};
118
0
  }
119
0
  case 0x01: {
120
0
    std::vector<AST::Component::InlineExport> Exports;
121
0
    EXPECTED_TRY(loadVec<AST::Component::Instance>(Exports, LoadInlineExp));
122
0
    Instance.setInlineExports(std::move(Exports));
123
0
    return {};
124
0
  }
125
0
  default:
126
0
    return logLoadError(ErrCode::Value::MalformedInstance, FMgr.getLastOffset(),
127
0
                        ASTNodeAttr::Comp_Instance);
128
0
  }
129
0
}
130
131
} // namespace Loader
132
} // namespace WasmEdge