From: John L. <jr...@us...> - 2007-03-23 00:09:56
|
Update of /cvsroot/wxlua/wxLua/modules/wxlua/src In directory sc8-pr-cvs9.sourceforge.net:/tmp/cvs-serv18290/wxLua/modules/wxlua/src Modified Files: wxlbind.cpp wxlcallb.cpp wxlstate.cpp Log Message: change tget to not push nil on the stack if it fails since we'd never use it store the WXLUACLASS in our wxLuaReferences LUA_REGISTRY table to allow lookup by tag store the WXLUACLASS in our wxLuaClasses LUA_REGISTRY table to allow lookup by name use lua_createtable for pushing wxArrayInt/String to preallocate them don't call lua_objlen on the tables for getting wxArrayInt/String since objlen traverses it so why do it twice. Index: wxlstate.cpp =================================================================== RCS file: /cvsroot/wxlua/wxLua/modules/wxlua/src/wxlstate.cpp,v retrieving revision 1.92 retrieving revision 1.93 diff -C2 -d -r1.92 -r1.93 *** wxlstate.cpp 21 Mar 2007 23:43:12 -0000 1.92 --- wxlstate.cpp 23 Mar 2007 00:09:36 -0000 1.93 *************** *** 289,296 **** } ! lua_pushvalue(L, stack_idx); // get value to store int nTop = lua_gettop(L); // this is where the value is ! wxLua_lua_push_wxLuaReferences(L); // push name of wxLua's reference registry table lua_rawget(L, LUA_REGISTRYINDEX); // pop key, push result (the refs table) --- 289,296 ---- } ! lua_pushvalue(L, stack_idx); // push a copy of value to store to top int nTop = lua_gettop(L); // this is where the value is ! wxLua_lua_push_wxLuaReferences(L); // push name of table to get as key lua_rawget(L, LUA_REGISTRYINDEX); // pop key, push result (the refs table) *************** *** 300,304 **** // in our refs table ! lua_pop(L, 3); // pop value return table_idx; --- 300,304 ---- // in our refs table ! lua_pop(L, 3); // pop value, table, value return table_idx; *************** *** 307,319 **** bool wxLua_lua_tremove(lua_State* L, int wxlref_index) { ! if (wxlref_index == LUA_REFNIL) return true; ! wxLua_lua_push_wxLuaReferences(L); lua_rawget(L, LUA_REGISTRYINDEX); // pop key, push result (the refs table) luaL_unref(L, -1, wxlref_index); // remove key and value in refs table // note: this key will be used for the next tinsert ! lua_pop(L, -1); // pop the refs table return true; } --- 307,319 ---- bool wxLua_lua_tremove(lua_State* L, int wxlref_index) { ! if (wxlref_index == LUA_REFNIL) // nothing to remove return true; ! wxLua_lua_push_wxLuaReferences(L); // push name of table to get as key lua_rawget(L, LUA_REGISTRYINDEX); // pop key, push result (the refs table) luaL_unref(L, -1, wxlref_index); // remove key and value in refs table // note: this key will be used for the next tinsert ! lua_pop(L, 1); // pop the refs table return true; } *************** *** 321,357 **** bool LUACALL wxLua_lua_tget(lua_State *L, int wxlref_index) { ! if (wxlref_index == LUA_REFNIL) ! { ! lua_pushnil(L); ! return true; ! } ! wxLua_lua_push_wxLuaReferences(L); // push name of wxLua's reference registry table lua_rawget(L, LUA_REGISTRYINDEX); // pop key, push result (the refs table) ! /* ! // Note: We used to check that wxlref_index <= length of the table and ! // if it wasn't pushnil. I don't think this is a good idea since ! // the table may have holes in it temporarily. -JL ! bool ret = false; ! lua_pushliteral(L, "n"); // push the key where we store table length ! lua_rawget(L, -2); // pop key, push table value ! int len = (int)lua_tonumber(L, -1); // get result value the length ! lua_pop(L, 1); // pop result ! // ensure ref index in range of table ! if ((wxlref_index > 0) && (wxlref_index <= len)) { ! lua_rawgeti(L, -1, wxlref_index); // push result ! ret = true; } - else - lua_pushnil(L); // push result - */ - - lua_rawgeti(L, -1, wxlref_index); // get table value at wxlref_index - lua_remove(L, -2); // remove wxLua's registry table ! return !lua_isnil(L, -1); // return if table has a valid value at index } --- 321,340 ---- bool LUACALL wxLua_lua_tget(lua_State *L, int wxlref_index) { ! if (wxlref_index == LUA_REFNIL) // nothing to get ! return false; ! wxLua_lua_push_wxLuaReferences(L); // push name of table to get as key lua_rawget(L, LUA_REGISTRYINDEX); // pop key, push result (the refs table) ! lua_rawgeti(L, -1, wxlref_index); // get table value at wxlref_index ! lua_remove(L, -2); // remove wxLua's registry table ! if (lua_isnil(L, -1)) // not a valid table key { ! lua_pop(L, 1); ! return false; } ! return true; // return if table has a valid value and it's on the stack } *************** *** 380,385 **** { *ptr = u; ! // try to get the object and set the metatable to the object ! if ((tag != TLUA_NOTAG) && wxLua_lua_tget(L, tag)) { if (lua_setmetatable(L, -2) == 0) --- 363,368 ---- { *ptr = u; ! // try to get the object's references table and set the metatable to the object ! if (wxLua_lua_tget(L, tag)) { if (lua_setmetatable(L, -2) == 0) *************** *** 393,405 **** int LUACALL wxLua_lua_ttag(lua_State *L, int stack_idx) { ! //wxPrintf(wxT("wxLua_lua_ttag %d"), index); int tag = TLUA_NOTAG; ! if (lua_getmetatable(L, stack_idx) != 0) { //wxPrintf(wxT(" has metatable")); ! lua_pushliteral(L, "tag"); lua_rawget(L, -2); ! if (lua_isnumber(L, -1)) { tag = (int)lua_tonumber(L, -1); --- 376,388 ---- int LUACALL wxLua_lua_ttag(lua_State *L, int stack_idx) { ! //wxPrintf(wxT("wxLua_lua_ttag %d"), stack_idx); int tag = TLUA_NOTAG; ! if (lua_getmetatable(L, stack_idx) != 0) // see tnewtag { //wxPrintf(wxT(" has metatable")); ! lua_pushliteral(L, "tag"); // get t["tag"] lua_rawget(L, -2); ! if (lua_isnumber(L, -1)) // FIXME this is really an error is this ever happens { tag = (int)lua_tonumber(L, -1); *************** *** 430,434 **** { lua_newtable(L); // create a table for our new tag ! lua_pushvalue(L, -1); // push a copy of our new table onto the top of the stack int tag = wxLua_lua_tinsert(L, -1); // insert the table into the wxLuaReferences registry table --- 413,417 ---- { lua_newtable(L); // create a table for our new tag ! lua_pushvalue(L, -1); // push a copy of new table to top of the stack int tag = wxLua_lua_tinsert(L, -1); // insert the table into the wxLuaReferences registry table *************** *** 436,439 **** --- 419,423 ---- lua_pushnumber(L, tag); lua_rawset(L, -3); + lua_pop(L, 1); // pop our new table return tag; *************** *** 442,445 **** --- 426,431 ---- int wxLua_lua_tnewweaktag(lua_State* L, bool weak_keys, bool weak_values) { + //if (!weak_keys && !weak_values) return wxLua_lua_tnewtag(L); + lua_newtable(L); // main table lua_newtable(L); // metatable *************** *** 457,461 **** if (weak_values) lua_pushliteral(L, "v"); ! else lua_pushnil(L); } --- 443,447 ---- if (weak_values) lua_pushliteral(L, "v"); ! else // yeah, we really shouldn't reach here lua_pushnil(L); } *************** *** 464,473 **** lua_rawset(L, -3); // set mode of main table lua_setmetatable(L, -2); // via the metatable ! lua_pushvalue(L, -1); ! int tag = wxLua_lua_tinsert(L, -1); ! lua_pushliteral(L, "tag"); lua_pushnumber(L, tag); lua_rawset(L, -3); ! lua_pop(L, 1); return tag; } --- 450,462 ---- lua_rawset(L, -3); // set mode of main table lua_setmetatable(L, -2); // via the metatable ! ! lua_pushvalue(L, -1); // push a copy of main table to top of stack ! int tag = wxLua_lua_tinsert(L, -1); // insert the table into the wxLuaReferences registry table ! ! lua_pushliteral(L, "tag"); // add t["tag"] = tag to new table lua_pushnumber(L, tag); lua_rawset(L, -3); ! ! lua_pop(L, 1); // pop our new table return tag; } *************** *** 475,479 **** void LUACALL wxLua_lua_tsettag(lua_State *L, int tag) { ! if ((tag != TLUA_NOTAG) && wxLua_lua_tget(L, tag)) { if (!lua_setmetatable(L, -2)) --- 464,468 ---- void LUACALL wxLua_lua_tsettag(lua_State *L, int tag) { ! if (wxLua_lua_tget(L, tag)) { if (!lua_setmetatable(L, -2)) *************** *** 482,494 **** } ! bool LUACALL wxLua_lua_tsettagmethod(lua_State *L, int tag, const char *method, lua_CFunction func, void *pClass /* = NULL*/) { ! if ((tag != TLUA_NOTAG) && wxLua_lua_tget(L, tag)) { if (pClass != NULL) { ! lua_pushstring(L, method); ! lua_pushlightuserdata(L, pClass); ! lua_pushcclosure(L, func, 1); } else --- 471,484 ---- } ! bool LUACALL wxLua_lua_tsettagmethod(lua_State *L, int tag, const char *method, ! lua_CFunction func, void *pClass /* = NULL*/) { ! if (wxLua_lua_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 } else *************** *** 497,502 **** lua_pushcclosure(L, func, 0); } ! lua_rawset(L, -3); ! lua_pop(L, 1); return true; } --- 487,492 ---- 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; } *************** *** 505,513 **** } - void LUACALL wxLua_lua_push_wxLuaReferences(lua_State *L) - { - lua_pushlstring(L, "wxLuaReferences", 15); // exclude trailing \0 - } - bool LUACALL wxLua_lua_isstringtype(lua_State* L, int stack_idx) { --- 495,498 ---- *************** *** 646,664 **** int LUACALL wxLua_lua_getwxArrayString(lua_State* L, int stack_idx, wxArrayString& wxarrString) { ! int idx, count = 0; if (lua_istable(L, stack_idx)) { ! int nItems = luaL_getn(L, stack_idx); ! wxarrString.Alloc(nItems); ! ! for (idx = 1; idx <= nItems; ++idx) { ! lua_rawgeti(L, stack_idx, idx); ! if (!wxLua_lua_isstringtype(L, -1)) { wxString msg(wxString::Format(_("wxLua: Expected a table of strings for parameter %d, but table index %d is '%s'."), ! stack_idx, idx, lua2wx(lua_typename(L, lua_type(L, -1))).c_str())); wxLua_lua_terror(L, wx2lua(msg)); return count; --- 631,651 ---- int LUACALL wxLua_lua_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_lua_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_lua_terror(L, wx2lua(msg)); return count; *************** *** 667,671 **** const char *pString = lua_tostring(L, -1); wxarrString.Add(lua2wx(pString)); ! count++; lua_pop(L, 1); --- 654,658 ---- const char *pString = lua_tostring(L, -1); wxarrString.Add(lua2wx(pString)); ! ++count; lua_pop(L, 1); *************** *** 690,694 **** if (lua_istable(L, stack_idx)) { ! int nItems = luaL_getn(L, stack_idx); if (nItems > 0) pItems = new const char *[nItems]; --- 677,681 ---- if (lua_istable(L, stack_idx)) { ! int nItems = lua_objlen(L, stack_idx); if (nItems > 0) pItems = new const char *[nItems]; *************** *** 725,743 **** int LUACALL wxLua_lua_getwxArrayInt(lua_State *L, int stack_idx, wxArrayInt &wxarrInt) { ! int idx, count = 0; if (lua_istable(L, stack_idx)) { ! int nItems = luaL_getn(L, stack_idx); ! wxarrInt.Alloc(nItems); ! ! for (idx = 1; idx <= nItems; ++idx) { ! lua_rawgeti(L, stack_idx, idx); ! if (!wxLua_lua_isnumbertype(L, -1)) { wxString msg(wxString::Format(_("wxLua: Expected a table of integers for parameter %d, but table index %d is '%s'."), ! stack_idx, idx, lua2wx(lua_typename(L, lua_type(L, -1))).c_str())); wxLua_lua_terror(L, wx2lua(msg)); return count; --- 712,732 ---- int LUACALL wxLua_lua_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_lua_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_lua_terror(L, wx2lua(msg)); return count; *************** *** 763,769 **** int LUACALL wxLua_lua_pushwxArrayStringtable(lua_State *L, const wxArrayString &strArray) { - lua_newtable(L); - size_t idx, count = strArray.GetCount(); for (idx = 0; idx < count; ++idx) { --- 752,758 ---- int LUACALL wxLua_lua_pushwxArrayStringtable(lua_State *L, const wxArrayString &strArray) { size_t idx, count = strArray.GetCount(); + lua_createtable(L, count, 0); + for (idx = 0; idx < count; ++idx) { *************** *** 776,782 **** int LUACALL wxLua_lua_pushwxArrayInttable(lua_State *L, const wxArrayInt &intArray) { - lua_newtable(L); - size_t idx, count = intArray.GetCount(); for (idx = 0; idx < count; ++idx) { --- 765,771 ---- int LUACALL wxLua_lua_pushwxArrayInttable(lua_State *L, const wxArrayInt &intArray) { size_t idx, count = intArray.GetCount(); + lua_createtable(L, count, 0); + for (idx = 0; idx < count; ++idx) { *************** *** 1726,1729 **** --- 1715,1723 ---- lua_State* L = M_WXLSTATEDATA->m_lua_State; + // create the wxLuaClasses table in the registry + wxLua_lua_push_wxLuaClasses(L); + lua_newtable(L); + lua_rawset(L, LUA_REGISTRYINDEX); + // create the wxLuaReferences table in registry wxLua_lua_push_wxLuaReferences(L); *************** *** 1798,1801 **** --- 1792,1813 ---- { wxCHECK_MSG(GetRefData() != NULL, NULL, wxT("Invalid wxLuaState")); + + lua_State* L = M_WXLSTATEDATA->m_lua_State; + + if (wxLua_lua_tget(L, iClassTag) && lua_istable(L, -1)) + { + lua_pushstring(L, "WXLUACLASS"); // t[iClassTag] = { __gc = WXLUACLASS (or nil if not a class tag) + lua_rawget(L, -2); + const WXLUACLASS* wxlClass = (WXLUACLASS *)lua_touserdata(L, -1); // actually lightuserdata + + lua_pop(L, 2); // pop wxLuaReferences table and lightuserdata (or nil if none) + + return wxlClass; + } + else + return NULL; + + + /* WXLUACLASS classItem; classItem.class_tag = &iClassTag; *************** *** 1818,1821 **** --- 1830,1834 ---- return NULL; + */ } *************** *** 1824,1844 **** wxCHECK_MSG(GetRefData() != NULL, NULL, wxT("Invalid wxLuaState")); ! wxLuaBindingList::compatibility_iterator node; ! for (node = M_WXLSTATEDATA->m_wxlStateData->m_bindingList.GetFirst(); node; node = node->GetNext() ) ! { ! wxLuaBinding* binding = node->GetData(); ! WXLUACLASS *pLuaClass = (WXLUACLASS *)binding->GetLuaClassList(); ! size_t i, class_count = binding->GetLuaClassCount(); ! for (i = 0; i < class_count; i++) ! { ! if (strcmp(pLuaClass->name, className) == 0) ! return pLuaClass; ! pLuaClass++; ! } ! } ! return NULL; } --- 1837,1852 ---- wxCHECK_MSG(GetRefData() != NULL, NULL, wxT("Invalid wxLuaState")); ! lua_State* L = M_WXLSTATEDATA->m_lua_State; ! wxLua_lua_push_wxLuaClasses(L); ! lua_rawget(L, LUA_REGISTRYINDEX); // pop key, push result (the classes table) ! lua_pushstring(L, className); // t["className"] = WXLUACLASS (or nil if not a class tag) ! lua_rawget(L, -2); ! const WXLUACLASS* wxlClass = (WXLUACLASS *)lua_touserdata(L, -1); // actually lightuserdata ! lua_pop(L, 2); // pop wxLuaClasses table and lightuserdata ! ! return wxlClass; } Index: wxlbind.cpp =================================================================== RCS file: /cvsroot/wxlua/wxLua/modules/wxlua/src/wxlbind.cpp,v retrieving revision 1.57 retrieving revision 1.58 diff -C2 -d -r1.57 -r1.58 *** wxlbind.cpp 21 Mar 2007 05:19:56 -0000 1.57 --- wxlbind.cpp 23 Mar 2007 00:09:36 -0000 1.58 *************** *** 156,160 **** } ! return (m_iReference != LUA_NOREF) && (m_wxlState->tget(m_iReference) != 0); } --- 156,164 ---- } ! if (m_wxlState->tget(m_iReference)) ! return true; ! ! lua_pushnil(L); // oops, put something on the stack ! return false; } *************** *** 610,613 **** --- 614,633 ---- iTag = *pClass->class_tag; + // store a lookup table for the class tags to WXLUACLASS structs + wxLua_lua_tget(L, *pClass->class_tag); + lua_pushstring(L, "WXLUACLASS"); + lua_pushlightuserdata(L, (void *)pClass); + lua_rawset(L, -3); // t[name] = tag + lua_remove(L, -1); // remove wxLua's registry wxLuaClasses table + + // store a lookup table for the class names to WXLUACLASS structs + wxLua_lua_push_wxLuaClasses(L); + lua_rawget(L, LUA_REGISTRYINDEX); // pop key, push result (the classes table) + + lua_pushstring(L, pClass->name); + lua_pushlightuserdata(L, (void *)pClass); + lua_rawset(L, -3); // t[name] = tag + lua_remove(L, -1); // remove wxLua's registry wxLuaClasses table + // set the metatable functions for the classes for (size_t i_func = 0; i_func < s_funcCount; ++i_func) Index: wxlcallb.cpp =================================================================== RCS file: /cvsroot/wxlua/wxLua/modules/wxlua/src/wxlcallb.cpp,v retrieving revision 1.27 retrieving revision 1.28 diff -C2 -d -r1.27 -r1.28 *** wxlcallb.cpp 19 Mar 2007 03:47:21 -0000 1.27 --- wxlcallb.cpp 23 Mar 2007 00:09:36 -0000 1.28 *************** *** 164,168 **** // and set the metatable of the proxy ! if ((iTag != TLUA_NOTAG) && m_wxlState.tget(iTag)) { if (lua_setmetatable(L, -2) == 0) --- 164,168 ---- // and set the metatable of the proxy ! if (m_wxlState.tget(iTag)) { if (lua_setmetatable(L, -2) == 0) |