Skip to content

Commit 2ba9dfe

Browse files
Make getters const
1 parent cfa8abd commit 2ba9dfe

File tree

2 files changed

+52
-47
lines changed

2 files changed

+52
-47
lines changed

src/wasm.h

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2699,30 +2699,30 @@ class Module {
26992699
public:
27002700
Module() = default;
27012701

2702-
Export* getExport(Name name);
2703-
Function* getFunction(Name name);
2704-
Table* getTable(Name name);
2705-
ElementSegment* getElementSegment(Name name);
2706-
Memory* getMemory(Name name);
2707-
DataSegment* getDataSegment(Name name);
2708-
Global* getGlobal(Name name);
2709-
Tag* getTag(Name name);
2710-
2711-
Export* getExportOrNull(Name name);
2712-
Table* getTableOrNull(Name name);
2713-
Memory* getMemoryOrNull(Name name);
2714-
ElementSegment* getElementSegmentOrNull(Name name);
2715-
DataSegment* getDataSegmentOrNull(Name name);
2716-
Function* getFunctionOrNull(Name name);
2717-
Global* getGlobalOrNull(Name name);
2718-
Tag* getTagOrNull(Name name);
2702+
Export* getExport(Name name) const;
2703+
Function* getFunction(Name name) const;
2704+
Table* getTable(Name name) const;
2705+
ElementSegment* getElementSegment(Name name) const;
2706+
Memory* getMemory(Name name) const;
2707+
DataSegment* getDataSegment(Name name) const;
2708+
Global* getGlobal(Name name) const;
2709+
Tag* getTag(Name name) const;
2710+
2711+
Export* getExportOrNull(Name name) const;
2712+
Table* getTableOrNull(Name name) const;
2713+
Memory* getMemoryOrNull(Name name) const;
2714+
ElementSegment* getElementSegmentOrNull(Name name) const;
2715+
DataSegment* getDataSegmentOrNull(Name name) const;
2716+
Function* getFunctionOrNull(Name name) const;
2717+
Global* getGlobalOrNull(Name name) const;
2718+
Tag* getTagOrNull(Name name) const;
27192719

27202720
// get* methods that are generic over the kind, that is, items are identified
27212721
// by their kind and their name. Otherwise, they are similar to the above
27222722
// get* methods. These return items that can be imports.
27232723
// TODO: Add methods for things that cannot be imports (segments).
2724-
Importable* getImport(ModuleItemKind kind, Name name);
2725-
Importable* getImportOrNull(ModuleItemKind kind, Name name);
2724+
Importable* getImport(ModuleItemKind kind, Name name) const;
2725+
Importable* getImportOrNull(ModuleItemKind kind, Name name) const;
27262726

27272727
Export* addExport(Export* curr);
27282728
Function* addFunction(Function* curr);

src/wasm/wasm.cpp

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,21 @@
2222

2323
namespace wasm {
2424

25+
namespace {
26+
27+
template<typename Value>
28+
const Value& getModuleElement(const std::unordered_map<Name, Value>& m,
29+
Name name,
30+
std::string_view funcName) {
31+
auto iter = m.find(name);
32+
if (iter == m.end()) {
33+
Fatal() << "Module::" << funcName << ": " << name << " does not exist";
34+
}
35+
return iter->second;
36+
}
37+
38+
} // namespace
39+
2540
// shared constants
2641

2742
Name RETURN_FLOW("*return:)*");
@@ -1705,45 +1720,35 @@ void Function::clearDebugInfo() {
17051720
epilogLocation.reset();
17061721
}
17071722

1708-
template<typename Map>
1709-
typename Map::mapped_type&
1710-
getModuleElement(Map& m, Name name, std::string_view funcName) {
1711-
auto iter = m.find(name);
1712-
if (iter == m.end()) {
1713-
Fatal() << "Module::" << funcName << ": " << name << " does not exist";
1714-
}
1715-
return iter->second;
1716-
}
1717-
1718-
Export* Module::getExport(Name name) {
1723+
Export* Module::getExport(Name name) const {
17191724
return getModuleElement(exportsMap, name, "getExport");
17201725
}
17211726

1722-
Function* Module::getFunction(Name name) {
1727+
Function* Module::getFunction(Name name) const {
17231728
return getModuleElement(functionsMap, name, "getFunction");
17241729
}
17251730

1726-
Table* Module::getTable(Name name) {
1731+
Table* Module::getTable(Name name) const {
17271732
return getModuleElement(tablesMap, name, "getTable");
17281733
}
17291734

1730-
ElementSegment* Module::getElementSegment(Name name) {
1735+
ElementSegment* Module::getElementSegment(Name name) const {
17311736
return getModuleElement(elementSegmentsMap, name, "getElementSegment");
17321737
}
17331738

1734-
Memory* Module::getMemory(Name name) {
1739+
Memory* Module::getMemory(Name name) const {
17351740
return getModuleElement(memoriesMap, name, "getMemory");
17361741
}
17371742

1738-
DataSegment* Module::getDataSegment(Name name) {
1743+
DataSegment* Module::getDataSegment(Name name) const {
17391744
return getModuleElement(dataSegmentsMap, name, "getDataSegment");
17401745
}
17411746

1742-
Global* Module::getGlobal(Name name) {
1747+
Global* Module::getGlobal(Name name) const {
17431748
return getModuleElement(globalsMap, name, "getGlobal");
17441749
}
17451750

1746-
Tag* Module::getTag(Name name) {
1751+
Tag* Module::getTag(Name name) const {
17471752
return getModuleElement(tagsMap, name, "getTag");
17481753
}
17491754

@@ -1756,39 +1761,39 @@ typename Map::mapped_type getModuleElementOrNull(Map& m, Name name) {
17561761
return iter->second;
17571762
}
17581763

1759-
Export* Module::getExportOrNull(Name name) {
1764+
Export* Module::getExportOrNull(Name name) const {
17601765
return getModuleElementOrNull(exportsMap, name);
17611766
}
17621767

1763-
Function* Module::getFunctionOrNull(Name name) {
1768+
Function* Module::getFunctionOrNull(Name name) const {
17641769
return getModuleElementOrNull(functionsMap, name);
17651770
}
17661771

1767-
Table* Module::getTableOrNull(Name name) {
1772+
Table* Module::getTableOrNull(Name name) const {
17681773
return getModuleElementOrNull(tablesMap, name);
17691774
}
17701775

1771-
ElementSegment* Module::getElementSegmentOrNull(Name name) {
1776+
ElementSegment* Module::getElementSegmentOrNull(Name name) const {
17721777
return getModuleElementOrNull(elementSegmentsMap, name);
17731778
}
17741779

1775-
Memory* Module::getMemoryOrNull(Name name) {
1780+
Memory* Module::getMemoryOrNull(Name name) const {
17761781
return getModuleElementOrNull(memoriesMap, name);
17771782
}
17781783

1779-
DataSegment* Module::getDataSegmentOrNull(Name name) {
1784+
DataSegment* Module::getDataSegmentOrNull(Name name) const {
17801785
return getModuleElementOrNull(dataSegmentsMap, name);
17811786
}
17821787

1783-
Global* Module::getGlobalOrNull(Name name) {
1788+
Global* Module::getGlobalOrNull(Name name) const {
17841789
return getModuleElementOrNull(globalsMap, name);
17851790
}
17861791

1787-
Tag* Module::getTagOrNull(Name name) {
1792+
Tag* Module::getTagOrNull(Name name) const {
17881793
return getModuleElementOrNull(tagsMap, name);
17891794
}
17901795

1791-
Importable* Module::getImport(ModuleItemKind kind, Name name) {
1796+
Importable* Module::getImport(ModuleItemKind kind, Name name) const {
17921797
switch (kind) {
17931798
case ModuleItemKind::Function:
17941799
return getFunction(name);
@@ -1809,7 +1814,7 @@ Importable* Module::getImport(ModuleItemKind kind, Name name) {
18091814
WASM_UNREACHABLE("unexpected kind");
18101815
}
18111816

1812-
Importable* Module::getImportOrNull(ModuleItemKind kind, Name name) {
1817+
Importable* Module::getImportOrNull(ModuleItemKind kind, Name name) const {
18131818
auto doReturn = [](Importable* importable) {
18141819
return importable ? importable->imported() ? importable : nullptr : nullptr;
18151820
};

0 commit comments

Comments
 (0)