Lua 5.1f: How to dump stack contents from C?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Lua 5.1f: How to dump stack contents from C?

joe rossi
I'm using Lua 5.1 final, and I need a stack dump function.  What I came up with doesn't seem to print accurately, or anything at all.  Does anyone know how I might change this to make it work ? It's very difficult for me to work if I cannot see my Lua error messages:


static void stackDump (lua_State *L) {
      int i=lua_gettop(L);
      printf(" ----------------  Stack Dump ----------------" );
      while(  i   ) {
        int t = lua_type(L, i);
        switch (t) {
          case LUA_TSTRING:
            printf("%d:`%s'", i, lua_tostring(L, i));
          break;
          case LUA_TBOOLEAN:
            printf("%d: %s",i,lua_toboolean(L, i) ? "true" : "false");
          break;
          case LUA_TNUMBER:
            printf("%d: %g",  i, lua_tonumber(L, i));
         break;
         default: printf("%d: %s", i, lua_typename(L, t)); break;
        }
       i--;
      }
     printf("--------------- Stack Dump Finished ---------------" );
}


Relax. Yahoo! Mail virus scanning helps detect nasty viruses!
Reply | Threaded
Open this post in threaded view
|

Re: Lua 5.1f: How to dump stack contents from C?

Achromat
is that what u like ?
 

int CLuaCallBacks::_print(lua_State *lua)//DirectCallFunction
{
lua_pushlightuserdata(lua, pid);
lua_gettable(lua, LUA_REGISTRYINDEX);
CLuaScript *pThis = (CLuaScript *)lua_touserdata(lua, -1);

if(!pThis) return 0;
int n = lua_gettop(lua); //number of arguments
for (int i = 1; i < n; i++)
{
pThis->Print("%s\r\n",lua_tostring(lua, i));
}
return 0;
}

----- Original Message -----
Sent: Saturday, March 11, 2006 11:04 PM
Subject: Lua 5.1f: How to dump stack contents from C?

I'm using Lua 5.1 final, and I need a stack dump function.  What I came up with doesn't seem to print accurately, or anything at all.  Does anyone know how I might change this to make it work ? It's very difficult for me to work if I cannot see my Lua error messages:


static void stackDump (lua_State *L) {
      int i=lua_gettop(L);
      printf(" ----------------  Stack Dump ----------------" );
      while(  i   ) {
        int t = lua_type(L, i);
        switch (t) {
          case LUA_TSTRING:
            printf("%d:`%s'", i, lua_tostring(L, i));
          break;
          case LUA_TBOOLEAN:
            printf("%d: %s",i,lua_toboolean(L, i) ? "true" : "false");
          break;
          case LUA_TNUMBER:
            printf("%d: %g",  i, lua_tonumber(L, i));
         break;
         default: printf("%d: %s", i, lua_typename(L, t)); break;
        }
       i--;
      }
     printf("--------------- Stack Dump Finished ---------------" );
}


Relax. Yahoo! Mail virus scanning helps detect nasty viruses!
Reply | Threaded
Open this post in threaded view
|

Re: Lua 5.1f: How to dump stack contents from C?

David Morris-Oliveros-2
This won't work 100%. Namely, it won't do boolean values (since they
can't be coerced), and also, you modify the stack if there's a number
that's converted to a string.

// David Morris-Oliveros
// Camera & Lua Coder
// [hidden email]



Karsten Schulz wrote:

> is that what u like ?
>  
>
> int CLuaCallBacks::_print(lua_State *lua)//DirectCallFunction
> {
> lua_pushlightuserdata(lua, pid);
> lua_gettable(lua, LUA_REGISTRYINDEX);
> CLuaScript *pThis = (CLuaScript *)lua_touserdata(lua, -1);
>
> if(!pThis) return 0;
> int n = lua_gettop(lua); //number of arguments
> for (int i = 1; i < n; i++)
> {
> pThis->Print("%s\r\n",lua_tostring(lua, i));
> }
> return 0;
> }
>
>     ----- Original Message -----
>     *From:* joe rossi <mailto:[hidden email]>
>     *To:* [hidden email]
>     <mailto:[hidden email]>
>     *Sent:* Saturday, March 11, 2006 11:04 PM
>     *Subject:* Lua 5.1f: How to dump stack contents from C?
>
>     I'm using Lua 5.1 final, and I need a stack dump function.  What I
>     came up with doesn't seem to print accurately, or anything at
>     all.  Does anyone know how I might change this to make it work ?
>     It's very difficult for me to work if I cannot see my Lua error
>     messages:
>
>
>     static void stackDump (lua_State *L) {
>           int i=lua_gettop(L);
>           printf(" ----------------  Stack Dump ----------------" );
>           while(  i   ) {
>             int t = lua_type(L, i);
>             switch (t) {
>               case LUA_TSTRING:
>                 printf("%d:`%s'", i, lua_tostring(L, i));
>               break;
>               case LUA_TBOOLEAN:
>                 printf("%d: %s",i,lua_toboolean(L, i) ? "true" : "false");
>               break;
>               case LUA_TNUMBER:
>                 printf("%d: %g",  i, lua_tonumber(L, i));
>              break;
>              default: printf("%d: %s", i, lua_typename(L, t)); break;
>             }
>            i--;
>           }
>          printf("--------------- Stack Dump Finished ---------------" );
>     }
>
>     ------------------------------------------------------------------------
>     Relax. Yahoo! Mail virus scanning
>     <http://us.rd.yahoo.com/mail_us/taglines/virusall/*http://communications.yahoo.com/features.php?page=221>
>     helps detect nasty viruses!
>