From: John L. <jr...@us...> - 2009-10-01 04:21:11
|
Update of /cvsroot/wxlua/wxLua/modules/wxlua/src In directory 23jxhf1.ch3.sourceforge.com:/tmp/cvs-serv4255/wxLua/modules/wxlua/src Modified Files: wxlbind.cpp wxlstate.cpp wxlua_bind.cpp Log Message: Removed %encapsulate and %noclassinfo tags for %classes in the bindings. No longer encapsulating non wxObjects in a wxObject class for deletion. We now provide delete functions for each class in the wxLuaBindClass structs to be called on the void* we get from Lua. Index: wxlstate.cpp =================================================================== RCS file: /cvsroot/wxlua/wxLua/modules/wxlua/src/wxlstate.cpp,v retrieving revision 1.183 retrieving revision 1.184 diff -C2 -d -r1.183 -r1.184 *** wxlstate.cpp 25 Sep 2009 18:47:58 -0000 1.183 --- wxlstate.cpp 1 Oct 2009 04:21:02 -0000 1.184 *************** *** 482,490 **** // ---------------------------------------------------------------------------- ! void LUACALL wxluaO_addgcobject(lua_State *L, wxObject* wxobj) ! { ! wxluaO_addgcobject(L, (void*)wxobj, wxobj); ! } ! void LUACALL wxluaO_addgcobject(lua_State *L, void *obj_ptr, wxObject* wxobj) { lua_pushlightuserdata(L, &wxlua_lreg_gcobjects_key); // push key --- 482,486 ---- // ---------------------------------------------------------------------------- ! void LUACALL wxluaO_addgcobject(lua_State *L, void *obj_ptr, int wxl_type) { lua_pushlightuserdata(L, &wxlua_lreg_gcobjects_key); // push key *************** *** 497,501 **** { lua_pop(L, 2); // pop table and value ! wxFAIL_MSG(wxT("Tracking an object twice in wxluaO_addgcobject: ") + wxString(wxobj->GetClassInfo()->GetClassName())); return; } --- 493,497 ---- { lua_pop(L, 2); // pop table and value ! wxFAIL_MSG(wxT("Tracking an object twice in wxluaO_addgcobject: ") + wxluaT_typename(L, wxl_type)); return; } *************** *** 504,508 **** // Then add it lua_pushlightuserdata(L, obj_ptr); // push key ! lua_pushlightuserdata(L, wxobj); // push value lua_rawset(L, -3); // set t[key] = value, pops key and value --- 500,504 ---- // Then add it lua_pushlightuserdata(L, obj_ptr); // push key ! lua_pushnumber(L, wxl_type); // push value lua_rawset(L, -3); // set t[key] = value, pops key and value *************** *** 510,526 **** } ! bool LUACALL wxluaO_deletegcobject(lua_State *L, void* udata, void *obj_ptr, int flags) { ! if (obj_ptr == NULL) return false; // can happen bool delete_all = WXLUA_HASBIT(flags, WXLUA_DELETE_OBJECT_ALL); ! // Remove the weak ref to it int udata_count = wxluaO_untrackweakobject(L, delete_all ? NULL : udata, obj_ptr); if (delete_all || (udata_count < 1)) { wxlua_removederivedmethods(L, obj_ptr); lua_pushlightuserdata(L, &wxlua_lreg_gcobjects_key); // push key lua_rawget(L, LUA_REGISTRYINDEX); // pop key, push value (table) --- 506,538 ---- } ! bool LUACALL wxluaO_deletegcobject(lua_State *L, int stack_idx, int flags) { ! void* udata = lua_touserdata(L, stack_idx); ! void* obj_ptr = wxlua_touserdata(L, stack_idx, true); // clear lua userdata's ptr ! ! //if (obj_ptr == NULL) return false; // can happen bool delete_all = WXLUA_HASBIT(flags, WXLUA_DELETE_OBJECT_ALL); ! wxLuaBindClass *wxlClass = NULL; ! ! if (lua_getmetatable(L, stack_idx)) ! { ! lua_pushlightuserdata(L, &wxlua_metatable_wxluabindclass_key); // push key ! lua_rawget(L, -2); // get t[key] = value; pop key push value ! wxlClass = (wxLuaBindClass *)lua_touserdata(L, -1); ! lua_pop(L, 2); // pop metatable and lightuserdata value ! } ! ! // Remove the weak ref to it, will optionally clear all the metatables ! // for an userdata created for this object to make them unusable. int udata_count = wxluaO_untrackweakobject(L, delete_all ? NULL : udata, obj_ptr); if (delete_all || (udata_count < 1)) { + // remove any derived methods attached to this object wxlua_removederivedmethods(L, obj_ptr); + // check if we are really supposed to delete it lua_pushlightuserdata(L, &wxlua_lreg_gcobjects_key); // push key lua_rawget(L, LUA_REGISTRYINDEX); // pop key, push value (table) *************** *** 529,539 **** lua_rawget(L, -2); // get t[key] = value, pops key ! if (lua_islightuserdata(L, -1)) // is the wxObject* we delete { ! // delete the real object for the case where it's encapsulated ! wxObject *wxobj = (wxObject*)lua_touserdata(L, -1); ! delete wxobj; ! ! lua_pop(L, 1); // pop lightuserdata value lua_pushlightuserdata(L, obj_ptr); // push key --- 541,547 ---- lua_rawget(L, -2); // get t[key] = value, pops key ! if (wxlClass && lua_isnumber(L, -1)) // the wxLua type for it { ! lua_pop(L, 1); // pop number value lua_pushlightuserdata(L, obj_ptr); // push key *************** *** 542,545 **** --- 550,559 ---- lua_pop(L, 1); // pop delobj table + + // delete the object using the function stored in the wxLuaBindClass + if (obj_ptr) + wxlClass->delete_fn(&obj_ptr); + else + return false; return true; *************** *** 567,573 **** lua_rawget(L, -2); // get t[key] = value, pops key ! if (lua_islightuserdata(L, -1)) // is the wxObject* we delete { ! lua_pop(L, 1); // pop lightuserdata lua_pushlightuserdata(L, obj_ptr); // push key --- 581,587 ---- lua_rawget(L, -2); // get t[key] = value, pops key ! if (lua_isnumber(L, -1)) // is the wxLua type of the object { ! lua_pop(L, 1); // pop number lua_pushlightuserdata(L, obj_ptr); // push key *************** *** 592,596 **** lua_rawget(L, -2); // get t[key] = value, pops key ! bool found = lua_islightuserdata(L, -1); lua_pop(L, 2); // pop udata and table --- 606,610 ---- lua_rawget(L, -2); // get t[key] = value, pops key ! bool found = lua_isnumber(L, -1); lua_pop(L, 2); // pop udata and table *************** *** 611,619 **** wxString name(wxT("wxObject?")); ! wxObject* obj = (wxObject*)lua_touserdata(L, -1); ! if (obj && obj->GetClassInfo() && obj->GetClassInfo()->GetClassName()) ! name = obj->GetClassInfo()->GetClassName(); ! arrStr.Add(wxString::Format(wxT("%s(%p)"), name.c_str(), obj)); lua_pop(L, 1); // pop value, lua_next will pop key at end --- 625,632 ---- wxString name(wxT("wxObject?")); ! int wxl_type = (int)lua_tonumber(L, -1); ! name = wxluaT_typename(L, wxl_type); ! arrStr.Add(wxString::Format(wxT("%s(%p)"), name.c_str(), lua_touserdata(L, -2))); lua_pop(L, 1); // pop value, lua_next will pop key at end *************** *** 2314,2342 **** ClearCallbacks(); - /* - // Let the Lua garbage collector do it for shutdown. - - // Delete all the wxObject data - lua_pushlightuserdata(m_lua_State, &wxlua_lreg_gcobjects_key); - lua_rawget(m_lua_State, LUA_REGISTRYINDEX); - - lua_pushnil(m_lua_State); - while (lua_next(m_lua_State, -2) != 0) - { - // value = -1, key = -2, table = -3 - wxObject* o = (wxObject*)lua_touserdata(m_lua_State, -1); // delete object not key - delete o; - - lua_pop(m_lua_State, 1); // pop value, lua_next will pop key at end - } - - lua_pop(m_lua_State, 1); // pop table - - // remove table since we've deleted everything in it - lua_pushlightuserdata(m_lua_State, &wxlua_lreg_gcobjects_key); - lua_newtable(m_lua_State); - lua_rawset(m_lua_State, LUA_REGISTRYINDEX); - */ - // remove refs table to try to clear memory gracefully wxlua_lreg_createtable(m_lua_State, &wxlua_lreg_refs_key); --- 2327,2330 ---- *************** *** 3041,3059 **** // memory tracking functions ! void wxLuaState::AddGCObject(wxObject *wxobj) ! { ! AddGCObject(wxobj, wxobj); ! } ! ! void wxLuaState::AddGCObject(void* obj_ptr, wxObject *wxobj) { ! wxCHECK_RET(Ok() && wxobj, wxT("Invalid wxLuaState or wxObject to track")); ! wxluaO_addgcobject(M_WXLSTATEDATA->m_lua_State, obj_ptr, wxobj); } ! bool wxLuaState::DeleteGCObject(void* udata, void *obj_ptr, int flags) { ! wxCHECK_MSG(Ok() && obj_ptr, false, wxT("Invalid wxLuaState or object")); ! return wxluaO_deletegcobject(M_WXLSTATEDATA->m_lua_State, udata, obj_ptr, flags); } --- 3029,3042 ---- // memory tracking functions ! void wxLuaState::AddGCObject(void* obj_ptr, int wxl_type) { ! wxCHECK_RET(Ok() && obj_ptr, wxT("Invalid wxLuaState or wxObject to track")); ! wxluaO_addgcobject(M_WXLSTATEDATA->m_lua_State, obj_ptr, wxl_type); } ! bool wxLuaState::DeleteGCObject(int stack_idx, int flags) { ! wxCHECK_MSG(Ok(), false, wxT("Invalid wxLuaState or object")); ! return wxluaO_deletegcobject(M_WXLSTATEDATA->m_lua_State, stack_idx, flags); } Index: wxlbind.cpp =================================================================== RCS file: /cvsroot/wxlua/wxLua/modules/wxlua/src/wxlbind.cpp,v retrieving revision 1.124 retrieving revision 1.125 diff -C2 -d -r1.124 -r1.125 *** wxlbind.cpp 25 Sep 2009 18:47:58 -0000 1.124 --- wxlbind.cpp 1 Oct 2009 04:21:02 -0000 1.125 *************** *** 291,299 **** int LUACALL wxlua_userdata_delete(lua_State *L) { - void* udata = lua_touserdata(L, 1); - void* obj_ptr = wxlua_touserdata(L, 1, false); - // if removed from tracked mem list, remove the metatable so that __gc is not called on this object. ! if ((obj_ptr != NULL) && wxluaO_deletegcobject(L, udata, obj_ptr, WXLUA_DELETE_OBJECT_ALL)) { lua_pushnil(L); --- 291,296 ---- int LUACALL wxlua_userdata_delete(lua_State *L) { // if removed from tracked mem list, remove the metatable so that __gc is not called on this object. ! if (wxluaO_deletegcobject(L, 1, WXLUA_DELETE_OBJECT_ALL)) { lua_pushnil(L); *************** *** 303,307 **** { wxString msg; ! msg.Printf(wxT("wxLua: Unable to call wxuserdata:delete() on object %p, someone else owns it."), obj_ptr); // leave this printf since we really want to know if this happens --- 300,304 ---- { wxString msg; ! msg.Printf(wxT("wxLua: Unable to call wxuserdata:delete() on object!")); // leave this printf since we really want to know if this happens *************** *** 323,331 **** if ((wxlClass != NULL) && wxlua_iswxuserdata(L, 1) && (wxluaT_type(L, 1) == *wxlClass->wxluatype)) { - void* udata = lua_touserdata(L, 1); - void* obj_ptr = wxlua_touserdata(L, 1, true); // clear lua userdata's ptr - // clean up the rest of this, this won't error if the key doesn't exist ! wxluaO_deletegcobject(L, udata, obj_ptr, WXLUA_DELETE_OBJECT_LAST); } --- 320,325 ---- if ((wxlClass != NULL) && wxlua_iswxuserdata(L, 1) && (wxluaT_type(L, 1) == *wxlClass->wxluatype)) { // clean up the rest of this, this won't error if the key doesn't exist ! wxluaO_deletegcobject(L, 1, WXLUA_DELETE_OBJECT_LAST); } Index: wxlua_bind.cpp =================================================================== RCS file: /cvsroot/wxlua/wxLua/modules/wxlua/src/wxlua_bind.cpp,v retrieving revision 1.34 retrieving revision 1.35 diff -C2 -d -r1.34 -r1.35 *** wxlua_bind.cpp 27 Sep 2009 03:13:55 -0000 1.34 --- wxlua_bind.cpp 1 Oct 2009 04:21:02 -0000 1.35 *************** *** 38,41 **** --- 38,47 ---- + void wxLua_wxLuaState_delete_function(void** p) + { + wxLuaState* o = (wxLuaState*)(*p); + delete o; + } + // Map Lua Class Methods to C Binding Functions wxLuaBindMethod wxLuaState_methods[] = { *************** *** 118,122 **** returns = new wxLuaObject(wxlState, 1); // add to tracked memory list ! wxluaO_addgcobject(L, returns); // push the constructed class pointer wxluaT_pushuserdatatype(L, returns, wxluatype_wxLuaObject); --- 124,128 ---- returns = new wxLuaObject(wxlState, 1); // add to tracked memory list ! wxluaO_addgcobject(L, returns, wxluatype_wxLuaObject); // push the constructed class pointer wxluaT_pushuserdatatype(L, returns, wxluatype_wxLuaObject); *************** *** 128,131 **** --- 134,143 ---- + void wxLua_wxLuaObject_delete_function(void** p) + { + wxLuaObject* o = (wxLuaObject*)(*p); + delete o; + } + // Map Lua Class Methods to C Binding Functions wxLuaBindMethod wxLuaObject_methods[] = { *************** *** 622,625 **** --- 634,671 ---- return 0; } + else if (strcmp(idx_str, "baseclass_wxluatypes") == 0) + { + if (wxlClass->baseclass_wxluatypes) + { + lua_newtable(L); + size_t i = 0; + while (wxlClass->baseclass_wxluatypes[i]) + { + lua_pushnumber(L, *wxlClass->baseclass_wxluatypes[i]); + lua_rawseti(L, -2, i + 1); + ++i; + } + return 1; + } + + return 0; + } + else if (strcmp(idx_str, "baseclass_vtable_offsets") == 0) + { + if (wxlClass->baseclass_wxluatypes) // check this for NULL not baseclass_vtable_offsets + { + lua_newtable(L); + size_t i = 0; + while (wxlClass->baseclass_wxluatypes[i]) // see above + { + lua_pushnumber(L, wxlClass->baseclass_vtable_offsets[i]); + lua_rawseti(L, -2, i + 1); + ++i; + } + return 1; + } + + return 0; + } else if (strcmp(idx_str, "enums") == 0) { *************** *** 1215,1220 **** --- 1261,1268 ---- extern wxLuaBindMethod wxLuaObject_methods[]; extern int wxLuaObject_methodCount; + extern void wxLua_wxLuaObject_delete_function(void** p); extern wxLuaBindMethod wxLuaState_methods[]; extern int wxLuaState_methodCount; + extern void wxLua_wxLuaState_delete_function(void** p); *************** *** 1225,1230 **** static wxLuaBindClass classList[] = { ! { wxluaclassname_wxLuaObject, wxLuaObject_methods, wxLuaObject_methodCount, CLASSINFO(wxLuaObject), &wxluatype_wxLuaObject, wxluabaseclassnames_wxLuaObject, wxluabaseclassbinds_wxLuaObject, NULL, NULL, NULL, 0, }, ! { wxluaclassname_wxLuaState, wxLuaState_methods, wxLuaState_methodCount, CLASSINFO(wxLuaState), &wxluatype_wxLuaState, wxluabaseclassnames_wxLuaState, wxluabaseclassbinds_wxLuaState, NULL, NULL, NULL, 0, }, { 0, 0, 0, 0, 0, 0, 0 }, --- 1273,1278 ---- static wxLuaBindClass classList[] = { ! { wxluaclassname_wxLuaObject, wxLuaObject_methods, wxLuaObject_methodCount, CLASSINFO(wxLuaObject), &wxluatype_wxLuaObject, wxluabaseclassnames_wxLuaObject, wxluabaseclassbinds_wxLuaObject, NULL, NULL, NULL, 0, &wxLua_wxLuaObject_delete_function, }, ! { wxluaclassname_wxLuaState, wxLuaState_methods, wxLuaState_methodCount, CLASSINFO(wxLuaState), &wxluatype_wxLuaState, wxluabaseclassnames_wxLuaState, wxluabaseclassbinds_wxLuaState, NULL, NULL, NULL, 0, &wxLua_wxLuaState_delete_function, }, { 0, 0, 0, 0, 0, 0, 0 }, |