Example #1
0
        bool TryCoreCompile(MethodDefinition initComp, MethodDefinition initCompRuntime, ILRootNode rootnode, string resourceId, out Exception exception)
        {
            try {
                var body   = new MethodBody(initComp);
                var module = body.Method.Module;
                var il     = body.GetILProcessor();
                il.Emit(OpCodes.Nop);

                if (initCompRuntime != null)
                {
                    // Generating branching code for the Previewer

                    //First using the ResourceLoader
                    var nop = Instruction.Create(OpCodes.Nop);
                    var getResourceProvider = module.ImportReference(module.ImportReference(typeof(Internals.ResourceLoader))
                                                                     .Resolve()
                                                                     .Properties.FirstOrDefault(pd => pd.Name == "ResourceProvider")
                                                                     .GetMethod);
                    il.Emit(OpCodes.Call, getResourceProvider);
                    il.Emit(OpCodes.Brfalse, nop);
                    il.Emit(OpCodes.Call, getResourceProvider);
                    il.Emit(OpCodes.Ldstr, resourceId);
                    var func = module.ImportReference(module.ImportReference(typeof(Func <string, string>))
                                                      .Resolve()
                                                      .Methods.FirstOrDefault(md => md.Name == "Invoke"));
                    func = func.ResolveGenericParameters(module.ImportReference(typeof(Func <string, string>)), module);
                    il.Emit(OpCodes.Callvirt, func);
                    il.Emit(OpCodes.Brfalse, nop);
                    il.Emit(OpCodes.Ldarg_0);
                    il.Emit(OpCodes.Call, initCompRuntime);
                    il.Emit(OpCodes.Ret);
                    il.Append(nop);

                    //Or using the deprecated XamlLoader
                    nop = Instruction.Create(OpCodes.Nop);
#pragma warning disable 0618
                    var getXamlFileProvider = module.ImportReference(module.ImportReference(typeof(Xaml.Internals.XamlLoader))
                                                                     .Resolve()
                                                                     .Properties.FirstOrDefault(pd => pd.Name == "XamlFileProvider")
                                                                     .GetMethod);
#pragma warning restore 0618

                    il.Emit(OpCodes.Call, getXamlFileProvider);
                    il.Emit(OpCodes.Brfalse, nop);
                    il.Emit(OpCodes.Call, getXamlFileProvider);
                    il.Emit(OpCodes.Ldarg_0);
                    var getType = module.ImportReference(module.ImportReference(typeof(object))
                                                         .Resolve()
                                                         .Methods.FirstOrDefault(md => md.Name == "GetType"));
                    il.Emit(OpCodes.Call, getType);
                    func = module.ImportReference(module.ImportReference(typeof(Func <Type, string>))
                                                  .Resolve()
                                                  .Methods.FirstOrDefault(md => md.Name == "Invoke"));
                    func = func.ResolveGenericParameters(module.ImportReference(typeof(Func <Type, string>)), module);
                    il.Emit(OpCodes.Callvirt, func);
                    il.Emit(OpCodes.Brfalse, nop);
                    il.Emit(OpCodes.Ldarg_0);
                    il.Emit(OpCodes.Call, initCompRuntime);
                    il.Emit(OpCodes.Ret);
                    il.Append(nop);
                }

                var visitorContext = new ILContext(il, body, module);

                rootnode.Accept(new XamlNodeVisitor((node, parent) => node.Parent = parent), null);
                rootnode.Accept(new ExpandMarkupsVisitor(visitorContext), null);
                rootnode.Accept(new PruneIgnoredNodesVisitor(), null);
                rootnode.Accept(new CreateObjectVisitor(visitorContext), null);
                rootnode.Accept(new SetNamescopesAndRegisterNamesVisitor(visitorContext), null);
                rootnode.Accept(new SetFieldVisitor(visitorContext), null);
                rootnode.Accept(new SetResourcesVisitor(visitorContext), null);
                rootnode.Accept(new SetPropertiesVisitor(visitorContext, true), null);

                il.Emit(OpCodes.Ret);
                initComp.Body = body;
                exception     = null;
                return(true);
            } catch (Exception e) {
                exception = e;
                return(false);
            }
        }
        bool TryCoreCompile(MethodDefinition initComp, MethodDefinition initCompRuntime, ILRootNode rootnode, out Exception exception)
        {
            try {
                var body   = new MethodBody(initComp);
                var module = body.Method.Module;
                body.InitLocals = true;
                var il           = body.GetILProcessor();
                var resourcePath = GetPathForType(module, initComp.DeclaringType);

                il.Emit(Nop);

                if (initCompRuntime != null)
                {
                    // Generating branching code for the Previewer

                    //First using the ResourceLoader
                    var nop = Instruction.Create(Nop);
                    var getResourceProvider = module.ImportPropertyGetterReference(("Xamarin.Forms.Core", "Xamarin.Forms.Internals", "ResourceLoader"), "ResourceProvider");
                    il.Emit(Call, getResourceProvider);
                    il.Emit(Brfalse, nop);
                    il.Emit(Call, getResourceProvider);

                    il.Emit(Ldtoken, module.ImportReference(initComp.DeclaringType));
                    il.Emit(Call, module.ImportMethodReference(("mscorlib", "System", "Type"), methodName: "GetTypeFromHandle", paramCount: 1, predicate: md => md.IsStatic));
                    il.Emit(Call, module.ImportMethodReference(("mscorlib", "System.Reflection", "IntrospectionExtensions"), methodName: "GetTypeInfo", paramCount: 1, predicate: md => md.IsStatic));
                    il.Emit(Callvirt, module.ImportPropertyGetterReference(("mscorlib", "System.Reflection", "TypeInfo"), propertyName: "Assembly", flatten: true));
                    il.Emit(Callvirt, module.ImportMethodReference(("mscorlib", "System.Reflection", "Assembly"), methodName: "GetName", paramCount: 0)); //assemblyName

                    il.Emit(Ldstr, resourcePath);                                                                                                         //resourcePath
                    il.Emit(Callvirt, module.ImportMethodReference(("mscorlib", "System", "Func`3"),
                                                                   methodName: "Invoke",
                                                                   paramCount: 2,
                                                                   classArguments: new[] { ("mscorlib", "System.Reflection", "AssemblyName"), ("mscorlib", "System", "String"), ("mscorlib", "System", "String") }));
        bool TryCoreCompile(MethodDefinition initComp, MethodDefinition initCompRuntime, ILRootNode rootnode, string xamlFilePath, out Exception exception)
        {
            try {
                var body   = new MethodBody(initComp);
                var module = body.Method.Module;
                body.InitLocals = true;
                var il           = body.GetILProcessor();
                var resourcePath = GetPathForType(module, initComp.DeclaringType);

                il.Emit(Nop);

                if (initCompRuntime != null)
                {
                    // Generating branching code for the Previewer

                    //First using the ResourceLoader
                    var nop = Instruction.Create(Nop);

                    il.Emit(Newobj, module.ImportCtorReference(("Xamarin.Forms.Core", "Xamarin.Forms.Internals", "ResourceLoader/ResourceLoadingQuery"), 0));

                    //AssemblyName
                    il.Emit(Dup);                     //dup the RLQ
                    il.Emit(Ldtoken, module.ImportReference(initComp.DeclaringType));
                    il.Emit(Call, module.ImportMethodReference(("mscorlib", "System", "Type"), methodName: "GetTypeFromHandle", parameterTypes: new[] { ("mscorlib", "System", "RuntimeTypeHandle") }, isStatic: true));
Example #4
0
        bool TryCoreCompile(MethodDefinition initComp, MethodDefinition initCompRuntime, ILRootNode rootnode, out Exception exception)
        {
            try {
                var body = new MethodBody(initComp);
                var il   = body.GetILProcessor();
                il.Emit(OpCodes.Nop);

                if (initCompRuntime != null)
                {
                    // Generating branching code for the Previewer
                    //	IL_0007:  call class [mscorlib]System.Func`2<class [mscorlib]System.Type,string> class [Xamarin.Forms.Xaml.Internals]Xamarin.Forms.Xaml.XamlLoader::get_XamlFileProvider()
                    //  IL_000c:  brfalse IL_0031
                    //  IL_0011:  call class [mscorlib]System.Func`2<class [mscorlib]System.Type,string> class [Xamarin.Forms.Xaml.Internals]Xamarin.Forms.Xaml.XamlLoader::get_XamlFileProvider()
                    //  IL_0016:  ldarg.0
                    //  IL_0017:  call instance class [mscorlib]System.Type object::GetType()
                    //  IL_001c:  callvirt instance !1 class [mscorlib]System.Func`2<class [mscorlib]System.Type, string>::Invoke(!0)
                    //  IL_0021:  brfalse IL_0031
                    //  IL_0026:  ldarg.0
                    //  IL_0027:  call instance void class Xamarin.Forms.Xaml.UnitTests.XamlLoaderGetXamlForTypeTests::__InitComponentRuntime()
                    //  IL_002c:  ret
                    //  IL_0031:  nop

                    var nop = Instruction.Create(OpCodes.Nop);

                    var getXamlFileProvider = body.Method.Module.ImportReference(body.Method.Module.ImportReference(typeof(Xamarin.Forms.Xaml.Internals.XamlLoader))
                                                                                 .Resolve()
                                                                                 .Properties.FirstOrDefault(pd => pd.Name == "XamlFileProvider")
                                                                                 .GetMethod);
                    il.Emit(OpCodes.Call, getXamlFileProvider);
                    il.Emit(OpCodes.Brfalse, nop);
                    il.Emit(OpCodes.Call, getXamlFileProvider);
                    il.Emit(OpCodes.Ldarg_0);
                    var getType = body.Method.Module.ImportReference(body.Method.Module.ImportReference(typeof(object))
                                                                     .Resolve()
                                                                     .Methods.FirstOrDefault(md => md.Name == "GetType"));
                    il.Emit(OpCodes.Call, getType);
                    var func = body.Method.Module.ImportReference(body.Method.Module.ImportReference(typeof(Func <Type, string>))
                                                                  .Resolve()
                                                                  .Methods.FirstOrDefault(md => md.Name == "Invoke"));
                    func = func.ResolveGenericParameters(body.Method.Module.ImportReference(typeof(Func <Type, string>)), body.Method.Module);
                    il.Emit(OpCodes.Callvirt, func);
                    il.Emit(OpCodes.Brfalse, nop);
                    il.Emit(OpCodes.Ldarg_0);
                    il.Emit(OpCodes.Call, initCompRuntime);
                    il.Emit(OpCodes.Ret);
                    il.Append(nop);
                }

                var visitorContext = new ILContext(il, body, body.Method.Module);

                rootnode.Accept(new XamlNodeVisitor((node, parent) => node.Parent = parent), null);
                rootnode.Accept(new ExpandMarkupsVisitor(visitorContext), null);
                rootnode.Accept(new PruneIgnoredNodesVisitor(), null);
                rootnode.Accept(new CreateObjectVisitor(visitorContext), null);
                rootnode.Accept(new SetNamescopesAndRegisterNamesVisitor(visitorContext), null);
                rootnode.Accept(new SetFieldVisitor(visitorContext), null);
                rootnode.Accept(new SetResourcesVisitor(visitorContext), null);
                rootnode.Accept(new SetPropertiesVisitor(visitorContext, true), null);

                il.Emit(OpCodes.Ret);
                initComp.Body = body;
                exception     = null;
                return(true);
            } catch (Exception e) {
                exception = e;
                return(false);
            }
        }
Example #5
0
        bool TryCoreCompile(MethodDefinition initComp, MethodDefinition initCompRuntime, ILRootNode rootnode, out Exception exception)
        {
            try {
                var body   = new MethodBody(initComp);
                var module = body.Method.Module;
                body.InitLocals = true;
                var il           = body.GetILProcessor();
                var resourcePath = GetPathForType(module, initComp.DeclaringType);

                il.Emit(Nop);

                if (initCompRuntime != null)
                {
                    // Generating branching code for the Previewer

                    //First using the ResourceLoader
                    var nop = Instruction.Create(Nop);

                    il.Emit(Newobj, module.ImportCtorReference(("Xamarin.Forms.Core", "Xamarin.Forms.Internals", "ResourceLoader/ResourceLoadingQuery"), 0));
                        << << << < HEAD

                        //AssemblyName
                        il.Emit(Dup);                 //dup the RLQ
Example #6
0
        bool TryCoreCompile(MethodDefinition initComp, MethodDefinition initCompRuntime, ILRootNode rootnode, out Exception exception)
        {
            try {
                var body   = new MethodBody(initComp);
                var module = body.Method.Module;
                body.InitLocals = true;
                var il           = body.GetILProcessor();
                var resourcePath = GetPathForType(module, initComp.DeclaringType);

                il.Emit(Nop);

                if (initCompRuntime != null)
                {
                    // Generating branching code for the Previewer

                    //First using the ResourceLoader
                    var nop = Instruction.Create(Nop);
                    var getResourceProvider = module.ImportReference(module.ImportReference(typeof(Internals.ResourceLoader))
                                                                     .Resolve()
                                                                     .Properties.FirstOrDefault(pd => pd.Name == "ResourceProvider")
                                                                     .GetMethod);
                    il.Emit(Call, getResourceProvider);
                    il.Emit(Brfalse, nop);
                    il.Emit(Call, getResourceProvider);

                    var getTypeFromHandle = module.ImportReference(typeof(Type).GetMethod("GetTypeFromHandle", new[] { typeof(RuntimeTypeHandle) }));
                    var getTypeInfo       = module.ImportReference(typeof(System.Reflection.IntrospectionExtensions).GetMethod("GetTypeInfo", new Type[] { typeof(Type) }));
                    var getAssembly       = module.ImportReference(typeof(Type).GetProperty("Assembly").GetMethod);
                    var getAssemblyName   = module.ImportReference(typeof(System.Reflection.Assembly).GetMethod("GetName", new Type[] { }));
                    il.Emit(Ldtoken, module.ImportReference(initComp.DeclaringType));
                    il.Emit(Call, module.ImportReference(getTypeFromHandle));
                    il.Emit(Call, module.ImportReference(getTypeInfo));
                    il.Emit(Callvirt, module.ImportReference(getAssembly));
                    il.Emit(Callvirt, module.ImportReference(getAssemblyName)); //assemblyName

                    il.Emit(Ldstr, resourcePath);                               //resourcePath
                    var func = module.ImportReference(module.ImportReference(typeof(Func <System.Reflection.AssemblyName, string, string>))
                                                      .Resolve()
                                                      .Methods.FirstOrDefault(md => md.Name == "Invoke"));
                    func = func.ResolveGenericParameters(module.ImportReference(typeof(Func <System.Reflection.AssemblyName, string, string>)), module);
                    il.Emit(Callvirt, func);
                    il.Emit(Brfalse, nop);
                    il.Emit(Ldarg_0);
                    il.Emit(Call, initCompRuntime);
                    il.Emit(Ret);
                    il.Append(nop);

                    //Or using the deprecated XamlLoader
                    nop = Instruction.Create(Nop);
#pragma warning disable 0618
                    var getXamlFileProvider = module.ImportReference(module.ImportReference(typeof(Xaml.Internals.XamlLoader))
                                                                     .Resolve()
                                                                     .Properties.FirstOrDefault(pd => pd.Name == "XamlFileProvider")
                                                                     .GetMethod);
#pragma warning restore 0618

                    il.Emit(Call, getXamlFileProvider);
                    il.Emit(Brfalse, nop);
                    il.Emit(Call, getXamlFileProvider);
                    il.Emit(Ldarg_0);
                    var getType = module.ImportReference(module.ImportReference(typeof(object))
                                                         .Resolve()
                                                         .Methods.FirstOrDefault(md => md.Name == "GetType"));
                    il.Emit(Call, getType);
                    func = module.ImportReference(module.ImportReference(typeof(Func <Type, string>))
                                                  .Resolve()
                                                  .Methods.FirstOrDefault(md => md.Name == "Invoke"));
                    func = func.ResolveGenericParameters(module.ImportReference(typeof(Func <Type, string>)), module);
                    il.Emit(Callvirt, func);
                    il.Emit(Brfalse, nop);
                    il.Emit(Ldarg_0);
                    il.Emit(Call, initCompRuntime);
                    il.Emit(Ret);
                    il.Append(nop);
                }

                var visitorContext = new ILContext(il, body, module);

                rootnode.Accept(new XamlNodeVisitor((node, parent) => node.Parent = parent), null);
                rootnode.Accept(new ExpandMarkupsVisitor(visitorContext), null);
                rootnode.Accept(new PruneIgnoredNodesVisitor(), null);
                rootnode.Accept(new CreateObjectVisitor(visitorContext), null);
                rootnode.Accept(new SetNamescopesAndRegisterNamesVisitor(visitorContext), null);
                rootnode.Accept(new SetFieldVisitor(visitorContext), null);
                rootnode.Accept(new SetResourcesVisitor(visitorContext), null);
                rootnode.Accept(new SetPropertiesVisitor(visitorContext, true), null);

                il.Emit(Ret);
                initComp.Body = body;
                exception     = null;
                return(true);
            } catch (Exception e) {
                exception = e;
                return(false);
            }
        }