Coverage Report

Created: 2026-07-16 07:09

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
19.1k
Expect<void> Loader::loadDesc(AST::ImportDesc &ImpDesc) {
11
19.1k
  auto ReportError = [this](auto E) {
12
183
    return logLoadError(E, FMgr.getLastOffset(), ASTNodeAttr::Desc_Import);
13
183
  };
14
15
  // Read the module name.
16
19.1k
  EXPECTED_TRY(FMgr.readName().map_error(ReportError).map([&](std::string S) {
17
19.0k
    ImpDesc.setModuleName(S);
18
19.0k
  }));
19
20
  // Read the external name.
21
19.0k
  EXPECTED_TRY(FMgr.readName().map_error(ReportError).map([&](std::string S) {
22
19.0k
    ImpDesc.setExternalName(S);
23
19.0k
  }));
24
25
  // Read the external type.
26
19.0k
  EXPECTED_TRY(FMgr.readByte().map_error(ReportError).map([&](uint8_t B) {
27
19.0k
    ImpDesc.setExternalType(static_cast<ExternalType>(B));
28
19.0k
  }));
29
30
  // Create the content node according to the external type.
31
19.0k
  switch (ImpDesc.getExternalType()) {
32
6.11k
  case ExternalType::Function: {
33
    // Read the function type index.
34
6.11k
    EXPECTED_TRY(FMgr.readU32().map_error(ReportError).map([&](uint32_t Idx) {
35
6.10k
      ImpDesc.setExternalFuncTypeIdx(Idx);
36
6.10k
    }));
37
6.10k
    return {};
38
6.11k
  }
39
1.14k
  case ExternalType::Table: {
40
    // Read the table type node.
41
1.14k
    return loadType(ImpDesc.getExternalTableType());
42
6.11k
  }
43
473
  case ExternalType::Memory: {
44
    // Read the memory type node.
45
473
    return loadType(ImpDesc.getExternalMemoryType());
46
6.11k
  }
47
11.0k
  case ExternalType::Global: {
48
    // Read the global type node.
49
11.0k
    EXPECTED_TRY(loadType(ImpDesc.getExternalGlobalType()));
50
    // Mutable global imports are for the ImportExportMutGlobals proposal.
51
11.0k
    if (ImpDesc.getExternalGlobalType().getValMut() == ValMut::Var &&
52
865
        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
11.0k
    return {};
58
11.0k
  }
59
180
  case ExternalType::Tag: {
60
180
    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
180
    return loadType(ImpDesc.getExternalTagType());
67
180
  }
68
24
  default:
69
24
    return logLoadError(ErrCode::Value::MalformedImportKind,
70
24
                        FMgr.getLastOffset(), ASTNodeAttr::Desc_Import);
71
19.0k
  }
72
19.0k
}
73
74
// Load binary of Export description. See "include/loader/loader.h".
75
24.2k
Expect<void> Loader::loadDesc(AST::ExportDesc &ExpDesc) {
76
24.2k
  auto ReportError = [this](auto E) {
77
139
    return logLoadError(E, FMgr.getLastOffset(), ASTNodeAttr::Desc_Export);
78
139
  };
79
80
  // Read external name to export.
81
24.2k
  EXPECTED_TRY(FMgr.readName().map_error(ReportError).map([&](std::string S) {
82
24.1k
    ExpDesc.setExternalName(S);
83
24.1k
  }));
84
85
  // Read external type.
86
24.1k
  EXPECTED_TRY(FMgr.readByte().map_error(ReportError).map([&](uint8_t B) {
87
24.1k
    ExpDesc.setExternalType(static_cast<ExternalType>(B));
88
24.1k
  }));
89
24.1k
  switch (ExpDesc.getExternalType()) {
90
19.6k
  case ExternalType::Function:
91
21.8k
  case ExternalType::Table:
92
22.7k
  case ExternalType::Memory:
93
23.3k
  case ExternalType::Global:
94
23.3k
    break;
95
726
  case ExternalType::Tag:
96
726
    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
726
    break;
102
726
  default:
103
18
    return logLoadError(ErrCode::Value::MalformedExportKind,
104
18
                        FMgr.getLastOffset(), ASTNodeAttr::Desc_Export);
105
24.1k
  }
106
107
  // Read external index to export.
108
24.1k
  EXPECTED_TRY(FMgr.readU32().map_error(ReportError).map([&](uint32_t Idx) {
109
24.0k
    ExpDesc.setExternalIndex(Idx);
110
24.0k
  }));
111
24.0k
  return {};
112
24.1k
}
113
114
} // namespace Loader
115
} // namespace WasmEdge