From: John L. <jr...@us...> - 2008-01-03 00:06:03
|
Update of /cvsroot/wxlua/wxLua/modules/wxlua/src In directory sc8-pr-cvs9.sourceforge.net:/tmp/cvs-serv27681/wxLua/modules/wxlua/src Modified Files: wxlcallb.cpp wxlstate.cpp Log Message: Change wxLuaEventCallback so it doesn't connect in the constructor to allow deleting it if it fails before calling lua_error() and it's long jmp. Renamed the event handler functions to OnEvent() to match the wxWidgets semantics. Index: wxlstate.cpp =================================================================== RCS file: /cvsroot/wxlua/wxLua/modules/wxlua/src/wxlstate.cpp,v retrieving revision 1.156 retrieving revision 1.157 diff -C2 -d -r1.156 -r1.157 *** wxlstate.cpp 22 Dec 2007 06:07:16 -0000 1.156 --- wxlstate.cpp 3 Jan 2008 00:05:58 -0000 1.157 *************** *** 992,996 **** wxCHECK_MSG(wxlState.Ok(), false, wxT("Invalid wxLuaState")); wxLuaWinDestroyCallback *pCallback = ! new wxLuaWinDestroyCallback(wxlState, win, wxl_type); if (pCallback == NULL) --- 992,996 ---- wxCHECK_MSG(wxlState.Ok(), false, wxT("Invalid wxLuaState")); wxLuaWinDestroyCallback *pCallback = ! new wxLuaWinDestroyCallback(wxlState, win); if (pCallback == NULL) Index: wxlcallb.cpp =================================================================== RCS file: /cvsroot/wxlua/wxLua/modules/wxlua/src/wxlcallb.cpp,v retrieving revision 1.53 retrieving revision 1.54 diff -C2 -d -r1.53 -r1.54 *** wxlcallb.cpp 22 Dec 2007 06:07:15 -0000 1.53 --- wxlcallb.cpp 3 Jan 2008 00:05:58 -0000 1.54 *************** *** 26,42 **** IMPLEMENT_ABSTRACT_CLASS(wxLuaEventCallback, wxObject) ! wxLuaEventCallback::wxLuaEventCallback(const wxLuaState& wxlState, int lua_func_stack_idx, ! wxWindowID win_id, wxWindowID last_id, ! wxEventType eventType, wxEvtHandler *evtHandler) ! : m_routine(0), m_wxlState(wxlState), ! m_evtHandler(evtHandler), m_id(win_id), m_last_id(last_id), m_wxlBindEvent(NULL) { ! wxCHECK_RET(m_wxlState.Ok(), wxT("Invalid wxLuaState")); ! m_wxlBindEvent = wxlState.GetBindEvent(eventType); ! m_wxlState.AddTrackedEventCallback(this); if (m_wxlBindEvent == NULL) --- 26,61 ---- IMPLEMENT_ABSTRACT_CLASS(wxLuaEventCallback, wxObject) ! wxLuaEventCallback::wxLuaEventCallback() ! : m_luafunc_ref(0), m_wxlState(wxNullLuaState), ! m_evtHandler(NULL), m_id(wxID_ANY), m_last_id(wxID_ANY), m_wxlBindEvent(NULL) + { + } + wxLuaEventCallback::~wxLuaEventCallback() { ! // Remove the reference to the Lua function that we call ! if (m_wxlState.Ok()) ! { ! m_wxlState.wxluaR_Unref(m_luafunc_ref, &wxlua_lreg_refs_key); ! // delete the reference to this handler ! m_wxlState.RemoveTrackedEventCallback(this); ! } ! } ! wxString wxLuaEventCallback::Connect(const wxLuaState& wxlState, int lua_func_stack_idx, ! wxWindowID win_id, wxWindowID last_id, ! wxEventType eventType, wxEvtHandler *evtHandler) ! { ! wxCHECK_MSG(evtHandler != NULL, wxT("Invalid wxEvtHandler in wxLuaEventCallback::Connect()"), wxT("Invalid wxEvtHandler in wxLuaEventCallback::Connect()")); ! wxCHECK_MSG((m_evtHandler == NULL) && (m_luafunc_ref == 0), wxT("Attempting to reconnect a wxLuaEventCallback"), wxT("Attempting to reconnect a wxLuaEventCallback")); ! wxCHECK_MSG(wxlState.Ok(), wxT("Invalid wxLuaState"), wxT("Invalid wxLuaState")); ! m_wxlState = wxlState; ! m_evtHandler = evtHandler; ! m_id = win_id; ! m_last_id = last_id; ! ! m_wxlBindEvent = wxlState.GetBindEvent(eventType); if (m_wxlBindEvent == NULL) *************** *** 44,71 **** // Do not install this invalid or unknown event type since we won't know // what wxEvent type class to use and someone probably made a mistake. ! wxString msg = wxString::Format(wxT("Invalid or unknown wxEventType : %d, winIds %d %d."), ! (int)eventType, win_id, last_id); ! m_wxlState.wxlua_Error(msg); ! return; } // create a reference to the Lua event handler function if (lua_func_stack_idx != WXLUAEVENTCALLBACK_NOROUTINE) ! m_routine = m_wxlState.wxluaR_Ref(lua_func_stack_idx, &wxlua_lreg_refs_key); m_evtHandler->Connect(win_id, last_id, eventType, ! (wxObjectEventFunction)&wxLuaEventCallback::EventHandler, this); ! } ! ! wxLuaEventCallback::~wxLuaEventCallback() ! { ! // Remove the reference to the Lua function that we are going to call ! if (m_wxlState.Ok()) ! { ! m_wxlState.wxluaR_Unref(m_routine, &wxlua_lreg_refs_key); ! // delete the reference to this handler ! m_wxlState.RemoveTrackedEventCallback(this); ! } } --- 63,82 ---- // Do not install this invalid or unknown event type since we won't know // what wxEvent type class to use and someone probably made a mistake. ! return wxString::Format(wxT("wxLua: Invalid or unknown wxEventType for wxEvtHandler::Connect() : %d, winIds %d, %d."), ! (int)eventType, win_id, last_id); } + m_wxlState.AddTrackedEventCallback(this); + // create a reference to the Lua event handler function if (lua_func_stack_idx != WXLUAEVENTCALLBACK_NOROUTINE) ! m_luafunc_ref = m_wxlState.wxluaR_Ref(lua_func_stack_idx, &wxlua_lreg_refs_key); + // Note: We use the callback userdata and not the event sink since the event sink + // requires a wxEvtHandler object which is a fairly large class. m_evtHandler->Connect(win_id, last_id, eventType, ! (wxObjectEventFunction)&wxLuaEventCallback::OnAllEvents, this); ! return wxEmptyString; } *************** *** 83,87 **** } ! void wxLuaEventCallback::EventHandler(wxEvent& event) { wxEventType evtType = event.GetEventType(); --- 94,98 ---- } ! void wxLuaEventCallback::OnAllEvents(wxEvent& event) { wxEventType evtType = event.GetEventType(); *************** *** 91,100 **** wxCHECK_RET(theCallback != NULL, wxT("Invalid wxLuaEventCallback in wxEvent user data")); ! // Ok if !Ok() since the wxLuaState may been cleared during shutdown or after destroy event wxLuaState wxlState(theCallback->GetwxLuaState()); if (wxlState.Ok()) { wxlState.SetInEventType(evtType); ! theCallback->CallFunction(&event); wxlState.SetInEventType(wxEVT_NULL); } --- 102,111 ---- wxCHECK_RET(theCallback != NULL, wxT("Invalid wxLuaEventCallback in wxEvent user data")); ! // Ok if !Ok() since the wxLuaState may been cleared during shutdown or after a destroy event wxLuaState wxlState(theCallback->GetwxLuaState()); if (wxlState.Ok()) { wxlState.SetInEventType(evtType); ! theCallback->OnEvent(&event); wxlState.SetInEventType(wxEVT_NULL); } *************** *** 102,109 **** // we want the wxLuaWinDestroyCallback to get this too if (evtType == wxEVT_DESTROY) ! event.Skip(); } ! void wxLuaEventCallback::CallFunction(wxEvent *event) { // Cannot call it if Lua is gone or the interpreter has been destroyed --- 113,120 ---- // we want the wxLuaWinDestroyCallback to get this too if (evtType == wxEVT_DESTROY) ! event.Skip(true); } ! void wxLuaEventCallback::OnEvent(wxEvent *event) { // Cannot call it if Lua is gone or the interpreter has been destroyed *************** *** 118,122 **** int event_wxl_type = 0; ! // If !m_wxlBindEvent, we would have errored in the constructor, but... if (m_wxlBindEvent != NULL) { --- 129,133 ---- int event_wxl_type = 0; ! // If !m_wxlBindEvent, we would have errored in Connect(), but... if (m_wxlBindEvent != NULL) { *************** *** 144,153 **** wxlState.lua_CheckStack(LUA_MINSTACK); int oldTop = wxlState.lua_GetTop(); ! if (wxlState.wxluaR_GetRef(m_routine, &wxlua_lreg_refs_key)) { wxlState.lua_PushValue(LUA_GLOBALSINDEX); if (wxlState.lua_SetFenv(-2) != 0) { ! // don't track this since we didn't create it // Tracking this causes clashes in the object registry table // since many can be created and deleted and the mem address is resused by C++. --- 155,164 ---- wxlState.lua_CheckStack(LUA_MINSTACK); int oldTop = wxlState.lua_GetTop(); ! if (wxlState.wxluaR_GetRef(m_luafunc_ref, &wxlua_lreg_refs_key)) { wxlState.lua_PushValue(LUA_GLOBALSINDEX); if (wxlState.lua_SetFenv(-2) != 0) { ! // don't track this since we don't own it // Tracking this causes clashes in the object registry table // since many can be created and deleted and the mem address is resused by C++. *************** *** 170,174 **** wxLuaWinDestroyCallback::wxLuaWinDestroyCallback(const wxLuaState& wxlState, ! wxWindow* win, int iTag) :m_wxlState(wxlState), m_window(win) { --- 181,185 ---- wxLuaWinDestroyCallback::wxLuaWinDestroyCallback(const wxLuaState& wxlState, ! wxWindow* win) :m_wxlState(wxlState), m_window(win) { *************** *** 179,183 **** // connect the event handler m_window->Connect(m_window->GetId(), wxEVT_DESTROY, ! (wxObjectEventFunction)&wxLuaWinDestroyCallback::EventHandler, this); } --- 190,194 ---- // connect the event handler m_window->Connect(m_window->GetId(), wxEVT_DESTROY, ! (wxObjectEventFunction)&wxLuaWinDestroyCallback::OnAllDestroyEvents, this); } *************** *** 208,212 **** } ! void wxLuaWinDestroyCallback::EventHandler(wxWindowDestroyEvent& event) { // Central handler for events, forward to the specific instance --- 219,223 ---- } ! void wxLuaWinDestroyCallback::OnAllDestroyEvents(wxWindowDestroyEvent& event) { // Central handler for events, forward to the specific instance *************** *** 261,265 **** // remove the ref to the routine since we're clearing the wxLuaState // See ~wxLuaEventCallback ! m_wxlState.wxluaR_Unref(wxlCallback->GetLuaRoutine(), &wxlua_lreg_refs_key); wxlCallback->ClearwxLuaState(); --- 272,276 ---- // remove the ref to the routine since we're clearing the wxLuaState // See ~wxLuaEventCallback ! m_wxlState.wxluaR_Unref(wxlCallback->GetLuaFuncRef(), &wxlua_lreg_refs_key); wxlCallback->ClearwxLuaState(); |