Coverage Report

Created: 2026-09-28 06:22

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