Exemplo n.º 1
0
        IXamlIlType EmitTypeDescriptorContextStub(IXamlIlTypeSystem typeSystem, IXamlIlTypeBuilder builder,
                                                  XamlIlLanguageTypeMappings mappings)
        {
            if (mappings.TypeDescriptorContext == null)
            {
                return(null);
            }
            var tdc       = mappings.TypeDescriptorContext;
            var tdcPrefix = tdc.Namespace + "." + tdc.Name + ".";

            builder.AddInterfaceImplementation(mappings.TypeDescriptorContext);
            void PropertyStub(string name) => ImplementInterfacePropertyGetter(builder, tdc, name).Generator.Ldnull().Ret();

            PropertyStub("Container");
            PropertyStub("Instance");
            PropertyStub("PropertyDescriptor");

            void MethodStub(string name)
            {
                var original = tdc.FindMethod(m => m.Name == name);

                builder.DefineMethod(original.ReturnType, original.Parameters, tdcPrefix + name,
                                     false, false, true,
                                     original)
                .Generator
                .Emit(OpCodes.Newobj,
                      typeSystem.FindType("System.NotSupportedException").FindConstructor(new List <IXamlIlType>()))
                .Emit(OpCodes.Throw);
            }

            MethodStub("OnComponentChanging");
            MethodStub("OnComponentChanged");

            return(mappings.TypeDescriptorContext);
        }
Exemplo n.º 2
0
        private XamlIlContext(IXamlIlTypeBuilder builder,
                              IXamlIlTypeSystem typeSystem, XamlIlLanguageTypeMappings mappings,
                              IXamlIlType rootType, IEnumerable <IXamlIlField> staticProviders, string baseUri)
        {
            RootObjectField             = builder.DefineField(rootType, "RootObject", true, false);
            _parentServiceProviderField = builder.DefineField(mappings.ServiceProvider, "_sp", false, false);
            if (mappings.InnerServiceProviderFactoryMethod != null)
            {
                _innerServiceProviderField = builder.DefineField(mappings.ServiceProvider, "_innerSp", false, false);
            }
            var so         = typeSystem.GetType("System.Object");
            var systemType = typeSystem.GetType("System.Type");
            var getServiceInterfaceMethod = mappings.ServiceProvider.FindMethod("GetService", so, false, systemType);

            var ownServices   = new List <IXamlIlType>();
            var ctorCallbacks = new List <Action <IXamlIlEmitter> >();

            if (mappings.RootObjectProvider != null)
            {
                builder.AddInterfaceImplementation(mappings.RootObjectProvider);
                var rootGen = ImplementInterfacePropertyGetter(builder, mappings.RootObjectProvider, "RootObject")
                              .Generator;
                var tryParent          = rootGen.DefineLabel();
                var fail               = rootGen.DefineLabel();
                var parentRootProvider = rootGen.DefineLocal(mappings.RootObjectProvider);
                rootGen
                // if(RootObject!=null) return RootObject;
                .LdThisFld(RootObjectField)
                .Brfalse(tryParent)
                .LdThisFld(RootObjectField)
                .Ret()
                // if(_sp == null) goto fail;
                .MarkLabel(tryParent)
                .LdThisFld(_parentServiceProviderField)
                .Brfalse(fail)
                // parentProv =  (IRootObjectProvider)_sp.GetService(typeof(IRootObjectProvider));
                .LdThisFld(_parentServiceProviderField)
                .Ldtype(mappings.RootObjectProvider)
                .EmitCall(getServiceInterfaceMethod)
                .Castclass(mappings.RootObjectProvider)
                .Stloc(parentRootProvider)
                // if(parentProv == null) goto fail;
                .Ldloc(parentRootProvider)
                .Brfalse(fail)
                // return parentProv.Root;
                .Ldloc(parentRootProvider)
                .EmitCall(mappings.RootObjectProvider.FindMethod(m => m.Name == "get_RootObject"))
                .Ret()
                // fail:
                .MarkLabel(fail)
                .Ldnull()
                .Ret();

                ownServices.Add(mappings.RootObjectProvider);
            }

            if (mappings.ParentStackProvider != null)
            {
                builder.AddInterfaceImplementation(mappings.ParentStackProvider);
                var objectListType = typeSystem.GetType("System.Collections.Generic.List`1")
                                     .MakeGenericType(new[] { typeSystem.GetType("System.Object") });
                ParentListField = builder.DefineField(objectListType, "ParentsStack", true, false);

                var enumerator = EmitParentEnumerable(typeSystem, builder, mappings);
                CreateCallbacks.Add(enumerator.createCallback);
                _parentStackEnumerableField = builder.DefineField(
                    typeSystem.GetType("System.Collections.Generic.IEnumerable`1").MakeGenericType(new[] { so }),
                    "_parentStackEnumerable", false, false);

                ImplementInterfacePropertyGetter(builder, mappings.ParentStackProvider, "Parents")
                .Generator.LdThisFld(_parentStackEnumerableField).Ret();

                ctorCallbacks.Add(g => g
                                  .Emit(OpCodes.Ldarg_0)
                                  .Emit(OpCodes.Newobj, objectListType.FindConstructor(new List <IXamlIlType>()))
                                  .Emit(OpCodes.Stfld, ParentListField)
                                  .Emit(OpCodes.Ldarg_0)
                                  .Emit(OpCodes.Ldarg_0)
                                  .Emit(OpCodes.Newobj, enumerator.ctor)
                                  .Emit(OpCodes.Stfld, _parentStackEnumerableField));
                ownServices.Add(mappings.ParentStackProvider);
            }

            ownServices.Add(EmitTypeDescriptorContextStub(typeSystem, builder, mappings));

            if (mappings.ProvideValueTarget != null)
            {
                builder.AddInterfaceImplementation(mappings.ProvideValueTarget);
                PropertyTargetObject   = builder.DefineField(so, "ProvideTargetObject", true, false);
                PropertyTargetProperty = builder.DefineField(so, "ProvideTargetProperty", true, false);
                ImplementInterfacePropertyGetter(builder, mappings.ProvideValueTarget, "TargetObject")
                .Generator.LdThisFld(PropertyTargetObject).Ret();
                ImplementInterfacePropertyGetter(builder, mappings.ProvideValueTarget, "TargetProperty")
                .Generator.LdThisFld(PropertyTargetProperty).Ret();
                ownServices.Add(mappings.ProvideValueTarget);
            }

            if (mappings.UriContextProvider != null)
            {
                var systemUri = typeSystem.GetType("System.Uri");
                var cached    = builder.DefineField(systemUri, "_baseUri", false, false);
                builder.AddInterfaceImplementation(mappings.UriContextProvider);
                var getter = builder.DefineMethod(systemUri, new IXamlIlType[0], "get_BaseUri", true, false, true);
                var setter = builder.DefineMethod(typeSystem.GetType("System.Void"), new[] { systemUri },
                                                  "set_BaseUri", true, false, true);


                var noCache = getter.Generator.DefineLabel();
                getter.Generator
                .LdThisFld(cached)
                .Brfalse(noCache)
                .LdThisFld(cached)
                .Ret()
                .MarkLabel(noCache)
                .Ldarg_0()
                .Ldstr(baseUri)
                .Newobj(systemUri.FindConstructor(new List <IXamlIlType>
                {
                    typeSystem.GetType("System.String")
                }))
                .Stfld(cached)
                .LdThisFld(cached)
                .Ret();

                setter.Generator
                .Ldarg_0()
                .Ldarg(1)
                .Stfld(cached)
                .Ret();
                builder.DefineProperty(systemUri, "BaseUri", setter, getter);


                ownServices.Add(mappings.UriContextProvider);
            }

            builder.AddInterfaceImplementation(mappings.ServiceProvider);
            var getServiceMethod = builder.DefineMethod(so,
                                                        new[] { systemType },
                                                        "GetService", true, false, true);

            ownServices = ownServices.Where(s => s != null).ToList();


            if (_innerServiceProviderField != null)
            {
                var next        = getServiceMethod.Generator.DefineLabel();
                var innerResult = getServiceMethod.Generator.DefineLocal(so);
                getServiceMethod.Generator
                //if(_inner == null) goto next;
                .LdThisFld(_innerServiceProviderField)
                .Brfalse(next)
                // var innerRes = _inner.GetService(type);
                .LdThisFld(_innerServiceProviderField)
                .Ldarg(1)
                .EmitCall(getServiceInterfaceMethod)
                .Stloc(innerResult)
                // if(innerRes == null) goto next;
                .Ldloc(innerResult)
                .Brfalse(next)
                // return innerRes
                .Ldloc(innerResult)
                .Ret()
                .MarkLabel(next);
            }
            var compare = systemType.FindMethod("Equals", typeSystem.GetType("System.Boolean"),
                                                false, systemType);
            var fromHandle = systemType.Methods.First(m => m.Name == "GetTypeFromHandle");

            if (ownServices.Count != 0)
            {
                for (var c = 0; c < ownServices.Count; c++)
                {
                    var next = getServiceMethod.Generator.DefineLabel();
                    getServiceMethod.Generator
                    .Emit(OpCodes.Ldtoken, ownServices[c])
                    .Emit(OpCodes.Call, fromHandle)
                    .Emit(OpCodes.Ldarg_1)
                    .Emit(OpCodes.Callvirt, compare)
                    .Emit(OpCodes.Brfalse, next)
                    .Emit(OpCodes.Ldarg_0)
                    .Emit(OpCodes.Ret)
                    .MarkLabel(next);
                }
            }

            if (staticProviders != null)
            {
                foreach (var sprov in staticProviders)
                {
                    var next = getServiceMethod.Generator.DefineLabel();
                    getServiceMethod.Generator
                    .Ldtoken(sprov.FieldType)
                    .EmitCall(fromHandle)
                    .Ldarg(1)
                    .EmitCall(compare)
                    .Brfalse(next)
                    .Ldsfld(sprov)
                    .Ret()
                    .MarkLabel(next);
                }
            }

            var noParentProvider = getServiceMethod.Generator.DefineLabel();

            getServiceMethod.Generator
            .LdThisFld(_parentServiceProviderField)
            .Brfalse(noParentProvider)
            .LdThisFld(_parentServiceProviderField)
            .Ldarg(1)
            .EmitCall(getServiceInterfaceMethod)
            .Emit(OpCodes.Ret)
            .MarkLabel(noParentProvider)
            .Ldnull()
            .Ret();

            var ctor = builder.DefineConstructor(false, mappings.ServiceProvider);

            ctor.Generator
            .Emit(OpCodes.Ldarg_0)
            .Emit(OpCodes.Call, so.Constructors.First())
            .Emit(OpCodes.Ldarg_0)
            .Emit(OpCodes.Ldarg_1)
            .Emit(OpCodes.Stfld, _parentServiceProviderField);
            foreach (var feature in ctorCallbacks)
            {
                feature(ctor.Generator);
            }

            // We are calling this last to ensure that our own services are ready
            if (_innerServiceProviderField != null)
            {
                ctor.Generator
                // _innerSp = InnerServiceProviderFactory(this)
                .Ldarg_0()
                .Ldarg_0()
                .EmitCall(mappings.InnerServiceProviderFactoryMethod)
                .Stfld(_innerServiceProviderField);
            }

            ctor.Generator.Emit(OpCodes.Ret);

            Constructor = ctor;
            CreateCallbacks.Add(() => { ContextType = builder.CreateType(); });
            ContextType = builder;
        }
Exemplo n.º 3
0
        public static XamlIlContext GenerateContextClass(IXamlIlTypeBuilder builder,
                                                         IXamlIlTypeSystem typeSystem, XamlIlLanguageTypeMappings mappings,
                                                         IXamlIlType rootType)
        {
            var rootObjectField = builder.DefineField(rootType, "RootObject", true, false);
            var so          = typeSystem.GetType("System.Object");
            var systemType  = typeSystem.GetType("System.Type");
            var runtimeType = typeSystem.GetType("System.RuntimeTypeHandle");

            var ownServices = new List <IXamlIlType>();

            if (mappings.RootObjectProvider != null)
            {
                builder.AddInterfaceImplementation(mappings.RootObjectProvider);
                var getRootObject = builder.DefineMethod(so, new IXamlIlType[0],
                                                         "get_RootObject", true, false, true);
                builder.DefineProperty(so, "RootObject", null, getRootObject);
                getRootObject.Generator
                .Emit(OpCodes.Ldarg_0)
                .Emit(OpCodes.Ldfld, rootObjectField)
                .Emit(OpCodes.Ret);
                ownServices.Add(mappings.RootObjectProvider);
            }

            var spField = builder.DefineField(mappings.ServiceProvider, "_sp", false, false);

            builder.AddInterfaceImplementation(mappings.ServiceProvider);
            var getServiceMethod = builder.DefineMethod(so,
                                                        new[] { systemType },
                                                        "GetService", true, false, true);

            if (ownServices.Count != 0)
            {
                var compare = systemType.FindMethod("Equals", typeSystem.GetType("System.Boolean"),
                                                    false, systemType);
                var fromHandle = systemType.Methods.First(m => m.Name == "GetTypeFromHandle");

                for (var c = 0; c < ownServices.Count; c++)
                {
                    var next = getServiceMethod.Generator.DefineLabel();
                    getServiceMethod.Generator
                    .Emit(OpCodes.Ldtoken, ownServices[c])
                    .Emit(OpCodes.Call, fromHandle)
                    .Emit(OpCodes.Ldarg_1)
                    .Emit(OpCodes.Callvirt, compare)
                    .Emit(OpCodes.Brfalse, next)
                    .Emit(OpCodes.Ldarg_0)
                    .Emit(OpCodes.Ret)
                    .MarkLabel(next);
                }
            }

            getServiceMethod.Generator
            .Emit(OpCodes.Ldarg_0)
            .Emit(OpCodes.Ldfld, spField)
            .Emit(OpCodes.Ldarg_1)
            .Emit(OpCodes.Callvirt, mappings.ServiceProvider.FindMethod("GetService", so, false, systemType))
            .Emit(OpCodes.Ret);


            var ctor = builder.DefineConstructor(mappings.ServiceProvider);

            ctor.Generator
            .Emit(OpCodes.Ldarg_0)
            .Emit(OpCodes.Call, so.Constructors.First())
            .Emit(OpCodes.Ldarg_0)
            .Emit(OpCodes.Ldarg_1)
            .Emit(OpCodes.Stfld, spField)
            .Emit(OpCodes.Ret);


            return(new XamlIlContext
            {
                Constructor = ctor,
                RootObjectField = rootObjectField,
                ContextType = builder.CreateType()
            });
        }
Exemplo n.º 4
0
        public static IXamlIlField EmitNamespaceInfoProvider(XamlIlTransformerConfiguration configuration,
                                                             IXamlIlTypeBuilder typeBuilder, XamlIlDocument document)
        {
            var iface = configuration.TypeMappings.XmlNamespaceInfoProvider;

            typeBuilder.AddInterfaceImplementation(iface);
            var method         = iface.FindMethod(m => m.Name == "get_XmlNamespaces");
            var instField      = typeBuilder.DefineField(method.ReturnType, "_services", false, false);
            var singletonField = typeBuilder.DefineField(iface, "Singleton", true, true);

            var impl = typeBuilder.DefineMethod(method.ReturnType, null, method.Name, true, false, true);

            typeBuilder.DefineProperty(method.ReturnType, "XmlNamespaces", null, impl);
            impl.Generator
            .LdThisFld(instField)
            .Ret();

            var infoType = method.ReturnType.GenericArguments[1].GenericArguments[0];

            var ctor     = typeBuilder.DefineConstructor(false);
            var listType = configuration.TypeSystem.FindType("System.Collections.Generic.List`1")
                           .MakeGenericType(infoType);
            var listInterfaceType = configuration.TypeSystem.FindType("System.Collections.Generic.IReadOnlyList`1")
                                    .MakeGenericType(infoType);
            var listAdd = listType.FindMethod("Add", configuration.WellKnownTypes.Void, true, infoType);

            var dictionaryType = configuration.TypeSystem.FindType("System.Collections.Generic.Dictionary`2")
                                 .MakeGenericType(configuration.WellKnownTypes.String, listInterfaceType);
            var dictionaryAdd = dictionaryType.FindMethod("Add", configuration.WellKnownTypes.Void, true,
                                                          configuration.WellKnownTypes.String, listInterfaceType);

            var dicLocal  = ctor.Generator.DefineLocal(dictionaryType);
            var listLocal = ctor.Generator.DefineLocal(listType);

            ctor.Generator
            .Ldarg_0()
            .Emit(OpCodes.Call, configuration.WellKnownTypes.Object.FindConstructor())
            .Emit(OpCodes.Newobj, dictionaryType.FindConstructor())
            .Stloc(dicLocal)
            .Ldarg_0()
            .Ldloc(dicLocal)
            .Stfld(instField);

            foreach (var alias in document.NamespaceAliases)
            {
                ctor.Generator
                .Newobj(listType.FindConstructor(new List <IXamlIlType>()))
                .Stloc(listLocal);

                var resolved = TryResolve(configuration, alias.Value);
                if (resolved != null)
                {
                    foreach (var rns in resolved)
                    {
                        ctor.Generator
                        .Ldloc(listLocal)
                        .Newobj(infoType.FindConstructor());
                        if (rns.ClrNamespace != null)
                        {
                            ctor.Generator
                            .Dup()
                            .Ldstr(rns.ClrNamespace)
                            .EmitCall(infoType.FindMethod(m => m.Name == "set_ClrNamespace"));
                        }

                        var asmName = rns.AssemblyName ?? rns.Assembly?.Name;
                        if (asmName != null)
                        {
                            ctor.Generator
                            .Dup()
                            .Ldstr(asmName)
                            .EmitCall(infoType.FindMethod(m => m.Name == "set_ClrAssemblyName"));
                        }

                        ctor.Generator.EmitCall(listAdd);
                    }
                }

                ctor.Generator
                .Ldloc(dicLocal)
                .Ldstr(alias.Key)
                .Ldloc(listLocal)
                .EmitCall(dictionaryAdd, true);
            }

            ctor.Generator.Ret();

            var sctor = typeBuilder.DefineConstructor(true);

            sctor.Generator
            .Newobj(ctor)
            .Stsfld(singletonField)
            .Ret();

            return(singletonField);
            //return typeBuilder.CreateType().Fields.First(f => f.Name == "Singleton");
        }