4

我的 C 程序可能有一个愚蠢的错误。Lua 堆栈在某些情况下不包含我认为它应该包含的值。

为了调试它,我想在程序的某个点打印 Lua 堆栈的内容。我怎样才能做到这一点而不会弄乱过程中的堆栈?

4

2 回答 2

5

这个答案是@lhf 在评论中提供的答案的略微编辑版本。

它的优点是它不会修改堆栈中的任何值,并且不需要任何额外的空间。

static void dumpstack (lua_State *L) {
  int top=lua_gettop(L);
  for (int 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;
    }
  }
}

如果需要,您还可以lua_isinteger(L, i)在 LUA_TNUMBER 情况下使用,以区分整数和浮点数。

于 2019-11-29T02:00:38.700 回答
1

此代码从上到下遍历堆栈并调用tostring每个值,打印结果(如果未获得结果,则打印类型名称)。

assert(lua_checkstack(L, 3));
int top = lua_gettop(L);
int bottom = 1;
lua_getglobal(L, "tostring");
for(int i = top; i >= bottom; i--)
{
    lua_pushvalue(L, -1);
    lua_pushvalue(L, i);
    lua_pcall(L, 1, 1, 0);
    const char *str = lua_tostring(L, -1);
    if (str) {
        printf("%s\n", str);
    }else{
        printf("%s\n", luaL_typename(L, i));
    }
    lua_pop(L, 1);
}
lua_pop(L, 1);
于 2019-11-28T15:38:49.260 回答