From: John L. <jr...@us...> - 2007-08-10 01:41:11
|
Update of /cvsroot/wxlua/wxLua/modules/wxlua/src In directory sc8-pr-cvs9.sourceforge.net:/tmp/cvs-serv10860/wxLua/modules/wxlua/src Modified Files: wxlstate.cpp Log Message: Push the wxLuaState into Lua instead of the wxLuaStateRefData Also Hash the wxLuaState instead of the wxLuaStateRefData Index: wxlstate.cpp =================================================================== RCS file: /cvsroot/wxlua/wxLua/modules/wxlua/src/wxlstate.cpp,v retrieving revision 1.131 retrieving revision 1.132 diff -C2 -d -r1.131 -r1.132 *** wxlstate.cpp 8 Aug 2007 02:55:33 -0000 1.131 --- wxlstate.cpp 10 Aug 2007 01:41:08 -0000 1.132 *************** *** 26,30 **** int wxlua_lreg_classes_key = WXLUA_LREG_CLASSES; int wxlua_lreg_derivedmethods_key = WXLUA_LREG_DERIVED_METHODS; ! int wxlua_lreg_wxluastaterefdata_key = WXLUA_LREG_WXLUASTATEREFDATA; int wxlua_lreg_objects_key = WXLUA_LREG_OBJECTS; int wxlua_lreg_callbaseclassfunc_key = WXLUA_LREG_CALLBASECLASSFUNC; --- 26,30 ---- int wxlua_lreg_classes_key = WXLUA_LREG_CLASSES; int wxlua_lreg_derivedmethods_key = WXLUA_LREG_DERIVED_METHODS; ! int wxlua_lreg_wxluastate_key = WXLUA_LREG_WXLUASTATE; int wxlua_lreg_objects_key = WXLUA_LREG_OBJECTS; int wxlua_lreg_callbaseclassfunc_key = WXLUA_LREG_CALLBASECLASSFUNC; *************** *** 1246,1250 **** //---------------------------------------------------------------------------- ! wxHashMapLuaStateRefData wxLuaStateRefData::s_wxHashMapLuaStateRefData; wxLuaStateRefData::wxLuaStateRefData(bool create_data) --- 1246,1250 ---- //---------------------------------------------------------------------------- ! wxHashMapLuaState wxLuaStateRefData::s_wxHashMapLuaState; wxLuaStateRefData::wxLuaStateRefData(bool create_data) *************** *** 1317,1334 **** ClearCallbacks(); - CleanupWxLua(!m_lua_State_static); ! return true; ! } ! ! void wxLuaStateRefData::CleanupWxLua(bool closeLua) ! { ! if (closeLua && (m_lua_State != NULL)) { UnRegisterBindings(); lua_close(m_lua_State); - s_wxHashMapLuaStateRefData.erase(m_lua_State); - m_lua_State = NULL; } } --- 1317,1345 ---- ClearCallbacks(); ! if ((m_lua_State != NULL) && !m_lua_State_static) { UnRegisterBindings(); lua_close(m_lua_State); } + + // Clear out the wxLuaState we hashed, note it's not refed so we have + // NULL it's ref data. Note: even though the lua_State is closed the pointer + // value is still good. + if (m_lua_State != NULL) + { + wxHashMapLuaState::iterator it = s_wxHashMapLuaState.find(m_lua_State); + if (it != s_wxHashMapLuaState.end()) + { + wxLuaState* wxlState = it->second; + wxlState->SetRefData(NULL); + delete wxlState; + s_wxHashMapLuaState.erase(m_lua_State); + } + } + + m_lua_State = NULL; + + return true; } *************** *** 1388,1416 **** } ! wxLuaStateRefData* wxLuaStateRefData::GetLuaStateRefData(lua_State* L) { // try our hashtable for faster lookup ! wxHashMapLuaStateRefData::iterator it = s_wxHashMapLuaStateRefData.find(L); ! if (it != s_wxHashMapLuaStateRefData.end()) { //wxPrintf(wxT("GetLuaStateRefData L %d data %d\n"), (int)L, (int)it->second); ! return it->second; } - else // it's a coroutine? look up the state data from Lua - { - wxLuaStateRefData* d = NULL; ! // try to get the state we've stored ! wxlua_pushkey_wxLuaStateRefData(L); ! lua_rawget( L, LUA_REGISTRYINDEX ); ! // if nothing was returned or it wasn't a ptr, abort ! if ( lua_islightuserdata(L, -1) ) ! d = (wxLuaStateRefData*)lua_touserdata( L, -1 ); ! lua_pop(L, 1); // pop the wxLuaStateRefData or nil on failure ! return d; } } --- 1399,1447 ---- } ! wxLuaState wxLuaStateRefData::GetLuaState(lua_State* L) { // try our hashtable for faster lookup ! wxHashMapLuaState::iterator it = s_wxHashMapLuaState.find(L); ! if (it != s_wxHashMapLuaState.end()) { //wxPrintf(wxT("GetLuaStateRefData L %d data %d\n"), (int)L, (int)it->second); ! return wxLuaState(*it->second); } ! // else it's a coroutine? look up the state data from Lua ! wxLuaState* wxlState = NULL; ! // try to get the state we've stored ! wxlua_pushkey_wxLuaState(L); ! lua_rawget( L, LUA_REGISTRYINDEX ); ! // if nothing was returned or it wasn't a ptr, abort ! if ( lua_islightuserdata(L, -1) ) ! wxlState = (wxLuaState*)lua_touserdata( L, -1 ); ! lua_pop(L, 1); // pop the wxLuaStateRefData or nil on failure ! ! if (wxlState && (wxlState->GetLuaState() != L)) ! { ! // Create a new wxLuaState for the coroutine and set the wxLuaStateData ! // to the original wxLuaState's data ! wxLuaStateRefData* refData = new wxLuaStateRefData(false); ! refData->m_lua_State = L; ! refData->m_lua_State_static = true; ! refData->m_lua_State_coroutine = true; ! ! refData->m_wxlStateData = wxlState->GetLuaStateData(); ! refData->m_own_stateData = false; ! ! wxLuaState wxlState2(false); ! wxlState2.SetRefData(refData); ! return wxlState2; } + else if (wxlState) + { + return wxLuaState(*wxlState); + } + + return wxNullLuaState; } *************** *** 1456,1464 **** // track this newly created lua_State in our hashtable to help us // find the wxLuaStateRefData from the lua_State quickly ! wxLuaStateRefData::s_wxHashMapLuaStateRefData[L] = M_WXLSTATEDATA; // Stick us into the lua_State - push key, value ! wxlua_pushkey_wxLuaStateRefData(L); ! lua_pushlightuserdata( L, (void*)m_refData ); lua_rawset( L, LUA_REGISTRYINDEX ); // set the value --- 1487,1497 ---- // track this newly created lua_State in our hashtable to help us // find the wxLuaStateRefData from the lua_State quickly ! wxLuaState* hashState = new wxLuaState(false); ! hashState->SetRefData(m_refData); ! wxLuaStateRefData::s_wxHashMapLuaState[L] = hashState; // Stick us into the lua_State - push key, value ! wxlua_pushkey_wxLuaState(L); ! lua_pushlightuserdata( L, (void*)hashState ); lua_rawset( L, LUA_REGISTRYINDEX ); // set the value *************** *** 1484,1490 **** if (type == WXLUASTATE_ATTACH) { ! wxLuaStateRefData *refData = wxLuaStateRefData::GetLuaStateRefData(L); ! wxCHECK_MSG(refData != NULL, false, wxT("Unable to find lua_State")); ! return Create(refData, L); } else if (type == WXLUASTATE_SETSTATE) --- 1517,1522 ---- if (type == WXLUASTATE_ATTACH) { ! Ref(wxLuaStateRefData::GetLuaState(L)); ! return Ok(); } else if (type == WXLUASTATE_SETSTATE) *************** *** 1493,1501 **** M_WXLSTATEDATA->m_lua_State = L; M_WXLSTATEDATA->m_lua_State_static = true; ! wxLuaStateRefData::s_wxHashMapLuaStateRefData[L] = M_WXLSTATEDATA; // Stick us into the lua_State - push key, value ! wxlua_pushkey_wxLuaStateRefData(L); ! lua_pushlightuserdata( L, (void*)m_refData ); lua_rawset( L, LUA_REGISTRYINDEX ); // set the value --- 1525,1536 ---- M_WXLSTATEDATA->m_lua_State = L; M_WXLSTATEDATA->m_lua_State_static = true; ! ! wxLuaState* hashState = new wxLuaState(false); ! hashState->SetRefData(m_refData); ! wxLuaStateRefData::s_wxHashMapLuaState[L] = hashState; // Stick us into the lua_State - push key, value ! wxlua_pushkey_wxLuaState(L); ! lua_pushlightuserdata( L, (void*)hashState ); lua_rawset( L, LUA_REGISTRYINDEX ); // set the value *************** *** 1518,1555 **** } - bool wxLuaState::Create(wxLuaStateRefData* refData, lua_State* L) - { - // nothing to be done - if (m_refData == refData) - return true; - - // delete reference to old data - Destroy(); - - // reference new data - if ( refData && (L == refData->m_lua_State)) - { - // we need to increment the ref count, but it's private in wxObjectRefData - wxLuaState lS(false); - lS.SetRefData(refData); - Ref(lS); - lS.SetRefData(NULL); - } - else - { - // Create a new wxLuaState for the coroutine and set the wxLuaStateData - // to the original wxLuaState's data - m_refData = new wxLuaStateRefData(false); - M_WXLSTATEDATA->m_lua_State = L; - M_WXLSTATEDATA->m_lua_State_static = true; - M_WXLSTATEDATA->m_lua_State_coroutine = true; - - M_WXLSTATEDATA->m_wxlStateData = refData->m_wxlStateData; - M_WXLSTATEDATA->m_own_stateData = false; - } - - return true; - } - bool wxLuaState::Ok() const { --- 1553,1556 ---- *************** *** 1957,1966 **** lua_State* L = M_WXLSTATEDATA->m_lua_State; - // Stick us into the lua_State - push key, value - // make sure we're there, even though we might have already done this. - wxlua_pushkey_wxLuaStateRefData(L); - lua_pushlightuserdata( L, (void*)m_refData ); - lua_rawset( L, LUA_REGISTRYINDEX ); // set the value - // create the wxLuaReferences table in registry wxlua_pushkey_wxLuaReferences(L); --- 1958,1961 ---- *************** *** 3023,3031 **** wxCHECK_MSG(pObject, wxNullLuaState, wxT("Invalid object to wxLuaState::GetDerivedMethod")); ! wxHashMapLuaStateRefData::iterator it; ! for (it = wxLuaStateRefData::s_wxHashMapLuaStateRefData.begin(); ! it != wxLuaStateRefData::s_wxHashMapLuaStateRefData.end(); ++it) { ! wxLuaState wxlState((wxLuaStateRefData*)it->second, ((wxLuaStateRefData*)it->second)->m_lua_State); if (wxlState.HasDerivedMethod(pObject, method, false)) return wxlState; --- 3018,3026 ---- wxCHECK_MSG(pObject, wxNullLuaState, wxT("Invalid object to wxLuaState::GetDerivedMethod")); ! wxHashMapLuaState::iterator it; ! for (it = wxLuaStateRefData::s_wxHashMapLuaState.begin(); ! it != wxLuaStateRefData::s_wxHashMapLuaState.end(); ++it) { ! wxLuaState wxlState((wxLuaState*)it->second); if (wxlState.HasDerivedMethod(pObject, method, false)) return wxlState; |