Skip to content

Commit

Permalink
Merge pull request #133 from puff/fix/enum-marshalling
Browse files Browse the repository at this point in the history
Fix marshalling enums in CliMarshaller
  • Loading branch information
Washi1337 committed Jul 19, 2023
2 parents 4d324ef + 5af7636 commit ac3fb60
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,21 @@ private TypeSignature GetElementType(TypeSignature type)
{
switch (type.ElementType)
{
case ElementType.Enum:
case ElementType.ValueType:
if (!_resolvedTypes.TryGetValue(type, out var definition))
{
definition = type.Resolve();
if (definition is not null)
_resolvedTypes.Add(type, definition);
}

if (definition?.GetEnumUnderlyingType() is not { } enumUnderlyingType)
if (definition is null || !definition.IsEnum)
return type;

if (definition.GetEnumUnderlyingType() is not { } enumUnderlyingType)
throw new CilEmulatorException($"Could not resolve enum type {type.FullName}.");

return GetElementType(enumUnderlyingType);
return enumUnderlyingType;

default:
return type;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.Linq;
using Echo.Memory;
using Echo.Platforms.AsmResolver.Emulation;
using Echo.Platforms.AsmResolver.Emulation.Stack;
using Echo.Platforms.AsmResolver.Tests.Mock;
using Mocks;
using Xunit;

namespace Echo.Platforms.AsmResolver.Tests.Emulation;

public class CliMarshallerTest : IClassFixture<MockModuleFixture>
{
private readonly MockModuleFixture _fixture;
private readonly ValueFactory _factory;
private readonly CliMarshaller _marshaller;

public CliMarshallerTest(MockModuleFixture fixture)
{
_fixture = fixture;
_factory = new ValueFactory(_fixture.MockModule, false);
_marshaller = new CliMarshaller(_factory);
}

[Fact]
public void MarshalEnumToStackShouldMarshalAsInteger()
{
var int32EnumType = _fixture.MockModule.TopLevelTypes.First(x => x.Name == nameof(Int32Enum));
var marshalledEnum = _marshaller.ToCliValue(new BitVector(0x1337), int32EnumType.ToTypeSignature());

Assert.Equal(StackSlotTypeHint.Integer, marshalledEnum.TypeHint);
}
}

0 comments on commit ac3fb60

Please sign in to comment.