Skip to content

Commit

Permalink
Let app inspect global variables.
Browse files Browse the repository at this point in the history
  • Loading branch information
icculus committed Feb 28, 2007
1 parent 5ef9b1e commit 1ad1ec5
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 14 deletions.
42 changes: 35 additions & 7 deletions src/toby_app.c
Expand Up @@ -1121,6 +1121,37 @@ const TobyDebugInfo *TOBY_getCallstack(int *elementCount)
} /* TOBY_getCallstack */


static const TobyDebugInfo *getGlobals(lua_State *L, int *elementCount)
{
int elements = 0;

lua_pushnil(L); /* initial key value for iteration. */
while (lua_next(L, LUA_GLOBALSINDEX)) /* replaces key, pushes value. */
{
int rc = 1;
const char *name = lua_tostring(L, -2);
const char *val = lua_tostring(L, -1);
if (val == NULL) /* !!! FIXME: code dupe with getVariables */
{
if (lua_isboolean(L, -1))
val = ((lua_toboolean(L, -1)) ? "true" : "false");
else if (!lua_isfunction(L, -1)) /* ignore global functions. */
val = "??? (bug in Toby!)";
} /* if */

if (val != NULL)
rc = addDebugItem(&elements, &varCount, &varList, name, val, -1);
lua_pop(L, 1); /* remove value, keep key for next iteration. */
if (!rc)
return 0;
} /* while */
lua_pop(L, 1); /* pop iterator key */

*elementCount = elements;
return varList;
} /* getGlobals */


const TobyDebugInfo *TOBY_getVariables(int stackframe, int *elementCount)
{
const char *name = NULL;
Expand All @@ -1134,12 +1165,14 @@ const TobyDebugInfo *TOBY_getVariables(int stackframe, int *elementCount)
if (L == NULL) /* if frontend calls this when program isn't running. */
return NULL;

if (stackframe < 0)
return getGlobals(L, elementCount);

if (!lua_getstack(L, stackframe, &ldbg))
return NULL;

for (i = 1; (name = lua_getlocal(L, &ldbg, i)) != NULL; i++)
{
int dopop = 1;
int rc = 1;
if (*name != '(') /* internal Lua variable? */
{
Expand All @@ -1149,18 +1182,13 @@ const TobyDebugInfo *TOBY_getVariables(int stackframe, int *elementCount)
if (lua_isboolean(L, -1))
val = ((lua_toboolean(L, -1)) ? "true" : "false");
else
{
dopop = 0;
val = "??? (bug in Toby!)";
} /* else */
} /* if */

rc = addDebugItem(&elements, &varCount, &varList, name, val, -1);
} /* if */

if (dopop)
lua_pop(L, 1);

lua_pop(L, 1);
if (!rc)
return 0;
} /* for */
Expand Down
17 changes: 10 additions & 7 deletions src/toby_wxwidgets.cpp
Expand Up @@ -1421,10 +1421,7 @@ void TobyIDEFrame::pauseReached(int line, int fullstop,
int frames = 0;
const TobyDebugInfo *cs = TOBY_getCallstack(&frames);
if ((frames <= 0) || (cs == NULL))
{
this->callstackCtrl->Clear();
this->variablesCtrl->Clear();
} // if
else
{
wxString *items = new wxString[frames];
Expand All @@ -1436,11 +1433,11 @@ void TobyIDEFrame::pauseReached(int line, int fullstop,

this->callstackCtrl->Set(frames, items);
delete[] items;

wxCommandEvent evt;
this->callstackCtrl->Select(0);
this->updateVariablesCtrl(0);
} // else

this->callstackCtrl->Append(wxT("(global variables)"));
this->callstackCtrl->Select(0);
this->updateVariablesCtrl(0);
} // if
} // TobyIDEFrame::pauseReached

Expand Down Expand Up @@ -1476,6 +1473,12 @@ void TobyIDEFrame::openFileImpl(char *prog)

void TobyIDEFrame::updateVariablesCtrl(int frame)
{
if (frame >= 0)
{
if ( ((unsigned int) frame) == (this->callstackCtrl->GetCount()-1) )
frame = -1; // Requesting global variables, not a stack frame.
} // if

int varCount = 0;
const TobyDebugInfo *vars = TOBY_getVariables(frame, &varCount);
if ((varCount <= 0) || (vars == NULL))
Expand Down

0 comments on commit 1ad1ec5

Please sign in to comment.