stackdump.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #ifndef _STACK_DUMP_H
  2. #define _STACK_DUMP_H
  3. #ifdef DEBUG
  4. #define STACK_DUMP(...) stack_dump (__VA_ARGS__)
  5. static void
  6. stack_dump (lua_State *L, const char *title)
  7. {
  8. // print out the scalar value or type of args on the stack
  9. int i;
  10. int top = lua_gettop(L); /* depth of the stack */
  11. printf("%20s : [", title);
  12. for (i = 1; i <= top; i++) { /* repeat for each level */
  13. int t = lua_type(L, i);
  14. switch (t)
  15. {
  16. case LUA_TNIL:
  17. {
  18. printf("nil");
  19. break;
  20. }
  21. case LUA_TBOOLEAN:
  22. {
  23. printf(lua_toboolean(L, i) ? "true" : "false");
  24. break;
  25. }
  26. case LUA_TLIGHTUSERDATA:
  27. {
  28. printf("lud@%p", lua_touserdata(L, i));
  29. break;
  30. }
  31. case LUA_TNUMBER:
  32. {
  33. printf("%g", lua_tonumber(L, i));
  34. break;
  35. }
  36. case LUA_TSTRING:
  37. {
  38. printf("'%s'", lua_tostring(L, i));
  39. break;
  40. }
  41. case LUA_TTABLE:
  42. {
  43. printf("{}");
  44. break;
  45. }
  46. case LUA_TFUNCTION:
  47. {
  48. printf("f@%p", lua_touserdata(L, i));
  49. break;
  50. }
  51. case LUA_TUSERDATA:
  52. {
  53. printf("ud(%p)", lua_touserdata(L, i));
  54. break;
  55. }
  56. case LUA_TTHREAD:
  57. {
  58. printf("Thrd(%p)", lua_touserdata(L, i));
  59. break;
  60. }
  61. default:
  62. { // other values
  63. printf("%s", lua_typename(L, t));
  64. break;
  65. }
  66. }
  67. if (i<top) printf(" "); /* put a separator */
  68. }
  69. printf("]\n"); /* end the listing */
  70. }
  71. #else
  72. #define STACK_DUMP(...)
  73. #endif // DEBUG
  74. #endif // _STACK_DUMP_H