lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


It was thus said that the Great Luiz Henrique de Figueiredo once stated:
> >  static void stackprint(lua_State *L, int from) {
> >    int top=lua_gettop(L);
> >    printf("Stack:");
> >    while (from<=top) {
> >      printf(" %s",luaL_tolstring(L,from++,NULL));
> >      lua_pop(L,1); }
> >    printf("\n");
> > }
> 
> Using luaL_tolstring will change the type of number values to string.
> 
> I use this:
> 
> static void dumpstack (lua_State *L, const char *message) {
>   int top=lua_gettop(L);
>   int i;
>   printf("dumpstack -- %s\n",message);
>   for (i=1; i<=top; i++) {
>     printf("%d\t%s\t",i,luaL_typename(L,i));
>     switch (lua_type(L, i)) {
>       case LUA_TNUMBER:
>         printf("%g\n",lua_tonumber(L,i));
>         break;
>       case LUA_TSTRING:
>         printf("%s\n",lua_tostring(L,i));
>         break;
>       case LUA_TBOOLEAN:
>         printf("%s\n", (lua_toboolean(L, i) ? "true" : "false"));
>         break;
>       case LUA_TNIL:
>         printf("%s\n", "nil");
>         break;
>       default:
>         printf("%p\n",lua_topointer(L,i));
>         break;
>     }
>   }
>   printf("dumpstack -- END\n");
> }

  Since this seems to be the thing now, what I use is:

void lluaL_luastack(lua_State *L)
{
  int max;
  int i;

  printf("LUA STACK DUMP");
  max = lua_gettop(L);

  for (i = 1 ; i <= max ; i++)
  {
    char const *name;
    char const *type;
    int         ri;

    lua_getglobal(L,"tostring");
    lua_pushvalue(L,i);
    lua_call(L,1,1);

    name = lua_tostring(L,-1);
    type = luaL_typename(L,i);
    ri   = i - max - 1;

    printf("Stack: %d %d - %s %s",i,ri,type,name);
    lua_pop(L,1);
  }
}

  This won't destroy any of the values on the stack, and it calls any
user-supplied "__tostring" method.  Yes, it relies upon the Lua function
tostring() to be in the global namespace, but that isn't an issue with my
code.  I also print both the positive and negative stack indecies, which I
find useful.

  -spc