Skip to content

Commit 8095690

Browse files
authored
Fix type processing (#35)
1 parent 9e5b66a commit 8095690

File tree

6 files changed

+44
-24
lines changed

6 files changed

+44
-24
lines changed

source/MetadataProcessor.Core/Tables/nanoByteCodeTable.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,7 @@ public ushort GetMethodId(
8484
_methods.Add(method);
8585
_lastAvailableRva += (ushort)byteCode.Length;
8686

87-
// need to check if table already has this key (because of second pass to minimize)
88-
if (!_rvasByMethodNames.ContainsKey(method.FullName))
89-
{
90-
_rvasByMethodNames.Add(method.FullName, rva);
91-
}
87+
_rvasByMethodNames.Add(method.FullName, rva);
9288

9389
return id;
9490
}

source/MetadataProcessor.Core/Tables/nanoReferenceTableBase.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ public void RemoveUnusedItems(HashSet<MetadataToken> set)
169169
.Select((reference, index) => new { reference, index })
170170
.ToDictionary(item => item.reference, item => (ushort)item.index,
171171
_comparer);
172+
173+
_items = usedItems;
172174
}
173175
}
174176
}

source/MetadataProcessor.Core/Tables/nanoTablesContext.cs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ public nanoTablesContext(
6565
List<string> classNamesToExclude,
6666
ICustomStringSorter stringSorter,
6767
bool applyAttributesCompression,
68-
bool verbose)
68+
bool verbose,
69+
bool isCoreLibrary)
6970
{
7071
AssemblyDefinition = assemblyDefinition;
7172

@@ -122,6 +123,34 @@ public nanoTablesContext(
122123

123124
var types = GetOrderedTypes(mainModule, explicitTypesOrder);
124125

126+
// mscorlib requires a particular type order
127+
if(isCoreLibrary)
128+
{
129+
var indexOfSystemObject = types.IndexOf(types.FirstOrDefault(t => t.FullName == "System.Object"));
130+
if (indexOfSystemObject >= 0)
131+
{
132+
TypeDefinition item = types[indexOfSystemObject];
133+
types.RemoveAt(indexOfSystemObject);
134+
types.Insert(0, item);
135+
}
136+
137+
var indexOfSystemValueType = types.IndexOf(types.FirstOrDefault(t => t.FullName == "System.ValueType"));
138+
if (indexOfSystemValueType >= 0)
139+
{
140+
TypeDefinition item = types[indexOfSystemValueType];
141+
types.RemoveAt(indexOfSystemValueType);
142+
types.Insert(1, item);
143+
}
144+
145+
var indexOfSystemException = types.IndexOf(types.FirstOrDefault(t => t.FullName == "System.Exception1"));
146+
if (indexOfSystemException >= 0)
147+
{
148+
TypeDefinition item = types[indexOfSystemException];
149+
types.RemoveAt(indexOfSystemException);
150+
types.Insert(2, item);
151+
}
152+
}
153+
125154
TypeDefinitionTable = new nanoTypeDefinitionTable(types, this);
126155

127156
var fields = types

source/MetadataProcessor.Core/Tables/nanoTypeDefinitionTable.cs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,7 @@ protected override void WriteSingleItem(
123123
.PreProcessMethod(method, _context.ByteCodeTable.FakeStringTable)
124124
.ToList();
125125

126-
// need to check if table already has this key (because of second pass to minimize)
127-
if (!_byteCodeOffsets.ContainsKey(method.MetadataToken.ToUInt32()))
128-
{
129-
_byteCodeOffsets.Add(method.MetadataToken.ToUInt32(), offsets);
130-
}
126+
_byteCodeOffsets.Add(method.MetadataToken.ToUInt32(), offsets);
131127
}
132128
}
133129
foreach (var nestedType in item.NestedTypes)
@@ -138,11 +134,7 @@ protected override void WriteSingleItem(
138134
.PreProcessMethod(method, _context.ByteCodeTable.FakeStringTable)
139135
.ToList();
140136

141-
// need to check if table already has this key (because of second pass to minimize)
142-
if (!_byteCodeOffsets.ContainsKey(method.MetadataToken.ToUInt32()))
143-
{
144-
_byteCodeOffsets.Add(method.MetadataToken.ToUInt32(), offsets);
145-
}
137+
_byteCodeOffsets.Add(method.MetadataToken.ToUInt32(), offsets);
146138
}
147139
}
148140

@@ -160,27 +152,27 @@ private void WriteClassFields(
160152
IList<FieldDefinition> fieldsList,
161153
nanoBinaryWriter writer)
162154
{
163-
var firstStaticFieldId = _context.FieldsTable.MaxFieldId;
155+
ushort firstStaticFieldId = 0;
164156
var staticFieldsCount = 0;
165157
foreach (var field in fieldsList.Where(item => item.IsStatic && !item.IsLiteral))
166158
{
167159
ushort fieldReferenceId;
168160
_context.FieldsTable.TryGetFieldReferenceId(field, true, out fieldReferenceId);
169-
firstStaticFieldId = Math.Min(firstStaticFieldId, fieldReferenceId);
161+
firstStaticFieldId = Math.Min(_context.FieldsTable.MaxFieldId, fieldReferenceId);
170162

171163
_context.SignaturesTable.GetOrCreateSignatureId(field);
172164
_context.StringTable.GetOrCreateStringId(field.Name);
173165

174166
++staticFieldsCount;
175167
}
176168

177-
var firstInstanceFieldId = _context.FieldsTable.MaxFieldId;
169+
ushort firstInstanceFieldId = 0;
178170
var instanceFieldsCount = 0;
179171
foreach (var field in fieldsList.Where(item => !item.IsStatic && !item.IsLiteral))
180172
{
181173
ushort fieldReferenceId;
182174
_context.FieldsTable.TryGetFieldReferenceId(field, true, out fieldReferenceId);
183-
firstInstanceFieldId = Math.Min(firstInstanceFieldId, fieldReferenceId);
175+
firstInstanceFieldId = Math.Min(_context.FieldsTable.MaxFieldId, fieldReferenceId);
184176

185177
_context.SignaturesTable.GetOrCreateSignatureId(field);
186178
_context.StringTable.GetOrCreateStringId(field.Name);

source/MetadataProcessor.Core/nanoAssemblyBuilder.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ public nanoAssemblyBuilder(
5151
classNamesToExclude,
5252
stringSorter,
5353
applyAttributesCompression,
54-
verbose);
54+
verbose,
55+
isCoreLibrary);
5556

5657
_verbose = verbose;
5758
_isCoreLibrary = isCoreLibrary;
@@ -361,7 +362,7 @@ private HashSet<MetadataToken> BuildDependencyList(MetadataToken token)
361362
// attributes
362363
foreach (var c in fd.CustomAttributes)
363364
{
364-
if (!_tablesContext.ClassNamesToExclude.Contains(c.AttributeType.Name))
365+
if (!_tablesContext.ClassNamesToExclude.Contains(c.AttributeType.FullName))
365366
{
366367
set.Add(c.Constructor.MetadataToken);
367368
}
@@ -438,7 +439,7 @@ i.Operand is TypeDefinition ||
438439
// attributes
439440
foreach (var c in md.CustomAttributes)
440441
{
441-
if (!_tablesContext.ClassNamesToExclude.Contains(c.AttributeType.Name))
442+
if (!_tablesContext.ClassNamesToExclude.Contains(c.AttributeType.FullName))
442443
{
443444
set.Add(c.Constructor.MetadataToken);
444445
}

source/MetadataProcessor.Core/nanoSkeletonGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ private void GenerateStubs()
100100
private void GenerateAssemblyLookup()
101101
{
102102
// grab native version from assembly attribute
103-
var nativeVersionAttribute = _tablesContext.AssemblyDefinition.CustomAttributes.FirstOrDefault(a => a?.AttributeType.Name == "AssemblyNativeVersionAttribute");
103+
var nativeVersionAttribute = _tablesContext.AssemblyDefinition.CustomAttributes.FirstOrDefault(a => a?.AttributeType?.Name == "AssemblyNativeVersionAttribute");
104104
Version nativeVersion = new Version((string)nativeVersionAttribute.ConstructorArguments[0].Value);
105105

106106
var assemblyLookup = new AssemblyLookupTable()

0 commit comments

Comments
 (0)