use Lua locking

This commit is contained in:
Glenn Maynard
2005-06-16 06:55:34 +00:00
parent d48bce4b66
commit 11c368bf2a
3 changed files with 133 additions and 109 deletions
+9 -5
View File
@@ -154,23 +154,27 @@ void MenuTimer::Start()
void MenuTimer::SetText( float fSeconds ) void MenuTimer::SetText( float fSeconds )
{ {
Lua *L = LUA->Get();
for( int i=0; i<NUM_MENU_TIMER_TEXTS; i++ ) for( int i=0; i<NUM_MENU_TIMER_TEXTS; i++ )
{ {
// function // function
m_exprFormatText[i].PushSelf( LUA->L ); m_exprFormatText[i].PushSelf( L );
ASSERT( !lua_isnil(LUA->L, -1) ); ASSERT( !lua_isnil(L, -1) );
// 1st parameter // 1st parameter
LuaHelpers::Push( fSeconds, LUA->L ); LuaHelpers::Push( fSeconds, L );
// call function with 1 argument and 1 result // call function with 1 argument and 1 result
lua_call(LUA->L, 1, 1); lua_call(L, 1, 1);
CString sText; CString sText;
LuaHelpers::PopStack( sText, LUA->L ); LuaHelpers::PopStack( sText, L );
m_text[i].SetText( sText ); m_text[i].SetText( sText );
} }
LUA->Release(L);
} }
/* /*
+117 -101
View File
@@ -514,181 +514,191 @@ public:
m_sName = sLuaFunction; m_sName = sLuaFunction;
defOut.m_bAllowThemeItems = false; // Lua options are always dynamic and can theme themselves. defOut.m_bAllowThemeItems = false; // Lua options are always dynamic and can theme themselves.
Lua *L = LUA->Get();
/* Run the Lua expression. It should return a table. */ /* Run the Lua expression. It should return a table. */
m_pLuaTable->SetFromExpression( sLuaFunction ); m_pLuaTable->SetFromExpression( sLuaFunction );
if( m_pLuaTable->GetLuaType() != LUA_TTABLE ) if( m_pLuaTable->GetLuaType() != LUA_TTABLE )
RageException::Throw( "Result of \"%s\" is not a table", sLuaFunction.c_str() ); RageException::Throw( "Result of \"%s\" is not a table", sLuaFunction.c_str() );
m_pLuaTable->PushSelf( LUA->L ); m_pLuaTable->PushSelf( L );
lua_pushstring( LUA->L, "Name" ); lua_pushstring( L, "Name" );
lua_gettable( LUA->L, -2 ); lua_gettable( L, -2 );
const char *pStr = lua_tostring( LUA->L, -1 ); const char *pStr = lua_tostring( L, -1 );
if( pStr == NULL ) if( pStr == NULL )
RageException::Throw( "\"%s\" \"Name\" entry is not a string", sLuaFunction.c_str() ); RageException::Throw( "\"%s\" \"Name\" entry is not a string", sLuaFunction.c_str() );
defOut.name = pStr; defOut.name = pStr;
lua_pop( LUA->L, 1 ); lua_pop( L, 1 );
lua_pushstring( LUA->L, "OneChoiceForAllPlayers" ); lua_pushstring( L, "OneChoiceForAllPlayers" );
lua_gettable( LUA->L, -2 ); lua_gettable( L, -2 );
defOut.bOneChoiceForAllPlayers = !!lua_toboolean( LUA->L, -1 ); defOut.bOneChoiceForAllPlayers = !!lua_toboolean( L, -1 );
lua_pop( LUA->L, 1 ); lua_pop( L, 1 );
lua_pushstring( LUA->L, "ExportOnChange" ); lua_pushstring( L, "ExportOnChange" );
lua_gettable( LUA->L, -2 ); lua_gettable( L, -2 );
defOut.m_bExportOnChange = !!lua_toboolean( LUA->L, -1 ); defOut.m_bExportOnChange = !!lua_toboolean( L, -1 );
lua_pop( LUA->L, 1 ); lua_pop( L, 1 );
lua_pushstring( LUA->L, "LayoutType" ); lua_pushstring( L, "LayoutType" );
lua_gettable( LUA->L, -2 ); lua_gettable( L, -2 );
pStr = lua_tostring( LUA->L, -1 ); pStr = lua_tostring( L, -1 );
if( pStr == NULL ) if( pStr == NULL )
RageException::Throw( "\"%s\" \"LayoutType\" entry is not a string", sLuaFunction.c_str() ); RageException::Throw( "\"%s\" \"LayoutType\" entry is not a string", sLuaFunction.c_str() );
defOut.layoutType = StringToLayoutType( pStr ); defOut.layoutType = StringToLayoutType( pStr );
ASSERT( defOut.layoutType != LAYOUT_INVALID ); ASSERT( defOut.layoutType != LAYOUT_INVALID );
lua_pop( LUA->L, 1 ); lua_pop( L, 1 );
lua_pushstring( LUA->L, "SelectType" ); lua_pushstring( L, "SelectType" );
lua_gettable( LUA->L, -2 ); lua_gettable( L, -2 );
pStr = lua_tostring( LUA->L, -1 ); pStr = lua_tostring( L, -1 );
if( pStr == NULL ) if( pStr == NULL )
RageException::Throw( "\"%s\" \"SelectType\" entry is not a string", sLuaFunction.c_str() ); RageException::Throw( "\"%s\" \"SelectType\" entry is not a string", sLuaFunction.c_str() );
defOut.selectType = StringToSelectType( pStr ); defOut.selectType = StringToSelectType( pStr );
ASSERT( defOut.selectType != SELECT_INVALID ); ASSERT( defOut.selectType != SELECT_INVALID );
lua_pop( LUA->L, 1 ); lua_pop( L, 1 );
/* Iterate over the "Choices" table. */ /* Iterate over the "Choices" table. */
lua_pushstring( LUA->L, "Choices" ); lua_pushstring( L, "Choices" );
lua_gettable( LUA->L, -2 ); lua_gettable( L, -2 );
if( !lua_istable( LUA->L, -1 ) ) if( !lua_istable( L, -1 ) )
RageException::Throw( "\"%s\" \"Choices\" is not a table", sLuaFunction.c_str() ); RageException::Throw( "\"%s\" \"Choices\" is not a table", sLuaFunction.c_str() );
lua_pushnil( LUA->L ); lua_pushnil( L );
while( lua_next(LUA->L, -2) != 0 ) while( lua_next(L, -2) != 0 )
{ {
/* `key' is at index -2 and `value' at index -1 */ /* `key' is at index -2 and `value' at index -1 */
const char *pValue = lua_tostring( LUA->L, -1 ); const char *pValue = lua_tostring( L, -1 );
if( pValue == NULL ) if( pValue == NULL )
RageException::Throw( "\"%s\" Column entry is not a string", sLuaFunction.c_str() ); RageException::Throw( "\"%s\" Column entry is not a string", sLuaFunction.c_str() );
// LOG->Trace( "'%s'", pValue); // LOG->Trace( "'%s'", pValue);
defOut.choices.push_back( pValue ); defOut.choices.push_back( pValue );
lua_pop( LUA->L, 1 ); /* removes `value'; keeps `key' for next iteration */ lua_pop( L, 1 ); /* removes `value'; keeps `key' for next iteration */
} }
lua_pop( LUA->L, 1 ); /* pop choices table */ lua_pop( L, 1 ); /* pop choices table */
/* Iterate over the "EnabledForPlayers" table. */ /* Iterate over the "EnabledForPlayers" table. */
lua_pushstring( LUA->L, "EnabledForPlayers" ); lua_pushstring( L, "EnabledForPlayers" );
lua_gettable( LUA->L, -2 ); lua_gettable( L, -2 );
if( !lua_isnil( LUA->L, -1 ) ) if( !lua_isnil( L, -1 ) )
{ {
if( !lua_istable( LUA->L, -1 ) ) if( !lua_istable( L, -1 ) )
RageException::Throw( "\"%s\" \"EnabledForPlayers\" is not a table", sLuaFunction.c_str() ); RageException::Throw( "\"%s\" \"EnabledForPlayers\" is not a table", sLuaFunction.c_str() );
defOut.m_vEnabledForPlayers.clear(); // and fill in with supplied PlayerNumbers below defOut.m_vEnabledForPlayers.clear(); // and fill in with supplied PlayerNumbers below
lua_pushnil( LUA->L ); lua_pushnil( L );
while( lua_next(LUA->L, -2) != 0 ) while( lua_next(L, -2) != 0 )
{ {
/* `key' is at index -2 and `value' at index -1 */ /* `key' is at index -2 and `value' at index -1 */
PlayerNumber pn = (PlayerNumber)luaL_checkint( LUA->L, -1 ); PlayerNumber pn = (PlayerNumber)luaL_checkint( L, -1 );
defOut.m_vEnabledForPlayers.insert( pn ); defOut.m_vEnabledForPlayers.insert( pn );
lua_pop( LUA->L, 1 ); /* removes `value'; keeps `key' for next iteration */ lua_pop( L, 1 ); /* removes `value'; keeps `key' for next iteration */
} }
} }
lua_pop( LUA->L, 1 ); /* pop EnabledForPlayers table */ lua_pop( L, 1 ); /* pop EnabledForPlayers table */
/* Iterate over the "ReloadRowMessages" table. */ /* Iterate over the "ReloadRowMessages" table. */
lua_pushstring( LUA->L, "ReloadRowMessages" ); lua_pushstring( L, "ReloadRowMessages" );
lua_gettable( LUA->L, -2 ); lua_gettable( L, -2 );
if( !lua_isnil( LUA->L, -1 ) ) if( !lua_isnil( L, -1 ) )
{ {
if( !lua_istable( LUA->L, -1 ) ) if( !lua_istable( L, -1 ) )
RageException::Throw( "\"%s\" \"ReloadRowMessages\" is not a table", sLuaFunction.c_str() ); RageException::Throw( "\"%s\" \"ReloadRowMessages\" is not a table", sLuaFunction.c_str() );
lua_pushnil( LUA->L ); lua_pushnil( L );
while( lua_next(LUA->L, -2) != 0 ) while( lua_next(L, -2) != 0 )
{ {
/* `key' is at index -2 and `value' at index -1 */ /* `key' is at index -2 and `value' at index -1 */
const char *pValue = lua_tostring( LUA->L, -1 ); const char *pValue = lua_tostring( L, -1 );
if( pValue == NULL ) if( pValue == NULL )
RageException::Throw( "\"%s\" Column entry is not a string", sLuaFunction.c_str() ); RageException::Throw( "\"%s\" Column entry is not a string", sLuaFunction.c_str() );
LOG->Trace( "Found ReloadRowMessage '%s'", pValue); LOG->Trace( "Found ReloadRowMessage '%s'", pValue);
m_vsReloadRowMessages.push_back( pValue ); m_vsReloadRowMessages.push_back( pValue );
lua_pop( LUA->L, 1 ); /* removes `value'; keeps `key' for next iteration */ lua_pop( L, 1 ); /* removes `value'; keeps `key' for next iteration */
} }
} }
lua_pop( LUA->L, 1 ); /* pop ReloadRowMessages table */ lua_pop( L, 1 ); /* pop ReloadRowMessages table */
/* Look for "ExportOnChange" value. */ /* Look for "ExportOnChange" value. */
lua_pushstring( LUA->L, "ExportOnChange" ); lua_pushstring( L, "ExportOnChange" );
lua_gettable( LUA->L, -2 ); lua_gettable( L, -2 );
if( !lua_isnil( LUA->L, -1 ) ) if( !lua_isnil( L, -1 ) )
{ {
defOut.m_bExportOnChange = !!MyLua_checkboolean( LUA->L, -1 ); defOut.m_bExportOnChange = !!MyLua_checkboolean( L, -1 );
} }
lua_pop( LUA->L, 1 ); /* pop ExportOnChange value */ lua_pop( L, 1 ); /* pop ExportOnChange value */
lua_pop( LUA->L, 1 ); /* pop main table */ lua_pop( L, 1 ); /* pop main table */
ASSERT( lua_gettop(LUA->L) == 0 ); ASSERT( lua_gettop(L) == 0 );
LUA->Release(L);
} }
virtual void Reload( OptionRowDefinition &defOut ) virtual void Reload( OptionRowDefinition &defOut )
{ {
Lua *L = LUA->Get();
/* Run the Lua expression. It should return a table. */ /* Run the Lua expression. It should return a table. */
m_pLuaTable->SetFromExpression( m_sName ); m_pLuaTable->SetFromExpression( m_sName );
if( m_pLuaTable->GetLuaType() != LUA_TTABLE ) if( m_pLuaTable->GetLuaType() != LUA_TTABLE )
RageException::Throw( "Result of \"%s\" is not a table", m_sName.c_str() ); RageException::Throw( "Result of \"%s\" is not a table", m_sName.c_str() );
m_pLuaTable->PushSelf( LUA->L ); m_pLuaTable->PushSelf( L );
/* Iterate over the "EnabledForPlayers" table. */ /* Iterate over the "EnabledForPlayers" table. */
lua_pushstring( LUA->L, "EnabledForPlayers" ); lua_pushstring( L, "EnabledForPlayers" );
lua_gettable( LUA->L, -2 ); lua_gettable( L, -2 );
if( !lua_isnil( LUA->L, -1 ) ) if( !lua_isnil( L, -1 ) )
{ {
if( !lua_istable( LUA->L, -1 ) ) if( !lua_istable( L, -1 ) )
RageException::Throw( "\"%s\" \"EnabledForPlayers\" is not a table", m_sName.c_str() ); RageException::Throw( "\"%s\" \"EnabledForPlayers\" is not a table", m_sName.c_str() );
defOut.m_vEnabledForPlayers.clear(); // and fill in with supplied PlayerNumbers below defOut.m_vEnabledForPlayers.clear(); // and fill in with supplied PlayerNumbers below
lua_pushnil( LUA->L ); lua_pushnil( L );
while( lua_next(LUA->L, -2) != 0 ) while( lua_next(L, -2) != 0 )
{ {
/* `key' is at index -2 and `value' at index -1 */ /* `key' is at index -2 and `value' at index -1 */
PlayerNumber pn = (PlayerNumber)luaL_checkint( LUA->L, -1 ); PlayerNumber pn = (PlayerNumber)luaL_checkint( L, -1 );
defOut.m_vEnabledForPlayers.insert( pn ); defOut.m_vEnabledForPlayers.insert( pn );
lua_pop( LUA->L, 1 ); /* removes `value'; keeps `key' for next iteration */ lua_pop( L, 1 ); /* removes `value'; keeps `key' for next iteration */
} }
} }
lua_pop( LUA->L, 1 ); /* pop EnabledForPlayers table */ lua_pop( L, 1 ); /* pop EnabledForPlayers table */
lua_pop( LUA->L, 1 ); /* pop main table */ lua_pop( L, 1 ); /* pop main table */
ASSERT( lua_gettop(LUA->L) == 0 ); ASSERT( lua_gettop(L) == 0 );
LUA->Release(L);
} }
virtual void ImportOption( const OptionRowDefinition &def, const vector<PlayerNumber> &vpns, vector<bool> vbSelectedOut[NUM_PLAYERS] ) const virtual void ImportOption( const OptionRowDefinition &def, const vector<PlayerNumber> &vpns, vector<bool> vbSelectedOut[NUM_PLAYERS] ) const
{ {
ASSERT( lua_gettop(LUA->L) == 0 ); Lua *L = LUA->Get();
ASSERT( lua_gettop(L) == 0 );
FOREACH_CONST( PlayerNumber, vpns, pn ) FOREACH_CONST( PlayerNumber, vpns, pn )
{ {
@@ -703,44 +713,48 @@ public:
vbSelOut[i] = false; vbSelOut[i] = false;
/* Create the vbSelectedOut table. */ /* Create the vbSelectedOut table. */
LuaHelpers::CreateTableFromArrayB( LUA->L, vbSelOut ); LuaHelpers::CreateTableFromArrayB( L, vbSelOut );
ASSERT( lua_gettop(LUA->L) == 1 ); /* vbSelectedOut table */ ASSERT( lua_gettop(L) == 1 ); /* vbSelectedOut table */
/* Get the function to call from m_LuaTable. */ /* Get the function to call from m_LuaTable. */
m_pLuaTable->PushSelf( LUA->L ); m_pLuaTable->PushSelf( L );
ASSERT( lua_istable( LUA->L, -1 ) ); ASSERT( lua_istable( L, -1 ) );
lua_pushstring( LUA->L, "LoadSelections" ); lua_pushstring( L, "LoadSelections" );
lua_gettable( LUA->L, -2 ); lua_gettable( L, -2 );
if( !lua_isfunction( LUA->L, -1 ) ) if( !lua_isfunction( L, -1 ) )
RageException::Throw( "\"%s\" \"LoadSelections\" entry is not a function", def.name.c_str() ); RageException::Throw( "\"%s\" \"LoadSelections\" entry is not a function", def.name.c_str() );
/* Argument 1 (self): */ /* Argument 1 (self): */
m_pLuaTable->PushSelf( LUA->L ); m_pLuaTable->PushSelf( L );
/* Argument 2 (vbSelectedOut): */ /* Argument 2 (vbSelectedOut): */
lua_pushvalue( LUA->L, 1 ); lua_pushvalue( L, 1 );
/* Argument 3 (pn): */ /* Argument 3 (pn): */
LuaHelpers::PushStack( (int) p, LUA->L ); LuaHelpers::PushStack( (int) p, L );
ASSERT( lua_gettop(LUA->L) == 6 ); /* vbSelectedOut, m_iLuaTable, function, self, arg, arg */ ASSERT( lua_gettop(L) == 6 ); /* vbSelectedOut, m_iLuaTable, function, self, arg, arg */
lua_call( LUA->L, 3, 0 ); // call function with 3 arguments and 0 results lua_call( L, 3, 0 ); // call function with 3 arguments and 0 results
ASSERT( lua_gettop(LUA->L) == 2 ); ASSERT( lua_gettop(L) == 2 );
lua_pop( LUA->L, 1 ); /* pop option table */ lua_pop( L, 1 ); /* pop option table */
LuaHelpers::ReadArrayFromTableB( LUA->L, vbSelOut ); LuaHelpers::ReadArrayFromTableB( L, vbSelOut );
lua_pop( LUA->L, 1 ); /* pop vbSelectedOut table */ lua_pop( L, 1 ); /* pop vbSelectedOut table */
ASSERT( lua_gettop(LUA->L) == 0 ); ASSERT( lua_gettop(L) == 0 );
} }
LUA->Release(L);
} }
virtual int ExportOption( const OptionRowDefinition &def, const vector<PlayerNumber> &vpns, const vector<bool> vbSelected[NUM_PLAYERS] ) const virtual int ExportOption( const OptionRowDefinition &def, const vector<PlayerNumber> &vpns, const vector<bool> vbSelected[NUM_PLAYERS] ) const
{ {
ASSERT( lua_gettop(LUA->L) == 0 ); Lua *L = LUA->Get();
ASSERT( lua_gettop(L) == 0 );
FOREACH_CONST( PlayerNumber, vpns, pn ) FOREACH_CONST( PlayerNumber, vpns, pn )
{ {
@@ -753,38 +767,40 @@ public:
vector<bool> vbSelectedCopy = vbSel; vector<bool> vbSelectedCopy = vbSel;
/* Create the vbSelectedOut table. */ /* Create the vbSelectedOut table. */
LuaHelpers::CreateTableFromArrayB( LUA->L, vbSelectedCopy ); LuaHelpers::CreateTableFromArrayB( L, vbSelectedCopy );
ASSERT( lua_gettop(LUA->L) == 1 ); /* vbSelectedOut table */ ASSERT( lua_gettop(L) == 1 ); /* vbSelectedOut table */
/* Get the function to call. */ /* Get the function to call. */
m_pLuaTable->PushSelf( LUA->L ); m_pLuaTable->PushSelf( L );
ASSERT( lua_istable( LUA->L, -1 ) ); ASSERT( lua_istable( L, -1 ) );
lua_pushstring( LUA->L, "SaveSelections" ); lua_pushstring( L, "SaveSelections" );
lua_gettable( LUA->L, -2 ); lua_gettable( L, -2 );
if( !lua_isfunction( LUA->L, -1 ) ) if( !lua_isfunction( L, -1 ) )
RageException::Throw( "\"%s\" \"SaveSelections\" entry is not a function", def.name.c_str() ); RageException::Throw( "\"%s\" \"SaveSelections\" entry is not a function", def.name.c_str() );
/* Argument 1 (self): */ /* Argument 1 (self): */
m_pLuaTable->PushSelf( LUA->L ); m_pLuaTable->PushSelf( L );
/* Argument 2 (vbSelectedOut): */ /* Argument 2 (vbSelectedOut): */
lua_pushvalue( LUA->L, 1 ); lua_pushvalue( L, 1 );
/* Argument 3 (pn): */ /* Argument 3 (pn): */
LuaHelpers::PushStack( (int) p, LUA->L ); LuaHelpers::PushStack( (int) p, L );
ASSERT( lua_gettop(LUA->L) == 6 ); /* vbSelectedOut, m_iLuaTable, function, self, arg, arg */ ASSERT( lua_gettop(L) == 6 ); /* vbSelectedOut, m_iLuaTable, function, self, arg, arg */
lua_call( LUA->L, 3, 0 ); // call function with 3 arguments and 0 results lua_call( L, 3, 0 ); // call function with 3 arguments and 0 results
ASSERT( lua_gettop(LUA->L) == 2 ); ASSERT( lua_gettop(L) == 2 );
lua_pop( LUA->L, 1 ); /* pop option table */ lua_pop( L, 1 ); /* pop option table */
lua_pop( LUA->L, 1 ); /* pop vbSelected table */ lua_pop( L, 1 ); /* pop vbSelected table */
ASSERT( lua_gettop(LUA->L) == 0 ); ASSERT( lua_gettop(L) == 0 );
} }
LUA->Release(L);
// XXX: allow specifying the mask // XXX: allow specifying the mask
return 0; return 0;
} }
+7 -3
View File
@@ -288,12 +288,16 @@ public:
ASSERT( m_sName != "" ); ASSERT( m_sName != "" );
ASSERT( m_bIsLoaded ); ASSERT( m_bIsLoaded );
Lua *L = LUA->Get();
// function // function
m_Expr.PushSelf( LUA->L ); m_Expr.PushSelf( L );
ASSERT( !lua_isnil(LUA->L, -1) ); ASSERT( !lua_isnil(L, -1) );
// call function with 0 arguments and 1 result // call function with 0 arguments and 1 result
lua_call(LUA->L, 0, 1); lua_call(L, 0, 1);
LUA->Release(L);
LuaHelpers::PopStack( m_currentValue, LUA->L ); LuaHelpers::PopStack( m_currentValue, LUA->L );
return m_currentValue; return m_currentValue;