From 6f5e0f0a2f4ea382626703c673f5df8d7c9a972b Mon Sep 17 00:00:00 2001 From: jpbruyere Date: Sun, 1 Jan 2017 11:24:34 +0100 Subject: [PATCH] replace Activator.CreateInstance in Instantiator with an Emited NewOjb --- src/CompilerServices/CompilerServices.cs | 2 +- src/IML/Context.cs | 19 +++++++------------ src/Instantiator.cs | 11 +++++------ 3 files changed, 13 insertions(+), 19 deletions(-) diff --git a/src/CompilerServices/CompilerServices.cs b/src/CompilerServices/CompilerServices.cs index 0bd4b5c4..c0acc5d5 100644 --- a/src/CompilerServices/CompilerServices.cs +++ b/src/CompilerServices/CompilerServices.cs @@ -98,7 +98,7 @@ namespace Crow /// Il. 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); } diff --git a/src/IML/Context.cs b/src/IML/Context.cs index 01cfa475..7f739c37 100644 --- a/src/IML/Context.cs +++ b/src/IML/Context.cs @@ -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); - } - /// /// Emits cached delegate handler addition in the context of instantiator (ctx) /// diff --git a/src/Instantiator.cs b/src/Instantiator.cs index e92e8995..402df1cc 100644 --- a/src/Instantiator.cs +++ b/src/Instantiator.cs @@ -32,7 +32,7 @@ using Crow.IML; namespace Crow { - public delegate void InstanciatorInvoker(object instance, Interface iface); + public delegate object InstanciatorInvoker(Interface iface); /// /// 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 dsValueChangedDynMeths = new List(); @@ -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); -- 2.47.3