@ -28,7 +28,8 @@ using XamlX.IL.Cecil;
namespace Avalonia.Markup.Xaml.XamlIl
{
#if !RUNTIME_XAML_CECIL
[RequiresUnreferencedCode(XamlX.TrimmingMessages.DynamicXamlReference)]
[RequiresUnreferencedCode(XamlX.TrimmingMessages.Sre)]
[RequiresDynamicCode(XamlX.TrimmingMessages.Sre)]
#endif
internal static class AvaloniaXamlIlRuntimeCompiler
{
@ -41,30 +42,8 @@ namespace Avalonia.Markup.Xaml.XamlIl
private static XamlLanguageEmitMappings < IXamlILEmitter , XamlILNodeEmitResult > ? _ sreEmitMappings ;
private static XamlXmlnsMappings ? _ sreXmlns ;
private static AssemblyBuilder ? _ sreAsm ;
private static bool _ sreCanSave ;
[UnconditionalSuppressMessage("Trimming", "IL2026", Justification = XamlX.TrimmingMessages.CanBeSafelyTrimmed)]
public static void DumpRuntimeCompilationResults ( )
{
if ( _ sreBuilder = = null )
return ;
var saveMethod = _ sreAsm ! . GetType ( ) . GetMethods ( )
. FirstOrDefault ( m = > m . Name = = "Save" & & m . GetParameters ( ) . Length = = 1 ) ;
if ( saveMethod = = null )
return ;
try
{
_ sreBuilder . CreateGlobalFunctions ( ) ;
saveMethod . Invoke ( _ sreAsm , new Object [ ] { "XamlIlLoader.ildump" } ) ;
}
catch
{
//Ignore
}
}
[CompilerDynamicDependencies]
[UnconditionalSuppressMessage("Trimming", "IL2072", Justification = XamlX.TrimmingMessages.GeneratedTypes)]
[MemberNotNull(nameof(_sreTypeSystem))]
[MemberNotNull(nameof(_sreBuilder))]
[MemberNotNull(nameof(_sreMappings))]
@ -74,7 +53,7 @@ namespace Avalonia.Markup.Xaml.XamlIl
[MemberNotNull(nameof(_ignoresAccessChecksFromAttribute))]
static void InitializeSre ( )
{
// SRE backend doesn't load assemblies, unless they are already in the memory.
// SRE backend doesn't load assemblies unless they are already in the memory.
// At the very least, we should make sure that assemblies necessary for `AvaloniaXamlIlWellKnownTypes` are loaded.
// Root `Avalonia.Controls`.
GC . KeepAlive ( typeof ( Avalonia . Controls . Control ) ) ;
@ -87,26 +66,10 @@ namespace Avalonia.Markup.Xaml.XamlIl
_ sreTypeSystem = new SreTypeSystem ( ) ;
if ( _ sreBuilder = = null )
{
_ sreCanSave = ! ( RuntimeInformation . FrameworkDescription . StartsWith ( ".NET Core" ) ) ;
var name = new AssemblyName ( Guid . NewGuid ( ) . ToString ( "N" ) ) ;
if ( _ sreCanSave )
{
var define = GetDefineDynamicAssembly ( ) ;
if ( define ! = null )
_ sreAsm = ( AssemblyBuilder ) define . Invoke ( AppDomain . CurrentDomain , new object [ ]
{
name , ( AssemblyBuilderAccess ) 3 ,
Path . GetDirectoryName ( typeof ( AvaloniaXamlIlRuntimeCompiler ) . Assembly . GetModules ( ) [ 0 ]
. FullyQualifiedName ) !
} ) ! ;
else
_ sreCanSave = false ;
}
if ( _ sreAsm = = null )
_ sreAsm = AssemblyBuilder . DefineDynamicAssembly ( name ,
AssemblyBuilderAccess . RunAndCollect ) ;
_ sreAsm ? ? = AssemblyBuilder . DefineDynamicAssembly ( name , AssemblyBuilderAccess . RunAndCollect ) ;
_ sreBuilder = _ sreAsm . DefineDynamicModule ( "XamlIlLoader.ildump" ) ;
}
@ -123,12 +86,6 @@ namespace Avalonia.Markup.Xaml.XamlIl
_ ignoresAccessChecksFromAttribute = EmitIgnoresAccessCheckAttributeDefinition ( _ sreBuilder ) ;
}
[UnconditionalSuppressMessage("Trimming", "IL2026", Justification = XamlX.TrimmingMessages.CanBeSafelyTrimmed)]
static MethodInfo ? GetDefineDynamicAssembly ( ) = > AppDomain . CurrentDomain . GetType ( ) . GetMethods ( )
. FirstOrDefault ( m = > m . Name = = "DefineDynamicAssembly"
& & m . GetParameters ( ) . Length = = 3 & &
m . GetParameters ( ) [ 2 ] . ParameterType = = typeof ( string ) ) ;
static Type EmitIgnoresAccessCheckAttributeDefinition ( ModuleBuilder builder )
{
var tb = builder . DefineType ( "System.Runtime.CompilerServices.IgnoresAccessChecksToAttribute" ,
@ -237,37 +194,15 @@ namespace Avalonia.Markup.Xaml.XamlIl
return result ;
}
static object LoadSre ( RuntimeXamlLoaderDocument document , RuntimeXamlLoaderConfiguration configuration )
public static object LoadSre ( RuntimeXamlLoaderDocument document , RuntimeXamlLoaderConfiguration configuration )
{
var success = false ;
try
{
var rv = LoadSreCore ( document , configuration ) ;
success = true ;
return rv ;
}
finally
{
if ( ! success & & _ sreCanSave )
DumpRuntimeCompilationResults ( ) ;
}
return LoadSreCore ( document , configuration ) ;
}
static IReadOnlyList < object > LoadGroupSre ( IReadOnlyCollection < RuntimeXamlLoaderDocument > documents ,
public static IReadOnlyList < object > LoadGroupSre ( IReadOnlyCollection < RuntimeXamlLoaderDocument > documents ,
RuntimeXamlLoaderConfiguration configuration )
{
var success = false ;
try
{
var rv = LoadGroupSreCore ( documents , configuration ) ;
success = true ;
return rv ;
}
finally
{
if ( ! success & & _ sreCanSave )
DumpRuntimeCompilationResults ( ) ;
}
return LoadGroupSreCore ( documents , configuration ) ;
}
[UnconditionalSuppressMessage("Trimming", "IL2072", Justification = XamlX.TrimmingMessages.GeneratedTypes)]