From: John L. <jr...@us...> - 2005-11-26 08:47:06
|
Update of /cvsroot/wxlua/wxLua/modules/wxlua/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv25224/wxLua/modules/wxlua/src Modified Files: internal.cpp wxlbind.cpp wxlcallb.cpp wxlintrp.cpp wxlstate.cpp Log Message: Use the new wxLuaState in place of wxLuaStateVariables (removed) Index: internal.cpp =================================================================== RCS file: /cvsroot/wxlua/wxLua/modules/wxlua/src/internal.cpp,v retrieving revision 1.11 retrieving revision 1.12 diff -C2 -d -r1.11 -r1.12 *** internal.cpp 26 Nov 2005 03:15:19 -0000 1.11 --- internal.cpp 26 Nov 2005 08:46:57 -0000 1.12 *************** *** 37,42 **** #include "wxlua/include/internal.h" #include "wxlua/include/wxlcallb.h" - #include "wxlua/include/wxlintrp.h" #include "wxbind/include/wxbind.h" class wxStyledTextCtrlDocument; // for void* wxStyledTextCtrl::GetDocPointer() --- 37,42 ---- #include "wxlua/include/internal.h" #include "wxlua/include/wxlcallb.h" #include "wxbind/include/wxbind.h" [...1497 lines suppressed...] returns = new wxBitmap(cut_xpm); ! wxlState.addToTrackedMemoryList(returns); ! wxlState.pushuserdatatype(s_wxBitmap, returns ); lua_setglobal(L, "cutBitmap"); returns = new wxBitmap(copy_xpm); ! wxlState.addToTrackedMemoryList(returns); ! wxlState.pushuserdatatype(s_wxBitmap, returns ); lua_setglobal(L, "copyBitmap"); returns = new wxBitmap(paste_xpm); ! wxlState.addToTrackedMemoryList(returns); ! wxlState.pushuserdatatype(s_wxBitmap, returns ); lua_setglobal(L, "pasteBitmap"); returns = new wxBitmap(help_xpm); ! wxlState.addToTrackedMemoryList(returns); ! wxlState.pushuserdatatype(s_wxBitmap, returns ); lua_setglobal(L, "helpBitmap"); Index: wxlstate.cpp =================================================================== RCS file: /cvsroot/wxlua/wxLua/modules/wxlua/src/wxlstate.cpp,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** wxlstate.cpp 26 Nov 2005 03:15:19 -0000 1.4 --- wxlstate.cpp 26 Nov 2005 08:46:57 -0000 1.5 *************** *** 43,49 **** #include "wxlua/include/wxlcallb.h" #include "wxbind/include/wxbind.h" ! extern int wxLuaEventListCompareFn(const void *p1, const void *p2); // in wxlbind.cpp ! extern int wxLuaClassListCompareByTag(const void *p1, const void *p2); // in wxlbind.cpp const char *wxLuaReferences; // in internal.cpp const char *wxLuaNull; // in internal.cpp --- 43,50 ---- #include "wxlua/include/wxlcallb.h" #include "wxbind/include/wxbind.h" + #include "wxlua/include/wxlintrp.h" ! extern int wxLuaEventListCompareFn(const void *p1, const void *p2); // wxlbind.cpp ! extern int wxLuaClassListCompareByTag(const void *p1, const void *p2); // wxlbind.cpp const char *wxLuaReferences; // in internal.cpp const char *wxLuaNull; // in internal.cpp *************** *** 194,200 **** m_pDerivedList = new wxHashTable(wxKEY_INTEGER); m_pTrackedList = new wxHashTable(wxKEY_INTEGER); ! //m_pAppHandlerList = new wxList; ! //m_pDestroyHandlerList = new wxList; ! //m_pWindowList = new wxList; } --- 195,205 ---- m_pDerivedList = new wxHashTable(wxKEY_INTEGER); m_pTrackedList = new wxHashTable(wxKEY_INTEGER); ! m_pAppHandlerList = new wxList; ! m_pDestroyHandlerList = new wxList; ! m_pWindowList = new wxList; ! ! // always add wxWidgets binding ! m_bindings.Append(new wx_Binding()); ! m_bindings.DeleteContents(true); } *************** *** 210,233 **** wxLuaStateRefData::~wxLuaStateRefData() { ! Destroy(true); delete m_pDerivedList; delete m_pTrackedList; ! //delete m_pAppHandlerList; ! //delete m_pDestroyHandlerList; ! //delete m_pWindowList; } ! bool wxLuaStateRefData::Destroy(bool force) { ! wxCHECK_MSG(m_lua_State, false, wxT("Interpreter not created")); // wxCHECK_MSG(!m_is_running, false, wxT("Interpreter still running, can't destroy")); FIXME // remove deleted windows first ! if (m_pWindowList.GetCount()) ! wxLuaCleanupWindows(&m_pWindowList, true); // are there still windows? ask to abort deleting them if !force ! if (m_pWindowList.GetCount() != 0) { int ret = wxOK; --- 215,241 ---- wxLuaStateRefData::~wxLuaStateRefData() { ! CloseLuaState(true); delete m_pDerivedList; delete m_pTrackedList; ! delete m_pAppHandlerList; ! delete m_pDestroyHandlerList; ! delete m_pWindowList; } ! bool wxLuaStateRefData::CloseLuaState(bool force) { ! if (m_lua_State == NULL) ! return true; ! ! //wxCHECK_MSG(m_lua_State, false, wxT("Interpreter not created")); // wxCHECK_MSG(!m_is_running, false, wxT("Interpreter still running, can't destroy")); FIXME // remove deleted windows first ! if (m_pWindowList->GetCount()) ! wxLuaCleanupWindows(m_pWindowList, true); // are there still windows? ask to abort deleting them if !force ! if (m_pWindowList->GetCount() != 0) { int ret = wxOK; *************** *** 246,261 **** // delete windows and their eventhandler since they delete the LuaCallbacks // which require a lua_State ! wxLuaCleanupWindows(&m_pWindowList, false); // wait for wxWindow::Destroy() to really delete the windows //wxYieldIfNeeded(); } ! CleanupWxLua(m_lua_State, !m_lua_State_static); s_wxHashMapLuaStateRefData.erase(m_lua_State); - ClearCallbacks(); m_pDerivedList->DeleteContents(false); m_pTrackedList->DeleteContents(false); ! m_pWindowList.DeleteContents(false); m_lua_State = NULL; --- 254,268 ---- // delete windows and their eventhandler since they delete the LuaCallbacks // which require a lua_State ! wxLuaCleanupWindows(m_pWindowList, false); // wait for wxWindow::Destroy() to really delete the windows //wxYieldIfNeeded(); } ! CleanupWxLua(!m_lua_State_static); s_wxHashMapLuaStateRefData.erase(m_lua_State); m_pDerivedList->DeleteContents(false); m_pTrackedList->DeleteContents(false); ! m_pWindowList->DeleteContents(false); m_lua_State = NULL; *************** *** 264,271 **** } void wxLuaStateRefData::ClearCallbacks() { // remove any and all callbacks that use this event handler since its gone ! wxNode* node = m_pAppHandlerList.GetFirst(); while (node) { --- 271,306 ---- } + void wxLuaStateRefData::CleanupWxLua(bool closeLua /* = true*/) + { + // from lua50/lib/lbaselib.c, the collectgarbage lua function + // see int luaB_collectgarbage(lua_State *L); + //lua_setgcthreshold(L, luaL_optint(L, 1, 0)); // = luaB_collectgarbage(L); + + m_pDerivedList->DeleteContents(true); + m_pTrackedList->DeleteContents(true); + + ClearCallbacks(); + + if (closeLua) + { + UnRegisterBindings(); + lua_close(m_lua_State); + } + + // don't "own" pointers to wxLuaCallbacks, let wxEventHandler do it + m_pAppHandlerList->DeleteContents(false); + m_pAppHandlerList->Clear(); + // don't delete wxLuaDestroyCallbacks, let wxEventHandler do it + m_pDestroyHandlerList->DeleteContents(false); + m_pDestroyHandlerList->Clear(); + // don't delete wxWindow derived classes, let wxWidgets do it + m_pWindowList->DeleteContents(false); + m_pWindowList->Clear(); + } + void wxLuaStateRefData::ClearCallbacks() { // remove any and all callbacks that use this event handler since its gone ! wxNode* node = m_pAppHandlerList->GetFirst(); while (node) { *************** *** 275,284 **** { pCallback->ClearState(); - pCallback->ClearStateVars(); } node = node->GetNext(); } ! node = m_pDestroyHandlerList.GetFirst(); while (node) { --- 310,318 ---- { pCallback->ClearState(); } node = node->GetNext(); } ! node = m_pDestroyHandlerList->GetFirst(); while (node) { *************** *** 288,292 **** { pCallback->ClearState(); - pCallback->ClearStateVars(); } --- 322,325 ---- *************** *** 295,301 **** // don't "own" pointers to wxLuaCallbacks, let wxEventHandler do it ! m_pAppHandlerList.DeleteContents(false); // don't delete wxLuaDestroyCallbacks, let wxEventHandler do it ! m_pDestroyHandlerList.DeleteContents(false); } --- 328,353 ---- // don't "own" pointers to wxLuaCallbacks, let wxEventHandler do it ! m_pAppHandlerList->DeleteContents(false); ! m_pAppHandlerList->Clear(); // don't delete wxLuaDestroyCallbacks, let wxEventHandler do it ! m_pDestroyHandlerList->DeleteContents(false); ! m_pDestroyHandlerList->Clear(); ! } ! ! void wxLuaStateRefData::UnRegisterBindings() ! { ! if (m_lua_State == NULL) return; ! ! // UnRegister bindings ! wxLuaBindingList::Node *node; ! for (node = m_bindings.GetFirst(); node; node = node->GetNext() ) ! { ! wxLuaBinding* binding = node->GetData(); ! binding->UnRegisterBinding(m_lua_State); ! } ! ! lua_pushstring(m_lua_State, WXLUAREFERENCES); ! lua_pushnil(m_lua_State); ! lua_rawset(m_lua_State, LUA_REGISTRYINDEX); } *************** *** 345,349 **** bool wxLuaState::Create() { ! if (GetRefData() != NULL) UnRef(); m_refData = new wxLuaStateRefData(); --- 397,401 ---- bool wxLuaState::Create() { ! UnRef(); m_refData = new wxLuaStateRefData(); *************** *** 395,399 **** return Create(refData); } ! m_refData = new wxLuaStateRefData(); --- 447,451 ---- return Create(refData); } ! m_refData = new wxLuaStateRefData(); *************** *** 426,429 **** --- 478,493 ---- } + bool wxLuaState::Ok() const + { + return (m_refData != NULL) && GetLuaStateRefData()->m_lua_State; + } + + bool wxLuaState::CloseLuaState(bool force) + { + wxCHECK_MSG(Ok(), false, wxT("Invalid wxLuaState")); + return GetLuaStateRefData()->CloseLuaState(force); + } + + lua_State* wxLuaState::GetLuaState() const { *************** *** 439,443 **** // create references table in registry ! lua_pushstring(L, wxLuaReferences); lua_newtable(L); lua_rawset(L, LUA_REGISTRYINDEX); --- 503,507 ---- // create references table in registry ! lua_pushstring(L, WXLUAREFERENCES); lua_newtable(L); lua_rawset(L, LUA_REGISTRYINDEX); *************** *** 491,513 **** } - void LUACALL wxLuaState::UnRegisterBindings() - { - wxCHECK_RET(Ok(), wxT("Invalid wxLuaState")); - lua_State* L = M_WXLSTATEDATA->m_lua_State; - wxCHECK_RET(L, wxT("Invalid wxLuaState")); - - // UnRegister bindings - wxLuaBindingList::Node *node; - for (node = M_WXLSTATEDATA->m_bindings.GetFirst(); node; node = node->GetNext() ) - { - wxLuaBinding* binding = node->GetData(); - binding->UnRegisterBinding(L); - } - - lua_pushstring(L, wxLuaReferences); - lua_pushnil(L); - lua_rawset(L, LUA_REGISTRYINDEX); - } - // Find binding with given nameSpace wxLuaBinding* wxLuaState::GetLuaBinding(const wxString& nameSpace) --- 555,558 ---- *************** *** 623,626 **** --- 668,736 ---- } + void wxLuaState::GarbageCollectWindows(bool closeWindows) + { + wxCHECK_RET(Ok(), wxT("Invalid wxLuaState")) + // remove deleted windows + if (M_WXLSTATEDATA->m_pWindowList->GetCount()) + wxLuaCleanupWindows(M_WXLSTATEDATA->m_pWindowList, !closeWindows); + } + + // ---------------------------------------------------------------------------- + // memory tracking functions + + bool wxLuaState::removeTrackedMemory(void *pObject, bool fDelete) + { + wxCHECK_MSG(Ok(), false, wxT("Invalid wxLuaState")) + bool result = false; + + wxObject *pDeleteObject = M_WXLSTATEDATA->m_pTrackedList->Delete((int) pObject); + if (fDelete && pDeleteObject != NULL) + { + delete pDeleteObject; + result = true; + } + return result; + } + + void wxLuaState::addToTrackedMemoryList(wxObject *pObject) + { + wxCHECK_RET(pObject, wxT("Invalid lua state and/or wxObject")); + wxCHECK_RET(Ok(), wxT("Invalid wxLuaState")) + M_WXLSTATEDATA->m_pTrackedList->Put((int)pObject, pObject); + } + + bool wxLuaIsParentWindowTracked(wxList *list, wxWindow* win) + { + wxCHECK_MSG(list && win, false, wxT("Invalid list or parent")); + wxWindow *parent = win; + + while (parent) + { + if (list->Find(parent)) + return true; + + parent = parent->GetParent(); + } + + return false; + } + + void wxLuaState::addToTrackedWindowList(wxWindow *pWindow) + { + wxCHECK_RET(pWindow, wxT("Invalid lua state and/or wxWindow")); + wxCHECK_RET(Ok(), wxT("Invalid wxLuaState")) + + // don't track these windows since they're supposed to be attached + if (wxDynamicCast(pWindow, wxMenuBar) != NULL) return; + if (wxDynamicCast(pWindow, wxToolBar) != NULL) return; + + // only need to track parent window, it deletes children for us + if (!wxLuaIsParentWindowTracked(M_WXLSTATEDATA->m_pWindowList, pWindow)) + { + M_WXLSTATEDATA->m_pWindowList->Append(pWindow); + new wxLuaCallback(*this, 3, pWindow->GetId(), wxEVT_DESTROY, pWindow->GetEventHandler()); + } + } + // ---------------------------------------------------------------------------- // wxLua Lua Registry Table Functions *************** *** 642,646 **** int nTop = lua_gettop(L); ! tpushliteralstring(L, wxLuaReferences); lua_rawget(L, LUA_REGISTRYINDEX); // pop key, push result --- 752,756 ---- int nTop = lua_gettop(L); ! tpushliteralstring(L, WXLUAREFERENCES); lua_rawget(L, LUA_REGISTRYINDEX); // pop key, push result *************** *** 692,696 **** bool ret = false; // false indicates index out of range ! tpushliteralstring(L, wxLuaReferences); lua_rawget(L, LUA_REGISTRYINDEX); // pop key, push result (refs table) --- 802,806 ---- bool ret = false; // false indicates index out of range ! tpushliteralstring(L, WXLUAREFERENCES); lua_rawget(L, LUA_REGISTRYINDEX); // pop key, push result (refs table) *************** *** 725,729 **** bool ret = false; // false indicates index out of range ! tpushliteralstring(L, wxLuaReferences); lua_rawget(L, LUA_REGISTRYINDEX); // pop key, push result (refs table) --- 835,839 ---- bool ret = false; // false indicates index out of range ! tpushliteralstring(L, WXLUAREFERENCES); lua_rawget(L, LUA_REGISTRYINDEX); // pop key, push result (refs table) *************** *** 749,753 **** lua_State* L = M_WXLSTATEDATA->m_lua_State; ! tpushliteralstring(L, wxLuaReferences); lua_rawget(L, LUA_REGISTRYINDEX); // pop key, push result (refs table) --- 859,863 ---- lua_State* L = M_WXLSTATEDATA->m_lua_State; ! tpushliteralstring(L, WXLUAREFERENCES); lua_rawget(L, LUA_REGISTRYINDEX); // pop key, push result (refs table) Index: wxlbind.cpp =================================================================== RCS file: /cvsroot/wxlua/wxLua/modules/wxlua/src/wxlbind.cpp,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** wxlbind.cpp 25 Nov 2005 10:09:11 -0000 1.2 --- wxlbind.cpp 26 Nov 2005 08:46:57 -0000 1.3 *************** *** 46,49 **** --- 46,56 ---- } + // Function to compare the class tag of two classes + int wxLuaClassListCompareByTag(const void *p1, const void *p2) + { + return (*((const WXLUACLASS *) p1)->class_tag) - (*((const WXLUACLASS *) p2)->class_tag); + } + + // ---------------------------------------------------------------------------- // wxLuaBinding *************** *** 101,105 **** // create references table in registry ! // lua_pushstring(L, wxLuaReferences); // lua_newtable(L); // lua_rawset(L, LUA_REGISTRYINDEX); --- 108,112 ---- // create references table in registry ! // lua_pushstring(L, WXLUAREFERENCES); // lua_newtable(L); // lua_rawset(L, LUA_REGISTRYINDEX); Index: wxlintrp.cpp =================================================================== RCS file: /cvsroot/wxlua/wxLua/modules/wxlua/src/wxlintrp.cpp,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** wxlintrp.cpp 25 Nov 2005 10:09:11 -0000 1.2 --- wxlintrp.cpp 26 Nov 2005 08:46:57 -0000 1.3 *************** *** 89,93 **** wxLuaInterpreter *wxFindLuaInterpreter(lua_State *L) { - int top = lua_gettop( L ); --- 89,92 ---- *************** *** 108,120 **** } - wxLuaStateVariables *wxFindLuaStateVariables(lua_State *L) - { - wxLuaInterpreter *luaInterpreter = wxFindLuaInterpreter(L); - if (luaInterpreter && luaInterpreter->Ok()) - return luaInterpreter->GetStateVariables(); - - return NULL; - } - // ---------------------------------------------------------------------------- // lua hooks and function replacements for embedding into wxWidgets --- 107,110 ---- *************** *** 272,276 **** // FIXME - for select event types we don't want to do anything ! wxEventType evtType = luaInterpreter->GetStateVariables()->m_inEventType; if ((evtType != -1)) //(evtType == wxEVT_IDLE) && (evtType == wxEVT_PAINT) && --- 262,266 ---- // FIXME - for select event types we don't want to do anything ! wxEventType evtType = luaInterpreter->GetwxLuaState().GetLuaStateRefData()->m_inEventType; if ((evtType != -1)) //(evtType == wxEVT_IDLE) && (evtType == wxEVT_PAINT) && *************** *** 325,330 **** // FIXME - wxGTK crashes if wxYield[IfNeeded] is called within a paint handler? I think ! luaInterpreter->GetStateVariables()->m_pTrackedList->BeginFind(); ! node = luaInterpreter->GetStateVariables()->m_pTrackedList->Next(); while (node) { --- 315,320 ---- // FIXME - wxGTK crashes if wxYield[IfNeeded] is called within a paint handler? I think ! luaInterpreter->GetwxLuaState().GetLuaStateRefData()->m_pTrackedList->BeginFind(); ! node = luaInterpreter->GetwxLuaState().GetLuaStateRefData()->m_pTrackedList->Next(); while (node) { *************** *** 334,338 **** break; } ! node = luaInterpreter->GetStateVariables()->m_pTrackedList->Next(); } --- 324,328 ---- break; } ! node = luaInterpreter->GetwxLuaState().GetLuaStateRefData()->m_pTrackedList->Next(); } *************** *** 392,398 **** m_id = wxID_ANY; m_evtHandler = NULL; - m_lua_State = NULL; - m_luaAllocated = false; - m_luaStateVariables = NULL; m_lua_debug_hook_count = 100; --- 382,385 ---- *************** *** 407,411 **** return false; - m_luaStateVariables = new wxLuaStateVariables; m_evtHandler = handler; m_id = id; --- 394,397 ---- *************** *** 414,446 **** if (inL != NULL) { ! m_lua_State = inL; ! m_luaAllocated = false; } else { ! m_lua_State = lua_open(); ! m_luaAllocated = true; } ! lua_State *L = m_lua_State; ! // load some useful libraries ! luaopen_base(L); ! luaopen_table(L); ! luaopen_string(L); ! luaopen_math(L); ! luaopen_debug(L); ! luaopen_io(L); ! luaopen_loadlib(L); ! ! s_wxHashMapLuaInterpreter[L] = this; // Stick us into the lua interpreter - push key, value ! lua_pushstring( L, "__wxLuaInterpreter" ); ! lua_pushlightuserdata( L, (void*)this ); // set the value ! lua_settable( L, LUA_REGISTRYINDEX ); // register handlers to send events ! if (m_luaAllocated) { RegisterFunction(LuaPrint, wxT("print")); --- 400,420 ---- if (inL != NULL) { ! m_wxlState = wxLuaState(inL, WXLUASTATE_SETSTATE); } else { ! m_wxlState = wxLuaState(true); } ! s_wxHashMapLuaInterpreter[m_wxlState.GetLuaState()] = this; // Stick us into the lua interpreter - push key, value ! lua_pushstring( m_wxlState.GetLuaState(), "__wxLuaInterpreter" ); ! lua_pushlightuserdata( m_wxlState.GetLuaState(), (void*)this ); // set the value ! lua_settable( m_wxlState.GetLuaState(), LUA_REGISTRYINDEX ); // register handlers to send events ! if (inL != NULL) { RegisterFunction(LuaPrint, wxT("print")); *************** *** 453,459 **** SendEvent( event ); - // now register bindings - m_luaStateVariables->RegisterBindings(m_lua_State, true); - return true; } --- 427,430 ---- *************** *** 466,471 **** Destroy(true); } - - delete m_luaStateVariables; } --- 437,440 ---- *************** *** 473,480 **** { // remove deleted windows ! if (Ok() && ! m_luaStateVariables->m_pWindowList && ! m_luaStateVariables->m_pWindowList->GetCount()) ! wxLuaCleanupWindows(m_luaStateVariables->m_pWindowList, !closeWindows); } --- 442,446 ---- { // remove deleted windows ! if (Ok()) m_wxlState.GarbageCollectWindows(closeWindows); } *************** *** 484,523 **** wxCHECK_MSG(!m_is_running, false, wxT("Interpreter still running, can't destroy")); ! // remove deleted windows first ! if (m_luaStateVariables->m_pWindowList && ! m_luaStateVariables->m_pWindowList->GetCount()) ! wxLuaCleanupWindows(m_luaStateVariables->m_pWindowList, true); ! ! // are there still windows? ask to abort deleting them if !force ! if (m_luaStateVariables->m_pWindowList && ! m_luaStateVariables->m_pWindowList->GetCount()) ! { ! int ret = wxOK; ! ! if (!force) ! { ! ret = wxMessageBox(wxT("Windows are still open, would you like to delete them?"), ! wxT("Delete existing windows?"), wxOK|wxCANCEL|wxICON_QUESTION); ! } ! ! if (ret == wxCANCEL) ! return false; ! ! //wxPrintf(wxT("Deleting windows\n")); ! ! // delete windows and their eventhandler since they delete the LuaCallbacks ! // which require a lua_State ! wxLuaCleanupWindows(m_luaStateVariables->m_pWindowList, false); ! // wait for wxWindow::Destroy() to really delete the windows ! //wxYieldIfNeeded(); ! } ! ! CleanupWxLua(m_lua_State, m_luaAllocated); ! s_wxHashMapLuaInterpreter.erase(m_lua_State); ! delete m_luaStateVariables; ! m_luaStateVariables = NULL; ! m_lua_State = NULL; m_evtHandler = NULL; --- 450,460 ---- wxCHECK_MSG(!m_is_running, false, wxT("Interpreter still running, can't destroy")); ! // remember pointer value, even though CloseLuaState actually frees it ! lua_State* L = m_wxlState.GetLuaState(); ! if (!m_wxlState.CloseLuaState(force)) ! return false; ! s_wxHashMapLuaInterpreter.erase(L); m_evtHandler = NULL; *************** *** 527,531 **** bool wxLuaInterpreter::Ok() const { ! return m_lua_State != NULL; } --- 464,468 ---- bool wxLuaInterpreter::Ok() const { ! return m_wxlState.Ok(); } *************** *** 543,547 **** m_is_running = true; // int ret_val = lua_dofile(m_lua_State, wx2lua(filename)); ! int ret_val = LuaDoFile(m_lua_State, filename); // FIXME use LuaDoFile in RunFile ? m_is_running = false; if (!m_user_break) --- 480,484 ---- m_is_running = true; // int ret_val = lua_dofile(m_lua_State, wx2lua(filename)); ! int ret_val = LuaDoFile(m_wxlState.GetLuaState(), filename); // FIXME use LuaDoFile in RunFile ? m_is_running = false; if (!m_user_break) *************** *** 560,564 **** m_is_running = true; //int ret_val = luaL_loadbuffer(m_lua_State, wx2lua(script), script.Len(), wx2lua(name)); ! int ret_val = lua_dostring(m_lua_State, wx2lua(script)); m_is_running = false; --- 497,501 ---- m_is_running = true; //int ret_val = luaL_loadbuffer(m_lua_State, wx2lua(script), script.Len(), wx2lua(name)); ! int ret_val = lua_dostring(m_wxlState.GetLuaState(), wx2lua(script)); m_is_running = false; *************** *** 579,583 **** m_is_running = true; //int ret_val = luaL_loadbuffer(m_lua_State, (const char*)buf, size, wx2lua(name)); ! int ret_val = lua_dobuffer(m_lua_State, (const char*)buf, size, wx2lua(name)); m_is_running = false; --- 516,520 ---- m_is_running = true; //int ret_val = luaL_loadbuffer(m_lua_State, (const char*)buf, size, wx2lua(name)); ! int ret_val = lua_dobuffer(GetLuaState(), (const char*)buf, size, wx2lua(name)); m_is_running = false; *************** *** 617,624 **** //LUA_MASKCALL, LUA_MASKRET, LUA_MASKLINE, and LUA_MASKCOUNT //lua_sethook(m_lua_State, LuaDebugHook, LUA_MASKCALL | LUA_MASKLINE | LUA_MASKRET, 0); ! lua_sethook(m_lua_State, LuaDebugHook, hook, count); } else ! lua_sethook(m_lua_State, LuaDebugHook, 0, 0); // disable hook } --- 554,561 ---- //LUA_MASKCALL, LUA_MASKRET, LUA_MASKLINE, and LUA_MASKCOUNT //lua_sethook(m_lua_State, LuaDebugHook, LUA_MASKCALL | LUA_MASKLINE | LUA_MASKRET, 0); ! lua_sethook(m_wxlState.GetLuaState(), LuaDebugHook, hook, count); } else ! lua_sethook(GetLuaState(), LuaDebugHook, 0, 0); // disable hook } *************** *** 634,642 **** lua_State *wxLuaInterpreter::GetLuaState() const { ! return m_lua_State; ! } ! wxLuaStateVariables *wxLuaInterpreter::GetStateVariables() const ! { ! return m_luaStateVariables; } --- 571,575 ---- lua_State *wxLuaInterpreter::GetLuaState() const { ! return m_wxlState.GetLuaState(); } Index: wxlcallb.cpp =================================================================== RCS file: /cvsroot/wxlua/wxLua/modules/wxlua/src/wxlcallb.cpp,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** wxlcallb.cpp 25 Nov 2005 02:46:53 -0000 1.1 --- wxlcallb.cpp 26 Nov 2005 08:46:57 -0000 1.2 *************** *** 35,50 **** // Encapsulate a lua function reference for use by a event callback ! wxLuaCallback::wxLuaCallback( lua_State *L, int theRoutine, int id, wxEventType eventType, wxEvtHandler *pEvtHandler ) ! : m_luaState(L), m_luaStateVars(NULL), m_pHandler(pEvtHandler), m_id(id), m_eventType(eventType) { ! GET_LUASTATEVARS_RET(L) ! m_luaStateVars = stateVars; ! m_routine = tinsert(L, theRoutine); pEvtHandler->Connect( id, --- 35,50 ---- // Encapsulate a lua function reference for use by a event callback ! wxLuaCallback::wxLuaCallback( const wxLuaState& state, int theRoutine, int id, wxEventType eventType, wxEvtHandler *pEvtHandler ) ! : m_wxlState(state), m_pHandler(pEvtHandler), m_id(id), m_eventType(eventType) { ! wxCHECK_RET(m_wxlState.Ok(), wxT("Invalid wxLuaState")); ! ! m_routine = m_wxlState.tinsert(theRoutine); pEvtHandler->Connect( id, *************** *** 57,61 **** //if (pEvtHandler->IsKindOf(CLASSINFO(wxApp))) { ! m_luaStateVars->m_pAppHandlerList->Append(this); } } --- 57,61 ---- //if (pEvtHandler->IsKindOf(CLASSINFO(wxApp))) { ! m_wxlState.GetLuaStateRefData()->m_pAppHandlerList->Append(this); } } *************** *** 64,72 **** { // Remove the reference to the Lua function that we are going to call ! if (m_luaState) ! tremove(m_luaState, m_routine); ! // delete the reference to this handler ! if (m_luaStateVars) ! m_luaStateVars->m_pAppHandlerList->DeleteObject(this); } --- 64,82 ---- { // Remove the reference to the Lua function that we are going to call ! if (m_wxlState.Ok()) ! { ! m_wxlState.tremove(m_routine); ! // delete the reference to this handler ! m_wxlState.GetLuaStateRefData()->m_pAppHandlerList->DeleteObject(this); ! } ! } ! ! void wxLuaCallback::ClearState() ! { ! if (m_wxlState.Ok()) ! { ! m_wxlState.GetLuaStateRefData()->m_pAppHandlerList->DeleteObject(this); ! m_wxlState.UnRef(); ! } } *************** *** 77,89 **** wxLuaCallback *theCallback = (wxLuaCallback *) event.m_callbackUserData; ! if (!theCallback || !theCallback->GetLuaState() || !theCallback->m_luaStateVars) return; ! theCallback->m_luaStateVars->m_inEventType = event.GetEventType(); if (event.GetEventType() == wxEVT_DESTROY) { event.Skip(); ! theCallback->m_luaStateVars->m_pWindowList->DeleteObject((wxWindow*)event.GetEventObject()); // Disconnect all callbacks associated with this window's evthandler --- 87,102 ---- wxLuaCallback *theCallback = (wxLuaCallback *) event.m_callbackUserData; ! if (!theCallback || !theCallback->GetwxLuaState().Ok()) return; ! wxLuaState wxlState(theCallback->GetwxLuaState()); ! wxlState.GetLuaStateRefData()->m_inEventType = event.GetEventType(); if (event.GetEventType() == wxEVT_DESTROY) { event.Skip(); ! wxlState.GetLuaStateRefData()->m_pWindowList->DeleteObject((wxWindow*)event.GetEventObject()); ! // delete the reference to this handler since we're clearing it ! m_wxlState.GetLuaStateRefData()->m_pAppHandlerList->DeleteObject(this); // Disconnect all callbacks associated with this window's evthandler *************** *** 92,96 **** wxEvtHandler *evtHandler = ((wxWindow*)event.GetEventObject())->GetEventHandler(); ! wxNode* node = theCallback->m_luaStateVars->m_pAppHandlerList->GetFirst(); while (node) { --- 105,109 ---- wxEvtHandler *evtHandler = ((wxWindow*)event.GetEventObject())->GetEventHandler(); ! wxNode* node = wxlState.GetLuaStateRefData()->m_pAppHandlerList->GetFirst(); while (node) { *************** *** 99,106 **** if ((pCallback != NULL) && (pCallback->GetEvtHandler() == evtHandler)) { pCallback->ClearState(); } ! ! node = node->GetNext(); } } --- 112,124 ---- if ((pCallback != NULL) && (pCallback->GetEvtHandler() == evtHandler)) { + // go to new node before we delete it + node = node->GetNext(); + // delete the reference to all other handlers that are cleared + m_wxlState.GetLuaStateRefData()->m_pAppHandlerList->DeleteObject(pCallback); + pCallback->ClearState(); } ! else ! node = node->GetNext(); } } *************** *** 108,112 **** theCallback->CallFunction(&event); ! theCallback->m_luaStateVars->m_inEventType = -1; } --- 126,130 ---- theCallback->CallFunction(&event); ! wxlState.GetLuaStateRefData()->m_inEventType = -1; } *************** *** 119,149 **** // after lua has been deleted ! if (!m_luaState || !wxFindLuaStateVariables(m_luaState)) return; int eventClassTag = s_wxEvent; ! GET_LUASTATEVARS_RET(m_luaState) ! const WXLUAEVENT *pLuaEvent = stateVars->GetLuaEvent(pEvent); if (pLuaEvent) eventClassTag = *pLuaEvent->eventClassTag; ! lua_checkstack(m_luaState,LUA_MINSTACK); ! int oldTop = lua_gettop(m_luaState); ! if (tget(m_luaState, m_routine)) { ! lua_pushvalue(m_luaState, LUA_GLOBALSINDEX); ! if (lua_setfenv(m_luaState, -2) != 0) { ! tpushusertag(m_luaState, pEvent, eventClassTag); ! LuaCall(m_luaState, 1, true); } else ! terror(m_luaState, "wxLua: CallFunction: function is not a Lua function."); } else ! terror(m_luaState, "wxLua: CallFunction: function has been garbage collected."); ! lua_settop(m_luaState, oldTop); } --- 137,166 ---- // after lua has been deleted ! if (!m_wxlState.Ok()) return; int eventClassTag = s_wxEvent; ! const WXLUAEVENT *pLuaEvent = m_wxlState.GetLuaEvent(pEvent); if (pLuaEvent) eventClassTag = *pLuaEvent->eventClassTag; ! lua_checkstack(m_wxlState.GetLuaState(), LUA_MINSTACK); ! int oldTop = lua_gettop(m_wxlState.GetLuaState()); ! if (m_wxlState.tget(m_routine)) { ! lua_pushvalue(m_wxlState.GetLuaState(), LUA_GLOBALSINDEX); ! if (lua_setfenv(m_wxlState.GetLuaState(), -2) != 0) { ! m_wxlState.tpushusertag(pEvent, eventClassTag); ! m_wxlState.LuaCall(1, true); } else ! m_wxlState.terror("wxLua: CallFunction: function is not a Lua function."); } else ! m_wxlState.terror("wxLua: CallFunction: function has been garbage collected."); ! lua_settop(m_wxlState.GetLuaState(), oldTop); } *************** *** 154,162 **** IMPLEMENT_CLASS(wxLuaDestroyCallback, wxEvtHandler); ! wxLuaDestroyCallback::wxLuaDestroyCallback(lua_State *L, int id, wxEvtHandler *pHandler, int iTag) ! : m_luaState(L), m_luaStateVars(NULL), m_pHandler(pHandler), m_id(id) { ! GET_LUASTATEVARS_RET(L) ! m_luaStateVars = stateVars; // allocate a LUA proxy for the object --- 171,180 ---- IMPLEMENT_CLASS(wxLuaDestroyCallback, wxEvtHandler); ! wxLuaDestroyCallback::wxLuaDestroyCallback(const wxLuaState& state, int id, ! wxEvtHandler *pHandler, int iTag) ! : m_wxlState(state), m_pHandler(pHandler), m_id(id) { ! wxCHECK_RET(m_wxlState.Ok(), wxT("Invalid wxLuaState")); ! lua_State *L = m_wxlState.GetLuaState(); // allocate a LUA proxy for the object *************** *** 172,181 **** if (lua_setmetatable(L, -2) == 0) { ! terror(L, "wxLua: Unable to set metatable"); } } // get a reference to the destroy handler table ! if (tget(L, stateVars->m_wxDeleteTable)) { // create a reference to object --- 190,199 ---- if (lua_setmetatable(L, -2) == 0) { ! m_wxlState.terror("wxLua: Unable to set metatable"); } } // get a reference to the destroy handler table ! if (tget(L, m_wxlState.GetLuaStateRefData()->m_wxDeleteTable)) { // create a reference to object *************** *** 187,191 **** } ! stateVars->m_pDestroyHandlerList->Append(this); // connect the event handler --- 205,209 ---- } ! m_wxlState.GetLuaStateRefData()->m_pDestroyHandlerList->Append(this); // connect the event handler *************** *** 197,201 **** else { ! terror(L, "wxLua: Out of memory"); } } --- 215,219 ---- else { ! m_wxlState.terror("wxLua: Out of memory"); } } *************** *** 203,208 **** wxLuaDestroyCallback::~wxLuaDestroyCallback() { ! if (m_luaStateVars) ! m_luaStateVars->m_pDestroyHandlerList->DeleteObject(this); } --- 221,226 ---- wxLuaDestroyCallback::~wxLuaDestroyCallback() { ! if (m_wxlState.Ok()) ! m_wxlState.GetLuaStateRefData()->m_pDestroyHandlerList->DeleteObject(this); } *************** *** 222,238 **** // FIXME - Is it an error to receive an event after you've deleted lua? // probably not if lua is getting shutdown ! if (!m_luaState || !m_luaStateVars) return; // Note: do not remove from m_pDestroyHandlerList here, wait 'till destructor ! if (tget(m_luaState, m_luaStateVars->m_wxDeleteTable)) { // clear the metatable reference in the lua proxy. ! lua_pushlightuserdata(m_luaState, m_pHandler); ! lua_rawget(m_luaState, -2); ! lua_pushnil(m_luaState); ! lua_setmetatable(m_luaState, -2); ! lua_pop(m_luaState, 2); } } --- 240,257 ---- // FIXME - Is it an error to receive an event after you've deleted lua? // probably not if lua is getting shutdown ! if (!m_wxlState.Ok()) return; // Note: do not remove from m_pDestroyHandlerList here, wait 'till destructor ! if (m_wxlState.tget(m_wxlState.GetLuaStateRefData()->m_wxDeleteTable)) { + lua_State* L = m_wxlState.GetLuaState(); // clear the metatable reference in the lua proxy. ! lua_pushlightuserdata(L, m_pHandler); ! lua_rawget(L, -2); ! lua_pushnil(L); ! lua_setmetatable(L, -2); ! lua_pop(L, 2); } } |