Skip to content

Commit

Permalink
Add static method and field integration test.
Browse files Browse the repository at this point in the history
  • Loading branch information
Washi1337 committed Mar 20, 2024
1 parent 75d449a commit ce8ddfe
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ private bool UnwindCallStack(ref ObjectHandle exceptionObject)

// If the exception happened in a .cctor, register it and wrap it in a type initialization error.
if (currentFrame.Body?.Owner is { IsConstructor: true, IsStatic: true, DeclaringType: {} type })
exceptionObject = Machine.TypeManager.RegisterTypeInitializationException(type, exceptionObject);
exceptionObject = Machine.TypeManager.RegisterInitializationException(type, exceptionObject);

var result = currentFrame.ExceptionHandlerStack.RegisterException(exceptionObject);
if (result.IsSuccess)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private TypeInitialization GetInitialization(ITypeDescriptor type)
/// <param name="type">The type that failed to initialize.</param>
/// <param name="innerException">The exception object that describes the failure.</param>
/// <returns>The resulting TypeInitializationException instance.</returns>
public ObjectHandle RegisterTypeInitializationException(ITypeDescriptor type, ObjectHandle innerException)
public ObjectHandle RegisterInitializationException(ITypeDescriptor type, ObjectHandle innerException)
{
var initialization = GetInitialization(type);
if (!initialization.Exception.IsNull)
Expand All @@ -62,7 +62,7 @@ public ObjectHandle RegisterTypeInitializationException(ITypeDescriptor type, Ob
.AsObjectHandle(_machine);
}

// TODO: incorporate `exceptionObject`.
// TODO: incorporate `innerException`.
}

return initialization.Exception;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Echo.Platforms.AsmResolver.Emulation.Invocation;
using Echo.Platforms.AsmResolver.Tests.Emulation.Dispatch;
using Echo.Platforms.AsmResolver.Tests.Mock;
using Mocks;
using Xunit;
using Xunit.Abstractions;
using Xunit.Sdk;
Expand Down Expand Up @@ -587,5 +588,23 @@ public void CallAndThrowUnhandledException(string methodName, object parameter)
Assert.Equal(expectedException.GetType().FullName, result.ExceptionObject.GetObjectType().FullName);
}

[Fact]
public void StaticFieldWithInitialValueUpdate()
{
var type = _fixture.MockModule.LookupMember<TypeDefinition>(typeof(ClassWithInitializer).MetadataToken);
var increment = type.Methods.First(m => m.Name == nameof(ClassWithInitializer.IncrementCounter));
var counter = type.Fields.First(f => f.Name == nameof(ClassWithInitializer.Counter));

// Verify uninitialized value.
Assert.Equal(0, _vm.StaticFields.GetFieldSpan(counter).I32);

// Call and verify value.
_mainThread.Call(increment, Array.Empty<BitVector>());
Assert.Equal(1337 + 1, _vm.StaticFields.GetFieldSpan(counter).I32);
_mainThread.Call(increment, Array.Empty<BitVector>());
Assert.Equal(1337 + 2, _vm.StaticFields.GetFieldSpan(counter).I32);
_mainThread.Call(increment, Array.Empty<BitVector>());
Assert.Equal(1337 + 3, _vm.StaticFields.GetFieldSpan(counter).I32);
}
}
}
3 changes: 3 additions & 0 deletions test/Platforms/Mocks/ClassWithInitializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ namespace Mocks;
public class ClassWithInitializer
{
public static string Field = "Test";
public static int Counter = 1337;

public static string MethodNoFieldAccess() => "MethodNoFieldAccess";

public static string MethodFieldAccess() => "MethodFieldAccess: " + Field;

public static void IncrementCounter() => Counter++;
}

0 comments on commit ce8ddfe

Please sign in to comment.