public void CodePlexExample7() { using (Lua l = new Lua()) { var f = l.CreateLambda <Func <double, double> >("f", "return clr.System.Math:Abs(x) * 2", "x"); Console.WriteLine("f({0}) = {1}", 2, f(2)); Console.WriteLine("f({0}) = {1}", 2, f(-2)); var f2 = l.CreateLambda("f2", "local Math = clr.System.Math; return Math:Abs(x) * 2;", null, typeof(double), new KeyValuePair <string, Type>("x", typeof(double))); Console.WriteLine("f2({0}) = {1}", 2, f2.DynamicInvoke(2)); Console.WriteLine("f2({0}) = {1}", 2, f2.DynamicInvoke(-2)); } }
public void TestFunctions50() { using (Lua l = new Lua()) { l.PrintExpressionTree = Console.Out; var f = l.CreateLambda <Func <int> >("test", "local function test(a:int):int return 1 + a; end; return test(2);"); TestResult(new LuaResult(f()), 3); } }
public void Delegate03() { using (Lua l = new Lua()) { l.PrintExpressionTree = Console.Out; var dlg = l.CreateLambda <Func <int, int, int> >("Test", "return a + b", "a", "b"); Assert.IsTrue(dlg(1, 2) == 3); } } // proc Delegate03
public void Delegate02() { using (Lua l = new Lua()) { l.PrintExpressionTree = true; var dlg = l.CreateLambda <Func <int, int, int> >("Test", "return arg1 + arg2"); Assert.IsTrue(dlg(1, 2) == 3); } } // proc Delegate02
public void Delegate01() { using (Lua l = new Lua()) { l.PrintExpressionTree = Console.Out; TestDelegate dlg = l.CreateLambda <TestDelegate>("Test", "return value:ToString();"); string sR = dlg(3); Assert.IsTrue(sR == "3"); } } // proc Delegate01
public void TestToXml() { using (var lua = new Lua()) { var f = lua.CreateLambda <Func <LuaTable> >("test.lua", "return { ['test a'] = 12, hallo = 'Welt', 1, 4, sub = { sub = 'test', guid = clr.System.Guid.NewGuid() }, subarray = { 1, 2, 3, 4, 5 } }"); var t = f(); var x = t.ToXml(); Console.WriteLine(x.ToString()); var t2 = Procs.CreateLuaTable(x); Assert.AreEqual(12, t2["test a"]); Assert.AreEqual("Welt", t2["hallo"]); Assert.AreEqual(4, t2[2]); // todo: } }
} // func CompileAsync /// <summary></summary> /// <typeparam name="T"></typeparam> /// <param name="code"></param> /// <param name="sourceLocation">Source location for the debug information.</param> /// <param name="throwException">If the compile fails, should be raised a exception.</param> /// <param name="argumentNames"></param> /// <returns></returns> public async Task <T> CompileLambdaAsync <T>(string code, string sourceLocation, bool throwException, params string[] argumentNames) where T : class { try { return(await Task.Run(() => Lua.CreateLambda <T>(sourceLocation, code, argumentNames))); } catch (LuaParseException e) { if (throwException) { throw; } else { await ShowExceptionAsync(ExceptionShowFlags.Background, e, $"Compile for {sourceLocation} failed."); return(null); } } } // func CompileLambdaAsync
public void TestFunctions50() { using (Lua l = new Lua()) { l.PrintExpressionTree = Console.Out; var f = l.CreateLambda<Func<int>>("test", "local function test(a:int):int return 1 + a; end; return test(2);"); TestResult(new LuaResult(f()), 3); } }
public void CodePlexExample7() { using (Lua l = new Lua()) { var f = l.CreateLambda<Func<double, double>>("f", "return clr.System.Math:Abs(x) * 2", "x"); Console.WriteLine("f({0}) = {1}", 2, f(2)); Console.WriteLine("f({0}) = {1}", 2, f(-2)); var f2 = l.CreateLambda("f2", "local Math = clr.System.Math; return Math:Abs(x) * 2;", null, typeof(double), new KeyValuePair<string, Type>("x", typeof(double))); Console.WriteLine("f2({0}) = {1}", 2, f2.DynamicInvoke(2)); Console.WriteLine("f2({0}) = {1}", 2, f2.DynamicInvoke(-2)); } }