From: John L. <jr...@us...> - 2007-06-08 22:50:25
|
Update of /cvsroot/wxlua/wxLua/modules/wxlua/src In directory sc8-pr-cvs9.sourceforge.net:/tmp/cvs-serv18758/wxLua/modules/wxlua/src Modified Files: wxlbind.cpp wxlstate.cpp Log Message: Add some functions to compare the wxlua tags type to lua_type() within lua also get the tag number for any object in lua Cleanup the bindings.wx.lua to make it more useful Index: wxlstate.cpp =================================================================== RCS file: /cvsroot/wxlua/wxLua/modules/wxlua/src/wxlstate.cpp,v retrieving revision 1.104 retrieving revision 1.105 diff -C2 -d -r1.104 -r1.105 *** wxlstate.cpp 8 Jun 2007 03:40:51 -0000 1.104 --- wxlstate.cpp 8 Jun 2007 22:50:17 -0000 1.105 *************** *** 40,46 **** #include "wx/tokenzr.h" - extern int wxLuaEventListCompareFn(const void *p1, const void *p2); // wxlbind.cpp - extern int wxLuaClassListCompareByTag(const void *p1, const void *p2); // wxlbind.cpp - wxLuaState wxNullLuaState(false); --- 40,43 ---- *************** *** 329,334 **** } ! void LUACALL wxlua_tpushusertag(lua_State *L, const void *u, int tag) { const void **ptr = (const void **)lua_newuserdata(L, sizeof(void *)); if (ptr != NULL) --- 326,332 ---- } ! bool LUACALL wxlua_tpushusertag(lua_State *L, const void *u, int tag) { + // Wrap the void* pointer in a newuserdata const void **ptr = (const void **)lua_newuserdata(L, sizeof(void *)); if (ptr != NULL) *************** *** 338,347 **** --- 336,350 ---- if (wxlua_tget(L, tag)) { + // pop the table and set it as the metatable for the newuserdata if (lua_setmetatable(L, -2) == 0) wxlua_terror(L, "wxLua: Unable to set metatable"); + else + return true; } } else wxlua_terror(L, "wxLua: Out of memory"); + + return false; } *************** *** 362,366 **** } ! void * LUACALL wxlua_ttouserdata(lua_State *L, int stack_idx, bool null_ptr /* = false*/) { void *pdata = NULL; --- 365,369 ---- } ! void* LUACALL wxlua_ttouserdata(lua_State *L, int stack_idx, bool null_ptr /* = false*/) { void *pdata = NULL; *************** *** 369,373 **** if (ptr != NULL) { ! pdata = *ptr; if (null_ptr) *ptr = NULL; --- 372,376 ---- if (ptr != NULL) { ! pdata = *ptr; // get the pointer the userdata holds if (null_ptr) *ptr = NULL; *************** *** 429,449 **** } ! void LUACALL wxlua_tsettag(lua_State *L, int tag) { ! if (wxlua_tget(L, tag)) { ! if (!lua_setmetatable(L, -2)) wxlua_terror(L, "wxLua: Unable to set metatable"); } } ! bool LUACALL wxlua_tsettagmethod(lua_State *L, int tag, const char *method, lua_CFunction func, void *pClass /* = NULL*/) { ! if (wxlua_tget(L, tag)) ! { if (pClass != NULL) { ! lua_pushstring(L, method); // push method name lua_pushlightuserdata(L, pClass); // push the userdata lua_pushcclosure(L, func, 1); // push func with pClass as upvalue --- 432,456 ---- } ! bool LUACALL wxlua_tsettag(lua_State *L, int tag) { ! if (wxlua_tget(L, tag)) // get the table from the wxLuaReferences registry table { ! if (!lua_setmetatable(L, -2)) // set it as the metatable of the object at the top of the stack wxlua_terror(L, "wxLua: Unable to set metatable"); + else + return true; } + + return false; } ! bool LUACALL wxlua_tsettagmethod(lua_State *L, int tag, const char *method_name, lua_CFunction func, void *pClass /* = NULL*/) { ! if (wxlua_tget(L, tag)) // get the metatable for the object from the ! { // wxLuaReferences registy table if (pClass != NULL) { ! lua_pushstring(L, method_name); // push method name lua_pushlightuserdata(L, pClass); // push the userdata lua_pushcclosure(L, func, 1); // push func with pClass as upvalue *************** *** 451,458 **** else { ! lua_pushstring(L, method); lua_pushcclosure(L, func, 0); } ! lua_rawset(L, -3); // t["method"] = func lua_pop(L, 1); // pop the table from tget we got from the wxLuaReferences registry table return true; --- 458,466 ---- else { ! lua_pushstring(L, method_name); lua_pushcclosure(L, func, 0); } ! ! lua_rawset(L, -3); // t["method_name"] = func lua_pop(L, 1); // pop the table from tget we got from the wxLuaReferences registry table return true; *************** *** 534,537 **** --- 542,585 ---- } + int wxlua_getwxluatype(int luatype) + { + switch (luatype) + { + case LUA_TNONE : return WXLUAARG_None; + case LUA_TNIL : return WXLUAARG_Nil; + case LUA_TBOOLEAN : return WXLUAARG_Boolean; + case LUA_TLIGHTUSERDATA : return WXLUAARG_LightUserData; + case LUA_TNUMBER : return WXLUAARG_Number; + case LUA_TSTRING : return WXLUAARG_String; + case LUA_TTABLE : return WXLUAARG_LuaTable; + case LUA_TFUNCTION : return WXLUAARG_LuaFunction; + case LUA_TUSERDATA : return WXLUAARG_UserData; + case LUA_TTHREAD : return WXLUAARG_LuaThread; + //case LUA_T??? : return WXLUAARG_Enum; + } + + return -1; + } + + int wxlua_getluatype(int wxluaarg_tag) + { + switch (wxluaarg_tag) + { + case WXLUAARG_None : return LUA_TNONE; + case WXLUAARG_Nil : return LUA_TNIL; + case WXLUAARG_Boolean : return LUA_TBOOLEAN; + case WXLUAARG_LightUserData : return LUA_TLIGHTUSERDATA; + case WXLUAARG_Number : return LUA_TNUMBER; + case WXLUAARG_String : return LUA_TSTRING; + case WXLUAARG_LuaTable : return LUA_TTABLE; + case WXLUAARG_LuaFunction : return LUA_TFUNCTION; + case WXLUAARG_UserData : return LUA_TUSERDATA; + case WXLUAARG_LuaThread : return LUA_TTHREAD; + case WXLUAARG_Enum : return LUA_TNUMBER; + } + + return -1; + } + const char* LUACALL wxlua_getstringtype(lua_State *L, int stack_idx) { *************** *** 1726,1738 **** { wxLuaBinding* binding = node->GetData(); ! ! wxLuaBindClass* pClass = (wxLuaBindClass *)binding->GetClassArray(); size_t i, class_count = binding->GetClassCount(); ! for (i = 0; i < class_count; i++) { ! wxLuaBindClass* pLuaClass = pClass + i; ! // pLuaClass->baseclass = NULL; FIXME ! if (pLuaClass->baseclassName) { wxLuaBindingList::compatibility_iterator basenode; --- 1774,1785 ---- { wxLuaBinding* binding = node->GetData(); ! wxLuaBindClass* wxlClass = binding->GetClassArray(); size_t i, class_count = binding->GetClassCount(); ! ! for (i = 0; i < class_count; ++i, ++wxlClass) { ! // wxlClass->baseclass = NULL; FIXME ! if (wxlClass->baseclassName) // does it have a name { wxLuaBindingList::compatibility_iterator basenode; *************** *** 1742,1746 **** // found base class in binding? ! if (basebinding->SetBaseClass(pLuaClass)) break; } --- 1789,1793 ---- // found base class in binding? ! if (basebinding->SetBaseClass(wxlClass)) break; } *************** *** 1776,1780 **** } ! const wxLuaBindClass* wxLuaState::GetLuaClass(int iClassTag) const { wxCHECK_MSG(GetRefData() != NULL, NULL, wxT("Invalid wxLuaState")); --- 1823,1827 ---- } ! const wxLuaBindClass* wxLuaState::GetLuaClass(int class_tag) const { wxCHECK_MSG(GetRefData() != NULL, NULL, wxT("Invalid wxLuaState")); *************** *** 1782,1788 **** lua_State* L = M_WXLSTATEDATA->m_lua_State; ! if (wxlua_tget(L, iClassTag) && lua_istable(L, -1)) { ! lua_pushstring(L, "wxLuaBindClass"); // t[iClassTag] = { __gc = wxLuaBindClass (or nil if not a class tag) lua_rawget(L, -2); const wxLuaBindClass* wxlClass = (wxLuaBindClass *)lua_touserdata(L, -1); // actually lightuserdata --- 1829,1835 ---- 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 *************** *** 1793,1821 **** } else - return NULL; - - - /* - wxLuaBindClass classItem; - classItem.class_tag = &iClassTag; - - wxLuaBindingList::compatibility_iterator node; - for (node = M_WXLSTATEDATA->m_wxlStateData->m_bindingList.GetFirst(); node; node = node->GetNext() ) { ! wxLuaBinding* binding = node->GetData(); ! ! // this relies on LUA allocating tags in ascending order of definition ! // if LUA stops doing this, then the search may break. ! const wxLuaBindClass *pLuaClass = (wxLuaBindClass *) bsearch(&classItem, ! binding->GetLuaClassList(), ! binding->GetLuaClassCount(), ! sizeof(wxLuaBindClass), ! wxLuaClassListCompareByTag); ! if (pLuaClass) ! return pLuaClass; } return NULL; - */ } --- 1840,1857 ---- } else { ! // we shouldn't ever need this code ! wxLuaBindingList::compatibility_iterator node; ! for (node = M_WXLSTATEDATA->m_wxlStateData->m_bindingList.GetFirst(); node; node = node->GetNext() ) ! { ! wxLuaBinding* binding = node->GetData(); ! const wxLuaBindClass *wxlClass = binding->GetLuaClass(class_tag); ! ! if (wxlClass) ! return wxlClass; ! } } return NULL; } *************** *** 2130,2137 **** } ! void wxLuaState::tpushusertag(const void *u, int tag) { ! wxCHECK_RET(Ok(), wxT("Invalid wxLuaState")); ! wxlua_tpushusertag(M_WXLSTATEDATA->m_lua_State, u, tag); } --- 2166,2173 ---- } ! bool wxLuaState::tpushusertag(const void *u, int tag) { ! wxCHECK_MSG(Ok(), false, wxT("Invalid wxLuaState")); ! return wxlua_tpushusertag(M_WXLSTATEDATA->m_lua_State, u, tag); } *************** *** 2160,2167 **** } ! void wxLuaState::tsettag(int tag) { ! wxCHECK_RET(Ok(), wxT("Invalid wxLuaState")); ! wxlua_tsettag(M_WXLSTATEDATA->m_lua_State, tag); } --- 2196,2203 ---- } ! bool wxLuaState::tsettag(int tag) { ! wxCHECK_MSG(Ok(), false, wxT("Invalid wxLuaState")); ! return wxlua_tsettag(M_WXLSTATEDATA->m_lua_State, tag); } Index: wxlbind.cpp =================================================================== RCS file: /cvsroot/wxlua/wxLua/modules/wxlua/src/wxlbind.cpp,v retrieving revision 1.67 retrieving revision 1.68 diff -C2 -d -r1.67 -r1.68 *** wxlbind.cpp 8 Jun 2007 03:40:51 -0000 1.67 --- wxlbind.cpp 8 Jun 2007 22:50:17 -0000 1.68 *************** *** 999,1002 **** --- 999,1019 ---- } + + const wxLuaBindClass* wxLuaBinding::GetLuaClass(int class_tag) const + { + wxLuaBindClass classItem; + classItem.class_tag = &class_tag; + + // this relies on LUA allocating tags in ascending order of definition + // if LUA stops doing this, then the search may break. + const wxLuaBindClass *wxlClass = (wxLuaBindClass *)bsearch(&classItem, + m_classArray, + m_classCount, + sizeof(wxLuaBindClass), + wxLuaClassListCompareByTag); + + return wxlClass; // maybe NULL if class_tag not found + } + const wxLuaBindClass* wxLuaBinding::GetLuaClass(const wxLuaBindMethod* wxlMethod) const { *************** *** 1057,1061 **** void **ptr = (void **)lua_touserdata(L, 1); wxLuaBindCFunc* wxlCFunc= (wxLuaBindCFunc*)*ptr; ! //wxLuaBinding *wxlBinding = (wxLuaBinding *)lua_touserdata(L, lua_upvalueindex(1)); int idx_type = lua_type(L, 2); --- 1074,1078 ---- void **ptr = (void **)lua_touserdata(L, 1); wxLuaBindCFunc* wxlCFunc= (wxLuaBindCFunc*)*ptr; ! wxLuaBinding *wxlBinding = (wxLuaBinding *)lua_touserdata(L, lua_upvalueindex(1)); int idx_type = lua_type(L, 2); *************** *** 1099,1118 **** return 1; } ! else if (strcmp(idx_str, "argtags_name") == 0) { ! size_t idx, count = wxlCFunc->maxargs; ! lua_createtable(L, count, 0); ! ! // check for terminating null argtag ! for (idx = 0; (idx < count) && wxlCFunc->argtags[idx]; ++idx) ! { ! lua_pushstring(L, wx2lua(wxlState.GetLuaTagName(*wxlCFunc->argtags[idx]))); ! lua_rawseti(L, -2, idx + 1); ! } ! return 1; } - - } --- 1116,1124 ---- return 1; } ! else if (strcmp(idx_str, "class_name") == 0) { ! lua_pushstring(L, wxlBinding->GetLuaClass(wxlCFunc)->name); return 1; } } *************** *** 1194,1197 **** --- 1200,1208 ---- return 0; } + else if (strcmp(idx_str, "class_name") == 0) + { + lua_pushstring(L, wxlBinding->GetLuaClass(wxlMethod)->name); + return 1; + } } *************** *** 1249,1252 **** --- 1260,1267 ---- } + lua_pushstring(L, "wxLuaBindClass"); // so we know where this came from + lua_pushvalue(L, 1); + lua_rawset(L, -3); + return 1; } *************** *** 1323,1326 **** --- 1338,1345 ---- } + lua_pushstring(L, "wxLuaBindClass"); // so we know where this came from + lua_pushvalue(L, 1); + lua_rawset(L, -3); + return 1; } *************** *** 1376,1379 **** --- 1395,1403 ---- return 1; } + else if (strcmp(idx_str, "GetStringCount") == 0) + { + lua_pushnumber(L, wxlBinding->GetStringCount()); + return 1; + } else if (strcmp(idx_str, "GetEventCount") == 0) { |