예제 #1
0
    public static void Main(string[] args)
    {
        Lua L = new Lua();

        // testing out parameters and type coercion for object[] args.
        L["obj"] = new RefParms();
        dump("void,out,out", L.DoString("return obj:Args()"));
        dump("int,out,out", L.DoString("return obj:ArgsI()"));
        L.DoString("obj:ArgsVar{1}");
        Console.WriteLine("equals {0} {1} {2}", IsInteger(2.3), IsInteger(0), IsInteger(44));
        //Environment.Exit(0);

        object[] res = L.DoString("return 20,'hello'", "tmp");
        Console.WriteLine("returned {0} {1}", res[0], res[1]);



        L.DoString("answer = 42");
        Console.WriteLine("answer was {0}", L["answer"]);

        MyClass.Register(L);

        L.DoString(@"
        print(Sqr(4))
        print(Sum(1.2,10))
        -- please note that this isn't true varargs syntax!
        print(utils.sum {1,5,4.2})
        ");

        L.DoString("X = {1,2,3}; Y = {fred='dog',alice='cat'}");

        LuaTable X = (LuaTable)L["X"];

        Console.WriteLine("1st {0} 2nd {1}", X[1], X[2]);
        // (but no Length defined: an oversight?)
        Console.WriteLine("X[4] was nil {0}", X[4] == null);

        // only do this if the table only has string keys
        LuaTable Y = (LuaTable)L["Y"];

        foreach (string s in Y.Keys)
        {
            Console.WriteLine("{0}={1}", s, Y[s]);
        }

        // getting and calling functions by name
        LuaFunction f = L.GetFunction("string.gsub");

        object[] ans = f.Call("here is the dog's dog", "dog", "cat");
        Console.WriteLine("results '{0}' {1}", ans[0], ans[1]);

        // and it's entirely possible to do these things from Lua...
        L["L"] = L;
        L.DoString(@"
            L:DoString 'print(1,2,3)'
        ");

        // it is also possible to override a CLR class in Lua using luanet.make_object.
        // This defines a proxy object which will successfully fool any C# code
        // receiving it.
        object[] R = L.DoString(@"
            luanet.load_assembly 'CallLua'  -- load this program
            local CSharp = luanet.import_type 'CSharp'
            local T = {}
            function T:MyMethod(s) 
                return s:lower()
            end
            function T:Protected(s)
                return s:upper()
            end
			function T:ProtectedBool()
				return true
			end
            luanet.make_object(T,'CSharp')
            print(CSharp.UseMe(T,'CoOl'))
			io.flush()
            return T
        ");
        // but it's still a table, and there's no way to cast it to CSharp from here...
        Console.WriteLine("type of returned value {0}", R[0].GetType());
    }
예제 #2
0
    public static void Main(string[] args)
    {
        Lua L = new Lua();

        // testing out parameters and type coercion for object[] args.
        L["obj"] = new RefParms();
        dump("void,out,out",L.DoString("return obj:Args()"));
        dump("int,out,out",L.DoString("return obj:ArgsI()"));
        L.DoString("obj:ArgsVar{1}");
        Console.WriteLine("equals {0} {1} {2}",IsInteger(2.3),IsInteger(0),IsInteger(44));
        //Environment.Exit(0);

        object[] res = L.DoString("return 20,'hello'","tmp");
        Console.WriteLine("returned {0} {1}",res[0],res[1]);

        L.DoString("answer = 42");
        Console.WriteLine("answer was {0}",L["answer"]);

        MyClass.Register(L);

        L.DoString(@"
        print(Sqr(4))
        print(Sum(1.2,10))
        -- please note that this isn't true varargs syntax!
        print(utils.sum {1,5,4.2})
        ");

        L.DoString("X = {1,2,3}; Y = {fred='dog',alice='cat'}");

        LuaTable X = (LuaTable)L["X"];
        Console.WriteLine("1st {0} 2nd {1}",X[1],X[2]);
        // (but no Length defined: an oversight?)
        Console.WriteLine("X[4] was nil {0}",X[4] == null);

        // only do this if the table only has string keys
        LuaTable Y = (LuaTable)L["Y"];
        foreach (string s in Y.Keys)
            Console.WriteLine("{0}={1}",s,Y[s]);

        // getting and calling functions by name
        LuaFunction f = L.GetFunction("string.gsub");
        object[] ans = f.Call("here is the dog's dog","dog","cat");
        Console.WriteLine("results '{0}' {1}",ans[0],ans[1]);

        // and it's entirely possible to do these things from Lua...
        L["L"] = L;
        L.DoString(@"
            L:DoString 'print(1,2,3)'
        ");

        // it is also possible to override a CLR class in Lua using luanet.make_object.
        // This defines a proxy object which will successfully fool any C# code
        // receiving it.
         object[] R = L.DoString(@"
            luanet.load_assembly 'CallLua'  -- load this program
            local CSharp = luanet.import_type 'CSharp'
            local T = {}
            function T:MyMethod(s)
                return s:lower()
            end
            function T:Protected(s)
                return s:upper()
            end
            function T:ProtectedBool()
                return true
            end
            luanet.make_object(T,'CSharp')
            print(CSharp.UseMe(T,'CoOl'))
            io.flush()
            return T
        ");
        // but it's still a table, and there's no way to cast it to CSharp from here...
        Console.WriteLine("type of returned value {0}",R[0].GetType());
    }