Skip to content

Commit

Permalink
Core: Support "v29.2"'s isUnmanagedCallersOnly the *correct* way
Browse files Browse the repository at this point in the history
  • Loading branch information
SamboyCoding committed Aug 31, 2024
1 parent 6d5d326 commit 11302ac
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Cpp2IL.Core/Model/Contexts/SystemTypesContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class SystemTypesContext
public TypeAnalysisContext SystemTypedReferenceType { get; }
public TypeAnalysisContext SystemTypeType { get; }
public TypeAnalysisContext SystemAttributeType { get; }
public TypeAnalysisContext? UnmanagedCallersOnlyAttributeType { get; }

public SystemTypesContext(ApplicationAnalysisContext appContext)
{
Expand Down Expand Up @@ -62,5 +63,7 @@ public SystemTypesContext(ApplicationAnalysisContext appContext)

SystemExceptionType = systemAssembly.GetTypeByFullName("System.Exception")!;
SystemAttributeType = systemAssembly.GetTypeByFullName("System.Attribute")!;

UnmanagedCallersOnlyAttributeType = systemAssembly.GetTypeByFullName("System.Runtime.InteropServices.UnmanagedCallersOnlyAttribute");
}
}
11 changes: 10 additions & 1 deletion Cpp2IL.Core/Utils/AsmResolver/AsmResolverAssemblyPopulator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,16 @@ private static void CopyMethodsInType(ReferenceImporter importer, TypeAnalysisCo
var managedMethod = new MethodDefinition(methodCtx.MethodName, (MethodAttributes)methodCtx.Attributes, signature);

if (methodCtx.Definition != null)
managedMethod.ImplAttributes = (MethodImplAttributes)methodCtx.Definition.iflags;
{
managedMethod.ImplAttributes = (MethodImplAttributes)methodCtx.Definition.MethodImplAttributes;
if (methodCtx.Definition.IsUnmanagedCallersOnly && typeContext.AppContext.SystemTypes.UnmanagedCallersOnlyAttributeType != null)
{
var unmanagedCallersOnlyType = typeContext.AppContext.SystemTypes.UnmanagedCallersOnlyAttributeType.GetExtraData<TypeDefinition>("AsmResolverType");
if(unmanagedCallersOnlyType != null)
managedMethod.CustomAttributes.Add(new CustomAttribute((ICustomAttributeType)importer.ImportMethod(unmanagedCallersOnlyType.GetConstructor()!), new()));
}

}

//Add parameter definitions if we have them so we get names, defaults, out params, etc
foreach (var parameterDefinition in parameterDefinitions)
Expand Down

0 comments on commit 11302ac

Please sign in to comment.