Coverage Report

Created: 2026-08-08 06:32

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/WasmEdge/lib/loader/ast/description.cpp
Line
Count
Source
1
// SPDX-License-Identifier: Apache-2.0
2
// SPDX-FileCopyrightText: Copyright The WasmEdge Authors
3
4
#include "loader/loader.h"
5
6
namespace WasmEdge {
7
namespace Loader {
8
9
// Load binary of Import description. See "include/loader/loader.h".
10
36.5k
Expect<void> Loader::loadDesc(AST::ImportDesc &ImpDesc) {
11
36.5k
  auto ReportError = [this](auto E) {
12
191
    return logLoadError(E, FMgr.getLastOffset(), ASTNodeAttr::Desc_Import);
13
191
  };
14
15
  // Read the module name.
16
36.5k
  EXPECTED_TRY(FMgr.readName().map_error(ReportError).map([&](std::string S) {
17
36.4k
    ImpDesc.setModuleName(S);
18
36.4k
  }));
19
20
  // Read the external name.
21
36.4k
  EXPECTED_TRY(FMgr.readName().map_error(ReportError).map([&](std::string S) {
22
36.3k
    ImpDesc.setExternalName(S);
23
36.3k
  }));
24
25
  // Read the external type.
26
36.3k
  EXPECTED_TRY(FMgr.readByte().map_error(ReportError).map([&](uint8_t B) {
27
36.3k
    ImpDesc.setExternalType(static_cast<ExternalType>(B));
28
36.3k
  }));
29
30
  // Create the content node according to the external type.
31
36.3k
  switch (ImpDesc.getExternalType()) {
32
22.0k
  case ExternalType::Function: {
33
    // Read the function type index.
34
22.0k
    EXPECTED_TRY(FMgr.readU32().map_error(ReportError).map([&](uint32_t Idx) {
35
22.0k
      ImpDesc.setExternalFuncTypeIdx(Idx);
36
22.0k
    }));
37
22.0k
    return {};
38
22.0k
  }
39
862
  case ExternalType::Table: {
40
    // Read the table type node.
41
862
    return loadType(ImpDesc.getExternalTableType());
42
22.0k
  }
43
1.84k
  case ExternalType::Memory: {
44
    // Read the memory type node.
45
1.84k
    return loadType(ImpDesc.getExternalMemoryType());
46
22.0k
  }
47
10.9k
  case ExternalType::Global: {
48
    // Read the global type node.
49
10.9k
    EXPECTED_TRY(loadType(ImpDesc.getExternalGlobalType()));
50
    // Mutable global imports are for the ImportExportMutGlobals proposal.
51
10.9k
    if (ImpDesc.getExternalGlobalType().getValMut() == ValMut::Var &&
52
848
        unlikely(!Conf.hasProposal(Proposal::ImportExportMutGlobals))) {
53
0
      return logNeedProposal(ErrCode::Value::InvalidMut,
54
0
                             Proposal::ImportExportMutGlobals,
55
0
                             FMgr.getLastOffset(), ASTNodeAttr::Desc_Import);
56
0
    }
57
10.9k
    return {};
58
10.9k
  }
59
609
  case ExternalType::Tag: {
60
609
    if (!Conf.hasProposal(Proposal::ExceptionHandling)) {
61
0
      return logNeedProposal(ErrCode::Value::MalformedImportKind,
62
0
                             Proposal::ExceptionHandling, FMgr.getLastOffset(),
63
0
                             ASTNodeAttr::Module);
64
0
    }
65
    // Read the Tag type node.
66
609
    return loadType(ImpDesc.getExternalTagType());
67
609
  }
68
19
  default:
69
19
    return logLoadError(ErrCode::Value::MalformedImportKind,
70
19
                        FMgr.getLastOffset(), ASTNodeAttr::Desc_Import);
71
36.3k
  }
72
36.3k
}
73
74
// Load binary of Export description. See "include/loader/loader.h".
75
19.4k
Expect<void> Loader::loadDesc(AST::ExportDesc &ExpDesc) {
76
19.4k
  auto ReportError = [this](auto E) {
77
155
    return logLoadError(E, FMgr.getLastOffset(), ASTNodeAttr::Desc_Export);
78
155
  };
79
80
  // Read external name to export.
81
19.4k
  EXPECTED_TRY(FMgr.readName().map_error(ReportError).map([&](std::string S) {
82
19.3k
    ExpDesc.setExternalName(S);
83
19.3k
  }));
84
85
  // Read external type.
86
19.3k
  EXPECTED_TRY(FMgr.readByte().map_error(ReportError).map([&](uint8_t B) {
87
19.3k
    ExpDesc.setExternalType(static_cast<ExternalType>(B));
88
19.3k
  }));
89
19.3k
  switch (ExpDesc.getExternalType()) {
90
15.2k
  case ExternalType::Function:
91
16.7k
  case ExternalType::Table:
92
17.7k
  case ExternalType::Memory:
93
18.5k
  case ExternalType::Global:
94
18.5k
    break;
95
760
  case ExternalType::Tag:
96
760
    if (!Conf.hasProposal(Proposal::ExceptionHandling)) {
97
0
      return logNeedProposal(ErrCode::Value::MalformedImportKind,
98
0
                             Proposal::ExceptionHandling, FMgr.getLastOffset(),
99
0
                             ASTNodeAttr::Module);
100
0
    }
101
760
    break;
102
760
  default:
103
23
    return logLoadError(ErrCode::Value::MalformedExportKind,
104
23
                        FMgr.getLastOffset(), ASTNodeAttr::Desc_Export);
105
19.3k
  }
106
107
  // Read external index to export.
108
19.3k
  EXPECTED_TRY(FMgr.readU32().map_error(ReportError).map([&](uint32_t Idx) {
109
19.2k
    ExpDesc.setExternalIndex(Idx);
110
19.2k
  }));
111
19.2k
  return {};
112
19.3k
}
113
114
} // namespace Loader
115
} // namespace WasmEdge