From: John L. <jr...@us...> - 2007-06-11 03:58:19
|
Update of /cvsroot/wxlua/wxLua/modules/wxlua/src In directory sc8-pr-cvs9.sourceforge.net:/tmp/cvs-serv19140/wxLua/modules/wxlua/src Modified Files: wxlbind.cpp wxlcallb.cpp wxlstate.cpp Log Message: Remove wxArrayString_FromLuaTable and wxArrayInt_FromLuaTable binding tag since we convert from wxArrayInt/String automatically Fix listctrl sorting Fix validator code using wxLuaObject Rename wxLuaState::LuaCall to LuaPCall since that what it calls Lots more cleanup and shuffling of code to more appropriate places Index: wxlstate.cpp =================================================================== RCS file: /cvsroot/wxlua/wxLua/modules/wxlua/src/wxlstate.cpp,v retrieving revision 1.105 retrieving revision 1.106 diff -C2 -d -r1.105 -r1.106 *** wxlstate.cpp 8 Jun 2007 22:50:17 -0000 1.105 --- wxlstate.cpp 11 Jun 2007 03:58:10 -0000 1.106 *************** *** 441,445 **** return true; } ! return false; } --- 441,445 ---- return true; } ! return false; } *************** *** 470,473 **** --- 470,545 ---- } + const wxLuaBindClass* LUACALL wxlua_tgetclass(lua_State* L, int class_tag) + { + // note: wxlua_tget doesn't leave anything on the stack on failure + if (wxlua_tget(L, class_tag) && lua_istable(L, -1)) + { + // t[class_tag] = { ["wxLuaBindClass"] = lightuserdata wxLuaBindClass... (or nil if not a class tag) + lua_pushstring(L, "wxLuaBindClass"); + lua_rawget(L, -2); + const wxLuaBindClass* wxlClass = (wxLuaBindClass *)lua_touserdata(L, -1); // actually lightuserdata + + lua_pop(L, 2); // pop wxLuaReferences table and lightuserdata (or nil if none) + + return wxlClass; + } + + return NULL; + } + + const wxLuaBindClass* LUACALL wxlua_tgetclass(lua_State* L, const char* class_name) + { + wxlua_pushstring_wxLuaClasses(L); + lua_rawget(L, LUA_REGISTRYINDEX); // pop key, push result (the classes table) + + lua_pushstring(L, class_name); // t["class_name"] = lightuserdata wxLuaBindClass (or nil if not a class tag) + lua_rawget(L, -2); + const wxLuaBindClass* wxlClass = (wxLuaBindClass *)lua_touserdata(L, -1); // actually lightuserdata + + lua_pop(L, 2); // pop wxLuaClasses table and lightuserdata + + return wxlClass; + } + + int LUACALL wxlua_isderivedclass(lua_State* L, int class_tag, int base_class_tag) + { + if (class_tag == base_class_tag) + return 0; + + const wxLuaBindClass *wxlClass = wxlua_tgetclass(L, class_tag); + + int levels = 1; + + while (wxlClass != NULL) // check level by level + { + if (wxlClass->baseclass && (*wxlClass->baseclass->class_tag == base_class_tag)) + return levels; + + wxlClass = wxlClass->baseclass; + levels++; + } + + return -1; + } + + int LUACALL wxlua_isderivedclass(const wxLuaBindClass* wxlClass, const wxLuaBindClass* wxlBaseClass) + { + if ((wxlClass == NULL) || (wxlBaseClass == NULL)) + return -1; + + const wxLuaBindClass* c = wxlClass; + int levels = 0; + while (c != NULL) + { + if (c->class_tag == wxlBaseClass->class_tag) + return levels; + + c = c->baseclass; + levels++; + } + + return -1; + } + int LUACALL wxlua_iswxluatype(int luatype, int wxluaarg_tag) { *************** *** 644,702 **** } - wxString* LUACALL wxlua_getwxStringarray(lua_State *L, int stack_idx, int &count) - { - wxString *strArray = NULL; - wxArrayString wxarrString; - count = 0; - - if (wxlua_getwxArrayString(L, stack_idx, wxarrString) > 0) - { - count = (int)wxarrString.GetCount(); - strArray = new wxString[count]; - for (int idx = 0; idx < count; idx++) - strArray[idx] = wxarrString[idx]; - } - - return strArray; - } - int LUACALL wxlua_getwxArrayString(lua_State* L, int stack_idx, wxArrayString& wxarrString) - { - int count = 0; - - if (lua_istable(L, stack_idx)) - { - while(1) - { - lua_rawgeti(L, stack_idx, count+1); - - if (lua_isnil(L, -1)) - { - lua_pop(L, 1); - break; - } - else if (!wxlua_isstringtype(L, -1)) - { - wxString msg(wxString::Format(_("wxLua: Expected a table of strings for parameter %d, but table index %d is '%s'."), - stack_idx, count+1, lua2wx(lua_typename(L, lua_type(L, -1))).c_str())); - wxlua_terror(L, wx2lua(msg)); - return count; - } - - const char *pString = lua_tostring(L, -1); - wxarrString.Add(lua2wx(pString)); - ++count; - - lua_pop(L, 1); - } - } - else - { - wxString msg(wxString::Format(_("wxLua: Expected a table of strings for parameter %d, but got '%s'."), - stack_idx, lua2wx(lua_typename(L, lua_type(L, stack_idx))).c_str())); - wxlua_terror(L, wx2lua(msg)); - } - - return count; - } const char** LUACALL wxlua_getchararray(lua_State *L, int stack_idx, int &count) { --- 716,719 ---- *************** *** 724,783 **** return pItems; } - int * LUACALL wxlua_getintarray(lua_State *L, int stack_idx, int &count) - { - int *intArray = NULL; - wxArrayInt wxarrInt; - count = 0; - - if (wxlua_getwxArrayInt(L, stack_idx, wxarrInt) > 0) - { - count = (int)wxarrInt.GetCount(); - intArray = new int[count]; - for (int idx = 0; idx < count; idx++) - intArray[idx] = wxarrInt[idx]; - } - - return intArray; - } - - int LUACALL wxlua_getwxArrayInt(lua_State *L, int stack_idx, wxArrayInt &wxarrInt) - { - int count = 0; - - if (lua_istable(L, stack_idx)) - { - while(1) - { - lua_rawgeti(L, stack_idx, count+1); - - if (lua_isnil(L, -1)) - { - lua_pop(L, 1); - break; - } - else if (!wxlua_isnumbertype(L, -1)) - { - wxString msg(wxString::Format(_("wxLua: Expected a table of integers for parameter %d, but table index %d is '%s'."), - stack_idx, count+1, lua2wx(lua_typename(L, lua_type(L, -1))).c_str())); - wxlua_terror(L, wx2lua(msg)); - return count; - } - - int iValue = (int)lua_tonumber(L, -1); - wxarrInt.Add(iValue); - ++count; - - lua_pop(L, 1); - } - } - else - { - wxString msg(wxString::Format(_("wxLua: Expected a table of integers for parameter %d, but got '%s'."), - stack_idx, lua2wx(lua_typename(L, lua_type(L, stack_idx))).c_str())); - wxlua_terror(L, wx2lua(msg)); - } - - return count; - } int LUACALL wxlua_pushwxArrayStringtable(lua_State *L, const wxArrayString &strArray) --- 741,744 ---- *************** *** 1156,1160 **** // try to get the state we've stored ! lua_pushstring( L, "__wxLuaStateRefData" ); lua_rawget( L, LUA_REGISTRYINDEX ); --- 1117,1121 ---- // try to get the state we've stored ! wxlua_pushstring_wxLuaStateRefData(L); lua_rawget( L, LUA_REGISTRYINDEX ); *************** *** 1212,1216 **** // Stick us into the lua_State - push key, value ! lua_pushstring( L, "__wxLuaStateRefData" ); lua_pushlightuserdata( L, (void*)m_refData ); // set the value --- 1173,1177 ---- // Stick us into the lua_State - push key, value ! wxlua_pushstring_wxLuaStateRefData(L); lua_pushlightuserdata( L, (void*)m_refData ); // set the value *************** *** 1218,1222 **** // Create a table for overridden functions for C++ objects ! lua_pushstring( L, "__wxLuaDerivedFunctions" ); lua_newtable(L); // set the value --- 1179,1183 ---- // Create a table for overridden functions for C++ objects ! wxlua_pushstring_wxLuaDerivedFunctions(L); lua_newtable(L); // set the value *************** *** 1261,1265 **** // Stick us into the lua_State - push key, value ! lua_pushstring( L, "__wxLuaStateRefData" ); lua_pushlightuserdata( L, (void*)m_refData ); // set the value --- 1222,1226 ---- // Stick us into the lua_State - push key, value ! wxlua_pushstring_wxLuaStateRefData(L); lua_pushlightuserdata( L, (void*)m_refData ); // set the value *************** *** 1267,1271 **** // Create a table for overridden functions for C++ objects ! lua_pushstring( L, "__wxLuaDerivedFunctions" ); lua_newtable(L); // set the value --- 1228,1232 ---- // Create a table for overridden functions for C++ objects ! wxlua_pushstring_wxLuaDerivedFunctions(L); lua_newtable(L); // set the value *************** *** 1432,1436 **** if (status == 0) { ! status = LuaCall(0, 0); //status = lua_pcall(L, 0, LUA_MULTRET, 0); // call main } --- 1393,1397 ---- if (status == 0) { ! status = LuaPCall(0, 0); //status = lua_pcall(L, 0, LUA_MULTRET, 0); // call main } *************** *** 1467,1471 **** if (status == 0) { ! status = LuaCall(0, 0); //status = lua_pcall(L, 0, LUA_MULTRET, 0); // call main } --- 1428,1432 ---- if (status == 0) { ! status = LuaPCall(0, 0); //status = lua_pcall(L, 0, LUA_MULTRET, 0); // call main } *************** *** 1523,1527 **** } ! int wxLuaState::LuaCall(int narg, int clear) { wxCHECK_MSG(Ok(), LUA_ERRRUN, wxT("Invalid wxLuaState")); --- 1484,1488 ---- } ! int wxLuaState::LuaPCall(int narg, int nresults) { wxCHECK_MSG(Ok(), LUA_ERRRUN, wxT("Invalid wxLuaState")); *************** *** 1538,1542 **** lua_insert(L, base); // put it under chunk and args ! status = lua_pcall(L, narg, (clear ? 0 : LUA_MULTRET), base); lua_remove(L, base); // remove traceback function --- 1499,1503 ---- lua_insert(L, base); // put it under chunk and args ! status = lua_pcall(L, narg, nresults, base); lua_remove(L, base); // remove traceback function *************** *** 1825,1843 **** const wxLuaBindClass* wxLuaState::GetLuaClass(int class_tag) const { ! wxCHECK_MSG(GetRefData() != NULL, NULL, wxT("Invalid wxLuaState")); lua_State* L = M_WXLSTATEDATA->m_lua_State; ! if (wxlua_tget(L, class_tag) && lua_istable(L, -1)) ! { ! lua_pushstring(L, "wxLuaBindClass"); // t[class_tag] = { __gc = wxLuaBindClass (or nil if not a class tag) ! lua_rawget(L, -2); ! const wxLuaBindClass* wxlClass = (wxLuaBindClass *)lua_touserdata(L, -1); // actually lightuserdata ! ! lua_pop(L, 2); // pop wxLuaReferences table and lightuserdata (or nil if none) ! ! return wxlClass; ! } ! else { // we shouldn't ever need this code --- 1786,1795 ---- const wxLuaBindClass* wxLuaState::GetLuaClass(int class_tag) const { ! wxCHECK_MSG(Ok(), NULL, wxT("Invalid wxLuaState")); lua_State* L = M_WXLSTATEDATA->m_lua_State; + const wxLuaBindClass* wxlClass = wxlua_tgetclass(L, class_tag); ! if (wxlClass == NULL) { // we shouldn't ever need this code *************** *** 1846,1857 **** { wxLuaBinding* binding = node->GetData(); ! const wxLuaBindClass *wxlClass = binding->GetLuaClass(class_tag); ! ! if (wxlClass) ! return wxlClass; } } ! return NULL; } --- 1798,1808 ---- { wxLuaBinding* binding = node->GetData(); ! wxlClass = binding->GetLuaClass(class_tag); ! ! if (wxlClass) break; } } ! return wxlClass; } *************** *** 1861,1875 **** lua_State* L = M_WXLSTATEDATA->m_lua_State; ! ! wxlua_pushstring_wxLuaClasses(L); ! lua_rawget(L, LUA_REGISTRYINDEX); // pop key, push result (the classes table) ! ! lua_pushstring(L, className); // t["className"] = wxLuaBindClass (or nil if not a class tag) ! lua_rawget(L, -2); ! const wxLuaBindClass* wxlClass = (wxLuaBindClass *)lua_touserdata(L, -1); // actually lightuserdata ! ! lua_pop(L, 2); // pop wxLuaClasses table and lightuserdata ! ! return wxlClass; } --- 1812,1816 ---- lua_State* L = M_WXLSTATEDATA->m_lua_State; ! return wxlua_tgetclass(L, className); } *************** *** 1903,1916 **** } ! bool wxLuaState::IsDerivedClass(int iClassTag, int iBaseClassTag) const { ! if (iClassTag == iBaseClassTag) ! return true; ! ! const wxLuaBindClass *pLuaClass = GetLuaClass(iClassTag); ! if (pLuaClass && pLuaClass->baseclass) ! return IsDerivedClass(*pLuaClass->baseclass->class_tag, iBaseClassTag); ! return false; } --- 1844,1853 ---- } ! int wxLuaState::IsDerivedClass(int iClassTag, int iBaseClassTag) const { ! wxCHECK_MSG(Ok(), -1, wxT("Invalid wxLuaState")); ! lua_State* L = M_WXLSTATEDATA->m_lua_State; ! return wxlua_isderivedclass(L, iClassTag, iBaseClassTag); } *************** *** 1989,1993 **** wxCHECK_MSG(Ok(), 0, wxT("Invalid wxLuaState")); ! // Find the wxWindow tag once and store it for later use if (M_WXLSTATEDATA->m_wxlStateData->m_wxluatag_wxString == 0) { --- 1926,1930 ---- wxCHECK_MSG(Ok(), 0, wxT("Invalid wxLuaState")); ! // Find the wxString tag once and store it for later use if (M_WXLSTATEDATA->m_wxlStateData->m_wxluatag_wxString == 0) { *************** *** 2211,2214 **** --- 2148,2177 ---- // wxLua get data type + int wxLuaState::IswxLuaType(int luatype, int wxluaarg_tag) const + { + wxCHECK_MSG(Ok(), -1, wxT("Invalid wxLuaState")); + + int ok = wxlua_iswxluatype(luatype, wxluaarg_tag); + + // if we don't know the type (it's not predefined) + if (ok < 0) + { + if (luatype == LUA_TTABLE) + { + lua_State* L = M_WXLSTATEDATA->m_lua_State; + + const wxLuaBindClass* wxlClass = wxlua_tgetclass(L, "wxArrayString"); + if (wxlClass && IsDerivedClass(wxluaarg_tag, *wxlClass->class_tag)) + return 1; + + wxlClass = wxlua_tgetclass(L, "wxArrayInt"); + if (wxlClass && IsDerivedClass(wxluaarg_tag, *wxlClass->class_tag)) + return 1; + } + } + + return ok; + } + bool wxLuaState::IsUserDataType(int stack_idx, int tag) const { *************** *** 2219,2225 **** { int stack_tag = ttag(stack_idx); ! if ((M_WXLSTATEDATA->m_wxlStateData->m_wxluatag_NULL == stack_tag) || // FIXME ! (M_WXLSTATEDATA->m_wxlStateData->m_wxluatag_wxLuaFunction == stack_tag) || ! IsDerivedClass(stack_tag, tag)) return true; } --- 2182,2189 ---- { int stack_tag = ttag(stack_idx); ! if ((GetLuaNULLTag() == stack_tag) || // FIXME ! (GetwxLuaFunctionTag() == stack_tag) || ! ((tag == WXLUAARG_String) && (IsDerivedClass(stack_tag, GetwxStringTag()) >= 0)) || ! (IsDerivedClass(stack_tag, tag) >= 0)) return true; } *************** *** 2244,2248 **** else if (M_WXLSTATEDATA->m_wxlStateData->m_wxluatag_NULL == stack_tag) return NULL; ! else if (IsDerivedClass(stack_tag, tag)) return ttouserdata(stack_idx); } --- 2208,2212 ---- else if (M_WXLSTATEDATA->m_wxlStateData->m_wxluatag_NULL == stack_tag) return NULL; ! else if (IsDerivedClass(stack_tag, tag) >= 0) return ttouserdata(stack_idx); } *************** *** 2262,2266 **** // if the object we are referencing is derived from wxWindow ! if (IsDerivedClass(tag, GetwxWindowTag())) // s_wxluatag_wxWindow { wxWindow* win = wxDynamicCast(data, wxWindow); --- 2226,2230 ---- // if the object we are referencing is derived from wxWindow ! if (IsDerivedClass(tag, GetwxWindowTag()) >= 0) { wxWindow* win = wxDynamicCast(data, wxWindow); *************** *** 2325,2329 **** int stack_tag = wxlua_ttag(L, stack_idx); ! if (IsDerivedClass(stack_tag, GetwxStringTag())) { wxString* wxstr = (wxString*)wxlua_ttouserdata(L, stack_idx, false); --- 2289,2293 ---- int stack_tag = wxlua_ttag(L, stack_idx); ! if (IsDerivedClass(stack_tag, GetwxStringTag()) >= 0) { wxString* wxstr = (wxString*)wxlua_ttouserdata(L, stack_idx, false); *************** *** 2357,2361 **** { wxCHECK_MSG(Ok(), false, wxT("Invalid wxLuaState")); ! return wxlua_isstringtype(M_WXLSTATEDATA->m_lua_State, stack_idx); } bool wxLuaState::IsBooleanType(int stack_idx) const --- 2321,2337 ---- { wxCHECK_MSG(Ok(), false, wxT("Invalid wxLuaState")); ! ! lua_State* L = M_WXLSTATEDATA->m_lua_State; ! ! if (!wxlua_isstringtype(M_WXLSTATEDATA->m_lua_State, stack_idx)) ! { ! if (lua_isuserdata(L, stack_idx) && !lua_islightuserdata(L, stack_idx)) ! { ! int stack_tag = wxlua_ttag(L, stack_idx); ! return (IsDerivedClass(stack_tag, GetwxStringTag()) >= 0); ! } ! } ! ! return true; } bool wxLuaState::IsBooleanType(int stack_idx) const *************** *** 2377,2388 **** wxString* wxLuaState::GetStringArray(int stack_idx, int &count) { ! wxCHECK_MSG(Ok(), NULL, wxT("Invalid wxLuaState")); ! return wxlua_getwxStringarray(M_WXLSTATEDATA->m_lua_State, stack_idx, count); } ! int wxLuaState::GetwxArrayString(int stack_idx, wxArrayString &strArray) { wxCHECK_MSG(Ok(), 0, wxT("Invalid wxLuaState")); ! return wxlua_getwxArrayString(M_WXLSTATEDATA->m_lua_State, stack_idx, strArray); } --- 2353,2432 ---- wxString* wxLuaState::GetStringArray(int stack_idx, int &count) { ! // GetwxArrayString will error out if !Ok() and we'll return NULL ! wxString *strArray = NULL; ! wxArrayString wxarrString; ! count = 0; ! ! if (GetwxArrayString(stack_idx, wxarrString) > 0) ! { ! count = (int)wxarrString.GetCount(); ! strArray = new wxString[count]; ! for (int idx = 0; idx < count; idx++) ! strArray[idx] = wxarrString[idx]; ! } ! ! return strArray; } ! int wxLuaState::GetwxArrayString(int stack_idx, wxArrayString &wxarrString) { wxCHECK_MSG(Ok(), 0, wxT("Invalid wxLuaState")); ! ! lua_State* L = M_WXLSTATEDATA->m_lua_State; ! int count = -1; ! ! if (lua_istable(L, stack_idx)) ! { ! count = 0; ! ! while(1) ! { ! lua_rawgeti(L, stack_idx, count+1); ! ! if (lua_isnil(L, -1)) ! { ! lua_pop(L, 1); ! break; ! } ! else if (!IsStringType(-1)) ! { ! wxString msg(wxString::Format(_("wxLua: Expected a table of strings for parameter %d, but table index %d is '%s'."), ! stack_idx, count+1, lua2wx(lua_typename(L, lua_type(L, -1))).c_str())); ! terror(msg); ! return count; ! } ! ! wxarrString.Add(GetwxStringType(-1)); ! ++count; ! ! lua_pop(L, 1); ! } ! } ! else if (lua_isuserdata(L, stack_idx) && !lua_islightuserdata(L, stack_idx)) ! { ! int class_tag = wxlua_ttag(L, stack_idx); ! const wxLuaBindClass* wxlClass = wxlua_tgetclass(L, class_tag); ! const wxLuaBindClass* arrStrClass = wxlua_tgetclass(L, "wxArrayString"); ! ! if (wxlua_isderivedclass(wxlClass, arrStrClass) >= 0) ! { ! wxLuaState wxlState(L); ! wxArrayString *arrStr = (wxArrayString *)wxlState.GetUserDataType(stack_idx, *arrStrClass->class_tag); ! if (arrStr) ! { ! count = arrStr->GetCount(); ! wxarrString = *arrStr; ! } ! } ! } ! ! if (count < 0) ! { ! wxString msg(wxString::Format(_("wxLua: Expected a wxArrayString or table of strings for parameter %d, but got '%s'."), ! stack_idx, lua2wx(lua_typename(L, lua_type(L, stack_idx))).c_str())); ! terror(msg); ! } ! ! return count; } *************** *** 2407,2418 **** int* wxLuaState::GetIntArray(int stack_idx, int &count) { ! wxCHECK_MSG(Ok(), NULL, wxT("Invalid wxLuaState")); ! return wxlua_getintarray(M_WXLSTATEDATA->m_lua_State, stack_idx, count); } ! int wxLuaState::GetwxArrayInt(int stack_idx, wxArrayInt &intArray) { wxCHECK_MSG(Ok(), 0, wxT("Invalid wxLuaState")); ! return wxlua_getwxArrayInt(M_WXLSTATEDATA->m_lua_State, stack_idx, intArray); } --- 2451,2531 ---- int* wxLuaState::GetIntArray(int stack_idx, int &count) { ! // GetwxArrayInt will error out if !Ok() and we'll return NULL ! int *intArray = NULL; ! wxArrayInt wxarrInt; ! count = 0; ! ! if (GetwxArrayInt(stack_idx, wxarrInt) > 0) ! { ! count = (int)wxarrInt.GetCount(); ! intArray = new int[count]; ! for (int idx = 0; idx < count; idx++) ! intArray[idx] = wxarrInt[idx]; ! } ! ! return intArray; } ! int wxLuaState::GetwxArrayInt(int stack_idx, wxArrayInt &wxarrInt) { wxCHECK_MSG(Ok(), 0, wxT("Invalid wxLuaState")); ! ! lua_State* L = M_WXLSTATEDATA->m_lua_State; ! int count = -1; ! ! if (lua_istable(L, stack_idx)) ! { ! count = 0; ! ! while(1) ! { ! lua_rawgeti(L, stack_idx, count+1); ! ! if (lua_isnil(L, -1)) ! { ! lua_pop(L, 1); ! break; ! } ! else if (!wxlua_isnumbertype(L, -1)) ! { ! wxString msg(wxString::Format(_("wxLua: Expected a table of integers for parameter %d, but table index %d is '%s'."), ! stack_idx, count+1, lua2wx(lua_typename(L, lua_type(L, -1))).c_str())); ! terror(msg); ! return count; ! } ! ! int iValue = (int)lua_tonumber(L, -1); ! wxarrInt.Add(iValue); ! ++count; ! ! lua_pop(L, 1); ! } ! } ! else if (lua_isuserdata(L, stack_idx) && !lua_islightuserdata(L, stack_idx)) ! { ! int class_tag = wxlua_ttag(L, stack_idx); ! const wxLuaBindClass* wxlClass = wxlua_tgetclass(L, class_tag); ! const wxLuaBindClass* arrIntClass = wxlua_tgetclass(L, "wxArrayInt"); ! ! if (wxlua_isderivedclass(wxlClass, arrIntClass) >= 0) ! { ! wxLuaState wxlState(L); ! wxArrayInt *arrInt = (wxArrayInt *)wxlState.GetUserDataType(stack_idx, *arrIntClass->class_tag); ! if (arrInt) ! { ! count = arrInt->GetCount(); ! wxarrInt = *arrInt; ! } ! } ! } ! ! if (count < 0) ! { ! wxString msg(wxString::Format(_("wxLua: Expected a wxArrayInt or a table of integers for parameter %d, but got '%s'."), ! stack_idx, lua2wx(lua_typename(L, lua_type(L, stack_idx))).c_str())); ! terror(msg); ! } ! ! return count; } *************** *** 2436,2512 **** } ! wxLuaBindMethod* wxLuaState::GetLuaMethod(const wxLuaBindClass *wxlClass, const char *methodName, bool search_baseclasses) { ! wxCHECK_MSG(wxlClass, NULL, wxT("Invalid wxLuaBindClass to find method from.")); ! ! int i_method, method_count = wxlClass->methods_n; ! // find a method in the class, recurse through classes from which this class is derived. ! for (i_method = 0; i_method < method_count; ++i_method) ! { ! wxLuaBindMethod *wxlMethod = wxlClass->methods + i_method; ! if (WXLUA_HASBIT(wxlMethod->type, WXLUAMETHOD_METHOD | WXLUAMETHOD_GETPROP) ! && (strcmp(wxlMethod->name, methodName) == 0)) ! { ! // iterate through the base classes to find if this function is ! // an overload, but only if we haven't checked already. ! if (!WXLUA_HASBIT(wxlMethod->type, WXLUAMETHOD_OVERLOAD_BASE)) ! { ! wxLuaBindClass *baseClass = wxlClass->baseclass; ! wxLuaBindMethod *parentMethod = wxlMethod; ! while (baseClass) ! { ! parentMethod->type |= WXLUAMETHOD_OVERLOAD_BASE; // have checked this ! wxLuaBindMethod* baseMethod = GetLuaMethod(baseClass, methodName, false); ! if (baseMethod) ! { ! parentMethod->basemethod = baseMethod; ! parentMethod = baseMethod; ! // we have already checked the base classes below this ! if (WXLUA_HASBIT(baseMethod->type, WXLUAMETHOD_OVERLOAD_BASE)) ! break; ! } ! baseClass = baseClass->baseclass; ! } ! } ! return wxlMethod; ! } } ! if (search_baseclasses && wxlClass->baseclass) ! return GetLuaMethod(wxlClass->baseclass, methodName); ! ! return NULL; ! } ! ! wxLuaBindMethod* wxLuaState::GetLuaProperty(const wxLuaBindClass *wxlClass, const char *propName, bool isLuaSetProp) ! { ! wxCHECK_MSG(wxlClass, NULL, wxT("Invalid wxLuaBindClass in wxLuaState::GetLuaProperty")); ! int i_method, method_count = wxlClass->methods_n; ! // find a method in the class, recurse through classes from which this class is derived. ! for (i_method = 0; i_method < method_count; ++i_method) { ! wxLuaBindMethod *wxlMethod = wxlClass->methods + i_method; ! if (isLuaSetProp) ! { ! if (WXLUA_HASBIT(wxlMethod->type, WXLUAMETHOD_SETPROP) && (strcmp(wxlMethod->name, propName) == 0)) ! return wxlMethod; ! } ! else ! { ! if (WXLUA_HASBIT(wxlMethod->type, WXLUAMETHOD_GETPROP) && (strcmp(wxlMethod->name, propName) == 0)) ! return wxlMethod; ! } } ! if (wxlClass->baseclass) ! return GetLuaProperty(wxlClass->baseclass, propName, isLuaSetProp); ! return NULL; } --- 2549,2597 ---- } ! bool wxLuaState::SetDerivedMethod(void *pObject, const char *name, wxLuaObject* wxlObj) { ! wxCHECK_MSG(Ok() && pObject, false, wxT("Invalid wxLuaState or object to set derived method for.")); ! lua_State* L = M_WXLSTATEDATA->m_lua_State; ! wxlua_pushstring_wxLuaDerivedFunctions(L); ! lua_rawget( L, LUA_REGISTRYINDEX ); // pop key, push table ! lua_pushlightuserdata(L, (void *)pObject); ! lua_rawget(L, -2); // pop key, push table or nil ! if (!lua_istable(L, -1)) ! { ! lua_pop(L, 1); // pop nil value ! // add new table for this object ! lua_pushlightuserdata(L, (void *)pObject); ! lua_newtable(L); ! lua_rawset(L, -3); ! // put the new table back on the top of the stack ! lua_pushlightuserdata(L, (void *)pObject); ! lua_rawget(L, -2); } ! // see if there already is a method ! lua_pushstring( L, name ); ! lua_rawget(L, -2); ! if (lua_islightuserdata(L, -1)) { ! // already have a method, delete it before replacing it ! wxLuaObject* o = (wxLuaObject*)lua_touserdata( L, -1 ); ! delete o; } ! lua_pop(L, 1); // pop the deleted old object, or nil ! lua_pushstring( L, name ); ! lua_pushlightuserdata(L, (void*)wxlObj); ! lua_rawset(L, -3); ! ! lua_pop(L, 2); // pop the object and overridden function table ! ! return true; } *************** *** 2519,2523 **** bool found = false; ! lua_pushstring( L, "__wxLuaDerivedFunctions" ); lua_rawget( L, LUA_REGISTRYINDEX ); // pop key, push table --- 2604,2608 ---- bool found = false; ! wxlua_pushstring_wxLuaDerivedFunctions(L); lua_rawget( L, LUA_REGISTRYINDEX ); // pop key, push table *************** *** 2549,2552 **** --- 2634,2681 ---- } + bool wxLuaState::RemoveDerivedMethod(void *pObject) const + { + wxCHECK_MSG(Ok() && pObject, false, wxT("Invalid wxLuaState or object to remove.")); + + lua_State* L = M_WXLSTATEDATA->m_lua_State; + bool found = false; + + wxlua_pushstring_wxLuaDerivedFunctions(L); + lua_rawget( L, LUA_REGISTRYINDEX ); // pop key, push table + + lua_pushlightuserdata(L, (void *)pObject); + lua_rawget(L, -2); // pop key, push table or nil + if (lua_istable(L, -1)) + { + found = true; + int t = lua_gettop(L); + lua_pushnil(L); /* first key */ + while (lua_next(L, t) != 0) + { + // uses 'key' (at index -2) and 'value' (at index -1) + //wxPrintf(wxT("%s - %s\n"), lua2wx(lua_tostring(L, -2)).c_str(), lua2wx(lua_typename(L, lua_type(L, -1))).c_str()); + + if (lua_islightuserdata(L, -1)) + { + wxLuaObject* o = (wxLuaObject*)lua_touserdata(L, -1); + delete o; + } + + // removes 'value'; keeps 'key' for next iteration + lua_pop(L, 1); + } + + lua_pop(L, 1); // pop the obj table + lua_pushlightuserdata(L, (void *)pObject); + lua_pushnil(L); + lua_rawset(L, -3); // nil the obj table + lua_pop(L, 1); // pop reg table + } + else + lua_pop(L, 2); // pop the reg table and nil for the obj table + + return found; + } + wxLuaState wxLuaState::GetDerivedMethodState(void *pObject, const char *method) { *************** *** 3431,3438 **** // Does the lua type match the wxlua arg tag type ! int is_ok = wxlua_iswxluatype(ltype, tag); ! // unknown standard wxlua arg type, check binding tag ! if (is_ok == -1) { is_ok = (IsUserDataType(arg_lua, tag) || --- 3560,3567 ---- // Does the lua type match the wxlua arg tag type ! int is_ok = IswxLuaType(ltype, tag); ! // unknown/invalid standard wxlua arg type, check binding tag ! if ((is_ok == -1) || ((is_ok == 0) && (tag == WXLUAARG_String))) { is_ok = (IsUserDataType(arg_lua, tag) || Index: wxlbind.cpp =================================================================== RCS file: /cvsroot/wxlua/wxLua/modules/wxlua/src/wxlbind.cpp,v retrieving revision 1.68 retrieving revision 1.69 diff -C2 -d -r1.68 -r1.69 *** wxlbind.cpp 8 Jun 2007 22:50:17 -0000 1.68 --- wxlbind.cpp 11 Jun 2007 03:58:10 -0000 1.69 *************** *** 77,83 **** (wxlState.ttag(1) == wxlState.GetwxLuaFunctionTag())) { ! wxLuaFunction *pFunction = (wxLuaFunction *)wxlState.ttouserdata(1, true); ! if (pFunction != NULL) ! delete pFunction; } return 0; --- 77,83 ---- (wxlState.ttag(1) == wxlState.GetwxLuaFunctionTag())) { ! wxLuaFunction *wxlFunction = (wxLuaFunction *)wxlState.ttouserdata(1, true); ! if (wxlFunction != NULL) ! delete wxlFunction; } return 0; *************** *** 93,104 **** (wxlState.ttag(1) == wxlState.GetwxLuaFunctionTag())) { ! wxLuaFunction *pFunction = (wxLuaFunction *)wxlState.ttouserdata(1, false); // remove the userdata *this from the stack // can't do this for static and nonstatic overloaded functions ! //if (pFunction->m_wxlMethod && WXLUA_HASBIT(pFunction->m_wxlMethod->type, WXLUAMETHOD_STATIC)) // lua_remove(L, 1); ! return pFunction->CallMethod(L); } return 0; --- 93,104 ---- (wxlState.ttag(1) == wxlState.GetwxLuaFunctionTag())) { ! wxLuaFunction *wxlFunction = (wxLuaFunction *)wxlState.ttouserdata(1, false); // remove the userdata *this from the stack // can't do this for static and nonstatic overloaded functions ! //if (wxlFunction->m_wxlMethod && WXLUA_HASBIT(wxlFunction->m_wxlMethod->type, WXLUAMETHOD_STATIC)) // lua_remove(L, 1); ! return wxlFunction->CallMethod(L); } return 0; *************** *** 110,119 **** IMPLEMENT_ABSTRACT_CLASS(wxLuaObject, wxObject) ! wxLuaObject::wxLuaObject(const wxLuaState& wxlState, int iParam) : m_wxlState(new wxLuaState(wxlState)), m_bool(false), m_int(0), m_alloc_flags(wxLUAOBJECT_NONE) { ! // set up the reference ! m_iReference = m_wxlState->tinsert(iParam); } --- 110,119 ---- IMPLEMENT_ABSTRACT_CLASS(wxLuaObject, wxObject) ! wxLuaObject::wxLuaObject(const wxLuaState& wxlState, int stack_idx) : m_wxlState(new wxLuaState(wxlState)), m_bool(false), m_int(0), m_alloc_flags(wxLUAOBJECT_NONE) { ! // set up the reference to the item on the stack ! m_iReference = m_wxlState->tinsert(stack_idx); } *************** *** 149,153 **** if (HasAllocationFlag(wxLUAOBJECT_BOOL)) { ! lua_pushnumber(L, m_bool); return true; } --- 149,153 ---- if (HasAllocationFlag(wxLUAOBJECT_BOOL)) { ! lua_pushboolean(L, m_bool); return true; } *************** *** 164,186 **** else if (HasAllocationFlag(wxLUAOBJECT_ARRAYINT)) { ! lua_newtable(L); ! ! size_t idx, count = m_arrayInt.GetCount(); ! for (idx = 0; idx < count; ++idx) ! { ! lua_pushnumber(L, m_arrayInt.Item(idx)); ! lua_rawseti(L, -2, idx); ! } return true; } ! ! if (m_wxlState->tget(m_iReference)) return true; ! lua_pushnil(L); // oops, put something on the stack ! return false; } ! void wxLuaObject::SetObject(int iParam) { wxCHECK_RET(m_wxlState->Ok(), wxT("Invalid wxLuaState")); --- 164,177 ---- else if (HasAllocationFlag(wxLUAOBJECT_ARRAYINT)) { ! wxlua_pushwxArrayInttable(L, m_arrayInt); return true; } ! else if (m_wxlState->tget(m_iReference)) return true; ! return false; // nothing on the stack } ! void wxLuaObject::SetObject(int stack_idx) { wxCHECK_RET(m_wxlState->Ok(), wxT("Invalid wxLuaState")); *************** *** 191,195 **** if (m_wxlState->Ok()) ! m_iReference = m_wxlState->tinsert(iParam); } --- 182,186 ---- if (m_wxlState->Ok()) ! m_iReference = m_wxlState->tinsert(stack_idx); } *************** *** 199,204 **** if (m_wxlState->Ok() && (m_iReference != LUA_NOREF) && GetObject()) { ! m_bool = (lua_tonumber(m_wxlState->GetLuaState(), -1) != 0); m_alloc_flags |= wxLUAOBJECT_BOOL; } return &m_bool; --- 190,196 ---- if (m_wxlState->Ok() && (m_iReference != LUA_NOREF) && GetObject()) { ! m_bool = (lua_toboolean(m_wxlState->GetLuaState(), -1) != 0); m_alloc_flags |= wxLUAOBJECT_BOOL; + m_wxlState->lua_Pop(1); } return &m_bool; *************** *** 212,215 **** --- 204,208 ---- m_int = (int) lua_tonumber(m_wxlState->GetLuaState(), -1); m_alloc_flags |= wxLUAOBJECT_INT; + m_wxlState->lua_Pop(1); } return &m_int; *************** *** 223,227 **** --- 216,222 ---- m_string = lua2wx(lua_tostring(m_wxlState->GetLuaState(), -1)); m_alloc_flags |= wxLUAOBJECT_STRING; + m_wxlState->lua_Pop(1); } + return &m_string; } *************** *** 232,249 **** if (m_wxlState->Ok() && (m_iReference != LUA_NOREF) && GetObject()) { ! lua_State* L = m_wxlState->GetLuaState(); ! if (lua_istable(L, -1)) ! { ! int idx, nItems = luaL_getn(L, -1); ! for (idx = 1; idx <= nItems; ++idx) ! { ! lua_rawgeti(L, -1, idx); ! int iValue = (int) m_wxlState->GetNumberType(-1); ! m_arrayInt.Add(iValue); ! lua_pop(L, 1); ! } ! } ! lua_pop(L, 1); m_alloc_flags |= wxLUAOBJECT_ARRAYINT; } return &m_arrayInt; --- 227,233 ---- if (m_wxlState->Ok() && (m_iReference != LUA_NOREF) && GetObject()) { ! m_wxlState->GetwxArrayInt(-1, m_arrayInt); m_alloc_flags |= wxLUAOBJECT_ARRAYINT; + m_wxlState->lua_Pop(1); } return &m_arrayInt; *************** *** 284,320 **** //tracked = wxlState.RemoveTrackedObject((void*)key, true); // delete from m_pTrackedList ! ! lua_pushstring( L, "__wxLuaDerivedFunctions" ); ! lua_rawget( L, LUA_REGISTRYINDEX ); // pop key, push table ! ! lua_pushlightuserdata(L, (void *)key); ! lua_rawget(L, -2); // pop key, push table or nil ! if (lua_istable(L, -1)) ! { ! int t = lua_gettop(L); ! lua_pushnil(L); /* first key */ ! while (lua_next(L, t) != 0) ! { ! // uses 'key' (at index -2) and 'value' (at index -1) ! //wxPrintf(wxT("%s - %s\n"), lua2wx(lua_tostring(L, -2)).c_str(), lua2wx(lua_typename(L, lua_type(L, -1))).c_str()); ! ! if (lua_islightuserdata(L, -1)) ! { ! wxLuaObject* o = (wxLuaObject*)lua_touserdata(L, -1); ! delete o; ! } ! ! // removes 'value'; keeps 'key' for next iteration ! lua_pop(L, 1); ! } ! ! lua_pop(L, 1); // pop the obj table ! lua_pushlightuserdata(L, (void *)key); ! lua_pushnil(L); ! lua_rawset(L, -3); // nil the obj table ! lua_pop(L, 1); // pop reg table ! } ! else ! lua_pop(L, 2); // pop the reg table and nil for the obj table } --- 268,272 ---- //tracked = wxlState.RemoveTrackedObject((void*)key, true); // delete from m_pTrackedList ! wxlState.RemoveDerivedMethod((void*)key); } *************** *** 390,394 **** if (!found) { ! wxLuaBindMethod* wxlMethod = wxlState.GetLuaMethod(wxlClass, name); if (wxlMethod != NULL) { --- 342,346 ---- if (!found) { ! wxLuaBindMethod* wxlMethod = wxLuaBinding::GetClassMethod(wxlClass, name); if (wxlMethod != NULL) { *************** *** 417,421 **** memcpy(str+3, name, len+1); // include terminating NULL ! wxlMethod = wxlState.GetLuaMethod(wxlClass, str); if ((wxlMethod != NULL) && WXLUA_HASBIT(wxlMethod->type, WXLUAMETHOD_METHOD)) --- 369,373 ---- memcpy(str+3, name, len+1); // include terminating NULL ! wxlMethod = wxLuaBinding::GetClassMethod(wxlClass, str); if ((wxlMethod != NULL) && WXLUA_HASBIT(wxlMethod->type, WXLUAMETHOD_METHOD)) *************** *** 468,472 **** { // See if there is a WXLUAMETHOD_SETPROP in the wxLuaBindClass's wxLuaBindMethods ! wxLuaBindMethod *wxlMethod = wxlState.GetLuaProperty(wxlClass, name, true); if (wxlMethod != NULL) { --- 420,424 ---- { // See if there is a WXLUAMETHOD_SETPROP in the wxLuaBindClass's wxLuaBindMethods ! wxLuaBindMethod *wxlMethod = wxLuaBinding::GetClassProperty(wxlClass, name, true); if (wxlMethod != NULL) { *************** *** 489,493 **** //stk.DumpStack(); ! wxlMethod = wxlState.GetLuaMethod(wxlClass, str); if ((wxlMethod != NULL) && WXLUA_HASBIT(wxlMethod->type, WXLUAMETHOD_METHOD)) { --- 441,445 ---- //stk.DumpStack(); ! wxlMethod = wxLuaBinding::GetClassMethod(wxlClass, str); if ((wxlMethod != NULL) && WXLUA_HASBIT(wxlMethod->type, WXLUAMETHOD_METHOD)) { *************** *** 502,543 **** void *pObject = wxlState.ttouserdata(1); wxLuaObject* wxlObj = new wxLuaObject(wxlState, 3); ! ! lua_pushstring( L, "__wxLuaDerivedFunctions" ); ! lua_rawget( L, LUA_REGISTRYINDEX ); // pop key, push table ! ! lua_pushlightuserdata(L, (void *)pObject); ! lua_rawget(L, -2); // pop key, push table or nil ! if (!lua_istable(L, -1)) ! { ! lua_pop(L, 1); // pop nil value ! ! // add new table for this object ! lua_pushlightuserdata(L, (void *)pObject); ! lua_newtable(L); ! lua_rawset(L, -3); ! ! // put the new table back on the top of the stack ! lua_pushlightuserdata(L, (void *)pObject); ! lua_rawget(L, -2); ! } ! ! // see if there already is a method ! lua_pushstring( L, name ); ! lua_rawget(L, -2); ! ! if (lua_islightuserdata(L, -1)) ! { ! // already have a method, delete it before replacing it ! wxLuaObject* o = (wxLuaObject*)lua_touserdata( L, -1 ); ! delete o; ! } ! ! lua_pop(L, 1); // pop the deleted old object, or nil ! ! lua_pushstring( L, name ); ! lua_pushlightuserdata(L, (void*)wxlObj); ! lua_rawset(L, -3); ! ! lua_pop(L, 2); // pop the object and overridden function table } } --- 454,458 ---- void *pObject = wxlState.ttouserdata(1); wxLuaObject* wxlObj = new wxLuaObject(wxlState, 3); ! wxlState.SetDerivedMethod(pObject, name, wxlObj); } } *************** *** 1063,1066 **** --- 978,1056 ---- } + wxLuaBindMethod* wxLuaBinding::GetClassMethod(const wxLuaBindClass *wxlClass, const char *methodName, bool search_baseclasses) + { + wxCHECK_MSG(wxlClass, NULL, wxT("Invalid wxLuaBindClass to find method from.")); + + int i_method, method_count = wxlClass->methods_n; + // find a method in the class, recurse through classes from which this class is derived. + for (i_method = 0; i_method < method_count; ++i_method) + { + wxLuaBindMethod *wxlMethod = wxlClass->methods + i_method; + if (WXLUA_HASBIT(wxlMethod->type, WXLUAMETHOD_METHOD | WXLUAMETHOD_GETPROP) + && (strcmp(wxlMethod->name, methodName) == 0)) + { + // iterate through the base classes to find if this function is + // an overload, but only if we haven't checked already. + if (!WXLUA_HASBIT(wxlMethod->type, WXLUAMETHOD_OVERLOAD_BASE)) + { + wxLuaBindClass *baseClass = wxlClass->baseclass; + wxLuaBindMethod *parentMethod = wxlMethod; + + while (baseClass) + { + parentMethod->type |= WXLUAMETHOD_OVERLOAD_BASE; // have checked this + + wxLuaBindMethod* baseMethod = GetClassMethod(baseClass, methodName, false); + if (baseMethod) + { + parentMethod->basemethod = baseMethod; + parentMethod = baseMethod; + + // we have already checked the base classes below this + if (WXLUA_HASBIT(baseMethod->type, WXLUAMETHOD_OVERLOAD_BASE)) + break; + } + + baseClass = baseClass->baseclass; + } + } + + return wxlMethod; + } + } + + if (search_baseclasses && wxlClass->baseclass) + return GetClassMethod(wxlClass->baseclass, methodName); + + return NULL; + } + + wxLuaBindMethod* wxLuaBinding::GetClassProperty(const wxLuaBindClass *wxlClass, const char *propName, bool isLuaSetProp) + { + wxCHECK_MSG(wxlClass, NULL, wxT("Invalid wxLuaBindClass in wxLuaState::GetClassProperty")); + + int i_method, method_count = wxlClass->methods_n; + // find a method in the class, recurse through classes from which this class is derived. + for (i_method = 0; i_method < method_count; ++i_method) + { + wxLuaBindMethod *wxlMethod = wxlClass->methods + i_method; + if (isLuaSetProp) + { + if (WXLUA_HASBIT(wxlMethod->type, WXLUAMETHOD_SETPROP) && (strcmp(wxlMethod->name, propName) == 0)) + return wxlMethod; + } + else + { + if (WXLUA_HASBIT(wxlMethod->type, WXLUAMETHOD_GETPROP) && (strcmp(wxlMethod->name, propName) == 0)) + return wxlMethod; + } + } + + if (wxlClass->baseclass) + return GetClassProperty(wxlClass->baseclass, propName, isLuaSetProp); + + return NULL; + } + //----------------------------------------------------------------------------- // wxluabind_wxLuaBindCFunc_index *************** *** 1245,1249 **** { // Force the baseclass methods to be found ! wxlState.GetLuaMethod(wxlClass, wxlMethod->name, true); // Create table { wxLuaBindClass userdata } --- 1235,1239 ---- { // Force the baseclass methods to be found ! wxLuaBinding::GetClassMethod(wxlClass, wxlMethod->name, true); // Create table { wxLuaBindClass userdata } Index: wxlcallb.cpp =================================================================== RCS file: /cvsroot/wxlua/wxLua/modules/wxlua/src/wxlcallb.cpp,v retrieving revision 1.29 retrieving revision 1.30 diff -C2 -d -r1.29 -r1.30 *** wxlcallb.cpp 6 Jun 2007 23:43:16 -0000 1.29 --- wxlcallb.cpp 11 Jun 2007 03:58:10 -0000 1.30 *************** *** 130,134 **** { wxlState.tpushusertag(pEvent, eventClassTag); ! wxlState.LuaCall(1, true); } else --- 130,134 ---- { wxlState.tpushusertag(pEvent, eventClassTag); ! wxlState.LuaPCall(1, 0); } else |