Skip to content

Commit 622a0ef

Browse files
authored
Fix tag name section writing (#8431)
It is not guaranteed that impored tags come first in the `Module::tags` array, but the current code assumes that. This PR changes its printing so it is the same format with other module items, which calls `ModuleUtils::iterImported***` first and then `ModuleUtils::iterDefined***`. It is hard to add a wast test for this, because wast files mandate `import`s come before defined items.
1 parent 694f7e8 commit 622a0ef

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

src/wasm/wasm-binary.cpp

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,24 +1187,25 @@ void WasmBinaryWriter::writeNames() {
11871187
}
11881188

11891189
// tag names
1190-
if (!wasm->tags.empty()) {
1191-
Index count = 0;
1192-
for (auto& tag : wasm->tags) {
1193-
if (tag->hasExplicitName) {
1194-
count++;
1190+
{
1191+
std::vector<std::pair<Index, Tag*>> tagsWithNames;
1192+
Index checked = 0;
1193+
auto check = [&](Tag* curr) {
1194+
if (curr->hasExplicitName) {
1195+
tagsWithNames.push_back({checked, curr});
11951196
}
1196-
}
1197-
1198-
if (count) {
1197+
checked++;
1198+
};
1199+
ModuleUtils::iterImportedTags(*wasm, check);
1200+
ModuleUtils::iterDefinedTags(*wasm, check);
1201+
assert(checked == indexes.tagIndexes.size());
1202+
if (tagsWithNames.size() > 0) {
11991203
auto substart =
12001204
startSubsection(BinaryConsts::CustomSections::Subsection::NameTag);
1201-
o << U32LEB(count);
1202-
for (Index i = 0; i < wasm->tags.size(); i++) {
1203-
auto& tag = wasm->tags[i];
1204-
if (tag->hasExplicitName) {
1205-
o << U32LEB(i);
1206-
writeEscapedName(tag->name.str);
1207-
}
1205+
o << U32LEB(tagsWithNames.size());
1206+
for (auto& [index, tag] : tagsWithNames) {
1207+
o << U32LEB(index);
1208+
writeEscapedName(tag->name.str);
12081209
}
12091210
finishSubsection(substart);
12101211
}

0 commit comments

Comments
 (0)