From 7639ebebae746782ac2b8f2472e551ee5105a2fb Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Wed, 22 Dec 2021 16:59:51 -0600 Subject: [PATCH] Add feature switch to enable switching compiled bindings on by default. --- packages/Avalonia/AvaloniaBuildTasks.targets | 6 ++++++ .../CompileAvaloniaXamlTask.cs | 4 +++- .../XamlCompilerTaskExecutor.cs | 13 ++++++------ .../AvaloniaXamlIlRuntimeCompiler.cs | 20 ++++++++++++------- 4 files changed, 29 insertions(+), 14 deletions(-) diff --git a/packages/Avalonia/AvaloniaBuildTasks.targets b/packages/Avalonia/AvaloniaBuildTasks.targets index d43a5c1624..d64c2c5afc 100644 --- a/packages/Avalonia/AvaloniaBuildTasks.targets +++ b/packages/Avalonia/AvaloniaBuildTasks.targets @@ -5,6 +5,7 @@ low <_AvaloniaPatchComInterop Condition="'$(_AvaloniaPatchComInterop)' == ''">false <_AvaloniaSkipXamlCompilation Condition="'$(_AvaloniaSkipXamlCompilation)' == ''">false + false @@ -20,6 +21,10 @@ + + + + !string.IsNullOrWhiteSpace(l)).ToArray(), - ProjectDirectory, OutputPath, VerifyIl, outputImportance, + ProjectDirectory, OutputPath, VerifyIl, DefaultCompileBindings, outputImportance, (SignAssembly && !DelaySign) ? AssemblyOriginatorKeyFile : null, EnableComInteropPatching, SkipXamlCompilation, DebuggerLaunch); if (!res.Success) @@ -72,6 +72,8 @@ namespace Avalonia.Build.Tasks public string OutputPath { get; set; } public bool VerifyIl { get; set; } + + public bool DefaultCompileBindings { get; set; } public bool EnableComInteropPatching { get; set; } public bool SkipXamlCompilation { get; set; } diff --git a/src/Avalonia.Build.Tasks/XamlCompilerTaskExecutor.cs b/src/Avalonia.Build.Tasks/XamlCompilerTaskExecutor.cs index 593d79471e..52b58c2318 100644 --- a/src/Avalonia.Build.Tasks/XamlCompilerTaskExecutor.cs +++ b/src/Avalonia.Build.Tasks/XamlCompilerTaskExecutor.cs @@ -39,15 +39,15 @@ namespace Avalonia.Build.Tasks public static CompileResult Compile(IBuildEngine engine, string input, string[] references, string projectDirectory, - string output, bool verifyIl, MessageImportance logImportance, string strongNameKey, bool patchCom, + string output, bool verifyIl, bool defaultCompileBindings, MessageImportance logImportance, string strongNameKey, bool patchCom, bool skipXamlCompilation) { - return Compile(engine, input, references, projectDirectory, output, verifyIl, logImportance, strongNameKey, patchCom, skipXamlCompilation, debuggerLaunch:false); + return Compile(engine, input, references, projectDirectory, output, verifyIl, defaultCompileBindings, logImportance, strongNameKey, patchCom, skipXamlCompilation, debuggerLaunch:false); } internal static CompileResult Compile(IBuildEngine engine, string input, string[] references, string projectDirectory, - string output, bool verifyIl, MessageImportance logImportance, string strongNameKey, bool patchCom, bool skipXamlCompilation, bool debuggerLaunch) + string output, bool verifyIl, bool defaultCompileBindings, MessageImportance logImportance, string strongNameKey, bool patchCom, bool skipXamlCompilation, bool debuggerLaunch) { var typeSystem = new CecilTypeSystem(references .Where(r => !r.ToLowerInvariant().EndsWith("avalonia.build.tasks.dll")) @@ -57,7 +57,7 @@ namespace Avalonia.Build.Tasks if (!skipXamlCompilation) { - var compileRes = CompileCore(engine, typeSystem, projectDirectory, verifyIl, logImportance, debuggerLaunch); + var compileRes = CompileCore(engine, typeSystem, projectDirectory, verifyIl, defaultCompileBindings, logImportance, debuggerLaunch); if (compileRes == null && !patchCom) return new CompileResult(true); if (compileRes == false) @@ -78,7 +78,8 @@ namespace Avalonia.Build.Tasks } static bool? CompileCore(IBuildEngine engine, CecilTypeSystem typeSystem, - string projectDirectory, bool verifyIl, + string projectDirectory, bool verifyIl, + bool defaultCompileBindings, MessageImportance logImportance , bool debuggerLaunch = false) { @@ -143,7 +144,7 @@ namespace Avalonia.Build.Tasks var contextClass = XamlILContextDefinition.GenerateContextClass(typeSystem.CreateTypeBuilder(contextDef), typeSystem, xamlLanguage, emitConfig); - var compiler = new AvaloniaXamlIlCompiler(compilerConfig, emitConfig, contextClass) { EnableIlVerification = verifyIl }; + var compiler = new AvaloniaXamlIlCompiler(compilerConfig, emitConfig, contextClass) { EnableIlVerification = verifyIl, DefaultCompileBindings = defaultCompileBindings }; var editorBrowsableAttribute = typeSystem .GetTypeReference(typeSystem.FindType("System.ComponentModel.EditorBrowsableAttribute")) diff --git a/src/Markup/Avalonia.Markup.Xaml.Loader/AvaloniaXamlIlRuntimeCompiler.cs b/src/Markup/Avalonia.Markup.Xaml.Loader/AvaloniaXamlIlRuntimeCompiler.cs index ece90762cb..be293331df 100644 --- a/src/Markup/Avalonia.Markup.Xaml.Loader/AvaloniaXamlIlRuntimeCompiler.cs +++ b/src/Markup/Avalonia.Markup.Xaml.Loader/AvaloniaXamlIlRuntimeCompiler.cs @@ -24,6 +24,8 @@ namespace Avalonia.Markup.Xaml.XamlIl { static class AvaloniaXamlIlRuntimeCompiler { + private const string UseCompileBindingsByDefaultConfigSwitch = "Avalonia.UseCompiledBindingsByDefault"; + #if !RUNTIME_XAML_CECIL private static SreTypeSystem _sreTypeSystem; private static Type _ignoresAccessChecksFromAttribute; @@ -178,13 +180,14 @@ namespace Avalonia.Markup.Xaml.XamlIl var clrPropertyBuilder = tb.DefineNestedType("ClrProperties_" + Guid.NewGuid().ToString("N")); var indexerClosureType = _sreBuilder.DefineType("IndexerClosure_" + Guid.NewGuid().ToString("N")); + bool compileBindingsByDefault = AppContext.TryGetSwitch(UseCompileBindingsByDefaultConfigSwitch, out var compileBindingsSwitchValue) && compileBindingsSwitchValue; + var compiler = new AvaloniaXamlIlCompiler(new AvaloniaXamlIlCompilerConfiguration(_sreTypeSystem, asm, _sreMappings, _sreXmlns, AvaloniaXamlIlLanguage.CustomValueConverter, new XamlIlClrPropertyInfoEmitter(_sreTypeSystem.CreateTypeBuilder(clrPropertyBuilder)), new XamlIlPropertyInfoAccessorFactoryEmitter(_sreTypeSystem.CreateTypeBuilder(indexerClosureType))), _sreEmitMappings, - _sreContextType) { EnableIlVerification = true }; - + _sreContextType) { EnableIlVerification = true, DefaultCompileBindings = compileBindingsByDefault }; IXamlType overrideType = null; if (rootInstance != null) @@ -200,8 +203,8 @@ namespace Avalonia.Markup.Xaml.XamlIl return LoadOrPopulate(created, rootInstance); } #endif - - static object LoadOrPopulate(Type created, object rootInstance) + + static object LoadOrPopulate(Type created, object rootInstance) { var isp = Expression.Parameter(typeof(IServiceProvider)); @@ -299,8 +302,6 @@ namespace Avalonia.Markup.Xaml.XamlIl { overrideType = _cecilTypeSystem.GetType(rootInstance.GetType().FullName); } - - var safeUri = uri.ToString() .Replace(":", "_") @@ -324,13 +325,18 @@ namespace Avalonia.Markup.Xaml.XamlIl asm.MainModule.Types.Add(contextDef); var tb = _cecilTypeSystem.CreateTypeBuilder(def); + + bool compileBindingsByDefault = AppContext.TryGetSwitch(UseCompileBindingsByDefaultConfigSwitch, out var compileBindingsSwitchValue) && compileBindingsSwitchValue; var compiler = new AvaloniaXamlIlCompiler(new XamlIlTransformerConfiguration(_cecilTypeSystem, localAssembly == null ? null : _cecilTypeSystem.FindAssembly(localAssembly.GetName().Name), _cecilMappings, XamlIlXmlnsMappings.Resolve(_cecilTypeSystem, _cecilMappings), AvaloniaXamlIlLanguage.CustomValueConverter), _cecilEmitMappings, - _cecilTypeSystem.CreateTypeBuilder(contextDef)); + _cecilTypeSystem.CreateTypeBuilder(contextDef)) + { + DefaultCompileBindings = compileBindingsByDefault + }; compiler.ParseAndCompile(xaml, uri.ToString(), tb, overrideType); var asmPath = Path.Combine(_cecilEmitDir, safeUri + ".dll"); using(var f = File.Create(asmPath))