Exemplo n.º 1
0
        /*
         * Creates a new table as a global variable or as a field
         * inside an existing table
         */
        public void NewTable(string fullPath)
        {
            string[] path   = fullPath.Split(new char[] { '.' });
            int      oldTop = LuaLib.lua_gettop(luaState);

            if (path.Length == 1)
            {
                LuaLib.lua_newtable(luaState);
                LuaLib.lua_setglobal(luaState, fullPath);
            }
            else
            {
                LuaLib.lua_getglobal(luaState, path[0]);

                for (int i = 1; i < path.Length - 1; i++)
                {
                    LuaLib.lua_pushstring(luaState, path[i]);
                    LuaLib.lua_gettable(luaState, -2);
                }

                LuaLib.lua_pushstring(luaState, path[path.Length - 1]);
                LuaLib.lua_newtable(luaState);
                LuaLib.lua_settable(luaState, -3);
            }

            LuaLib.lua_settop(luaState, oldTop);
        }
Exemplo n.º 2
0
        /*
         * CAUTION: LuaInterface.Lua instances can't share the same lua state!
         */
        public Lua(LuaCore.lua_State lState)
        {
            LuaLib.lua_pushstring(lState, "LUAINTERFACE LOADED");
            LuaLib.lua_gettable(lState, (int)LuaIndexes.Registry);

            if (LuaLib.lua_toboolean(lState, -1))
            {
                LuaLib.lua_settop(lState, -2);
                throw new LuaException("There is already a LuaInterface.Lua instance associated with this Lua state");
            }
            else
            {
                LuaLib.lua_settop(lState, -2);
                LuaLib.lua_pushstring(lState, "LUAINTERFACE LOADED");
                LuaLib.lua_pushboolean(lState, true);
                LuaLib.lua_settable(lState, (int)LuaIndexes.Registry);
                luaState = lState;
                LuaLib.lua_pushvalue(lState, (int)LuaIndexes.Globals);
                LuaLib.lua_getglobal(lState, "luanet");
                LuaLib.lua_pushstring(lState, "getmetatable");
                LuaLib.lua_getglobal(lState, "getmetatable");
                LuaLib.lua_settable(lState, -3);
                LuaLib.lua_replace(lState, (int)LuaIndexes.Globals);
                translator = new ObjectTranslator(this, luaState);
                LuaLib.lua_replace(lState, (int)LuaIndexes.Globals);
                LuaLib.luaL_dostring(lState, Lua.init_luanet);                  // steffenj: lua_dostring renamed to luaL_dostring
            }

            _StatePassed = true;
        }
Exemplo n.º 3
0
        /*
         * Navigates a table to set the value of one of its fields
         */
        internal void setObject(string[] remainingPath, object val)
        {
            for (int i = 0; i < remainingPath.Length - 1; i++)
            {
                LuaLib.lua_pushstring(luaState, remainingPath[i]);
                LuaLib.lua_gettable(luaState, -2);
            }

            LuaLib.lua_pushstring(luaState, remainingPath[remainingPath.Length - 1]);
            translator.push(luaState, val);
            LuaLib.lua_settable(luaState, -3);
        }
Exemplo n.º 4
0
        /*
         * Gets a numeric field of the table or userdata corresponding the the provided reference
         */

        internal object getObject(int reference, object field)
        {
            int oldTop = LuaLib.lua_gettop(luaState);

            LuaLib.lua_getref(luaState, reference);
            translator.push(luaState, field);
            LuaLib.lua_gettable(luaState, -2);
            object returnValue = translator.getObject(luaState, -1);

            LuaLib.lua_settop(luaState, oldTop);
            return(returnValue);
        }
Exemplo n.º 5
0
        /*
         * Navigates a table in the top of the stack, returning
         * the value of the specified field
         */
        internal object getObject(string[] remainingPath)
        {
            object returnValue = null;

            for (int i = 0; i < remainingPath.Length; i++)
            {
                LuaLib.lua_pushstring(luaState, remainingPath[i]);
                LuaLib.lua_gettable(luaState, -2);
                returnValue = translator.getObject(luaState, -1);

                if (returnValue.IsNull())
                {
                    break;
                }
            }

            return(returnValue);
        }
Exemplo n.º 6
0
        /*
         * Implementation of free_object. Clears the metatable and the
         * base field, freeing the created object for garbage-collection
         */
        private int unregisterTable(LuaCore.lua_State luaState)
        {
            try
            {
                if (LuaLib.lua_getmetatable(luaState, 1) != 0)
                {
                    LuaLib.lua_pushstring(luaState, "__index");
                    LuaLib.lua_gettable(luaState, -2);
                    object obj = getRawNetObject(luaState, -1);

                    if (obj.IsNull())
                    {
                        throwError(luaState, "unregister_table: arg is not valid table");
                    }

                    var luaTableField = obj.GetType().GetField("__luaInterface_luaTable");

                    if (luaTableField.IsNull())
                    {
                        throwError(luaState, "unregister_table: arg is not valid table");
                    }

                    luaTableField.SetValue(obj, null);
                    LuaLib.lua_pushnil(luaState);
                    LuaLib.lua_setmetatable(luaState, 1);
                    LuaLib.lua_pushstring(luaState, "base");
                    LuaLib.lua_pushnil(luaState);
                    LuaLib.lua_settable(luaState, 1);
                }
                else
                {
                    throwError(luaState, "unregister_table: arg is not valid table");
                }
            }
            catch (Exception e)
            {
                throwError(luaState, e.Message);
            }

            return(0);
        }