Skip to content

Commit fea999e

Browse files
authored
[NFC] Simplify binary reading logic for globals (#4980)
Similar to #4969 but for globals.
1 parent d5b6972 commit fea999e

File tree

2 files changed

+11
-36
lines changed

2 files changed

+11
-36
lines changed

src/wasm-binary.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1540,11 +1540,6 @@ class WasmBinaryBuilder {
15401540
// we store data here after being read from binary, before we know their names
15411541
std::vector<std::unique_ptr<DataSegment>> dataSegments;
15421542

1543-
// we store globals here before wasm.addGlobal after we know their names
1544-
std::vector<std::unique_ptr<Global>> globals;
1545-
// we store global imports here before wasm.addGlobalImport after we know
1546-
// their names
1547-
std::vector<Global*> globalImports;
15481543
// at index i we have all refs to the global i
15491544
std::map<Index, std::vector<Name*>> globalRefs;
15501545

src/wasm/wasm-binary.cpp

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2362,7 +2362,6 @@ void WasmBinaryBuilder::readImports() {
23622362
mutable_ ? Builder::Mutable : Builder::Immutable);
23632363
curr->module = module;
23642364
curr->base = base;
2365-
globalImports.push_back(curr.get());
23662365
wasm.addGlobal(std::move(curr));
23672366
break;
23682367
}
@@ -2771,7 +2770,7 @@ void WasmBinaryBuilder::readGlobals() {
27712770
throwError("Global mutability must be 0 or 1");
27722771
}
27732772
auto* init = readExpression();
2774-
globals.push_back(
2773+
wasm.addGlobal(
27752774
Builder::makeGlobal("global$" + std::to_string(i),
27762775
type,
27772776
init,
@@ -2982,9 +2981,6 @@ void WasmBinaryBuilder::validateBinary() {
29822981
}
29832982

29842983
void WasmBinaryBuilder::processNames() {
2985-
for (auto& global : globals) {
2986-
wasm.addGlobal(std::move(global));
2987-
}
29882984
for (auto& segment : elementSegments) {
29892985
wasm.addElementSegment(std::move(segment));
29902986
}
@@ -3431,11 +3427,8 @@ void WasmBinaryBuilder::readNames(size_t payloadLen) {
34313427
auto index = getU32LEB();
34323428
auto rawName = getInlineString();
34333429
auto name = processor.process(rawName);
3434-
auto numGlobalImports = globalImports.size();
3435-
if (index < numGlobalImports) {
3436-
globalImports[index]->setExplicitName(name);
3437-
} else if (index - numGlobalImports < globals.size()) {
3438-
globals[index - numGlobalImports]->setExplicitName(name);
3430+
if (index < wasm.globals.size()) {
3431+
wasm.globals[index]->setExplicitName(name);
34393432
} else {
34403433
std::cerr << "warning: global index out of bounds in name section, "
34413434
"global subsection: "
@@ -4345,35 +4338,22 @@ void WasmBinaryBuilder::visitLocalSet(LocalSet* curr, uint8_t code) {
43454338
void WasmBinaryBuilder::visitGlobalGet(GlobalGet* curr) {
43464339
BYN_TRACE("zz node: GlobalGet " << pos << std::endl);
43474340
auto index = getU32LEB();
4348-
if (index < globalImports.size()) {
4349-
auto* import = globalImports[index];
4350-
curr->name = import->name;
4351-
curr->type = import->type;
4352-
} else {
4353-
Index adjustedIndex = index - globalImports.size();
4354-
if (adjustedIndex >= globals.size()) {
4355-
throwError("invalid global index");
4356-
}
4357-
auto& glob = globals[adjustedIndex];
4358-
curr->name = glob->name;
4359-
curr->type = glob->type;
4341+
if (index >= wasm.globals.size()) {
4342+
throwError("invalid global index");
43604343
}
4344+
auto* global = wasm.globals[index].get();
4345+
curr->name = global->name;
4346+
curr->type = global->type;
43614347
globalRefs[index].push_back(&curr->name); // we don't know the final name yet
43624348
}
43634349

43644350
void WasmBinaryBuilder::visitGlobalSet(GlobalSet* curr) {
43654351
BYN_TRACE("zz node: GlobalSet\n");
43664352
auto index = getU32LEB();
4367-
if (index < globalImports.size()) {
4368-
auto* import = globalImports[index];
4369-
curr->name = import->name;
4370-
} else {
4371-
Index adjustedIndex = index - globalImports.size();
4372-
if (adjustedIndex >= globals.size()) {
4373-
throwError("invalid global index");
4374-
}
4375-
curr->name = globals[adjustedIndex]->name;
4353+
if (index >= wasm.globals.size()) {
4354+
throwError("invalid global index");
43764355
}
4356+
curr->name = wasm.globals[index]->name;
43774357
curr->value = popNonVoidExpression();
43784358
globalRefs[index].push_back(&curr->name); // we don't know the final name yet
43794359
curr->finalize();

0 commit comments

Comments
 (0)