From: John L. <jr...@us...> - 2006-12-06 06:58:28
|
Update of /cvsroot/wxlua/wxLua/modules/wxlua/src In directory sc8-pr-cvs9.sourceforge.net:/tmp/cvs-serv21762/wxLua/modules/wxlua/src Modified Files: wxlstate.cpp Log Message: removed WXLUA_USE_NEWTHREAD_HANDLER and friends by replacing luaE_newthread, luaE_freethread we now look up coroutine wxLuaStateData by using a light user data pushed into the lua registry table the lua is now an unmodified copy of lua with no mods necessary Index: wxlstate.cpp =================================================================== RCS file: /cvsroot/wxlua/wxLua/modules/wxlua/src/wxlstate.cpp,v retrieving revision 1.78 retrieving revision 1.79 diff -C2 -d -r1.78 -r1.79 *** wxlstate.cpp 1 Dec 2006 06:25:47 -0000 1.78 --- wxlstate.cpp 6 Dec 2006 06:58:23 -0000 1.79 *************** *** 44,47 **** --- 44,52 ---- #include "wx/tokenzr.h" + extern "C" + { + #include "lua/src/lstate.h" + } + extern int wxLuaEventListCompareFn(const void *p1, const void *p2); // wxlbind.cpp extern int wxLuaClassListCompareByTag(const void *p1, const void *p2); // wxlbind.cpp *************** *** 76,157 **** // ---------------------------------------------------------------------------- - // WXLUA_LUA_NEWTHREAD - make sure that coroutines lua_State's are tracked - // ---------------------------------------------------------------------------- - - // add lua_States created from lua's coroutines - extern "C" - { - #ifdef WXLUA_LUA_NEWTHREAD - // See lua/src/lstate.c for added hook into luaE_newthread() - #include "lua/include/luathread.h" - #endif //WXLUA_LUA_NEWTHREAD - } - - #ifdef WXLUA_LUA_NEWTHREAD - - // Hook into function calls to luaE_newthread (calls from coroutine.create/wrap) - // and add the new lua_State as a child sharing it's wxLuaStateData - void wxLuaState_newthread_handler(lua_State *L, lua_State *L1) - { - // L is the original lua_State, L1 is the new coroutine thread - wxLuaState wxlState_orig(L); - wxCHECK_RET(wxlState_orig.Ok(), wxT("Invalid wxLuaState")); - - // Create a new wxLuaState for the coroutine and set the wxLuaStateData - // to the original wxLuaState's data - wxLuaState wxlState(L1, WXLUASTATE_COROUTINE); - delete wxlState.GetLuaStateRefData()->m_wxlStateData; - wxlState.GetLuaStateRefData()->m_wxlStateData = wxlState_orig.GetLuaStateRefData()->m_wxlStateData; - wxlState.GetLuaStateRefData()->m_own_stateData = false; - - wxlState_orig.GetLuaStateRefData()->m_coroutineStates.Add(wxlState); - - // put into the hashmap the coroutine's wxLuaStateRefData - wxLuaStateRefData::s_wxHashMapLuaStateRefData[L1] = wxlState.GetLuaStateRefData(); - - //wxPrintf(wxT("wxLuaState_newthread_handler L %d L1 %d, L_wxlsdata %d L1_wxlsdata %d\n"), (long)L, (long)L1, (long)wxlState_orig.GetLuaStateRefData(), (long)wxlState.GetLuaStateRefData()); - - // Stick us into the lua_State - push key, value - //lua_pushstring( L1, "__wxLuaStateRefData" ); - //lua_pushlightuserdata( L1, (void*)wxlState.GetRefData() ); - // set the value - //lua_settable( L1, LUA_REGISTRYINDEX ); - } - - // Hook into function calls to luaE_freethread (calls from coroutine.create/wrap) - // and remove the child lua_State - void wxLuaState_freethread_handler(lua_State *L, lua_State *L1) - { - // L is the original lua_State, L1 is the coroutine thread - wxLuaState wxlState_orig(L); - wxCHECK_RET(wxlState_orig.Ok(), wxT("Invalid wxLuaState")); - wxLuaState wxlState(L1); - wxCHECK_RET(wxlState.Ok(), wxT("Invalid wxLuaState")); - - size_t n, count = wxlState_orig.GetLuaStateRefData()->m_coroutineStates.GetCount(); - for (n = 0; n < count; n++) - { - if (wxlState_orig.GetLuaStateRefData()->m_coroutineStates[n] == wxlState) - { - wxlState_orig.GetLuaStateRefData()->m_coroutineStates.RemoveAt(n); - break; - } - } - - //wxPrintf(wxT("wxLuaInterpreter_freethread_handler lua_State from a thread L=%ld L1=%ld\n"), (long)L, (long)L1); fflush(stdout); - wxLuaStateRefData::s_wxHashMapLuaStateRefData.erase(L1); - } - - static void wxLuaState_SetupThreadHandlers() - { - if (wxLua_lua_getnewthreadhandler() == NULL) - wxLua_lua_setnewthreadhandler( wxLuaState_newthread_handler ); - if (wxLua_lua_getfreethreadhandler() == NULL) - wxLua_lua_setfreethreadhandler( wxLuaState_freethread_handler ); - } - - #endif //WXLUA_LUA_NEWTHREAD - - // ---------------------------------------------------------------------------- // C functions for lua used in wxLuaState // ---------------------------------------------------------------------------- --- 81,84 ---- *************** *** 1054,1067 **** m_lua_State = NULL; m_lua_State_static = false; m_wxlStateData = NULL; ! m_own_stateData = false; } wxLuaStateRefData::wxLuaStateRefData(bool create_data) { - #ifdef WXLUA_LUA_NEWTHREAD - wxLuaState_SetupThreadHandlers(); - #endif //WXLUA_LUA_NEWTHREAD - Init(); if (create_data) --- 981,991 ---- m_lua_State = NULL; m_lua_State_static = false; + m_lua_State_coroutine = false; m_wxlStateData = NULL; ! m_own_stateData = true; } wxLuaStateRefData::wxLuaStateRefData(bool create_data) { Init(); if (create_data) *************** *** 1075,1078 **** --- 999,1003 ---- { wxCHECK_RET((m_lua_State_static == true) || (m_lua_State == NULL), wxT("You must ALWAYS call wxLuaState::Destroy and not wxObject::UnRef")); + CloseLuaState(true); if (m_own_stateData) *************** *** 1085,1088 **** --- 1010,1015 ---- return true; + if (m_lua_State_coroutine) return true; + m_wxlStateData->m_is_closing = true; *************** *** 1195,1198 **** --- 1122,1126 ---- wxLuaStateRefData* wxLuaStateRefData::GetLuaStateRefData(lua_State* L) { + // try our hashtable for faster lookup wxHashMapLuaStateRefData::iterator it = s_wxHashMapLuaStateRefData.find(L); if (it != s_wxHashMapLuaStateRefData.end()) *************** *** 1201,1226 **** return it->second; } ! return NULL; ! /* ! // Get the wxLuaStateRefData from lua itself (doesn't work w/ coroutines) ! int top = lua_gettop( L ); ! // try to get the state ! lua_pushstring( L, "__wxLuaStateRefData" ); ! lua_gettable( L, LUA_REGISTRYINDEX ); - // if nothing was returned or it wasn't a ptr, abort - if ( (top == lua_gettop(L)) || !lua_islightuserdata(L, -1) ) - { lua_settop( L, top ); ! return NULL; } - - // get the pointer, clean the stack, return - wxLuaStateRefData* d = (wxLuaStateRefData*)lua_touserdata( L, -1 ); - lua_settop( L, top ); - return d; - */ } --- 1129,1155 ---- return it->second; } ! else // it's a coroutine? look up the state data from lua ! { ! // Get the wxLuaStateRefData from lua itself ! int top = lua_gettop( L ); ! // try to get the state ! lua_pushstring( L, "__wxLuaStateRefData" ); ! lua_gettable( L, LUA_REGISTRYINDEX ); ! // if nothing was returned or it wasn't a ptr, abort ! if ( (top == lua_gettop(L)) || !lua_islightuserdata(L, -1) ) ! { ! wxPrintf(wxT("wxLuaStateRefData::GetLuaStateRefData FAILED!\n")); ! lua_settop( L, top ); ! return NULL; ! } ! ! // get the pointer, clean the stack, return ! wxLuaStateRefData* d = (wxLuaStateRefData*)lua_touserdata( L, -1 ); lua_settop( L, top ); ! return d; } } *************** *** 1263,1266 **** --- 1192,1197 ---- luaL_openlibs(L); + // 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; *************** *** 1299,1303 **** wxLuaStateRefData *refData = wxLuaStateRefData::GetLuaStateRefData(L); wxCHECK_MSG(refData != NULL, false, wxT("Unable to find lua_State")); ! return Create(refData); } else if (type == WXLUASTATE_SETSTATE) --- 1230,1234 ---- 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) *************** *** 1328,1346 **** return true; } - else if (type == WXLUASTATE_COROUTINE) - { - m_refData = new wxLuaStateRefData(); - 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 - lua_pushstring( L, "__wxLuaStateRefData" ); - lua_pushlightuserdata( L, (void*)m_refData ); - // set the value - lua_settable( L, LUA_REGISTRYINDEX ); - - return true; - } m_refData = new wxLuaStateRefData(); --- 1259,1262 ---- *************** *** 1352,1356 **** } ! bool wxLuaState::Create(wxLuaStateRefData* refData) { // nothing to be done --- 1268,1272 ---- } ! bool wxLuaState::Create(wxLuaStateRefData* refData, lua_State* L) { // nothing to be done *************** *** 1362,1366 **** // reference new data ! if ( refData ) { // we need to increment the ref count, but it's private in wxObjectRefData --- 1278,1282 ---- // reference new data ! if ( refData && (L == refData->m_lua_State)) { // we need to increment the ref count, but it's private in wxObjectRefData *************** *** 1370,1373 **** --- 1286,1301 ---- 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; |