Example #1
0
        /*
         * Pushes a new object into the Lua stack with the provided
         * metatable
         */
        private void pushNewObject(ILuaState 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)LuaDef.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);
        }
Example #2
0
 /*
  * Creates the metatable for superclasses (the base
  * field of registered tables)
  */
 private void createBaseClassMetatable(ILuaState luaState)
 {
     LuaDLL.luaL_newmetatable(luaState, "luaNet_searchbase");
     LuaDLL.lua_pushstring(luaState, "__gc");
     LuaDLL.lua_pushstdcallcfunction(luaState, metaFunctions.gcFunction);
     LuaDLL.lua_settable(luaState, -3);
     LuaDLL.lua_pushstring(luaState, "__tostring");
     LuaDLL.lua_pushstdcallcfunction(luaState, metaFunctions.toStringFunction);
     LuaDLL.lua_settable(luaState, -3);
     LuaDLL.lua_pushstring(luaState, "__index");
     LuaDLL.lua_pushstdcallcfunction(luaState, metaFunctions.baseIndexFunction);
     LuaDLL.lua_settable(luaState, -3);
     LuaDLL.lua_pushstring(luaState, "__newindex");
     LuaDLL.lua_pushstdcallcfunction(luaState, metaFunctions.newindexFunction);
     LuaDLL.lua_settable(luaState, -3);
     LuaDLL.lua_settop(luaState, -2);
 }
Example #3
0
 /*
  * Creates the metatable for type references
  */
 private void createClassMetatable(ILuaState luaState)
 {
     LuaDLL.luaL_newmetatable(luaState, "luaNet_class");
     LuaDLL.lua_pushstring(luaState, "__gc");
     LuaDLL.lua_pushstdcallcfunction(luaState, metaFunctions.gcFunction);
     LuaDLL.lua_settable(luaState, -3);
     LuaDLL.lua_pushstring(luaState, "__tostring");
     LuaDLL.lua_pushstdcallcfunction(luaState, metaFunctions.test);
     LuaDLL.lua_settable(luaState, -3);
     LuaDLL.lua_pushstring(luaState, "__index");
     LuaDLL.lua_pushstdcallcfunction(luaState, metaFunctions.classIndexFunction);
     LuaDLL.lua_settable(luaState, -3);
     LuaDLL.lua_pushstring(luaState, "__newindex");
     LuaDLL.lua_pushstdcallcfunction(luaState, metaFunctions.classNewindexFunction);
     LuaDLL.lua_settable(luaState, -3);
     LuaDLL.lua_pushstring(luaState, "__call");
     LuaDLL.lua_pushstdcallcfunction(luaState, metaFunctions.callConstructorFunction);
     LuaDLL.lua_settable(luaState, -3);
     LuaDLL.lua_settop(luaState, -2);
 }