Skip to content

Commit

Permalink
isolate lua RString use and remove it
Browse files Browse the repository at this point in the history
  • Loading branch information
MinaciousGrace committed May 7, 2017
1 parent c141e20 commit de63892
Show file tree
Hide file tree
Showing 12 changed files with 1,070 additions and 385 deletions.
55 changes: 28 additions & 27 deletions src/LuaBinding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include "LuaBinding.h"
#include "LuaReference.h"
#include "RageUtil.h"
#include "Foreach.h"

#include "SubscriptionManager.h"
static SubscriptionManager<LuaBinding> m_Subscribers;
Expand All @@ -11,15 +10,17 @@ namespace
{
void RegisterTypes( lua_State *L )
{
if( m_Subscribers.m_pSubscribers == NULL )
if( m_Subscribers.m_pSubscribers == nullptr )
return;

/* Register base classes first. */
map<RString, LuaBinding *> mapToRegister;
FOREACHS( LuaBinding*, *m_Subscribers.m_pSubscribers, p )
mapToRegister[(*p)->GetClassName()] = (*p);
std::map<std::string, LuaBinding *> mapToRegister;
for (auto *p: *m_Subscribers.m_pSubscribers)
{
mapToRegister[p->GetClassName()] = p;
}

set<RString> setRegisteredAlready;
std::set<std::string> setRegisteredAlready;

while( !mapToRegister.empty() )
{
Expand All @@ -32,8 +33,8 @@ namespace
{
break;
}
RString sBase = pBinding->GetBaseClassName();
map<RString, LuaBinding *>::const_iterator it = mapToRegister.find(sBase);
std::string sBase = pBinding->GetBaseClassName();
auto it = mapToRegister.find(sBase);
if( it != mapToRegister.end() )
{
pBinding = it->second;
Expand Down Expand Up @@ -75,7 +76,7 @@ void LuaBinding::Register( lua_State *L )
int methods = lua_gettop( L );

/* Create a metatable for the userdata objects. */
luaL_newmetatable( L, GetClassName() );
luaL_newmetatable( L, GetClassName().c_str() );
int metatable = lua_gettop( L );

// We use the metatable to determine the type of the table, so don't
Expand All @@ -101,34 +102,34 @@ void LuaBinding::Register( lua_State *L )
// to the base class.
if( IsDerivedClass() )
{
lua_getfield( L, LUA_GLOBALSINDEX, GetBaseClassName() );
lua_getfield( L, LUA_GLOBALSINDEX, GetBaseClassName().c_str() );
lua_setfield( L, methods_metatable, "__index" );

lua_pushstring( L, GetBaseClassName() );
lua_pushstring( L, GetBaseClassName().c_str() );
lua_setfield( L, metatable, "base" );
}

lua_pushstring( L, GetClassName() );
lua_pushstring( L, GetClassName().c_str() );
lua_setfield( L, methods_metatable, "class" );

lua_pushstring( L, GetClassName() );
lua_pushstring( L, GetClassName().c_str() );
LuaHelpers::PushValueFunc( L, 1 );
lua_setfield( L, metatable, "__type" ); // for luaL_pushtype

{
lua_newtable( L );
int iHeirarchyTable = lua_gettop( L );

RString sClass = GetClassName();
std::string sClass = GetClassName();
int iIndex = 0;
while( !sClass.empty() )
{
lua_pushstring( L, sClass );
lua_pushstring( L, sClass.c_str() );
lua_pushinteger( L, iIndex );
lua_rawset( L, iHeirarchyTable );
++iIndex;

luaL_getmetatable( L, sClass );
luaL_getmetatable( L, sClass.c_str() );
ASSERT( !lua_isnil(L, -1) );
lua_getfield( L, -1, "base" );

Expand All @@ -155,11 +156,11 @@ void LuaBinding::Register( lua_State *L )
// types (eg. "Actor.x(GAMESTATE, 10)"), which will crash or cause corruption.
// #define FAST_LUA

void LuaBinding::CreateMethodsTable( lua_State *L, const RString &sName )
void LuaBinding::CreateMethodsTable( lua_State *L, const std::string &sName )
{
lua_newtable( L );
lua_pushvalue( L, -1 );
lua_setfield( L, LUA_GLOBALSINDEX, sName );
lua_setfield( L, LUA_GLOBALSINDEX, sName.c_str() );
}

int LuaBinding::PushEqual( lua_State *L )
Expand Down Expand Up @@ -217,7 +218,7 @@ bool LuaBinding::Equal( lua_State *L )
* Get a userdata, and check that it's either szType or a type
* derived from szType, by checking the heirarchy table.
*/
bool LuaBinding::CheckLuaObjectType( lua_State *L, int iArg, const char *szType )
bool LuaBinding::CheckLuaObjectType( lua_State *L, int iArg, std::string const &szType )
{
#if defined(FAST_LUA)
return true;
Expand All @@ -233,7 +234,7 @@ bool LuaBinding::CheckLuaObjectType( lua_State *L, int iArg, const char *szType
return false;
}

lua_getfield( L, -1, szType );
lua_getfield( L, -1, szType.c_str() );
bool bRet = !lua_isnil( L, -1 );
lua_pop( L, 2 );

Expand All @@ -255,7 +256,7 @@ static void GetGlobalTable( Lua *L )
/* 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 LuaBinding::ApplyDerivedType( Lua *L, const RString &sClassName, void *pSelf )
void LuaBinding::ApplyDerivedType( Lua *L, const std::string &sClassName, void *pSelf )
{
int iTable = lua_gettop( L );

Expand Down Expand Up @@ -290,14 +291,14 @@ void LuaBinding::ApplyDerivedType( Lua *L, const RString &sClassName, void *pSel
lua_settop( L, iTable );
}

luaL_getmetatable( L, sClassName );
luaL_getmetatable( L, sClassName.c_str() );
lua_setmetatable( L, iTable );
}

#include "RageUtil_AutoPtr.h"
REGISTER_CLASS_TRAITS( LuaClass, new LuaClass(*pCopy) )

void *LuaBinding::GetPointerFromStack( Lua *L, const RString &sType, int iArg )
void *LuaBinding::GetPointerFromStack( Lua *L, const std::string &sType, int iArg )
{
iArg = LuaHelpers::AbsIndex( L, iArg );

Expand All @@ -322,7 +323,7 @@ void *LuaBinding::GetPointerFromStack( Lua *L, const RString &sType, int iArg )
return *pData;
}
else
return NULL;
return nullptr;
}

/* Tricky: when an instance table is copied, we want to do a deep
Expand Down Expand Up @@ -354,7 +355,7 @@ LuaClass &LuaClass::operator=( const LuaClass &cpy )

LuaClass::~LuaClass()
{
if( LUA == NULL )
if( LUA == nullptr )
return;

Lua *L = LUA->Get();
Expand Down Expand Up @@ -394,7 +395,7 @@ float FArgGTEZero(lua_State* L, int index)
/*
* (c) 2005 Glenn Maynard
* All rights reserved.
*
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
Expand All @@ -404,7 +405,7 @@ float FArgGTEZero(lua_State* L, int index)
* copyright notice(s) and this permission notice appear in all copies of
* the Software and that both the above copyright notice(s) and this
* permission notice appear in supporting documentation.
*
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
Expand Down
Loading

0 comments on commit de63892

Please sign in to comment.