Skip to content

Commit 28fbf85

Browse files
committed
WasmBinaryReader: Use helper function to create names for items. NFC
As a followup we could probably make these more consistent. For example, we could use a single char prefix for defined functions/tables/globals (e.g. f0/t0/g0)
1 parent 53d54d7 commit 28fbf85

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

src/wasm/wasm-binary.cpp

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2239,13 +2239,17 @@ void WasmBinaryReader::readStart() {
22392239
startIndex = getU32LEB();
22402240
}
22412241

2242+
static Name makeName(std::string prefix, size_t counter) {
2243+
return Name(prefix + std::to_string(counter));
2244+
}
2245+
22422246
void WasmBinaryReader::readMemories() {
22432247
BYN_TRACE("== readMemories\n");
22442248
auto num = getU32LEB();
22452249
BYN_TRACE("num: " << num << std::endl);
22462250
for (size_t i = 0; i < num; i++) {
22472251
BYN_TRACE("read one\n");
2248-
auto memory = Builder::makeMemory(Name::fromInt(i));
2252+
auto memory = Builder::makeMemory(makeName("", i));
22492253
getResizableLimits(memory->initial,
22502254
memory->max,
22512255
memory->shared,
@@ -2534,7 +2538,7 @@ void WasmBinaryReader::readImports() {
25342538
// could occur later due to the names section.
25352539
switch (kind) {
25362540
case ExternalKind::Function: {
2537-
Name name(std::string("fimport$") + std::to_string(functionCounter++));
2541+
Name name = makeName("fimport$", functionCounter++);
25382542
auto index = getU32LEB();
25392543
functionTypes.push_back(getTypeByIndex(index));
25402544
auto type = getTypeByIndex(index);
@@ -2550,8 +2554,7 @@ void WasmBinaryReader::readImports() {
25502554
break;
25512555
}
25522556
case ExternalKind::Table: {
2553-
Name name(std::string("timport$") + std::to_string(tableCounter++));
2554-
auto table = builder.makeTable(name);
2557+
auto table = builder.makeTable(makeName("timport$", tableCounter++));
25552558
table->module = module;
25562559
table->base = base;
25572560
table->type = getType();
@@ -2570,8 +2573,7 @@ void WasmBinaryReader::readImports() {
25702573
break;
25712574
}
25722575
case ExternalKind::Memory: {
2573-
Name name(std::string("mimport$") + std::to_string(memoryCounter++));
2574-
auto memory = builder.makeMemory(name);
2576+
auto memory = builder.makeMemory(makeName("mimport$", memoryCounter++));
25752577
memory->module = module;
25762578
memory->base = base;
25772579
getResizableLimits(memory->initial,
@@ -2583,14 +2585,13 @@ void WasmBinaryReader::readImports() {
25832585
break;
25842586
}
25852587
case ExternalKind::Global: {
2586-
Name name(std::string("gimport$") + std::to_string(globalCounter++));
25872588
auto type = getConcreteType();
25882589
auto mutable_ = getU32LEB();
25892590
if (mutable_ & ~1) {
25902591
throwError("Global mutability must be 0 or 1");
25912592
}
25922593
auto curr =
2593-
builder.makeGlobal(name,
2594+
builder.makeGlobal(makeName("gimport$", globalCounter++),
25942595
type,
25952596
nullptr,
25962597
mutable_ ? Builder::Mutable : Builder::Immutable);
@@ -2600,7 +2601,7 @@ void WasmBinaryReader::readImports() {
26002601
break;
26012602
}
26022603
case ExternalKind::Tag: {
2603-
Name name(std::string("eimport$") + std::to_string(tagCounter++));
2604+
Name name = makeName("eimport$", tagCounter++);
26042605
getInt8(); // Reserved 'attribute' field
26052606
auto index = getU32LEB();
26062607
auto curr = builder.makeTag(name, getSignatureByTypeIndex(index));
@@ -2618,7 +2619,7 @@ void WasmBinaryReader::readImports() {
26182619

26192620
Name WasmBinaryReader::getNextLabel() {
26202621
requireFunctionContext("getting a label");
2621-
return Name("label$" + std::to_string(nextLabel++));
2622+
return makeName("label$", nextLabel++);
26222623
}
26232624

26242625
void WasmBinaryReader::requireFunctionContext(const char* error) {
@@ -2688,7 +2689,7 @@ void WasmBinaryReader::readFunctions() {
26882689
endOfFunction = pos + size;
26892690

26902691
auto func = std::make_unique<Function>();
2691-
func->name = Name::fromInt(i);
2692+
func->name = makeName("", i);
26922693
func->type = getTypeByFunctionIndex(numImports + i);
26932694
currFunction = func.get();
26942695

@@ -3046,7 +3047,7 @@ void WasmBinaryReader::readGlobals() {
30463047
}
30473048
auto* init = readExpression();
30483049
wasm.addGlobal(
3049-
Builder::makeGlobal("global$" + std::to_string(i),
3050+
Builder::makeGlobal(makeName("global$", i),
30503051
type,
30513052
init,
30523053
mutable_ ? Builder::Mutable : Builder::Immutable));
@@ -3366,7 +3367,7 @@ void WasmBinaryReader::readTableDeclarations() {
33663367
if (!elemType.isRef()) {
33673368
throwError("Table type must be a reference type");
33683369
}
3369-
auto table = Builder::makeTable(Name::fromInt(i), elemType);
3370+
auto table = Builder::makeTable(makeName("", i), elemType);
33703371
bool is_shared;
33713372
getResizableLimits(table->initial,
33723373
table->max,
@@ -3466,7 +3467,7 @@ void WasmBinaryReader::readTags() {
34663467
BYN_TRACE("read one\n");
34673468
getInt8(); // Reserved 'attribute' field
34683469
auto typeIndex = getU32LEB();
3469-
wasm.addTag(Builder::makeTag("tag$" + std::to_string(i),
3470+
wasm.addTag(Builder::makeTag(makeName("tag$", i),
34703471
getSignatureByTypeIndex(typeIndex)));
34713472
}
34723473
}

0 commit comments

Comments
 (0)