コード例 #1
0
ファイル: LuaDllTests.cs プロジェクト: kokuda/cslua
        public void lua_registerTest()
        {
            bool wasCalled = false;

            LuaDll.lua_CFunction testFunctionDelegate = delegate(lua_State state)
            {
                wasCalled = true;
                LuaDll.lua_pushstring(state, "Success!");
                return(1);
            };

            // Create a new state
            var L = LuaDll.luaL_newstate();

            // Register the given function by name
            LuaDll.lua_register(L, "test_function", testFunctionDelegate);

            // Call the function by name
            var result = LuaDll.luaL_dostring(L, "return test_function()");

            Assert.AreEqual(0, result);
            Assert.IsTrue(wasCalled);

            // Check that the function returned the same string.
            var resultIndex  = LuaDll.lua_gettop(L);
            var resultString = LuaDll.lua_tostring(L, resultIndex);

            Assert.AreEqual("Success!", resultString);
        }
コード例 #2
0
ファイル: LuaDllTests.cs プロジェクト: kokuda/cslua
        public void luaL_registerTest()
        {
            // Create a new state
            var L = LuaDll.luaL_newstate();

            LuaDll.lua_CFunction func = delegate(lua_State state)
            {
                LuaDll.lua_pushstring(state, "Success!");
                return(1);
            };

            LuaDll.luaL_Reg[] testLib =
            {
                new LuaDll.luaL_Reg {
                    name = "square", func = delegate(lua_State _L)
                    {
                        var value = LuaDll.lua_tonumber(_L, 1);
                        LuaDll.lua_pushnumber(_L, value * value);
                        return(1);
                    }
                },
                new LuaDll.luaL_Reg {
                    name = "add", func = delegate(lua_State _L)
                    {
                        var value1 = LuaDll.lua_tonumber(_L, 1);
                        var value2 = LuaDll.lua_tonumber(_L, 2);
                        LuaDll.lua_pushnumber(_L, value1 + value2);
                        return(1);
                    }
                },
                // Must have one nulled entry to indicate the end of the array
                new LuaDll.luaL_Reg {
                    name = null, func = null
                }
            };
            LuaDll.luaL_register(L, "TestLibrary", testLib);
            LuaDll.lua_pop(L, 1);
            var result = LuaDll.luaL_dostring(L, "return TestLibrary.square(9) + TestLibrary.add(3.14, 4.13)");

            Assert.AreEqual(0, result);

            Assert.AreEqual(88.27, LuaDll.lua_tonumber(L, 1));
        }
コード例 #3
0
ファイル: LuaDllTests.cs プロジェクト: kokuda/cslua
        public void lua_pushcfunctionTest()
        {
            // Create a new state
            var        L     = LuaDll.luaL_newstate();
            lua_Number value = 0;

            LuaDll.lua_CFunction squareandstorenumber = delegate(lua_State _L)
            {
                value = LuaDll.lua_tonumber(_L, 1);
                LuaDll.lua_pushnumber(_L, value * value);
                return(1);
            };

            string functionName = "squareandstorenumber";
            string tableName    = "TestTable";
            double testValue    = 3.14159;

            var top = LuaDll.lua_gettop(L);

            LuaDll.lua_newtable(L);
            LuaDll.lua_pushcfunction(L, squareandstorenumber);
            LuaDll.lua_setfield(L, -2, functionName);
            LuaDll.lua_setglobal(L, tableName);

            Assert.AreEqual(top, LuaDll.lua_gettop(L), "top doesn't match after registering function");

            LuaDll.lua_getglobal(L, tableName);              // push TestTable
            LuaDll.lua_getfield(L, -1, functionName);        // push TestTable["squareandstorenumber"]
            LuaDll.lua_pushnumber(L, testValue);             // push parameter
            var callResult   = LuaDll.lua_pcall(L, 1, 1, 0); // call function (pops parameter and function and pushes result)
            var squareResult = LuaDll.lua_tonumber(L, -1);   // get result

            LuaDll.lua_pop(L, 2);                            // pop the result and table

            Assert.AreEqual(top, LuaDll.lua_gettop(L), "top doesn't match after calling function");

            Assert.AreEqual(0, callResult, "Error calling squareandstorenumber");
            Assert.AreEqual(testValue, value, double.Epsilon);
            Assert.AreEqual(testValue * testValue, squareResult, double.Epsilon);
        }