Allow associating a table with Lua-bound objects, for storing state in Lua.

Design goals are: minimal overhead (one table); minimal extra code to use
it (two lines in the header, to have a space to store a reference to the
table), same API for classes with and without a table; allow switching to
a table-bound class without derived classes having to know about it; keep
zero per-object overhead for classes that don't need it.
This commit is contained in:
Glenn Maynard
2005-07-04 21:43:27 +00:00
parent 1e05d86b7d
commit ea6d806dad
2 changed files with 280 additions and 15 deletions
+191
View File
@@ -1,5 +1,6 @@
#include "global.h"
#include "LuaBinding.h"
#include "RageUtil.h"
void CreateMethodsTable( lua_State *L, const CString &szName )
{
@@ -63,6 +64,196 @@ bool CheckLuaObjectType( lua_State *L, int narg, const char *szType )
}
}
bool GetGlobalTable( Lua *L, bool bCreate )
{
lua_pushstring( L, "userdatas" );
lua_rawget( L, LUA_REGISTRYINDEX );
if( !lua_isnil(L, -1) )
return true;
lua_pop( L, 1 );
if( !bCreate )
return false;
/* Save it. */
lua_newtable( L );
lua_pushstring( L, "userdatas" );
lua_pushvalue( L, -2 );
lua_rawset( L, LUA_REGISTRYINDEX );
return true;
}
/* The object is on the stack. It's either a table or a userdata.
* If needed, associate the metatable; if a table, also add it to
* the userdata table. */
void ApplyDerivedType( Lua *L, const CString &sClassName, void *pSelf )
{
int iTable = lua_gettop( L );
int iType = lua_type( L, iTable );
ASSERT_M( iType == LUA_TTABLE || iType == LUA_TUSERDATA,
ssprintf("%i", iType) );
if( iType == LUA_TTABLE )
{
GetGlobalTable( L, true );
int iGlobalTable = lua_gettop( L );
/* If the table is already in the userdata table, then everything
* is already set up. */
lua_pushvalue( L, iTable );
lua_rawget( L, iGlobalTable );
if( !lua_isnil(L, -1) )
{
void *pData = lua_touserdata( L, -1 );
ASSERT( pSelf == pData );
ASSERT( pSelf == lua_touserdata(L, -1) );
lua_settop( L, iTable );
return;
}
/* Create the userdata, and add it to the global table. */
lua_pushvalue( L, iTable );
lua_pushlightuserdata( L, pSelf );
lua_rawset( L, iGlobalTable );
/* Pop everything except the table. */
lua_settop( L, iTable );
}
luaL_getmetatable( L, sClassName );
lua_setmetatable( L, iTable );
}
#include "RageUtil_AutoPtr.h"
REGISTER_CLASS_TRAITS( LuaClass, new LuaClass(*pCopy) )
LuaClass::LuaClass()
{
m_pSelf = NULL;
}
void *GetUserdataFromGlobalTable( Lua *L, const char *szType, int iArg )
{
if( !GetGlobalTable(L, false) )
luaL_error( L, "stale %s referenced (object used but no longer exists)", szType );
lua_pushvalue( L, iArg );
lua_rawget( L, -2 );
if( lua_isnil(L, -1) )
luaL_error( L, "stale %s referenced (object used but no longer exists)", szType );
void *pRet = lua_touserdata( L, -1 );
lua_pop( L, 2 );
return pRet;
}
/* Tricky: when an instance table is copied, we want to do a deep
* copy, not a reference copy. Otherwise, the new higher-level object
* will share a table with the original. Aside from being confusing,
* this breaks the global table, which assumes that we have a one-to-
* one mapping between tables and objects. */
LuaClass::LuaClass( const LuaClass &cpy ):
LuaTable(cpy)
{
if( !IsSet() )
return;
CString sData = Serialize();
LoadFromString( sData );
}
LuaClass &LuaClass::operator=( const LuaClass &cpy )
{
LuaTable::operator=(cpy);
if( !IsSet() )
return *this;
CString sData = Serialize();
LoadFromString( sData );
return *this;
}
LuaClass::~LuaClass()
{
if( LUA == NULL )
return;
Lua *L = LUA->Get();
int iTop = lua_gettop( L );
/* If we're registered in the global table, unregister. */
if( GetGlobalTable(L, false) )
{
this->PushSelf( L );
lua_pushnil( L );
lua_rawset( L, -3 );
}
lua_settop( L, iTop );
LUA->Release( L );
}
void LuaClass::BeforeReset()
{
LuaTable::BeforeReset();
/* Read pSelf the name of the class, so we can use them to restore in Register(). */
Lua *L = LUA->Get();
if( !GetGlobalTable(L, false) )
{
LUA->Release( L );
return;
}
this->PushSelf( L );
lua_rawget( L, -2 );
if( lua_isnil(L, -1) )
{
/* This table hasn't been pushed yet. */
lua_pop( L, 2 );
LUA->Release( L );
return;
}
m_pSelf = lua_touserdata( L, -1 );
lua_pop( L, 2 );
this->PushSelf( L );
lua_getmetatable( L, -1 );
lua_rawget( L, LUA_REGISTRYINDEX );
ASSERT( !lua_isnil(L, -1) );
m_sClassName = lua_tostring( L, -1 );
lua_pop( L, 1 );
LUA->Release( L );
}
void LuaClass::Register()
{
LuaTable::Register();
if( m_pSelf != NULL )
{
Lua *L = LUA->Get();
this->PushSelf(L);
ApplyDerivedType( L, m_sClassName, m_pSelf );
LUA->Release( L );
/* To conserve memory, clear the class name. We only need it while restoring. */
m_sClassName = CString();
m_pSelf = NULL;
}
}
/*
* (c) 2005 Glenn Maynard
* All rights reserved.