Lua stack dump for c++

1,163 views
Skip to first unread message

sagasw

unread,
Aug 7, 2009, 12:29:16 AM8/7/09
to lu...@googlegroups.com

Any interactions between c++ and lua are going through lua stack. Aware of the stack status may help for debugging. I always do that, will a peek of the current Lua stack not only helps me debug my codes but also helps me figure out the ways how I can pass table from c++ to Lua and vice versa.

  1. void stackdump_g(lua_State* l)  
  2. {  
  3.     int i;  
  4.     int top = lua_gettop(l);  
  5.   
  6.     printf("total in stack %d\n",top);  
  7.   
  8.     for (i = 1; i <= top; i++)  
  9.     {  /* repeat for each level */  
  10.         int t = lua_type(l, i);  
  11.         switch (t) {  
  12.             case LUA_TSTRING:  /* strings */  
  13.                 printf("string: '%s'\n", lua_tostring(l, i));  
  14.                 break;  
  15.             case LUA_TBOOLEAN:  /* booleans */  
  16.                 printf("boolean %s\n",lua_toboolean(l, i) ? "true" : "false");  
  17.                 break;  
  18.             case LUA_TNUMBER:  /* numbers */  
  19.                 printf("number: %g\n", lua_tonumber(l, i));  
  20.                 break;  
  21.             default:  /* other values */  
  22.                 printf("%s\n", lua_typename(l, t));  
  23.                 break;  
  24.         }  
  25.         printf("  ");  /* put a separator */  
  26.     }  
  27.     printf("\n");  /* end the listing */  
  28. }  

I usually interested on knowing how many blocks in my stack had been occupied and also each block’s variable type, if they are string, number or bool, I would like to know the value as well.

The usage is simple, you will only needs to pass in your current Lua State pointer.

The print line output may look like this:


total in stack 2
string: '5A5B5C8855778899'
number: 89
Related Posts
Accessing Lua global variables from c++
Calling Lua scripts from c++’s example was written in post How to embed Lua 5.1 in C++. Now, let us look at how to acces…

Calling Lua function from C++

Calling Lua function from c++ is very simple. Value passing between c++ and Lua goes through stack, Lua C API provides a…

calling c++ function from Lua, implement sleep function

You can’t call c function directly from Lua, you have to create a wrapper function that allows calling from Lua. In this…

sagasw

unread,
Aug 7, 2009, 12:29:54 AM8/7/09
to lu...@googlegroups.com

Calling Lua function from c++ is very simple. Value passing between c++ and Lua goes through stack, Lua C API provides a convenience ways for you to call Lua function from C. To call Lua function, you need to specify:

1. Function Name.
2. Parameters of function call.
3. Return values expected ( Lua function support multiple results reture)

Let say my lua function name call f in last.lua, takes 2 parameters.

-- last.lua
function f (x, y)
return (x^2 * math.sin(y))/(1 - x)
end

I perform function call from c++ like this:

  1. //last.cc  
  2. # extern "C" {    
  3. # #include "lua.h"    
  4. # #include "lualib.h"    
  5. # #include "lauxlib.h"    
  6. # }    
  7.   
  8. int main()  
  9. {  
  10.     double z;  
  11.     lua_State *L = lua_open();  
  12.     luaL_openlibs(L);  
  13.     if (luaL_loadfile(L, "last.lua") || lua_pcall(L, 0, 0, 0)) {  
  14.         printf("error: %s", lua_tostring(L, -1));  
  15.         return -1;  
  16.     }  
  17.   
  18.     lua_getglobal(L, "f");  
  19.     if(!lua_isfunction(L,-1))  
  20.     {  
  21.         lua_pop(L,1);  
  22.         return -1;  
  23.     }  
  24.     lua_pushnumber(L, 21);   /* push 1st argument */  
  25.     lua_pushnumber(L, 31);   /* push 2nd argument */  
  26.   
  27.     /* do the call (2 arguments, 1 result) */  
  28.     if (lua_pcall(L, 2, 1, 0) != 0) {  
  29.         printf("error running function `f': %s\n",lua_tostring(L, -1));  
  30.         return -1;  
  31.     }  
  32.   
  33.     /* retrieve result */  
  34.     if (!lua_isnumber(L, -1)) {  
  35.         printf("function `f' must return a number\n");  
  36.         return -1;  
  37.     }  
  38.     z = lua_tonumber(L, -1);  
  39.     printf("Result: %f\n",z);  
  40.     lua_pop(L, 1);  
  41.     lua_close(L);  
  42.   
  43.     return 0;  
  44. }  

Compile it with g++ like this:

g++ -o last{,.cc} -llua -ldl

The results:

Result: -12.158958

Brief explanation of the C++ codes above:
First, I trigger lua_getglobal to get the function name, then I push 2 parameters to stack. I make lua_pcall by telling Lua I have 2 params and expect 1 value return. Upon success, I retrieve the return value from the top of the stack.

Related Posts

calling c++ function from Lua, implement sleep function
You can’t call c function directly from Lua, you have to create a wrapper function that allows calling from Lua. In this…
Calling Lua scripts from c++’s example was written in post How to embed Lua 5.1 in C++. Now, let us look at how to acces…

if (true condition) runs here

The simplest stuff to learn about c and c++ programming is ” if “. Not only c/c++, almost all programming language have …
Trackback URL
Reply all
Reply to author
Forward
0 new messages