From: John L. <jr...@us...> - 2005-12-01 06:44:05
|
Update of /cvsroot/wxlua/wxLua/modules/wxluadebug/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv24710/wxLua/modules/wxluadebug/src Modified Files: wxldebug.cpp Log Message: Make wxLuaDebugData have functions to BuildStack, BuildStackList, BuildTableList These replace the same functions in wxLuaInterface Replaces EnumerateStack, EnumerateStackEntry, EnumerateTable in wxLuaTarget and wxLuaDebugee moved helper functions MakeNumber, MakeBoolean, GetTableInfo, GetUserDataInfo as well Index: wxldebug.cpp =================================================================== RCS file: /cvsroot/wxlua/wxLua/modules/wxluadebug/src/wxldebug.cpp,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** wxldebug.cpp 1 Dec 2005 04:17:29 -0000 1.6 --- wxldebug.cpp 1 Dec 2005 06:43:53 -0000 1.7 *************** *** 177,219 **** } ! // ---------------------------------------------------------------------------- ! // wxLuaInterface (local) ! // ---------------------------------------------------------------------------- ! ! wxLuaInterface::wxLuaInterface(const wxLuaState& wxlState) : m_wxlState(wxlState) ! { ! } ! ! wxLuaInterface::~wxLuaInterface() ! { ! size_t idx, idxMax = m_references.GetCount(); ! for (idx = 0; idx < idxMax; ++idx) ! { ! int iItem = m_references.Item(idx); ! m_wxlState.tremove(iItem); ! } ! } ! ! int wxLuaInterface::Ref() ! { ! int nReference = m_wxlState.tinsert(-1); ! m_references.Add(nReference); ! return nReference; ! } ! ! wxLuaDebugData *wxLuaInterface::BuildStack() { ! wxLuaDebugData *pSortedList = new wxLuaDebugData(); ! int nIndex = 0; lua_Debug luaDebug; ! bool fFirst = true; ! ! while (m_wxlState.GetStack(nIndex, &luaDebug) != 0) { ! if (m_wxlState.GetInfo("Sln", &luaDebug) != 0) { ! wxString itemName; wxString source = lua2wx(luaDebug.source); --- 177,196 ---- } ! int wxLuaDebugData::BuildStack(const wxLuaState& wxlState_) { ! wxLuaState wxlState(wxlState_); // unconst the state ! int nIndex = 0; ! bool fFirst = true; ! int count = 0; lua_Debug luaDebug; + lua_State* L = wxlState.GetLuaState(); ! while (lua_getstack(L, nIndex, &luaDebug) != 0) { ! if (lua_getinfo(L, "Sln", &luaDebug)) { ! wxString stackItem; ! wxString name = lua2wx(luaDebug.name); wxString source = lua2wx(luaDebug.source); *************** *** 226,235 **** if (luaDebug.name != NULL) ! itemName.Printf(_("function %s line %u"), luaDebug.name, currentLine); else ! itemName.Printf(_("line %u"), currentLine); ! wxLuaDebugDataItem *item = new wxLuaDebugDataItem(itemName, wxT(""), wxT(""), source, nIndex, 0); ! pSortedList->Add(item); fFirst = false; --- 203,217 ---- if (luaDebug.name != NULL) ! stackItem = wxString::Format(_("function %s line %u"), name.c_str(), currentLine); else ! stackItem = wxString::Format(_("line %u"), currentLine); ! wxLuaDebugDataItem *item = new wxLuaDebugDataItem(stackItem, wxT(""), wxT(""), source, nIndex, 0); ! ! if (item != NULL) ! { ! Add(item); ! count++; ! } fFirst = false; *************** *** 239,263 **** ++nIndex; } ! return pSortedList; } ! wxLuaDebugData *wxLuaInterface::BuildStackList(int nEntry) { ! wxLuaDebugData *pSortedList = new wxLuaDebugData(); ! if (pSortedList != NULL) { ! lua_Debug luaDebug; ! if (m_wxlState.GetStack(nEntry, &luaDebug) != 0) { ! int idx = 1; ! wxString name = m_wxlState.GetLocalwxString(&luaDebug, idx); ! while (!name.IsEmpty()) { bool fIsTable = false; wxString type; wxString value; ! wxString source = lua2wx(luaDebug.source); ! switch (m_wxlState.Type(-1)) { case LUA_TNIL: --- 221,406 ---- ++nIndex; } ! ! return count; } ! int wxLuaDebugData::BuildStackList(const wxLuaState& wxlState_, int stackRef, wxArrayInt& references) { ! wxLuaState wxlState(wxlState_); // unconst the state ! ! int count = 0; ! lua_Debug luaDebug; ! lua_State* L = wxlState.GetLuaState(); ! ! if (lua_getstack(L, stackRef, &luaDebug) != 0) { ! int idx = 1; ! wxString name = lua2wx(lua_getlocal(L, &luaDebug, idx)); ! while (!name.IsEmpty()) { ! bool fIsTable = false; ! wxString type; ! wxString value; ! wxString source = lua2wx(luaDebug.source); ! ! switch (lua_type(L, -1)) ! { ! case LUA_TNIL: ! type = wxT("Nil"); ! value = wxT("nil"); ! break; ! ! case LUA_TBOOLEAN: ! type = wxT("Boolean"); ! value = MakeBoolean(lua_toboolean(L, -1)); ! break; ! ! case LUA_TNUMBER: ! type = wxT("Number"); ! value = MakeNumber(lua_tonumber(L, -1)); ! break; ! ! case LUA_TSTRING: ! type = wxT("String"); ! value = lua2wx(lua_tostring(L, -1)); ! break; ! ! case LUA_TTABLE: ! type = wxT("Table"); ! value = GetTableInfo(wxlState, -1); ! fIsTable = true; ! break; ! ! case LUA_TFUNCTION: ! if (lua_iscfunction(L, -1)) ! { ! type = wxT("C Function"); ! value.Printf(wxT("%p"), lua_tocfunction(L, -1)); ! } ! else ! { ! type = wxT("Lua Function"); ! value.Printf(wxT("%p"), lua_topointer(L, -1)); ! } ! break; ! ! case LUA_TUSERDATA: ! type = wxT("User Data"); ! value = GetUserDataInfo(wxlState, -1); ! break; ! ! case LUA_TLIGHTUSERDATA: ! type = wxT("Light User Data"); ! value = GetUserDataInfo(wxlState, -1, false); ! break; ! ! case LUA_TTHREAD: ! type = wxT("Thread"); ! value.Printf(wxT("%p"), lua_topointer(L, -1)); ! break; ! } ! ! int nRef = LUA_NOREF; ! ! if (lua_istable(L, -1)) // FIXME or fIsTable ! { ! lua_pushvalue(L, -1); ! nRef = wxlState.tinsert(-1); ! references.Add(nRef); ! } ! ! wxLuaDebugDataItem *item = new wxLuaDebugDataItem(name, type, value, source, nRef, 0); ! if (item != NULL) ! { ! Add(item); ! count++; ! } ! ! name = lua2wx(lua_getlocal(L, &luaDebug, ++idx)); ! } ! } ! ! return count; ! } ! ! int wxLuaDebugData::BuildTableList(const wxLuaState& wxlState_, int tableRef, int nIndex, wxArrayInt& references) ! { ! wxLuaState wxlState(wxlState_); // unconst the state ! lua_State* L = wxlState.GetLuaState(); ! int count = 0; ! ! if (tableRef == -1) ! { ! lua_pushvalue(L, LUA_GLOBALSINDEX); ! int nRef = wxlState.tinsert(-1); ! Add(new wxLuaDebugDataItem(wxT("Globals"), ! wxT(""), ! wxT(""), ! wxT(""), ! nRef, ! 0)); ! references.Add(nRef); ! } ! else ! { ! if (wxlState.tget(tableRef) != 0) ! { ! int nTop = lua_gettop(L); ! ! // start iterating ! lua_pushnil(L); ! while (lua_next(L, nTop) != 0) { bool fIsTable = false; wxString type; wxString value; ! wxString name; ! wxString source; ! // get the index ! switch (lua_type(L, -2)) ! { ! case LUA_TNIL: ! name = wxT("Nil"); ! break; ! ! case LUA_TBOOLEAN: ! name = MakeBoolean(lua_toboolean(L, -2)); ! break; ! ! case LUA_TNUMBER: ! name = MakeNumber(lua_tonumber(L, -2)); ! break; ! ! case LUA_TSTRING: ! name = lua2wx(lua_tostring(L, -2)); ! break; ! ! case LUA_TTABLE: ! name = GetTableInfo(wxlState, -2); ! break; ! ! case LUA_TFUNCTION: ! if (lua_iscfunction(L, -2)) ! name.Printf(wxT("%p"), lua_tocfunction(L, -2)); ! else ! name.Printf(wxT("%p"), lua_topointer(L, -2)); ! break; ! ! case LUA_TUSERDATA: ! name = GetUserDataInfo(wxlState, -2); ! break; ! ! case LUA_TLIGHTUSERDATA: ! name = GetUserDataInfo(wxlState, -2, false); ! break; ! ! case LUA_TTHREAD: ! name.Printf(wxT("%p"), lua_topointer(L, -2)); ! break; ! } ! ! // get the value ! switch (lua_type(L, -1)) { case LUA_TNIL: *************** *** 267,300 **** case LUA_TBOOLEAN: type = wxT("Boolean"); - value = MakeBoolean(m_wxlState.ToBoolean(-1)); break; case LUA_TNUMBER: type = wxT("Number"); - value = MakeNumber(m_wxlState.ToNumber(-1)); break; case LUA_TSTRING: type = wxT("String"); ! value = m_wxlState.TowxString(-1); break; case LUA_TTABLE: type = wxT("Table"); ! value = GetTableInfo(-1); fIsTable = true; break; case LUA_TFUNCTION: ! if (m_wxlState.IsCFunction(-1)) { type = wxT("C Function"); ! value.Printf(wxT("%p"), m_wxlState.ToCFunction(-1)); } else { type = wxT("Lua Function"); ! value.Printf(wxT("%p"), m_wxlState.ToPointer(-1)); } break; --- 410,443 ---- case LUA_TBOOLEAN: + value = MakeBoolean(lua_toboolean(L, -1)); type = wxT("Boolean"); break; case LUA_TNUMBER: + value = MakeNumber(lua_tonumber(L, -1)); type = wxT("Number"); break; case LUA_TSTRING: type = wxT("String"); ! value = lua2wx(lua_tostring(L, -1)); break; case LUA_TTABLE: type = wxT("Table"); ! value = GetTableInfo(wxlState, -1); fIsTable = true; break; case LUA_TFUNCTION: ! if (lua_iscfunction(L, -1)) { type = wxT("C Function"); ! value.Printf(wxT("%p"), lua_tocfunction(L, -1)); } else { type = wxT("Lua Function"); ! value.Printf(wxT("%p"), lua_topointer(L, -1)); } break; *************** *** 302,530 **** case LUA_TUSERDATA: type = wxT("User Data"); ! value = GetUserDataInfo(-1); break; case LUA_TLIGHTUSERDATA: type = wxT("Light User Data"); ! value = GetUserDataInfo(-1, false); break; case LUA_TTHREAD: ! type = wxT("Thread"); ! value.Printf(wxT("%p"), m_wxlState.ToPointer(-1)); break; } int nRef = LUA_NOREF; - if (fIsTable) { ! nRef = Ref(); } else ! m_wxlState.Pop(1); ! ! wxLuaDebugDataItem *item = new wxLuaDebugDataItem(name, type, value, source, nRef, 0); ! pSortedList->Add(item); ! name = m_wxlState.GetLocalwxString(&luaDebug, ++idx); } } } ! return pSortedList; } ! wxLuaDebugData *wxLuaInterface::BuildTableList(int nRef, int nIndex) { ! wxLuaDebugData *pSortedList = new wxLuaDebugData(); ! if ((pSortedList != NULL) && (m_wxlState.tget(nRef) != 0)) { ! int nTop = m_wxlState.GetTop(); ! ! // start iterating ! m_wxlState.PushNil(); ! while (m_wxlState.Next(nTop) != 0) ! { ! bool fIsTable = false; ! wxString type; ! wxString value; ! wxString name; ! wxString source; ! ! // get the index ! switch (m_wxlState.Type(-2)) ! { ! case LUA_TNIL: ! name = wxT("Nil"); ! break; ! ! case LUA_TBOOLEAN: ! name = MakeBoolean(m_wxlState.ToBoolean(-2)); ! break; ! ! case LUA_TNUMBER: ! name = MakeNumber(m_wxlState.ToNumber(-2)); ! break; ! ! case LUA_TSTRING: ! name = m_wxlState.TowxString(-2); ! break; ! case LUA_TTABLE: ! name = GetTableInfo(-2); ! break; ! case LUA_TFUNCTION: ! if (m_wxlState.IsCFunction(-2)) ! name.Printf(wxT("%p"), m_wxlState.ToCFunction(-2)); ! else ! name.Printf(wxT("%p"), m_wxlState.ToPointer(-2)); ! break; ! case LUA_TUSERDATA: ! name = GetUserDataInfo(-2); ! break; ! case LUA_TLIGHTUSERDATA: ! name = GetUserDataInfo(-2, false); ! break; ! } ! // get the value ! switch (m_wxlState.Type(-1)) ! { ! case LUA_TNIL: ! type = wxT("Nil"); ! value = wxT("nil"); ! break; ! case LUA_TBOOLEAN: ! value = MakeBoolean(m_wxlState.ToBoolean(-1)); ! type = wxT("Boolean"); ! break; ! case LUA_TNUMBER: ! value = MakeNumber(m_wxlState.ToNumber(-1)); ! type = wxT("Number"); ! break; ! case LUA_TSTRING: ! type = wxT("String"); ! value = m_wxlState.TowxString(-1); ! break; ! case LUA_TTABLE: ! type = wxT("Table"); ! value = GetTableInfo(-1); ! fIsTable = true; ! break; ! case LUA_TFUNCTION: ! if (m_wxlState.IsCFunction(-1)) ! { ! type = wxT("C Function"); ! value.Printf(wxT("%p"), m_wxlState.ToCFunction(-1)); ! } ! else ! { ! type = wxT("Lua Function"); ! value.Printf(wxT("%p"), m_wxlState.ToPointer(-1)); ! } ! break; ! case LUA_TUSERDATA: ! type = wxT("User Data"); ! value = GetUserDataInfo(-1); ! break; ! case LUA_TLIGHTUSERDATA: ! type = wxT("Light User Data"); ! value = GetUserDataInfo(-1, false); ! break; - case LUA_TTHREAD: - type = wxT("Thread"); - value.Printf(wxT("%p"), m_wxlState.ToPointer(-1)); - break; - } ! int nRef = LUA_NOREF; ! if (fIsTable) ! nRef = Ref(); ! else ! m_wxlState.Pop(1); ! wxLuaDebugDataItem *item = new wxLuaDebugDataItem(name, type, value, source, nRef, nIndex); ! pSortedList->Add(item); ! } ! // remove reference ! m_wxlState.Pop(1); } - return pSortedList; } ! wxString wxLuaInterface::GetTableInfo(int index) { ! wxString tableInfo; ! ! int nItems = m_wxlState.GetN(index); ! const void *pItem = m_wxlState.ToPointer(index); ! ! if (nItems == 0) ! tableInfo.Printf(wxT("%p"), pItem); ! else ! tableInfo.Printf(wxT("%p approx %u items)"), pItem, nItems); ! ! return tableInfo; } ! wxString wxLuaInterface::GetUserDataInfo(int index, bool full) { ! wxString userdatainfo; ! ! if (full) ! { ! wxString pTagName; ! int nTag = m_wxlState.ttag(index); ! ! if (nTag != TLUA_NOTAG) ! pTagName = lua2wx(m_wxlState.GetLuaTagName(nTag)); ! ! if (pTagName != wxEmptyString) ! userdatainfo.Printf(wxT("%p (%s)"), m_wxlState.ToUserData(index), pTagName.c_str()); ! else ! userdatainfo.Printf(wxT("%p (%u)"), m_wxlState.ToUserData(index), nTag); ! } ! else ! userdatainfo.Printf(wxT("%p"), m_wxlState.ToUserData(index)); ! return userdatainfo; } ! wxString wxLuaInterface::MakeNumber(double dnum) { ! wxString number; ! ! long num = (long) dnum; ! if ((double) num == dnum) ! { ! if (num >= 0) ! number.Printf(wxT("%lu (0x%lx)"), num, num); ! else ! number.Printf(wxT("%ld (0x%lx)"), num, num); ! } ! else ! number.Printf(wxT("%g"), dnum); ! return number; } ! wxString wxLuaInterface::MakeBoolean(int num) { ! if (num) ! return wxT("True"); ! return wxT("False"); } --- 445,594 ---- case LUA_TUSERDATA: type = wxT("User Data"); ! value = GetUserDataInfo(wxlState, -1); break; case LUA_TLIGHTUSERDATA: type = wxT("Light User Data"); ! value = GetUserDataInfo(wxlState, -1, false); break; case LUA_TTHREAD: ! type = wxT("Thread"); ! value.Printf(wxT("%p"), lua_topointer(L, -1)); break; } int nRef = LUA_NOREF; if (fIsTable) { ! nRef = wxlState.tinsert(-1); ! references.Add(nRef); } else ! lua_pop(L, 1); ! wxLuaDebugDataItem *item = new wxLuaDebugDataItem(name, type, value, source, nRef, nIndex); ! if (item != NULL) ! { ! Add(item); ! count++; ! } } + + // remove reference + lua_pop(L, 1); } } ! return count; } ! wxString wxLuaDebugData::MakeNumber(double dnum) { ! long num = (long) dnum; ! if ((double) num == dnum) { ! if (num >= 0) ! return wxString::Format(wxT("%lu (0x%lx)"), num, num); ! return wxString::Format(wxT("%ld (0x%lx)"), num, num); ! } ! return wxString::Format(wxT("%g"), dnum); ! } ! wxString wxLuaDebugData::MakeBoolean(int num) ! { ! return num != 0 ? wxT("True") : wxT("False"); ! } ! wxString wxLuaDebugData::GetTableInfo(const wxLuaState& wxlState, int index) ! { ! lua_State* L = wxlState.GetLuaState(); ! int nItems = luaL_getn(L, index); ! const void *pItem = lua_topointer(L, index); ! if (nItems == 0) ! return wxString::Format(wxT("%p"), pItem); ! return wxString::Format(wxT("%p (approx %u items)"), pItem, nItems); ! } ! wxString wxLuaDebugData::GetUserDataInfo(const wxLuaState& wxlState_, int index, bool full) ! { ! wxLuaState wxlState(wxlState_); // unconst wxLuaState ! lua_State* L = wxlState.GetLuaState(); ! if (full) ! { ! int nTag = wxlState.ttag(index); ! const char *pTagName = NULL; ! if (nTag != TLUA_NOTAG) ! pTagName = wxlState.GetLuaTagName(nTag); ! if (pTagName != NULL) ! return wxString::Format(wxT("%p (%s)"), lua_touserdata(L, index), pTagName); ! return wxString::Format(wxT("%p (%u)"), lua_touserdata(L, index), nTag); ! } ! return wxString::Format(wxT("%p"), lua_touserdata(L, index)); ! } ! // ---------------------------------------------------------------------------- ! // wxLuaInterface (local) ! // ---------------------------------------------------------------------------- ! wxLuaInterface::wxLuaInterface(const wxLuaState& wxlState) : m_wxlState(wxlState) ! { ! } ! wxLuaInterface::~wxLuaInterface() ! { ! size_t idx, idxMax = m_references.GetCount(); ! for (idx = 0; idx < idxMax; ++idx) ! { ! int iItem = m_references.Item(idx); ! m_wxlState.tremove(iItem); } } ! int wxLuaInterface::Ref() { ! int nReference = m_wxlState.tinsert(-1); ! m_references.Add(nReference); ! return nReference; } ! wxLuaDebugData *wxLuaInterface::BuildStack() { ! wxLuaDebugData *pSortedList = new wxLuaDebugData(); ! if (pSortedList == NULL) ! return NULL; ! pSortedList->BuildStack(m_wxlState); ! return pSortedList; } ! wxLuaDebugData *wxLuaInterface::BuildStackList(int nEntry) { ! wxLuaDebugData *pSortedList = new wxLuaDebugData(); ! if (pSortedList == NULL) ! return NULL; ! pSortedList->BuildStackList(m_wxlState, nEntry, m_references); ! return pSortedList; } ! wxLuaDebugData *wxLuaInterface::BuildTableList(int nRef, int nIndex) { ! wxLuaDebugData *pSortedList = new wxLuaDebugData(); ! if (pSortedList == NULL) ! return NULL; ! ! pSortedList->BuildTableList(m_wxlState, nRef, nIndex, m_references); ! return pSortedList; } |