示例#1
0
 public void callTest()
 {
     CSLua.LuaState L = new CSLua.LuaState();
     L.dostring("square = function(x) return x*x end");
     CSLua.MultiValue result = L.call("square", 12.0);
     Assert.AreEqual(144.0, result[0]);
 }
示例#2
0
        /// <summary>
        /// Pushes the object v into the lua stack and returns the number of values that were pushed.
        /// If v is a MultiValue then it might push more than one value.
        /// Will automatically register the class of the object if not yet registered
        /// </summary>
        /// <param name="v">Object to push onto the stack</param>
        /// <returns></returns>
        private int pushValue(object v)
        {
            if (v == null)
            {
                return(0);
            }

            Type t = v.GetType();

            if (t == typeof(int))
            {
                LuaDll.lua_pushinteger(L, (int)v);
                return(1);
            }
            else if ((t == typeof(double)) || (t == typeof(float)))
            {
                LuaDll.lua_pushnumber(L, (double)v);
                return(1);
            }
            else if (t == typeof(string))
            {
                LuaDll.lua_pushstring(L, (string)v);
                return(1);
            }
            else if (t == typeof(bool))
            {
                LuaDll.lua_pushboolean(L, ((bool)v)?1:0);
                return(1);
            }
            else if (t == typeof(MultiValue))
            {
                // Special case to push multiple values onto the stack
                MultiValue mv     = (MultiValue)v;
                int        result = 0;

                for (int i = 0; i < mv.Length; ++i)
                {
                    // Since this is recursive, we could end up with arrays inside arrays.
                    // Should we catch that?
                    result += pushValue(mv[i]);
                }

                return(result);
            }
            else if (t.IsClass)
            {
                // If the is an object, then store a reference to it in the static
                // list of lua references.  It can be removed from this list once
                // lua is done with it.
                pushObject(v);
                return(1);
            }
            else
            {
                // Error, unexpected type, cannot push.
                Console.WriteLine("pushValue() Cannot push type [{0}]", t.FullName);
                return(0);
            }
        }
示例#3
0
        /// <summary>
        /// Returns count values on the lua stack, starting at index i.
        /// </summary>
        /// <param name="i">Index of first value</param>
        /// <param name="count">Number of values to retrieve</param>
        /// <returns>Returns a MultiValue containing count arguments from the stack</returns>
        private MultiValue getValues(int i, int count)
        {
            MultiValue result = null;

            if (count > 0)
            {
                result = new MultiValue(count);
                for (int d = 0; d < count; ++d)
                {
                    result[d] = getValue(i + d);
                }
            }
            return(result);
        }
示例#4
0
        public void registerObjectTest()
        {
            CSLua.LuaState L          = new CSLua.LuaState();
            Multiplier     multiplier = new Multiplier("MyMult");

            L.registerObject("testObject", multiplier);

            CSLua.MultiValue result = L.dostring("return testObject:multiply(7, 13)");
            Assert.AreEqual(91.0, result[0]);

            result = L.dostring("return testObject:multiply(1, 1)");
            Assert.AreEqual(1.0, result[0]);

            result = L.dostring("return testObject:multiply(100001, 7)");
            Assert.AreEqual(700007.0, result[0]);
        }
示例#5
0
        public MultiValue call(string func, params object[] args)
        {
            int top = LuaDll.lua_gettop(L);

            MultiValue mv = new MultiValue(args);

            LuaDll.lua_getfield(L, LuaDll.LUA_GLOBALSINDEX, func);
            int count = pushValue(mv);
            int ret   = LuaDll.lua_pcall(L, count, LuaDll.LUA_MULTRET, 0);

            if (ret != 0)
            {
                string err = LuaDll.lua_tostring(L, -1);
                Console.WriteLine("ERROR: " + err);
                LuaDll.lua_pop(L, 1);
            }
            else
            {
                int newtop = LuaDll.lua_gettop(L);
                return(getValues(top + 1, newtop - top));
            }

            return(null);
        }
示例#6
0
 public void stringTest()
 {
     CSLua.LuaState   L      = new CSLua.LuaState();
     CSLua.MultiValue result = L.dostring("return \"This is a string\"");
     Assert.AreEqual("This is a string", result[0]);
 }
示例#7
0
 public void numberTest()
 {
     CSLua.LuaState   L      = new CSLua.LuaState();
     CSLua.MultiValue result = L.dostring("return 1");
     Assert.AreEqual(1.0, result[0]);
 }
示例#8
0
 public void dostringTest()
 {
     CSLua.LuaState   L      = new CSLua.LuaState();
     CSLua.MultiValue result = L.dostring("return \"Hello World\"");
     Assert.AreEqual("Hello World", result[0]);
 }