public static void Run() { Console.WriteLine("1)"); DateTime start = DateTime.Now; DynamicMethod dm = Util.Compile("X = abs((5 - Y) * 3);", typeof(TestOne)); Console.WriteLine(DateTime.Now - start); TestOne obj = new TestOne(); start = DateTime.Now; for (int i = 0; i < 100000; i++) { dm.Invoke(null, new object[] { obj }); } Console.WriteLine(DateTime.Now - start); DMCall d = (DMCall)dm.CreateDelegate(typeof(DMCall), obj); start = DateTime.Now; for (int i = 0; i < 100000; i++) { d(); } Console.WriteLine(DateTime.Now - start); Console.WriteLine(obj.X); }
public static void Run() { DynamicMethod dm = Util.Compile("if (b >= 10) return; a=1;b=b+a;", typeof(TestPersist)); TestPersist obj = new TestPersist(); DMCall d = (DMCall)dm.CreateDelegate(typeof(DMCall), obj); for (int i = 0; i < 15; i++) { d(); Console.WriteLine(obj.State.GetValue <float>("b")); } dm = Util.Compile("print(x);x=2;nullobj.ToString();", typeof(TestPersist)); d = (DMCall)dm.CreateDelegate(typeof(DMCall), obj); try { d(); } catch { } try { d(); } catch { } }
public static void Run() { DynamicMethod dm = Util.Compile("Print(1);Print(0);Print(200);", typeof(TestCast)); DMCall d = (DMCall)dm.CreateDelegate(typeof(DMCall), null); d(); }
public static void Run() { DynamicMethod dm = Util.Compile("a=1+3;a=a*2;while (a > 0) { a = a-1; if (a < 4) continue; w(a); } w(0);", typeof(TestCall)); TestCall call = new TestCall(); DMCall d = (DMCall)dm.CreateDelegate(typeof(DMCall), call); d(); }
public static void Run() { AffeCompiler compiler = new AffeCompiler(typeof(TestScope)); compiler.SymbolTable.AddSymbol(new TypeSymbol("var", typeof(void))); DynamicMethod dm = compiler.Compile("if(1){var s = \"hi\"; }var s = 1;print(s);"); //var s = i;print(s);"); DMCall d = (DMCall)dm.CreateDelegate(typeof(DMCall), null); d(); }
public static void Run() { AffeCompiler compiler = new AffeCompiler(typeof(TestInvoke)); compiler.SymbolTable.AddSymbol(new TypeSymbol("Console", typeof(Console))); compiler.SymbolTable.AddSymbol(new TypeSymbol("string", typeof(string))); compiler.SymbolTable.AddSymbol(new TypeSymbol("var", typeof(void))); DynamicMethod dm = compiler.Compile("var s = \"test\";Console.Out.WriteLine(s.Substring(1));"); DMCall d = (DMCall)dm.CreateDelegate(typeof(DMCall), null); d(); }
public void Declaration() { this.State.Clear(); AffeCompiler comp = new AffeCompiler(typeof(Tests)); comp.SymbolTable.AddSymbol(new TypeSymbol("int", typeof(int))); DMCall dm = (DMCall)comp.Compile("a = 2.5; int i = a; BoundFloat = i;") .CreateDelegate(typeof(DMCall), this); BoundFloat = 0f; dm(); Assert.AreEqual(2f, this.BoundFloat, "Result"); }
public void Invocation() { AffeCompiler comp = new AffeCompiler(typeof(Tests)); comp.SymbolTable.AddSymbol(new TypeSymbol("BoundClass", typeof(BoundObjectTest))); DMCall dm = (DMCall)comp.Compile("Push(BoundObject.InstanceCall());" + "Push(BoundObject.Property.InstanceCall());" + "Push(BoundClass.StaticCall());" + "BoundObject.Field = 1;" + "BoundObject.FieldProperty = BoundObject.Field + 1;") .CreateDelegate(typeof(DMCall), this); this.FloatList.Clear(); dm(); this.AssertList(new float[] { 1, 1, 2 }, "results"); Assert.AreEqual(1f, this.BoundObject.Field, "BoundObject.Field"); Assert.AreEqual(2f, this.BoundObject.FieldProperty, "BoundObject.FieldProperty"); }