Skip to content

Commit

Permalink
BUGFIX: Instantiate declaring type of .cctor when necessary.
Browse files Browse the repository at this point in the history
  • Loading branch information
Washi1337 committed May 26, 2024
1 parent 52ea03e commit e57f8e3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using AsmResolver.DotNet;
using AsmResolver.DotNet.Signatures;
using AsmResolver.PE.DotNet.Cil;

namespace Echo.Platforms.AsmResolver.Emulation.Dispatch.ObjectModel;
Expand All @@ -16,7 +17,9 @@ public CilDispatchResult Dispatch(CilExecutionContext context, CilInstruction in
// Ensure the enclosing type is initialized in the runtime.
if (field.DeclaringType is { } declaringType)
{
var initResult = context.Machine.TypeManager.HandleInitialization(context.Thread, declaringType);
var genericContext = GenericContext.FromMember(context.CurrentFrame.Method);
var instantiated = declaringType.ToTypeSignature().InstantiateGenericTypes(genericContext);
var initResult = context.Machine.TypeManager.HandleInitialization(context.Thread, instantiated);
if (!initResult.IsNoAction)
return initResult.ToDispatchResult();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,18 +104,29 @@ public TypeInitializerResult HandleInitialization(CilThread thread, ITypeDescrip
initialization.Exception = _machine.Heap
.AllocateObject(_machine.ValueFactory.TypeInitializationExceptionType, true)
.AsObjectHandle(_machine);

return TypeInitializerResult.Exception(initialization.Exception);
}

// "Call" the constructor.
initialization.ConstructorCalled = true;

// Actually find the constructor and call it if it is there.
var cctor = definition.GetStaticConstructor();
if (cctor is not null)
{
thread.CallStack.Push(cctor);
var result = (IMethodDescriptor) cctor;

// Instantiate any args in the declaring type.
var context = GenericContext.FromType(type);
if (type.ToTypeSignature() is {} typeSignature)
{
var newType = typeSignature.InstantiateGenericTypes(context);
if (newType != typeSignature)
result = newType.ToTypeDefOrRef().CreateMemberReference(cctor.Name!, cctor.Signature!);
}

thread.CallStack.Push(result);
return TypeInitializerResult.Redirected();
}

Expand Down

0 comments on commit e57f8e3

Please sign in to comment.