simplify LuaBinding, move more code out of the template
This commit is contained in:
+189
-15
@@ -3,6 +3,119 @@
|
||||
#include "LuaReference.h"
|
||||
#include "RageUtil.h"
|
||||
|
||||
#include "SubscriptionManager.h"
|
||||
static SubscriptionManager<LuaBinding> m_Subscribers;
|
||||
|
||||
namespace
|
||||
{
|
||||
void RegisterTypes( lua_State *L )
|
||||
{
|
||||
if( m_Subscribers.m_pSubscribers == NULL )
|
||||
return;
|
||||
|
||||
/* Register base classes first. */
|
||||
map<RString, LuaBinding *> mapToRegister;
|
||||
FOREACHS( LuaBinding*, *m_Subscribers.m_pSubscribers, p )
|
||||
mapToRegister[(*p)->GetClassName()] = (*p);
|
||||
|
||||
set<RString> setRegisteredAlready;
|
||||
|
||||
while( !mapToRegister.empty() )
|
||||
{
|
||||
/* Look at the first class. If it has a base class that needs to be registered,
|
||||
* go there first. */
|
||||
LuaBinding *pBinding = mapToRegister.begin()->second;
|
||||
while(1)
|
||||
{
|
||||
if( !pBinding->IsDerivedClass() )
|
||||
break;
|
||||
|
||||
RString sBase = pBinding->GetBaseClassName();
|
||||
map<RString, LuaBinding *>::const_iterator it = mapToRegister.find(sBase);
|
||||
if( it != mapToRegister.end() )
|
||||
{
|
||||
pBinding = it->second;
|
||||
continue;
|
||||
}
|
||||
|
||||
/* If the base class wasn't found, and hasn't been registered already, then
|
||||
* a base class registration is missing. */
|
||||
if( setRegisteredAlready.find(sBase) != setRegisteredAlready.end() )
|
||||
break;
|
||||
|
||||
FAIL_M( ssprintf("Base class of \"%s\" not registered: \"%s\"",
|
||||
pBinding->GetClassName().c_str(),
|
||||
sBase.c_str()) );
|
||||
}
|
||||
|
||||
pBinding->Register( L );
|
||||
setRegisteredAlready.insert( pBinding->GetClassName() );
|
||||
mapToRegister.erase( pBinding->GetClassName() );
|
||||
}
|
||||
}
|
||||
};
|
||||
REGISTER_WITH_LUA_FUNCTION( RegisterTypes );
|
||||
|
||||
LuaBinding::LuaBinding()
|
||||
{
|
||||
m_Subscribers.Subscribe( this );
|
||||
}
|
||||
|
||||
LuaBinding::~LuaBinding()
|
||||
{
|
||||
m_Subscribers.Unsubscribe( this );
|
||||
}
|
||||
|
||||
void LuaBinding::Register( lua_State *L )
|
||||
{
|
||||
/* Create the methods table, if it doesn't already exist. */
|
||||
LuaBinding::CreateMethodsTable( L, GetClassName() );
|
||||
int methods = lua_gettop( L );
|
||||
|
||||
/* Create a metatable for the userdata objects. */
|
||||
luaL_newmetatable( L, GetClassName() );
|
||||
int metatable = lua_gettop( L );
|
||||
|
||||
// We use the metatable to determine the type of the table, so don't
|
||||
// allow it to be changed.
|
||||
lua_pushstring( L, "(hidden)" );
|
||||
lua_setfield( L, metatable, "__metatable" );
|
||||
|
||||
lua_pushvalue( L, methods );
|
||||
lua_setfield( L, metatable, "__index" );
|
||||
|
||||
lua_pushcfunction( L, PushEqual );
|
||||
lua_setfield( L, metatable, "__eq" );
|
||||
|
||||
/* Create a metatable for the methods table. */
|
||||
lua_newtable( L );
|
||||
int methods_metatable = lua_gettop( L );
|
||||
|
||||
// Hide the metatable.
|
||||
lua_pushstring( L, "(hidden)" );
|
||||
lua_setfield( L, methods_metatable, "__metatable" );
|
||||
|
||||
// If this type has a base class, set the __index of this type
|
||||
// to the base class.
|
||||
if( IsDerivedClass() )
|
||||
{
|
||||
lua_getfield( L, LUA_GLOBALSINDEX, GetBaseClassName() );
|
||||
lua_setfield( L, methods_metatable, "__index" );
|
||||
}
|
||||
|
||||
lua_pushstring( L, GetClassName() );
|
||||
lua_setfield( L, methods_metatable, "type" );
|
||||
|
||||
/* Set and pop the methods metatable. */
|
||||
lua_setmetatable( L, methods );
|
||||
|
||||
/* Allow the derived class to populate the method table, and set any other
|
||||
* metatable fields. */
|
||||
Register( L, methods, metatable );
|
||||
|
||||
lua_pop( L, 2 ); // drop metatable and method table
|
||||
}
|
||||
|
||||
// If defined, type checks for functions will be skipped. These add
|
||||
// up and can become expensive (performed for every function dispatch),
|
||||
// but without them it's possible to call functions on incompatible
|
||||
@@ -11,16 +124,65 @@
|
||||
|
||||
void LuaBinding::CreateMethodsTable( lua_State *L, const RString &sName )
|
||||
{
|
||||
lua_pushlstring( L, sName.data(), sName.size() );
|
||||
lua_rawget( L, LUA_GLOBALSINDEX );
|
||||
lua_getfield( L, LUA_GLOBALSINDEX, sName );
|
||||
if( !lua_isnil(L, -1) )
|
||||
return;
|
||||
|
||||
lua_pop( L, 1 );
|
||||
lua_newtable( L );
|
||||
lua_pushlstring( L, sName.data(), sName.size() );
|
||||
lua_pushvalue( L, -2 );
|
||||
lua_rawset( L, LUA_GLOBALSINDEX );
|
||||
lua_pushvalue( L, -1 );
|
||||
lua_setfield( L, LUA_GLOBALSINDEX, sName );
|
||||
}
|
||||
|
||||
int LuaBinding::PushEqual( lua_State *L )
|
||||
{
|
||||
lua_pushboolean( L, Equal(L) );
|
||||
return 1;
|
||||
}
|
||||
|
||||
bool LuaBinding::Equal( lua_State *L )
|
||||
{
|
||||
int iArg1 = lua_gettop( L ) - 1;
|
||||
int iArg2 = lua_gettop( L );
|
||||
|
||||
int iType = lua_type( L, iArg1 );
|
||||
if( iType != lua_type(L, iArg2) )
|
||||
return false;
|
||||
|
||||
if( iType != LUA_TTABLE && iType != LUA_TUSERDATA )
|
||||
return false;
|
||||
|
||||
/* Use the regular method for tables. If an object's table is
|
||||
* kept around after the actual object has been destroyed, the
|
||||
* table is still valid, and the pointer no longer exists. */
|
||||
if( iType == LUA_TTABLE )
|
||||
return !!lua_rawequal( L, iArg1, iArg2 );
|
||||
|
||||
// This checks that they're the same type. it does not check
|
||||
// that it's actually a LuaBinding type. If iArg1 is a non-LuaBinding
|
||||
// type, this function should not be called and the return value is
|
||||
// undefined, but the lua_objlen check below will prevent us from crashing.
|
||||
if( !lua_getmetatable(L, iArg1) )
|
||||
return false;
|
||||
if( !lua_getmetatable(L, iArg2) )
|
||||
{
|
||||
lua_pop( L, 1 );
|
||||
return false;
|
||||
}
|
||||
|
||||
bool bSameType = !!lua_rawequal( L, -1, -2 );
|
||||
lua_pop( L, 2 );
|
||||
if( !bSameType )
|
||||
return false;
|
||||
|
||||
if( lua_objlen(L, iArg1) != sizeof(void *) )
|
||||
return false;
|
||||
if( lua_objlen(L, iArg2) != sizeof(void *) )
|
||||
return false;
|
||||
|
||||
void **pData1 = (void **) lua_touserdata( L, iArg1 );
|
||||
void **pData2 = (void **) lua_touserdata( L, iArg2 );
|
||||
return *pData1 == *pData2;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -114,7 +276,6 @@ void LuaBinding::ApplyDerivedType( Lua *L, const RString &sClassName, void *pSel
|
||||
{
|
||||
void *pData = lua_touserdata( L, -1 );
|
||||
ASSERT( pSelf == pData );
|
||||
ASSERT( pSelf == lua_touserdata(L, -1) );
|
||||
|
||||
lua_settop( L, iTable );
|
||||
return;
|
||||
@@ -136,19 +297,32 @@ void LuaBinding::ApplyDerivedType( Lua *L, const RString &sClassName, void *pSel
|
||||
#include "RageUtil_AutoPtr.h"
|
||||
REGISTER_CLASS_TRAITS( LuaClass, new LuaClass(*pCopy) )
|
||||
|
||||
void *LuaBinding::GetUserdataFromGlobalTable( Lua *L, const char *szType, int iArg )
|
||||
void *LuaBinding::GetPointerFromStack( Lua *L, const RString &sType, int iArg )
|
||||
{
|
||||
GetGlobalTable( L );
|
||||
iArg = LuaHelpers::AbsIndex( L, iArg );
|
||||
|
||||
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 );
|
||||
/* The stack has a userdata or a table. If it's a table, look up the associated userdata. */
|
||||
if( lua_istable(L, iArg) )
|
||||
{
|
||||
GetGlobalTable( L );
|
||||
|
||||
void *pRet = lua_touserdata( L, -1 );
|
||||
lua_pop( L, 2 );
|
||||
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)", sType.c_str() );
|
||||
|
||||
return pRet;
|
||||
void *pRet = lua_touserdata( L, -1 );
|
||||
lua_pop( L, 2 );
|
||||
|
||||
return pRet;
|
||||
}
|
||||
else if( lua_isuserdata(L, iArg) )
|
||||
{
|
||||
void **pData = (void **) lua_touserdata( L, iArg );
|
||||
return *pData;
|
||||
}
|
||||
else
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Tricky: when an instance table is copied, we want to do a deep
|
||||
|
||||
Reference in New Issue
Block a user