public static IDisposable EmitAddChildMarker(
     this IXamlILEmitter emitter,
     string type,
     string property)
 {
     return(emitter.EmitMarker("AddChildMarker", null, type, property));
 }
        private static IDisposable EmitMarker(
            this IXamlILEmitter emitter,
            string startMethodName,
            string endMethodName = null,
            params string[] parameters)
        {
            var markers = emitter.TypeSystem.FindType("Avalonia.Markup.Xaml.HotReload.XamlMarkers");

            if (markers == null)
            {
                return(EmptyDisposable.Instance);
            }

            var startMarker = markers.FindMethod(m => m.Name == startMethodName);

            foreach (string parameter in parameters)
            {
                emitter.Emit(OpCodes.Ldstr, parameter);
            }

            emitter.EmitCall(startMarker);

            if (endMethodName != null)
            {
                var endMarker = markers.FindMethod(m => m.Name == endMethodName);
                return(new ActionDisposable(() => emitter.EmitCall(endMarker)));
            }

            return(EmptyDisposable.Instance);
        }
        public static void EmitFieldLiteral(IXamlField field, IXamlILEmitter codeGen)
        {
            var ftype = field.FieldType.IsEnum ? field.FieldType.GetEnumUnderlyingType() : field.FieldType;

            if (ftype.Name == "UInt64" || ftype.Name == "Int64")
            {
                codeGen.Emit(OpCodes.Ldc_I8,
                             TypeSystemHelpers.ConvertLiteralToLong(field.GetLiteralValue()));
            }
            else if (ftype.Name == "Double")
            {
                codeGen.Emit(OpCodes.Ldc_R8, (double)field.GetLiteralValue());
            }
            else if (ftype.Name == "Single")
            {
                codeGen.Emit(OpCodes.Ldc_R4, (float)field.GetLiteralValue());
            }
            else if (ftype.Name == "String")
            {
                codeGen.Emit(OpCodes.Ldstr, (string)field.GetLiteralValue());
            }
            else
            {
                codeGen.Emit(OpCodes.Ldc_I4,
                             TypeSystemHelpers.ConvertLiteralToInt(field.GetLiteralValue()));
            }
        }
 public static IDisposable EmitObjectInitializationMarker(this IXamlILEmitter emitter, string type)
 {
     return(emitter.EmitMarker(
                "StartObjectInitializationMarker",
                "EndObjectInitializationMarker",
                type));
 }
Esempio n. 5
0
        public static IXamlILEmitter Ldtype(this IXamlILEmitter emitter, IXamlType type)
        {
            var conv = emitter.TypeSystem.GetType("System.Type")
                       .FindMethod(m => m.IsStatic && m.IsPublic && m.Name == "GetTypeFromHandle");

            return(emitter.Ldtoken(type).EmitCall(conv));
        }
Esempio n. 6
0
 public override void EmitWithArguments(
     XamlEmitContextWithLocals <IXamlILEmitter, XamlILNodeEmitResult> context,
     IXamlILEmitter emitter,
     IReadOnlyList <IXamlAstValueNode> arguments)
 {
     EmitSetValue(emitter);
 }
Esempio n. 7
0
        public static IXamlILEmitter LdMethodInfo(this IXamlILEmitter emitter, IXamlMethod method)
        {
            var conv = emitter.TypeSystem.GetType("System.Reflection.MethodInfo")
                       .FindMethod(m => m.IsStatic && m.IsPublic && m.Name == "GetMethodFromHandle");

            return(emitter.Ldtoken(method).EmitCall(conv));
        }
Esempio n. 8
0
 public XamlILNodeEmitResult Emit(XamlIlEmitContext context, IXamlILEmitter codeGen)
 {
     if (!XamlIlAvaloniaPropertyHelper.Emit(context, codeGen, Property))
     {
         throw new XamlX.XamlLoadException(Property.Name + " is not an AvaloniaProperty", this);
     }
     return(XamlILNodeEmitResult.Type(0, Type.GetClrType()));
 }
        public virtual XamlILNodeEmitResult Emit(
            XamlEmitContext <IXamlILEmitter, XamlILNodeEmitResult> context,
            IXamlILEmitter codeGen)
        {
            codeGen.Newobj(_constructor);

            return(XamlILNodeEmitResult.Type(0, Type.GetClrType()));
        }
Esempio n. 10
0
 public override void Emit(IXamlILEmitter emitter)
 {
     using (var bloc = emitter.LocalsPool.GetLocal(Types.IBinding))
         emitter
         .Stloc(bloc.Local)
         .Ldsfld(AvaloniaProperty)
         .Ldloc(bloc.Local);
     EmitAnchorAndBind(emitter);
 }
Esempio n. 11
0
 public override void EmitWithArguments(
     XamlEmitContextWithLocals <IXamlILEmitter, XamlILNodeEmitResult> context,
     IXamlILEmitter emitter,
     IReadOnlyList <IXamlAstValueNode> arguments)
 {
     emitter.Ldsfld(AvaloniaProperty);
     context.Emit(arguments[1], emitter, Parameters[1]);
     EmitAnchorAndBind(emitter);
 }
Esempio n. 12
0
        public static IXamlILEmitter DebugHatch(this IXamlILEmitter emitter, string message)
        {
#if DEBUG
            var debug = emitter.TypeSystem.GetType("XamlX.XamlDebugHatch").FindMethod(m => m.Name == "Debug");
            emitter.Emit(OpCodes.Ldstr, message);
            emitter.Emit(OpCodes.Call, debug);
#endif
            return(emitter);
        }
Esempio n. 13
0
        public static IDisposable EmitSetPropertyMarker(this IXamlILEmitter emitter, IXamlPropertySetter setter)
        {
            (string type, string property) = GetPropertySetterInfo(setter);

            return(emitter.EmitMarker(
                       "StartSetPropertyMarker",
                       "EndSetPropertyMarker",
                       type,
                       property));
        }
        public override XamlILNodeEmitResult Emit(XamlEmitContext <IXamlILEmitter, XamlILNodeEmitResult> context,
                                                  IXamlILEmitter codeGen)
        {
            foreach (var value in Values)
            {
                codeGen.Emit(OpCodes.Ldc_I4, value);
            }

            return(base.Emit(context, codeGen));
        }
Esempio n. 15
0
 public override void Emit(IXamlILEmitter emitter)
 {
     using (var bloc = emitter.LocalsPool.GetLocal(Types.IBinding))
         emitter
         .Stloc(bloc.Local)
         .Ldsfld(AvaloniaProperty)
         .Ldloc(bloc.Local)
         // TODO: provide anchor?
         .Ldnull();
     emitter.EmitCall(Types.AvaloniaObjectBindMethod, true);
 }
Esempio n. 16
0
            private void EmitSetValue(IXamlILEmitter emitter)
            {
                // Ignore the instance and load one from the static field to avoid extra local variable
                var unsetValue = Types.AvaloniaProperty.Fields.First(f => f.Name == "UnsetValue");

                emitter
                .Ldsfld(AvaloniaProperty)
                .Ldsfld(unsetValue)
                .Ldc_I4(0)
                .EmitCall(Types.AvaloniaObjectSetValueMethod, true);
            }
        private void EmitLoadPropertyAccessorFactory(XamlIlEmitContext context, IXamlILEmitter codeGen, IXamlType type, string accessorFactoryName, bool isStatic = true)
        {
            var types             = context.GetAvaloniaTypes();
            var weakReferenceType = context.Configuration.TypeSystem.GetType("System.WeakReference`1").MakeGenericType(context.Configuration.WellKnownTypes.Object);
            FindMethodMethodSignature accessorFactorySignature = new FindMethodMethodSignature(accessorFactoryName, types.IPropertyAccessor, weakReferenceType, types.IPropertyInfo)
            {
                IsStatic = isStatic
            };

            codeGen.Ldftn(type.GetMethod(accessorFactorySignature));
        }
Esempio n. 18
0
            public override void Emit(IXamlILEmitter codegen)
            {
                var unsetValue = Types.AvaloniaProperty.Fields.First(f => f.Name == "UnsetValue");

                codegen
                // Ignore the instance and load one from the static field to avoid extra local variable
                .Pop()
                .Ldsfld(AvaloniaProperty)
                .Ldsfld(unsetValue)
                .Ldc_I4(0)
                .EmitCall(Types.AvaloniaObjectSetValueMethod, true);
            }
            public void EmitWithArguments(
                XamlEmitContextWithLocals <IXamlILEmitter, XamlILNodeEmitResult> context,
                IXamlILEmitter emitter,
                IReadOnlyList <IXamlAstValueNode> arguments)
            {
                emitter.EmitCall(_getter);

                for (var i = 0; i < arguments.Count; ++i)
                {
                    context.Emit(arguments[i], emitter, Parameters[i]);
                }

                emitter.EmitCall(_adder, true);
            }
Esempio n. 20
0
        public static bool Emit(XamlIlEmitContext context, IXamlILEmitter emitter, IXamlProperty property)
        {
            var type  = (property.Getter ?? property.Setter).DeclaringType;
            var name  = property.Name + "Property";
            var found = type.Fields.FirstOrDefault(f => f.IsStatic && f.Name == name);

            if (found == null)
            {
                return(false);
            }

            emitter.Ldsfld(found);
            return(true);
        }
        public IXamlType EmitLoadIndexerAccessorFactory(XamlIlEmitContext context, IXamlILEmitter codeGen, IXamlAstValueNode value)
        {
            var intType = context.Configuration.TypeSystem.GetType("System.Int32");

            if (_indexerClosureType is null)
            {
                _indexerClosureType = InitializeClosureType(context);
            }

            context.Emit(value, codeGen, intType);
            codeGen.Newobj(_indexerClosureType.FindConstructor(new List <IXamlType> {
                intType
            }));
            EmitLoadPropertyAccessorFactory(context, codeGen, _indexerClosureType, IndexerClosureFactoryMethodName, isStatic: false);
            return(EmitCreateAccessorFactoryDelegate(context, codeGen));
        }
Esempio n. 22
0
        public static IXamlILEmitter EmitCall(this IXamlILEmitter emitter, IXamlMethod method, bool swallowResult = false)
        {
            if (method is IXamlCustomEmitMethod <IXamlILEmitter> custom)
            {
                custom.EmitCall(emitter);
            }
            else
            {
                emitter.Emit(method.IsStatic ? OpCodes.Call : OpCodes.Callvirt, method);
            }

            if (swallowResult && !(method.ReturnType.Namespace == "System" && method.ReturnType.Name == "Void"))
            {
                emitter.Pop();
            }
            return(emitter);
        }
Esempio n. 23
0
        public static bool EmitProvideValueTarget(XamlIlEmitContext context, IXamlILEmitter emitter,
                                                  XamlAstClrProperty property)
        {
            if (Emit(context, emitter, property))
            {
                return(true);
            }
            var foundClr = property.DeclaringType.Properties.FirstOrDefault(p => p.Name == property.Name);

            if (foundClr == null)
            {
                return(false);
            }
            context
            .Configuration.GetExtra <XamlIlClrPropertyInfoEmitter>()
            .Emit(context, emitter, foundClr);
            return(true);
        }
Esempio n. 24
0
        public static bool Emit(XamlIlEmitContext context, IXamlILEmitter emitter, XamlAstClrProperty property)
        {
            if (property is IXamlIlAvaloniaProperty ap)
            {
                emitter.Ldsfld(ap.AvaloniaProperty);
                return(true);
            }
            var type  = property.DeclaringType;
            var name  = property.Name + "Property";
            var found = type.Fields.FirstOrDefault(f => f.IsStatic && f.Name == name);

            if (found == null)
            {
                return(false);
            }

            emitter.Ldsfld(found);
            return(true);
        }
            public void Emit(IXamlILEmitter emitter)
            {
                var locals = new Stack <XamlLocalsPool.PooledLocal>();

                // Save all "setter" parameters
                for (var c = Parameters.Count - 1; c >= 0; c--)
                {
                    var loc = emitter.LocalsPool.GetLocal(Parameters[c]);
                    locals.Push(loc);
                    emitter.Stloc(loc.Local);
                }

                emitter.EmitCall(_getter);
                while (locals.Count > 0)
                {
                    using (var loc = locals.Pop())
                        emitter.Ldloc(loc.Local);
                }
                emitter.EmitCall(_adder, true);
            }
Esempio n. 26
0
            public override void Emit(IXamlILEmitter emitter)
            {
                /*
                 * Current stack:
                 * - object
                 * - binding priority
                 * - value
                 */

                using (var valueLocal = emitter.LocalsPool.GetLocal(Parameters[1]))
                    using (var priorityLocal = emitter.LocalsPool.GetLocal(Types.Int))
                        emitter
                        .Stloc(valueLocal.Local)
                        .Stloc(priorityLocal.Local)
                        .Ldsfld(AvaloniaProperty)
                        .Ldloc(valueLocal.Local)
                        .Ldloc(priorityLocal.Local);

                EmitSetStyledPropertyValue(emitter);
            }
Esempio n. 27
0
                public void EmitCall(IXamlILEmitter emitter)
                {
                    var method = Parent._avaloniaObject
                                 .FindMethod(m => m.IsPublic && !m.IsStatic && m.Name == "GetValue"
                                             &&
                                             m.Parameters.Count == 1 &&
                                             m.Parameters[0].Equals(Parent._avaloniaPropertyType));

                    if (method == null)
                    {
                        throw new XamlTypeSystemException(
                                  "Unable to find T GetValue<T>(AvaloniaProperty<T>) on AvaloniaObject");
                    }
                    emitter
                    .Ldsfld(Parent._field)
                    .EmitCall(method);
                    if (Parent.PropertyType.IsValueType)
                    {
                        emitter.Unbox_Any(Parent.PropertyType);
                    }
                }
Esempio n. 28
0
        private static void EmitNameScopeField(XamlLanguageTypeMappings mappings,
                                               IXamlTypeSystem typeSystem,
                                               IXamlTypeBuilder <IXamlILEmitter> typebuilder, IXamlILEmitter constructor)
        {
            var nameScopeType = typeSystem.FindType("Avalonia.Controls.INameScope");
            var field         = typebuilder.DefineField(nameScopeType,
                                                        ContextNameScopeFieldName, true, false);

            constructor
            .Ldarg_0()
            .Ldarg(1)
            .Ldtype(nameScopeType)
            .EmitCall(mappings.ServiceProvider.GetMethod(new FindMethodMethodSignature("GetService",
                                                                                       typeSystem.FindType("System.Object"), typeSystem.FindType("System.Type"))))
            .Stfld(field);
        }
Esempio n. 29
0
        private static void EmitNameScopeField(XamlLanguageTypeMappings xamlLanguage, CecilTypeSystem typeSystem, IXamlTypeBuilder <IXamlILEmitter> typeBuilder, IXamlILEmitter constructor)
        {
            var nameScopeType = typeSystem.FindType("Robust.Client.UserInterface.XAML.NameScope");
            var field         = typeBuilder.DefineField(nameScopeType,
                                                        ContextNameScopeFieldName, true, false);

            constructor
            .Ldarg_0()
            .Newobj(nameScopeType.GetConstructor())
            .Stfld(field);
        }
                public XamlILNodeEmitResult Emit(IXamlAstNode node, XamlEmitContext <IXamlILEmitter, XamlILNodeEmitResult> context, IXamlILEmitter codeGen)
                {
                    if (!(node is HandleRootObjectScopeNode))
                    {
                        return(null);
                    }

                    var controlType = context.Configuration.TypeSystem.FindType("Robust.Client.UserInterface.Control");

                    var next              = codeGen.DefineLabel();
                    var dontAbsorb        = codeGen.DefineLabel();
                    var end               = codeGen.DefineLabel();
                    var contextScopeField = context.RuntimeContext.ContextType.Fields.First(f =>
                                                                                            f.Name == XamlCompiler.ContextNameScopeFieldName);
                    var controlNameScopeField = controlType.Fields.First(f => f.Name == "NameScope");
                    var nameScopeType         = context.Configuration.TypeSystem
                                                .FindType("Robust.Client.UserInterface.XAML.NameScope");
                    var nameScopeCompleteMethod = nameScopeType.Methods.First(m => m.Name == "Complete");
                    var nameScopeAbsorbMethod   = nameScopeType.Methods.First(m => m.Name == "Absorb");

                    using (var local = codeGen.LocalsPool.GetLocal(controlType))
                    {
                        codeGen
                        .Isinst(controlType)
                        .Dup()
                        .Stloc(local.Local) //store control in local field
                        .Brfalse(next)      //if control is null, move to next (this should never happen but whatev, avalonia does it)
                        .Ldloc(context.ContextLocal)
                        .Ldfld(contextScopeField)
                        .Ldloc(local.Local)           //load control from local field
                        .Ldfld(controlNameScopeField) //load namescope field from control
                        .EmitCall(nameScopeAbsorbMethod, true)
                        .Ldloc(local.Local)           //load control
                        .Ldloc(context.ContextLocal)  //load contextObject
                        .Ldfld(contextScopeField)     //load namescope field from context obj
                        .Stfld(controlNameScopeField) //store namescope field in control
                        .MarkLabel(next)
                        .Ldloc(context.ContextLocal)
                        .Ldfld(contextScopeField)
                        .EmitCall(nameScopeCompleteMethod, true);     //set the namescope as complete
                    }

                    return(XamlILNodeEmitResult.Void(1));
                }