LuaInterface.ObjectTranslator.pushNewObject C# (CSharp) Method

pushNewObject() private method

private pushNewObject ( IntPtr luaState, object o, int index, string metatable ) : void
luaState IntPtr
o object
index int
metatable string
return void
        private void pushNewObject(IntPtr luaState,object o,int index,string metatable)
        {
            if(metatable=="luaNet_metatable")
            {
                // Gets or creates the metatable for the object's type
                LuaDLL.luaL_getmetatable(luaState,o.GetType().AssemblyQualifiedName);

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

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

Usage Example

示例#1
0
        public static int registerTable(IntPtr luaState)
        {
#if __NOGEN__
            throwError(luaState, "Tables as Objects not implemnented");
#else
            ObjectTranslator translator = ObjectTranslator.FromState(luaState);
            if (LuaDLL.lua_type(luaState, 1) == LuaTypes.LUA_TTABLE)
            {
                LuaTable luaTable       = translator.getTable(luaState, 1);
                string   superclassName = LuaDLL.lua_tostring(luaState, 2);
                if (superclassName != null)
                {
                    Type klass = translator.FindType(superclassName);
                    if (klass != null)
                    {
                        // Creates and pushes the object in the stack, setting
                        // it as the  metatable of the first argument
                        object obj = CodeGeneration.Instance.GetClassInstance(klass, luaTable);
                        translator.pushObject(luaState, obj, "luaNet_metatable");
                        LuaDLL.lua_newtable(luaState);
                        LuaDLL.lua_pushstring(luaState, "__index");
                        LuaDLL.lua_pushvalue(luaState, -3);
                        LuaDLL.lua_settable(luaState, -3);
                        LuaDLL.lua_pushstring(luaState, "__newindex");
                        LuaDLL.lua_pushvalue(luaState, -3);
                        LuaDLL.lua_settable(luaState, -3);
                        LuaDLL.lua_setmetatable(luaState, 1);
                        // Pushes the object again, this time as the base field
                        // of the table and with the luaNet_searchbase metatable
                        LuaDLL.lua_pushstring(luaState, "base");
                        int index = translator.addObject(obj);
                        translator.pushNewObject(luaState, obj, index, "luaNet_searchbase");
                        LuaDLL.lua_rawset(luaState, 1);
                    }
                    else
                    {
                        translator.throwError(luaState, "register_table: can not find superclass '" + superclassName + "'");
                    }
                }
                else
                {
                    translator.throwError(luaState, "register_table: superclass name can not be null");
                }
            }
            else
            {
                translator.throwError(luaState, "register_table: first arg is not a table");
            }
#endif
            return(0);
        }
All Usage Examples Of LuaInterface.ObjectTranslator::pushNewObject