Skip to content

Commit c929cf6

Browse files
authored
[NFC] Simplify binary reading logic for tables (#4974)
Similar to #4969 but for tables.
1 parent 6bf41a5 commit c929cf6

File tree

2 files changed

+11
-32
lines changed

2 files changed

+11
-32
lines changed

src/wasm-binary.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1519,11 +1519,6 @@ class WasmBinaryBuilder {
15191519
// function to check
15201520
Index endOfFunction = -1;
15211521

1522-
// we store tables here before wasm.addTable after we know their names
1523-
std::vector<std::unique_ptr<Table>> tables;
1524-
// we store table imports here before wasm.addTableImport after we know
1525-
// their names
1526-
std::vector<Table*> tableImports;
15271522
// at index i we have all references to the table i
15281523
std::map<Index, std::vector<Name*>> tableRefs;
15291524

src/wasm/wasm-binary.cpp

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2334,7 +2334,6 @@ void WasmBinaryBuilder::readImports() {
23342334
throwError("Tables may not be 64-bit");
23352335
}
23362336

2337-
tableImports.push_back(table.get());
23382337
wasm.addTable(std::move(table));
23392338
break;
23402339
}
@@ -2986,9 +2985,6 @@ void WasmBinaryBuilder::processNames() {
29862985
for (auto& global : globals) {
29872986
wasm.addGlobal(std::move(global));
29882987
}
2989-
for (auto& table : tables) {
2990-
wasm.addTable(std::move(table));
2991-
}
29922988
for (auto& segment : elementSegments) {
29932989
wasm.addElementSegment(std::move(segment));
29942990
}
@@ -3115,7 +3111,7 @@ void WasmBinaryBuilder::readTableDeclarations() {
31153111
throwError("Tables may not be 64-bit");
31163112
}
31173113

3118-
tables.push_back(std::move(table));
3114+
wasm.addTable(std::move(table));
31193115
}
31203116
}
31213117

@@ -3154,17 +3150,10 @@ void WasmBinaryBuilder::readElementSegments() {
31543150
tableIdx = getU32LEB();
31553151
}
31563152

3157-
Table* table = nullptr;
3158-
auto numTableImports = tableImports.size();
3159-
if (tableIdx < numTableImports) {
3160-
table = tableImports[tableIdx];
3161-
} else if (tableIdx - numTableImports < tables.size()) {
3162-
table = tables[tableIdx - numTableImports].get();
3163-
}
3164-
if (!table) {
3153+
if (tableIdx >= wasm.tables.size()) {
31653154
throwError("Table index out of range.");
31663155
}
3167-
3156+
auto* table = wasm.tables[tableIdx].get();
31683157
segment->table = table->name;
31693158
segment->offset = readExpression();
31703159
}
@@ -3367,20 +3356,15 @@ void WasmBinaryBuilder::readNames(size_t payloadLen) {
33673356
auto index = getU32LEB();
33683357
auto rawName = getInlineString();
33693358
auto name = processor.process(rawName);
3370-
auto numTableImports = tableImports.size();
3371-
auto setTableName = [&](Table* table) {
3359+
3360+
if (index < wasm.tables.size()) {
3361+
auto* table = wasm.tables[index].get();
33723362
for (auto& segment : elementSegments) {
33733363
if (segment->table == table->name) {
33743364
segment->table = name;
33753365
}
33763366
}
33773367
table->setExplicitName(name);
3378-
};
3379-
3380-
if (index < numTableImports) {
3381-
setTableName(tableImports[index]);
3382-
} else if (index - numTableImports < tables.size()) {
3383-
setTableName(tables[index - numTableImports].get());
33843368
} else {
33853369
std::cerr << "warning: table index out of bounds in name section, "
33863370
"table subsection: "
@@ -5226,7 +5210,7 @@ bool WasmBinaryBuilder::maybeVisitTableSize(Expression*& out, uint32_t code) {
52265210
return false;
52275211
}
52285212
Index tableIdx = getU32LEB();
5229-
if (tableIdx >= tables.size()) {
5213+
if (tableIdx >= wasm.tables.size()) {
52305214
throwError("bad table index");
52315215
}
52325216
auto* curr = allocator.alloc<TableSize>();
@@ -5242,7 +5226,7 @@ bool WasmBinaryBuilder::maybeVisitTableGrow(Expression*& out, uint32_t code) {
52425226
return false;
52435227
}
52445228
Index tableIdx = getU32LEB();
5245-
if (tableIdx >= tables.size()) {
5229+
if (tableIdx >= wasm.tables.size()) {
52465230
throwError("bad table index");
52475231
}
52485232
auto* curr = allocator.alloc<TableGrow>();
@@ -6634,11 +6618,11 @@ void WasmBinaryBuilder::visitRefEq(RefEq* curr) {
66346618
void WasmBinaryBuilder::visitTableGet(TableGet* curr) {
66356619
BYN_TRACE("zz node: TableGet\n");
66366620
Index tableIdx = getU32LEB();
6637-
if (tableIdx >= tables.size()) {
6621+
if (tableIdx >= wasm.tables.size()) {
66386622
throwError("bad table index");
66396623
}
66406624
curr->index = popNonVoidExpression();
6641-
curr->type = tables[tableIdx]->type;
6625+
curr->type = wasm.tables[tableIdx]->type;
66426626
curr->finalize();
66436627
// Defer setting the table name for later, when we know it.
66446628
tableRefs[tableIdx].push_back(&curr->table);
@@ -6647,7 +6631,7 @@ void WasmBinaryBuilder::visitTableGet(TableGet* curr) {
66476631
void WasmBinaryBuilder::visitTableSet(TableSet* curr) {
66486632
BYN_TRACE("zz node: TableSet\n");
66496633
Index tableIdx = getU32LEB();
6650-
if (tableIdx >= tables.size()) {
6634+
if (tableIdx >= wasm.tables.size()) {
66516635
throwError("bad table index");
66526636
}
66536637
curr->value = popNonVoidExpression();

0 commit comments

Comments
 (0)