Beispiel #1
0
        /*
         * Pushes a new object into the Lua stack with the provided
         * metatable
         */
        private void pushNewObject(KopiLua.LuaState 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);
        }
Beispiel #2
0
        /*
         * Pushes a CLR object into the Lua stack as an userdata
         * with the provided metatable
         */
        internal void pushObject(KopiLua.LuaState luaState, object o, string metatable)
        {
            int index = -1;

            // Pushes nil
            if (o == null)
            {
                LuaDll.lua_pushnil(luaState);
                return;
            }

            // Object already in the list of Lua objects? Push the stored reference.
            bool found = objectsBackMap.TryGetValue(o, out index);

            if (found)
            {
                LuaDll.luaL_getmetatable(luaState, "luaNet_objects");
                LuaDll.lua_rawgeti(luaState, -1, index);

                // Note: starting with lua5.1 the garbage collector may remove weak reference items (such as our luaNet_objects values) when the initial GC sweep
                // occurs, but the actual call of the __gc finalizer for that object may not happen until a little while later.  During that window we might call
                // this routine and find the element missing from luaNet_objects, but collectObject() has not yet been called.  In that case, we go ahead and call collect
                // object here
                // did we find a non nil object in our table? if not, we need to call collect object
                LuaTypes type = LuaDll.lua_type(luaState, -1);
                if (type != LuaTypes.LUA_TNIL)
                {
                    LuaDll.lua_remove(luaState, -2);     // drop the metatable - we're going to leave our object on the stack

                    return;
                }

                // MetaFunctions.dumpStack(this, luaState);
                LuaDll.lua_remove(luaState, -1);    // remove the nil object value
                LuaDll.lua_remove(luaState, -1);    // remove the metatable

                collectObject(o, index);            // Remove from both our tables and fall out to get a new ID
            }
            index = addObject(o);

            pushNewObject(luaState, o, index, metatable);
        }