Esempio 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);
        }
Esempio n. 2
0
        public Lua()
        {
            luaState = LuaLib.luaL_newstate();                  // steffenj: Lua 5.1.1 API change (lua_open is gone)
            //LuaLib.luaopen_base(luaState);	// steffenj: luaopen_* no longer used
            LuaLib.luaL_openlibs(luaState);                     // steffenj: Lua 5.1.1 API change (luaopen_base is gone, just open all libs right here)
            LuaLib.lua_pushstring(luaState, "LUAINTERFACE LOADED");
            LuaLib.lua_pushboolean(luaState, true);
            LuaLib.lua_settable(luaState, (int)LuaIndexes.Registry);
            LuaLib.lua_newtable(luaState);
            LuaLib.lua_setglobal(luaState, "luanet");
            LuaLib.lua_pushvalue(luaState, (int)LuaIndexes.Globals);
            LuaLib.lua_getglobal(luaState, "luanet");
            LuaLib.lua_pushstring(luaState, "getmetatable");
            LuaLib.lua_getglobal(luaState, "getmetatable");
            LuaLib.lua_settable(luaState, -3);
            LuaLib.lua_replace(luaState, (int)LuaIndexes.Globals);
            translator = new ObjectTranslator(this, luaState);
            LuaLib.lua_replace(luaState, (int)LuaIndexes.Globals);
            LuaLib.luaL_dostring(luaState, Lua.init_luanet);                    // steffenj: lua_dostring renamed to luaL_dostring

            // We need to keep this in a managed reference so the delegate doesn't get garbage collected
            panicCallback = new LuaCore.lua_CFunction(PanicCallback);
            LuaLib.lua_atpanic(luaState, panicCallback);

            //LuaLib.lua_atlock(luaState, lockCallback = new LuaCore.lua_CFunction(LockCallback));
            //LuaLib.lua_atunlock(luaState, unlockCallback = new LuaCore.lua_CFunction(UnlockCallback));
        }
Esempio n. 3
0
 /*
  * Sets up the list of objects in the Lua side
  */
 private void createLuaObjectList(LuaCore.lua_State luaState)
 {
     LuaLib.lua_pushstring(luaState, "luaNet_objects");
     LuaLib.lua_newtable(luaState);
     LuaLib.lua_newtable(luaState);
     LuaLib.lua_pushstring(luaState, "__mode");
     LuaLib.lua_pushstring(luaState, "v");
     LuaLib.lua_settable(luaState, -3);
     LuaLib.lua_setmetatable(luaState, -2);
     LuaLib.lua_settable(luaState, (int)LuaIndexes.Registry);
 }
Esempio n. 4
0
        /*
         * Implementation of make_object. Registers a table (first
         * argument in the stack) as an object subclassing the
         * type passed as second argument in the stack.
         */
        private int registerTable(LuaCore.lua_State luaState)
        {
            if (LuaLib.lua_type(luaState, 1) == LuaTypes.Table)
            {
                var    luaTable       = getTable(luaState, 1);
                string superclassName = LuaLib.lua_tostring(luaState, 2).ToString();

                if (!superclassName.IsNull())
                {
                    var klass = FindType(superclassName);

                    if (!klass.IsNull())
                    {
                        // Creates and pushes the object in the stack, setting
                        // it as the  metatable of the first argument
                        object obj = CodeGeneration.Instance.GetClassInstance(klass, luaTable);
                        pushObject(luaState, obj, "luaNet_metatable");
                        LuaLib.lua_newtable(luaState);
                        LuaLib.lua_pushstring(luaState, "__index");
                        LuaLib.lua_pushvalue(luaState, -3);
                        LuaLib.lua_settable(luaState, -3);
                        LuaLib.lua_pushstring(luaState, "__newindex");
                        LuaLib.lua_pushvalue(luaState, -3);
                        LuaLib.lua_settable(luaState, -3);
                        LuaLib.lua_setmetatable(luaState, 1);
                        // Pushes the object again, this time as the base field
                        // of the table and with the luaNet_searchbase metatable
                        LuaLib.lua_pushstring(luaState, "base");
                        int index = addObject(obj);
                        pushNewObject(luaState, obj, index, "luaNet_searchbase");
                        LuaLib.lua_rawset(luaState, 1);
                    }
                    else
                    {
                        throwError(luaState, "register_table: can not find superclass '" + superclassName + "'");
                    }
                }
                else
                {
                    throwError(luaState, "register_table: superclass name can not be null");
                }
            }
            else
            {
                throwError(luaState, "register_table: first arg is not a table");
            }

            return(0);
        }
Esempio n. 5
0
        /*
         * Pushes a new object into the Lua stack with the provided
         * metatable
         */
        private void pushNewObject(LuaCore.lua_State luaState, object o, int index, string metatable)
        {
            if (metatable == "luaNet_metatable")
            {
                // Gets or creates the metatable for the object's type
                LuaLib.luaL_getmetatable(luaState, o.GetType().AssemblyQualifiedName);

                if (LuaLib.lua_isnil(luaState, -1))
                {
                    LuaLib.lua_settop(luaState, -2);
                    LuaLib.luaL_newmetatable(luaState, o.GetType().AssemblyQualifiedName);
                    LuaLib.lua_pushstring(luaState, "cache");
                    LuaLib.lua_newtable(luaState);
                    LuaLib.lua_rawset(luaState, -3);
                    LuaLib.lua_pushlightuserdata(luaState, LuaLib.luanet_gettag());
                    LuaLib.lua_pushnumber(luaState, 1);
                    LuaLib.lua_rawset(luaState, -3);
                    LuaLib.lua_pushstring(luaState, "__index");
                    LuaLib.lua_pushstring(luaState, "luaNet_indexfunction");
                    LuaLib.lua_rawget(luaState, (int)LuaIndexes.Registry);
                    LuaLib.lua_rawset(luaState, -3);
                    LuaLib.lua_pushstring(luaState, "__gc");
                    LuaLib.lua_pushstdcallcfunction(luaState, metaFunctions.gcFunction);
                    LuaLib.lua_rawset(luaState, -3);
                    LuaLib.lua_pushstring(luaState, "__tostring");
                    LuaLib.lua_pushstdcallcfunction(luaState, metaFunctions.toStringFunction);
                    LuaLib.lua_rawset(luaState, -3);
                    LuaLib.lua_pushstring(luaState, "__newindex");
                    LuaLib.lua_pushstdcallcfunction(luaState, metaFunctions.newindexFunction);
                    LuaLib.lua_rawset(luaState, -3);
                }
            }
            else
            {
                LuaLib.luaL_getmetatable(luaState, metatable);
            }

            // Stores the object index in the Lua list and pushes the
            // index into the Lua stack
            LuaLib.luaL_getmetatable(luaState, "luaNet_objects");
            LuaLib.luanet_newudata(luaState, index);
            LuaLib.lua_pushvalue(luaState, -3);
            LuaLib.lua_remove(luaState, -4);
            LuaLib.lua_setmetatable(luaState, -2);
            LuaLib.lua_pushvalue(luaState, -1);
            LuaLib.lua_rawseti(luaState, -3, index);
            LuaLib.lua_remove(luaState, -2);
        }