Skip to content

Commit

Permalink
(RSRP-499137) ImportLogic: skip special treatment for static struct f…
Browse files Browse the repository at this point in the history
…ields
  • Loading branch information
ForNeVeR committed Nov 1, 2024
1 parent ed9356a commit 136dda8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
23 changes: 14 additions & 9 deletions src/Refasmer/Importer/ImportLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,43 +41,48 @@ private TypeDefinitionHandle ImportTypeDefinitionSkeleton( TypeDefinitionHandle
Import(src.BaseType), NextFieldHandle(), NextMethodHandle());

Trace?.Invoke($"Imported {_reader.ToString(src)} -> {RowId(dstHandle):X}");

using var _ = WithLogPrefix($"[{_reader.ToString(src)}]");

var isValueType = _reader.GetFullname(src.BaseType) == "System::ValueType";
var forcePreservePrivateFields = isValueType && !Filter.OmitNonApiMembers;

List<FieldDefinition> importedFields = null;
List<FieldDefinition> skippedFields = null;
List<FieldDefinition> importedInstanceFields = null;
List<FieldDefinition> skippedInstanceFields = null;

if (forcePreservePrivateFields)
Trace?.Invoke($"{_reader.ToString(src)} is ValueType, all fields should be imported");
else
{
importedFields = [];
skippedFields = [];
importedInstanceFields = [];
skippedInstanceFields = [];
}

foreach (var srcFieldHandle in src.GetFields())
{
var srcField = _reader.GetFieldDefinition(srcFieldHandle);
var isStatic = (srcField.Attributes & FieldAttributes.Static) != 0;
var isForcedToInclude = forcePreservePrivateFields && !isStatic;

if (!forcePreservePrivateFields && Filter?.AllowImport(srcField, _reader) == false)
if (!isForcedToInclude && Filter?.AllowImport(srcField, _reader) == false)
{
Trace?.Invoke($"Not imported {_reader.ToString(srcField)}");
skippedFields?.Add(srcField);
if (!isStatic)
skippedInstanceFields?.Add(srcField);

continue;
}

var dstFieldHandle = _builder.AddFieldDefinition(srcField.Attributes, ImportValue(srcField.Name),
ImportSignatureWithHeader(srcField.Signature));
_fieldDefinitionCache.Add(srcFieldHandle, dstFieldHandle);
Trace?.Invoke($"Imported {_reader.ToString(srcFieldHandle)} -> {RowId(dstFieldHandle):X}");
importedFields?.Add(srcField);
if (!isStatic)
importedInstanceFields?.Add(srcField);
}

if (!forcePreservePrivateFields)
PostProcessSkippedValueTypeFields(skippedFields, importedFields);
PostProcessSkippedValueTypeFields(skippedInstanceFields, importedInstanceFields);

var implementations = src.GetMethodImplementations()
.Select(_reader.GetMethodImplementation)
Expand Down
3 changes: 2 additions & 1 deletion tests/Refasmer.Tests/IntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public async Task CheckRefasmedType(string typeName)
[TestCase("RefasmerTestAssembly.BlittableStructWithPrivateFields")]
[TestCase("RefasmerTestAssembly.NonBlittableStructWithPrivateFields")]
[TestCase("RefasmerTestAssembly.NonBlittableGraph")]

[TestCase("RefasmerTestAssembly.EmptyStructWithStaticMember")]
[TestCase("RefasmerTestAssembly.NonEmptyStructWithStaticMember")]
public async Task CheckRefasmedTypeOmitNonApi(string typeName)
{
var assemblyPath = await BuildTestAssembly();
Expand Down

0 comments on commit 136dda8

Please sign in to comment.