Skip to content

Commit

Permalink
Merge pull request #559 from ElektroKill/sort-exported-type-table
Browse files Browse the repository at this point in the history
Sort `ExportedType` table when writing to comply with new ECMA Augments
  • Loading branch information
wtfsck authored May 30, 2024
2 parents 006c640 + 080d759 commit 1e0ec26
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions src/DotNet/Writer/Metadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1811,9 +1811,39 @@ void InitializeVTableFixups() {
void AddExportedTypes() {
using var _ = errorContext.SetSource("exported types");
var exportedTypes = module.ExportedTypes;
int count = exportedTypes.Count;

var nestedTypeDict = new Dictionary<ExportedType, List<ExportedType>>();
for (var i = 0; i < exportedTypes.Count; i++) {
var exportedType = exportedTypes[i];
if (exportedType.Implementation is not ExportedType decl)
continue;
if (!nestedTypeDict.TryGetValue(decl, out var nestedTypes))
nestedTypes = nestedTypeDict[decl] = new List<ExportedType>();
nestedTypes.Add(exportedType);
}

var sortedTypes = new List<ExportedType>(exportedTypes.Count);
var visited = new Dictionary<ExportedType, bool>();
var stack = new Stack<IEnumerator<ExportedType>>();
stack.Push(exportedTypes.GetEnumerator());
while (stack.Count > 0) {
var enumerator = stack.Pop();
while (enumerator.MoveNext()) {
var type = enumerator.Current;
if (visited.ContainsKey(type))
continue;
visited[type] = true;
sortedTypes.Add(type);
if (nestedTypeDict.TryGetValue(type, out var nested) && nested.Count > 0) {
stack.Push(enumerator);
enumerator = nested.GetEnumerator();
}
}
}

int count = sortedTypes.Count;
for (int i = 0; i < count; i++)
AddExportedType(exportedTypes[i]);
AddExportedType(sortedTypes[i]);
}

/// <summary>
Expand Down

0 comments on commit 1e0ec26

Please # to comment.