【发布时间】:2021-05-31 18:08:45
【问题描述】:
我正在使用 C++ 中的 lua,我想找出 lua 堆栈中使用了多少“槽”(你可以说),如果可能,lua 堆栈的大小是多少?
【问题讨论】:
-
您可以使用 lua_checkstack 来测试您是否可以推送新实体
我正在使用 C++ 中的 lua,我想找出 lua 堆栈中使用了多少“槽”(你可以说),如果可能,lua 堆栈的大小是多少?
【问题讨论】:
lua_gettop(lua_State* L)
堆栈中的元素个数与顶部槽的索引相同。如果您对此感兴趣,可以使用此信息为您打印整个堆栈。
int top = lua_gettop(L);
std::string str = "From top to bottom, the lua stack is \n";
for (unsigned index = top; index > 0; index--)
{
int type = lua_type(L, index);
switch (type)
{
// booleans
case LUA_TBOOLEAN:
str = str + (lua_toboolean(L, index) ? "true" : "false") + "\n";
break;
// numbers
case LUA_TNUMBER:
str = str + std::to_string(lua_tonumber(L, index)) + "\n";
break;
// strings
case LUA_TSTRING:
str = str + lua_tostring(L, index) + "\n";
break;
// other
default:
str = str + lua_typename(L, type) + "\n";
break;
}
}
str = str + "\n";
std::cout << str;
【讨论】: