Skip to content

Commit 95ccf32

Browse files
authored
Refactor name processing (escaping/deduplication) to a shared place. NFC (#3609)
(not 100% NFC since it also fixes a bug by moving a line out of a loop)
1 parent 4311e46 commit 95ccf32

File tree

1 file changed

+29
-29
lines changed

1 file changed

+29
-29
lines changed

src/wasm/wasm-binary.cpp

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2719,6 +2719,27 @@ Name WasmBinaryBuilder::escape(Name name) {
27192719
return escaped;
27202720
}
27212721

2722+
// Performs necessary processing of names from the name section before using
2723+
// them. Specifically it escapes and deduplicates them.
2724+
class NameProcessor {
2725+
public:
2726+
Name process(Name name) {
2727+
return deduplicate(WasmBinaryBuilder::escape(name));
2728+
}
2729+
2730+
private:
2731+
std::unordered_set<Name> usedNames;
2732+
2733+
Name deduplicate(Name base) {
2734+
Name name = base;
2735+
// De-duplicate names by appending .1, .2, etc.
2736+
for (int i = 1; !usedNames.insert(name).second; ++i) {
2737+
name = std::string(base.str) + std::string(".") + std::to_string(i);
2738+
}
2739+
return name;
2740+
}
2741+
};
2742+
27222743
void WasmBinaryBuilder::readNames(size_t payloadLen) {
27232744
BYN_TRACE("== readNames\n");
27242745
auto sectionPos = pos;
@@ -2731,16 +2752,11 @@ void WasmBinaryBuilder::readNames(size_t payloadLen) {
27312752
} else if (nameType ==
27322753
BinaryConsts::UserSections::Subsection::NameFunction) {
27332754
auto num = getU32LEB();
2734-
std::unordered_set<Name> usedNames;
2755+
NameProcessor processor;
27352756
for (size_t i = 0; i < num; i++) {
27362757
auto index = getU32LEB();
27372758
auto rawName = getInlineString();
2738-
auto name = escape(rawName);
2739-
// De-duplicate names by appending .1, .2, etc.
2740-
for (int i = 1; !usedNames.insert(name).second; ++i) {
2741-
name = std::string(escape(rawName).str) + std::string(".") +
2742-
std::to_string(i);
2743-
}
2759+
auto name = processor.process(rawName);
27442760
auto numFunctionImports = functionImports.size();
27452761
if (index < numFunctionImports) {
27462762
functionImports[index]->setExplicitName(name);
@@ -2770,19 +2786,14 @@ void WasmBinaryBuilder::readNames(size_t payloadLen) {
27702786
<< std::to_string(funcIndex) << std::endl;
27712787
}
27722788
auto numLocals = getU32LEB();
2773-
std::unordered_set<Name> usedNames;
2789+
NameProcessor processor;
27742790
for (size_t j = 0; j < numLocals; j++) {
27752791
auto localIndex = getU32LEB();
27762792
auto rawLocalName = getInlineString();
27772793
if (!func) {
27782794
continue; // read and discard in case of prior error
27792795
}
2780-
auto localName = escape(rawLocalName);
2781-
// De-duplicate names by appending .1, .2, etc.
2782-
for (int i = 1; !usedNames.insert(localName).second; ++i) {
2783-
localName = std::string(escape(rawLocalName).str) +
2784-
std::string(".") + std::to_string(i);
2785-
}
2796+
auto localName = processor.process(rawLocalName);
27862797
if (localIndex < func->getNumLocals()) {
27872798
func->localNames[localIndex] = localName;
27882799
} else {
@@ -2796,17 +2807,11 @@ void WasmBinaryBuilder::readNames(size_t payloadLen) {
27962807
}
27972808
} else if (nameType == BinaryConsts::UserSections::Subsection::NameTable) {
27982809
auto num = getU32LEB();
2810+
NameProcessor processor;
27992811
for (size_t i = 0; i < num; i++) {
2800-
std::unordered_set<Name> usedNames;
28012812
auto index = getU32LEB();
28022813
auto rawName = getInlineString();
2803-
auto name = escape(rawName);
2804-
// De-duplicate names by appending .1, .2, etc.
2805-
for (int i = 1; !usedNames.insert(name).second; ++i) {
2806-
name = std::string(escape(rawName).str) + std::string(".") +
2807-
std::to_string(i);
2808-
}
2809-
2814+
auto name = processor.process(rawName);
28102815
auto numTableImports = tableImports.size();
28112816
if (index < numTableImports) {
28122817
tableImports[index]->setExplicitName(name);
@@ -2849,16 +2854,11 @@ void WasmBinaryBuilder::readNames(size_t payloadLen) {
28492854
}
28502855
} else if (nameType == BinaryConsts::UserSections::Subsection::NameGlobal) {
28512856
auto num = getU32LEB();
2852-
std::unordered_set<Name> usedNames;
2857+
NameProcessor processor;
28532858
for (size_t i = 0; i < num; i++) {
28542859
auto index = getU32LEB();
28552860
auto rawName = getInlineString();
2856-
auto name = escape(rawName);
2857-
// De-duplicate names by appending .1, .2, etc.
2858-
for (int i = 1; !usedNames.insert(name).second; ++i) {
2859-
name = std::string(escape(rawName).str) + std::string(".") +
2860-
std::to_string(i);
2861-
}
2861+
auto name = processor.process(rawName);
28622862
auto numGlobalImports = globalImports.size();
28632863
if (index < numGlobalImports) {
28642864
globalImports[index]->setExplicitName(name);

0 commit comments

Comments
 (0)