Skip to content

Commit f42d8f6

Browse files
authored
Refactor module element related functions (NFC) (#2550)
This does something similar to #2489 for more functions, removing boilerplate code for each module element using template functions.
1 parent 977aa84 commit f42d8f6

File tree

2 files changed

+72
-82
lines changed

2 files changed

+72
-82
lines changed

src/wasm.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1389,10 +1389,14 @@ class Module {
13891389

13901390
Export* addExport(Export* curr);
13911391
Function* addFunction(Function* curr);
1392-
Function* addFunction(std::unique_ptr<Function> curr);
13931392
Global* addGlobal(Global* curr);
13941393
Event* addEvent(Event* curr);
13951394

1395+
Export* addExport(std::unique_ptr<Export> curr);
1396+
Function* addFunction(std::unique_ptr<Function> curr);
1397+
Global* addGlobal(std::unique_ptr<Global> curr);
1398+
Event* addEvent(std::unique_ptr<Event> curr);
1399+
13961400
void addStart(const Name& s);
13971401

13981402
void removeExport(Name name);

src/wasm/wasm.cpp

Lines changed: 67 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -975,134 +975,120 @@ void Function::clearDebugInfo() {
975975
epilogLocation.clear();
976976
}
977977

978-
Export* Module::getExport(Name name) {
979-
auto iter = exportsMap.find(name);
980-
if (iter == exportsMap.end()) {
981-
Fatal() << "Module::getExport: " << name << " does not exist";
978+
template<typename Map>
979+
typename Map::mapped_type&
980+
getModuleElement(Map& m, Name name, const std::string& funcName) {
981+
auto iter = m.find(name);
982+
if (iter == m.end()) {
983+
Fatal() << "Module::" << funcName << ": " << name << " does not exist";
982984
}
983985
return iter->second;
984986
}
985987

988+
Export* Module::getExport(Name name) {
989+
return getModuleElement(exportsMap, name, "getExport");
990+
}
991+
986992
Function* Module::getFunction(Name name) {
987-
auto iter = functionsMap.find(name);
988-
if (iter == functionsMap.end()) {
989-
Fatal() << "Module::getFunction: " << name << " does not exist";
990-
}
991-
return iter->second;
993+
return getModuleElement(functionsMap, name, "getFunction");
992994
}
993995

994996
Global* Module::getGlobal(Name name) {
995-
auto iter = globalsMap.find(name);
996-
if (iter == globalsMap.end()) {
997-
assert(false);
998-
Fatal() << "Module::getGlobal: " << name << " does not exist";
999-
}
1000-
return iter->second;
997+
return getModuleElement(globalsMap, name, "getGlobal");
1001998
}
1002999

10031000
Event* Module::getEvent(Name name) {
1004-
auto iter = eventsMap.find(name);
1005-
if (iter == eventsMap.end()) {
1006-
Fatal() << "Module::getEvent: " << name << " does not exist";
1007-
}
1008-
return iter->second;
1001+
return getModuleElement(eventsMap, name, "getEvent");
10091002
}
10101003

1011-
Export* Module::getExportOrNull(Name name) {
1012-
auto iter = exportsMap.find(name);
1013-
if (iter == exportsMap.end()) {
1004+
template<typename Map>
1005+
typename Map::mapped_type getModuleElementOrNull(Map& m, Name name) {
1006+
auto iter = m.find(name);
1007+
if (iter == m.end()) {
10141008
return nullptr;
10151009
}
10161010
return iter->second;
10171011
}
10181012

1013+
Export* Module::getExportOrNull(Name name) {
1014+
return getModuleElementOrNull(exportsMap, name);
1015+
}
1016+
10191017
Function* Module::getFunctionOrNull(Name name) {
1020-
auto iter = functionsMap.find(name);
1021-
if (iter == functionsMap.end()) {
1022-
return nullptr;
1023-
}
1024-
return iter->second;
1018+
return getModuleElementOrNull(functionsMap, name);
10251019
}
10261020

10271021
Global* Module::getGlobalOrNull(Name name) {
1028-
auto iter = globalsMap.find(name);
1029-
if (iter == globalsMap.end()) {
1030-
return nullptr;
1031-
}
1032-
return iter->second;
1022+
return getModuleElementOrNull(globalsMap, name);
10331023
}
10341024

10351025
Event* Module::getEventOrNull(Name name) {
1036-
auto iter = eventsMap.find(name);
1037-
if (iter == eventsMap.end()) {
1038-
return nullptr;
1039-
}
1040-
return iter->second;
1041-
}
1042-
1043-
Export* Module::addExport(Export* curr) {
1044-
if (!curr->name.is()) {
1045-
Fatal() << "Module::addExport: empty name";
1046-
}
1047-
if (getExportOrNull(curr->name)) {
1048-
Fatal() << "Module::addExport: " << curr->name << " already exists";
1049-
}
1050-
exports.push_back(std::unique_ptr<Export>(curr));
1051-
exportsMap[curr->name] = curr;
1052-
return curr;
1026+
return getModuleElementOrNull(eventsMap, name);
10531027
}
10541028

10551029
// TODO(@warchant): refactor all usages to use variant with unique_ptr
1056-
Function* Module::addFunction(Function* curr) {
1030+
template<typename Vector, typename Map, typename Elem>
1031+
Elem* addModuleElement(Vector& v, Map& m, Elem* curr, std::string funcName) {
10571032
if (!curr->name.is()) {
1058-
Fatal() << "Module::addFunction: empty name";
1033+
Fatal() << "Module::" << funcName << ": empty name";
10591034
}
1060-
if (getFunctionOrNull(curr->name)) {
1061-
Fatal() << "Module::addFunction: " << curr->name << " already exists";
1035+
if (getModuleElementOrNull(m, curr->name)) {
1036+
Fatal() << "Module::" << funcName << ": " << curr->name
1037+
<< " already exists";
10621038
}
1063-
functions.push_back(std::unique_ptr<Function>(curr));
1064-
functionsMap[curr->name] = curr;
1039+
v.push_back(std::unique_ptr<Elem>(curr));
1040+
m[curr->name] = curr;
10651041
return curr;
10661042
}
10671043

1068-
Function* Module::addFunction(std::unique_ptr<Function> curr) {
1044+
template<typename Vector, typename Map, typename Elem>
1045+
Elem* addModuleElement(Vector& v,
1046+
Map& m,
1047+
std::unique_ptr<Elem> curr,
1048+
std::string funcName) {
10691049
if (!curr->name.is()) {
1070-
Fatal() << "Module::addFunction: empty name";
1050+
Fatal() << "Module::" << funcName << ": empty name";
10711051
}
1072-
if (getFunctionOrNull(curr->name)) {
1073-
Fatal() << "Module::addFunction: " << curr->name << " already exists";
1052+
if (getModuleElementOrNull(m, curr->name)) {
1053+
Fatal() << "Module::" << funcName << ": " << curr->name
1054+
<< " already exists";
10741055
}
1075-
auto* ret = functionsMap[curr->name] = curr.get();
1076-
functions.push_back(std::move(curr));
1056+
auto* ret = m[curr->name] = curr.get();
1057+
v.push_back(std::move(curr));
10771058
return ret;
10781059
}
10791060

1080-
Global* Module::addGlobal(Global* curr) {
1081-
if (!curr->name.is()) {
1082-
Fatal() << "Module::addGlobal: empty name";
1083-
}
1084-
if (getGlobalOrNull(curr->name)) {
1085-
Fatal() << "Module::addGlobal: " << curr->name << " already exists";
1086-
}
1061+
Export* Module::addExport(Export* curr) {
1062+
return addModuleElement(exports, exportsMap, curr, "addExport");
1063+
}
10871064

1088-
globals.emplace_back(curr);
1065+
Function* Module::addFunction(Function* curr) {
1066+
return addModuleElement(functions, functionsMap, curr, "addFunction");
1067+
}
10891068

1090-
globalsMap[curr->name] = curr;
1091-
return curr;
1069+
Global* Module::addGlobal(Global* curr) {
1070+
return addModuleElement(globals, globalsMap, curr, "addGlobal");
10921071
}
10931072

10941073
Event* Module::addEvent(Event* curr) {
1095-
if (!curr->name.is()) {
1096-
Fatal() << "Module::addEvent: empty name";
1097-
}
1098-
if (getEventOrNull(curr->name)) {
1099-
Fatal() << "Module::addEvent: " << curr->name << " already exists";
1100-
}
1074+
return addModuleElement(events, eventsMap, curr, "addEvent");
1075+
}
11011076

1102-
events.emplace_back(curr);
1077+
Export* Module::addExport(std::unique_ptr<Export> curr) {
1078+
return addModuleElement(exports, exportsMap, std::move(curr), "addExport");
1079+
}
11031080

1104-
eventsMap[curr->name] = curr;
1105-
return curr;
1081+
Function* Module::addFunction(std::unique_ptr<Function> curr) {
1082+
return addModuleElement(
1083+
functions, functionsMap, std::move(curr), "addFunction");
1084+
}
1085+
1086+
Global* Module::addGlobal(std::unique_ptr<Global> curr) {
1087+
return addModuleElement(globals, globalsMap, std::move(curr), "addGlobal");
1088+
}
1089+
1090+
Event* Module::addEvent(std::unique_ptr<Event> curr) {
1091+
return addModuleElement(events, eventsMap, std::move(curr), "addEvent");
11061092
}
11071093

11081094
void Module::addStart(const Name& s) { start = s; }

0 commit comments

Comments
 (0)