]> O.S.I.I.S - jp/crow.git/commitdiff
replace Activator.CreateInstance in Instantiator with an Emited NewOjb
authorjpbruyere <jp.bruyere@hotmail.com>
Sun, 1 Jan 2017 10:24:34 +0000 (11:24 +0100)
committerjpbruyere <jp.bruyere@hotmail.com>
Sun, 1 Jan 2017 10:24:34 +0000 (11:24 +0100)
src/CompilerServices/CompilerServices.cs
src/IML/Context.cs
src/Instantiator.cs

index 0bd4b5c4d12a25263cf9562212344dc93f417fd8..c0acc5d5d425511836f8dda591f8f1bd46c99e42 100644 (file)
@@ -98,7 +98,7 @@ namespace Crow
                /// <param name="il">Il.</param>
                public static void emitSetCurInterface(ILGenerator il){
                        il.Emit (OpCodes.Ldloc_0);
-                       il.Emit (OpCodes.Ldarg_2);
+                       il.Emit (OpCodes.Ldarg_1);
                        il.Emit (OpCodes.Stfld, miSetCurIface);
                }
 
index 01cfa475472396e9f1a1f6ae7026addbc999715d..7f739c37023afc43f23ef4fdc3ecd29b0fd729b3 100644 (file)
@@ -57,10 +57,15 @@ namespace Crow.IML
                {
                        RootType = rootType;
                        dm = new DynamicMethod ("dyn_instantiator",
-                               typeof (void), new Type [] { typeof (Instantiator), typeof (object), typeof (Interface) }, true);
+                               CompilerServices.TObject, new Type [] { typeof (Instantiator), typeof (Interface) }, true);
                        il = dm.GetILGenerator (256);
 
-                       initILGen ();
+                       il.DeclareLocal (typeof (GraphicObject));
+                       il.Emit (OpCodes.Nop);
+                       //set local GraphicObject to root object 
+                       il.Emit (OpCodes.Newobj, rootType.GetConstructors () [0]);
+                       il.Emit (OpCodes.Stloc_0);
+                       CompilerServices.emitSetCurInterface (il);
                }
 
                public NodeAddress CurrentNodeAddress {
@@ -148,16 +153,6 @@ namespace Crow.IML
                        bd.ResolveTargetName (resolvedNA);
                }
 
-               void initILGen ()
-               {
-                       il.DeclareLocal (typeof (GraphicObject));
-                       il.Emit (OpCodes.Nop);
-                       //set local GraphicObject to root object passed as 1st argument
-                       il.Emit (OpCodes.Ldarg_1);
-                       il.Emit (OpCodes.Stloc_0);
-                       CompilerServices.emitSetCurInterface (il);
-               }
-
                /// <summary>
                /// Emits cached delegate handler addition in the context of instantiator (ctx)
                /// </summary>
index e92e89950d198435b4bc5ac4f2a79218dca2a5e9..402df1cc0e417a68845127ae4f9f3389438d9fec 100644 (file)
@@ -32,7 +32,7 @@ using Crow.IML;
 
 namespace Crow
 {
-       public delegate void InstanciatorInvoker(object instance, Interface iface);
+       public delegate object InstanciatorInvoker(Interface iface);
 
        /// <summary>
        /// Instantiator
@@ -87,10 +87,8 @@ namespace Crow
                }
                #endregion
 
-               public GraphicObject CreateInstance(Interface iface){
-                       GraphicObject tmp = (GraphicObject)Activator.CreateInstance(RootType);
-                       loader (tmp, iface);
-                       return tmp;
+               public GraphicObject CreateInstance(Interface iface){                   
+                       return loader (iface) as GraphicObject;
                }
 
                List<DynamicMethod> dsValueChangedDynMeths = new List<DynamicMethod>();
@@ -116,6 +114,7 @@ namespace Crow
 
                        emitBindingDelegates (ctx);
 
+                       ctx.il.Emit (OpCodes.Ldloc_0);//load root obj to return
                        ctx.il.Emit(OpCodes.Ret);
 
                        reader.Read ();//close tag
@@ -209,7 +208,7 @@ namespace Crow
                                        if (string.IsNullOrEmpty (templatePath)) {
                                                ctx.il.Emit (OpCodes.Ldnull);//default template loading
                                        } else {
-                                               ctx.il.Emit (OpCodes.Ldarg_2);//load currentInterface
+                                               ctx.il.Emit (OpCodes.Ldarg_1);//load currentInterface
                                                ctx.il.Emit (OpCodes.Ldstr, templatePath); //Load template path string
                                                ctx.il.Emit (OpCodes.Callvirt,//call Interface.Load(path)
                                                        CompilerServices.miIFaceLoad);