summaryrefslogtreecommitdiff
path: root/src/wasm/wasm-binary.cpp
diff options
context:
space:
mode:
authorAlon Zakai <azakai@google.com>2022-08-26 15:47:27 -0700
committerGitHub <noreply@github.com>2022-08-26 15:47:27 -0700
commitc929cf6b5ce444e1fadd52f6d289142ad5b3df5a (patch)
treec8d3049833f0ea1a3f04747e4498064351b8e52f /src/wasm/wasm-binary.cpp
parent6bf41a5f717115f73626ca0fa2029a526616344c (diff)
downloadbinaryen-c929cf6b5ce444e1fadd52f6d289142ad5b3df5a.tar.gz
binaryen-c929cf6b5ce444e1fadd52f6d289142ad5b3df5a.tar.bz2
binaryen-c929cf6b5ce444e1fadd52f6d289142ad5b3df5a.zip
[NFC] Simplify binary reading logic for tables (#4974)
Similar to #4969 but for tables.
Diffstat (limited to 'src/wasm/wasm-binary.cpp')
-rw-r--r--src/wasm/wasm-binary.cpp38
1 files changed, 11 insertions, 27 deletions
diff --git a/src/wasm/wasm-binary.cpp b/src/wasm/wasm-binary.cpp
index 64464fde5..e067ae104 100644
--- a/src/wasm/wasm-binary.cpp
+++ b/src/wasm/wasm-binary.cpp
@@ -2334,7 +2334,6 @@ void WasmBinaryBuilder::readImports() {
throwError("Tables may not be 64-bit");
}
- tableImports.push_back(table.get());
wasm.addTable(std::move(table));
break;
}
@@ -2986,9 +2985,6 @@ void WasmBinaryBuilder::processNames() {
for (auto& global : globals) {
wasm.addGlobal(std::move(global));
}
- for (auto& table : tables) {
- wasm.addTable(std::move(table));
- }
for (auto& segment : elementSegments) {
wasm.addElementSegment(std::move(segment));
}
@@ -3115,7 +3111,7 @@ void WasmBinaryBuilder::readTableDeclarations() {
throwError("Tables may not be 64-bit");
}
- tables.push_back(std::move(table));
+ wasm.addTable(std::move(table));
}
}
@@ -3154,17 +3150,10 @@ void WasmBinaryBuilder::readElementSegments() {
tableIdx = getU32LEB();
}
- Table* table = nullptr;
- auto numTableImports = tableImports.size();
- if (tableIdx < numTableImports) {
- table = tableImports[tableIdx];
- } else if (tableIdx - numTableImports < tables.size()) {
- table = tables[tableIdx - numTableImports].get();
- }
- if (!table) {
+ if (tableIdx >= wasm.tables.size()) {
throwError("Table index out of range.");
}
-
+ auto* table = wasm.tables[tableIdx].get();
segment->table = table->name;
segment->offset = readExpression();
}
@@ -3367,20 +3356,15 @@ void WasmBinaryBuilder::readNames(size_t payloadLen) {
auto index = getU32LEB();
auto rawName = getInlineString();
auto name = processor.process(rawName);
- auto numTableImports = tableImports.size();
- auto setTableName = [&](Table* table) {
+
+ if (index < wasm.tables.size()) {
+ auto* table = wasm.tables[index].get();
for (auto& segment : elementSegments) {
if (segment->table == table->name) {
segment->table = name;
}
}
table->setExplicitName(name);
- };
-
- if (index < numTableImports) {
- setTableName(tableImports[index]);
- } else if (index - numTableImports < tables.size()) {
- setTableName(tables[index - numTableImports].get());
} else {
std::cerr << "warning: table index out of bounds in name section, "
"table subsection: "
@@ -5226,7 +5210,7 @@ bool WasmBinaryBuilder::maybeVisitTableSize(Expression*& out, uint32_t code) {
return false;
}
Index tableIdx = getU32LEB();
- if (tableIdx >= tables.size()) {
+ if (tableIdx >= wasm.tables.size()) {
throwError("bad table index");
}
auto* curr = allocator.alloc<TableSize>();
@@ -5242,7 +5226,7 @@ bool WasmBinaryBuilder::maybeVisitTableGrow(Expression*& out, uint32_t code) {
return false;
}
Index tableIdx = getU32LEB();
- if (tableIdx >= tables.size()) {
+ if (tableIdx >= wasm.tables.size()) {
throwError("bad table index");
}
auto* curr = allocator.alloc<TableGrow>();
@@ -6634,11 +6618,11 @@ void WasmBinaryBuilder::visitRefEq(RefEq* curr) {
void WasmBinaryBuilder::visitTableGet(TableGet* curr) {
BYN_TRACE("zz node: TableGet\n");
Index tableIdx = getU32LEB();
- if (tableIdx >= tables.size()) {
+ if (tableIdx >= wasm.tables.size()) {
throwError("bad table index");
}
curr->index = popNonVoidExpression();
- curr->type = tables[tableIdx]->type;
+ curr->type = wasm.tables[tableIdx]->type;
curr->finalize();
// Defer setting the table name for later, when we know it.
tableRefs[tableIdx].push_back(&curr->table);
@@ -6647,7 +6631,7 @@ void WasmBinaryBuilder::visitTableGet(TableGet* curr) {
void WasmBinaryBuilder::visitTableSet(TableSet* curr) {
BYN_TRACE("zz node: TableSet\n");
Index tableIdx = getU32LEB();
- if (tableIdx >= tables.size()) {
+ if (tableIdx >= wasm.tables.size()) {
throwError("bad table index");
}
curr->value = popNonVoidExpression();