Esempio n. 1
0
        private static void DynamicTest()
        {
            dynamic d = new DynObj();

            Console.WriteLine(d.GetTableValue(1, 2));
            Console.WriteLine(d.GetTableValue(-17, 390));
            Console.WriteLine(d.ProcessUsername("PU"));
            Console.WriteLine(d[1]);
            Console.WriteLine(d[2, 3]);

            d.foo = "bar";
            d.baz = "foo";
            Console.WriteLine(d.foo);
            Console.WriteLine(d.baz);
        }
Esempio n. 2
0
        private static void NeoLuaTest()
        {
            Stopwatch sw = new Stopwatch();

            string scriptCode = @"    
function fact (n)
	if (n == 0) then
		return 1
	else
		return n*fact(n - 1)
	end
end

function testcall ()
    -- return obj.calcHypotenuse(3, 4);
    return obj.calcHypotenuse2();
    -- return mul(3, 4);
    -- return clr.System.Math:Pow(3, 4);
    -- return MyClass.calcHypotenuse(3, 4);
end

function testtable ()
    t.c = t.a + t.b;
end;
    
function testprint ()
    clr.System.Console:WriteLine(""Lorem Ipsum"");
    return 0;
end;

function dyntest ()
    return t.GetTableValue(3, 2);
end;  

function navtest ()
    --return nav.test2(""3"", 2.74);
    --return nav.test0();
    return nav.mul3(10, 20, 30);
end;  
";

            Lua     l  = new Lua();
            var     g  = l.CreateEnvironment();
            dynamic dg = g;

            g.RegisterPackage("MyClass", typeof(StaticMyClass));
            g.RegisterPackage("DynClass", typeof(DynObj));

            dg.obj = new MyClass();
            dynamic d = new DynObj();

            dg.dynobj = d;

            dg.mul             = new Func <int, int, int>(Mul);
            dg.t               = new LuaTable();
            dg.t.a             = 2;
            dg.t.b             = 4;
            dg.t.GetTableValue = new Func <object, object, string>((a, b) => d.GetTableValue(a, b));

            NAVDispatcher nd  = new NAVDispatcher();
            var           nav = new LuaTable();

            dg.nav       = nav;
            nav["test2"] = new Func <object, object, string>((a, b) => nd.DispatchStringCall("test2", new object[] { a, b }));
            nav["test0"] = new Func <string>(() => nd.DispatchStringCall("test0", new object[] { }));
            nav["mul3"]  = new Func <object, object, object, int>((a, b, c) => nd.DispatchIntCall("mul", new object[] { a, b, c }));
            nav["add3"]  = new Func <object, object, object, int>((a, b, c) => nd.DispatchIntCall("add", new object[] { a, b, c }));

            g.DoChunk(scriptCode, "test.lua");

            Func <int> testcase =
                () => dg.navtest();

            // () => { dg.testtable(); return dg.t.c; };

            sw.Reset();
            sw.Start();
            var res = testcase();

            sw.Stop();

            Console.WriteLine("Result = {0}", res);
            Console.WriteLine("Initial Run Elapsed = {0}", sw.Elapsed);

            long total = 0;

            for (int i = 1; i < 20; i++)
            {
                sw.Reset();
                sw.Start();
                res = testcase();
                sw.Stop();
                total += sw.Elapsed.Ticks;

                Console.WriteLine("Rerun {0}: Result = {1}; Elapsed = {2}", i, res, sw.Elapsed);
            }

            Console.WriteLine("Average Runtime = {0}", total / 20);
        }