Coverage Report

Created: 2026-11-12 06:35

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
50.5k
Expect<void> Loader::loadDesc(AST::ImportDesc &ImpDesc) {
11
50.5k
  auto ReportError = [this](auto E) {
12
187
    return logLoadError(E, FMgr.getLastOffset(), ASTNodeAttr::Desc_Import);
13
187
  };
14
15
  // Read the module name.
16
50.5k
  EXPECTED_TRY(FMgr.readName().map_error(ReportError).map([&](std::string S) {
17
50.4k
    ImpDesc.setModuleName(S);
18
50.4k
  }));
19
20
  // Read the external name.
21
50.4k
  EXPECTED_TRY(FMgr.readName().map_error(ReportError).map([&](std::string S) {
22
50.4k
    ImpDesc.setExternalName(S);
23
50.4k
  }));
24
25
  // Read the external type.
26
50.4k
  EXPECTED_TRY(FMgr.readByte().map_error(ReportError).map([&](uint8_t B) {
27
50.3k
    ImpDesc.setExternalType(static_cast<ExternalType>(B));
28
50.3k
  }));
29
30
  // Create the content node according to the external type.
31
50.3k
  switch (ImpDesc.getExternalType()) {
32
32.9k
  case ExternalType::Function: {
33
    // Read the function type index.
34
32.9k
    EXPECTED_TRY(FMgr.readU32().map_error(ReportError).map([&](uint32_t Idx) {
35
32.9k
      ImpDesc.setExternalFuncTypeIdx(Idx);
36
32.9k
    }));
37
32.9k
    return {};
38
32.9k
  }
39
1.31k
  case ExternalType::Table: {
40
    // Read the table type node.
41
1.31k
    return loadType(ImpDesc.getExternalTableType());
42
32.9k
  }
43
1.99k
  case ExternalType::Memory: {
44
    // Read the memory type node.
45
1.99k
    return loadType(ImpDesc.getExternalMemoryType());
46
32.9k
  }
47
12.8k
  case ExternalType::Global: {
48
    // Read the global type node.
49
12.8k
    EXPECTED_TRY(loadType(ImpDesc.getExternalGlobalType()));
50
    // Mutable global imports are for the ImportExportMutGlobals proposal.
51
12.8k
    if (ImpDesc.getExternalGlobalType().getValMut() == ValMut::Var &&
52
1.72k
        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.8k
    return {};
58
12.8k
  }
59
1.20k
  case ExternalType::Tag: {
60
1.20k
    if (!Conf.hasProposal(Proposal::ExceptionHandling)) {
61
0
      return logNeedProposal(ErrCode::Value::MalformedImportKind,
62
0
                             Proposal::ExceptionHandling, FMgr.getLastOffset(),
63
0
                             ASTNodeAttr::Desc_Import);
64
0
    }
65
    // Read the Tag type node.
66
1.20k
    return loadType(ImpDesc.getExternalTagType());
67
1.20k
  }
68
25
  default:
69
25
    return logLoadError(ErrCode::Value::MalformedImportKind,
70
25
                        FMgr.getLastOffset(), ASTNodeAttr::Desc_Import);
71
50.3k
  }
72
50.3k
}
73
74
// Load binary of Export description. See "include/loader/loader.h".
75
36.0k
Expect<void> Loader::loadDesc(AST::ExportDesc &ExpDesc) {
76
36.0k
  auto ReportError = [this](auto E) {
77
138
    return logLoadError(E, FMgr.getLastOffset(), ASTNodeAttr::Desc_Export);
78
138
  };
79
80
  // Read external name to export.
81
36.0k
  EXPECTED_TRY(FMgr.readName().map_error(ReportError).map([&](std::string S) {
82
35.9k
    ExpDesc.setExternalName(S);
83
35.9k
  }));
84
85
  // Read external type.
86
35.9k
  EXPECTED_TRY(FMgr.readByte().map_error(ReportError).map([&](uint8_t B) {
87
35.9k
    ExpDesc.setExternalType(static_cast<ExternalType>(B));
88
35.9k
  }));
89
35.9k
  switch (ExpDesc.getExternalType()) {
90
23.5k
  case ExternalType::Function:
91
28.6k
  case ExternalType::Table:
92
29.7k
  case ExternalType::Memory:
93
35.1k
  case ExternalType::Global:
94
35.1k
    break;
95
789
  case ExternalType::Tag:
96
789
    if (!Conf.hasProposal(Proposal::ExceptionHandling)) {
97
0
      return logNeedProposal(ErrCode::Value::MalformedExportKind,
98
0
                             Proposal::ExceptionHandling, FMgr.getLastOffset(),
99
0
                             ASTNodeAttr::Desc_Export);
100
0
    }
101
789
    break;
102
789
  default:
103
31
    return logLoadError(ErrCode::Value::MalformedExportKind,
104
31
                        FMgr.getLastOffset(), ASTNodeAttr::Desc_Export);
105
35.9k
  }
106
107
  // Read external index to export.
108
35.9k
  EXPECTED_TRY(FMgr.readU32().map_error(ReportError).map([&](uint32_t Idx) {
109
35.8k
    ExpDesc.setExternalIndex(Idx);
110
35.8k
  }));
111
35.8k
  return {};
112
35.9k
}
113
114
} // namespace Loader
115
} // namespace WasmEdge