gpt4 book ai didi

c++ - 为什么 Lua 在此代码中返回一个空表?

转载 作者:太空狗 更新时间:2023-10-29 20:47:07 25 4
gpt4 key购买 nike

编辑:正如我之前评论的那样,我的问题是我无法在 C 中创建一个使用字符串作为键的表。我制作了一个快速测试程序来演示我遇到的问题:

这是它的 C++ 部分:

#include <lua.hpp>
#include <String.h>

BString
LuaTypeToString(lua_State *L, int index, int type)
{
BString out;
switch (type)
{
case LUA_TSTRING:
{
out << "'" << lua_tostring(L, index) << "'";
break;
}
case LUA_TBOOLEAN:
{
out << (lua_toboolean(L, index) ? "true" : "false");
break;
}
case LUA_TNUMBER:
{
out << (float)lua_tonumber(L, index);
break;
}
default:
{
out << lua_typename(L, type);
break;
}
}
return out;
}


void
DumpLuaTable(lua_State *L, int tableIndex)
{
lua_pushnil(L);
printf("\t{ ");
while (lua_next(L, tableIndex) != 0)
{
BString keyString = lua_tostring(L, -2);

BString valueString;
int type = lua_type(L, -1);
if (type == LUA_TTABLE)
DumpLuaTable(L, lua_gettop(L));
else
valueString = LuaTypeToString(L, -1, type);

printf("%s=%s,",
keyString.String(),
valueString.String());
lua_pop(L, 1);

if (lua_isnumber(L, -1))
{
lua_pop(L, 1);
break;
}
}
printf(" }");
}


void
DumpLuaStack(lua_State *L)
{
printf("DumpLuaStack:\n");
int top = lua_gettop(L);
for (int i = 1; i <= top; i++)
{
int type = lua_type(L, i);
if (type == LUA_TTABLE)
DumpLuaTable(L, i);
else
printf("\t%s ", LuaTypeToString(L, i, type).String());
}
printf("\n");
}



static
int
ReturnTable(lua_State *L)
{
lua_newtable(L);
lua_pushnumber(L, 3.14);
lua_pushstring(L, "1");
lua_settable(L, -3);

DumpLuaStack(L);

return 1;
}


int
main(void)
{
lua_State *L = luaL_newstate();
luaL_openlibs(L);

lua_pushcfunction(L, ReturnTable);
lua_setglobal(L, "ReturnTable");

int status = luaL_dofile(L, "luatest.lua");
if (status)
printf("Status = %d: %s\n", status, lua_tostring(L, -1));

lua_close(L);

return status;
}

还有它的 Lua 部分:

function DumpTable(table)
print("LuaDumpTable")
local out = "\t"
if #table > 0 then
for i, v in ipairs(table) do
out = out .. i .. "=" .. v .. " "
end
print(out)
else
print("\tEmpty table")
end
end

value = ReturnTable()
if (type(value) == "table") then
DumpTable(value)
else
print(type(value))
end

代码可以正常工作——Lua 脚本打印出您所期望的内容。将将 3.14 插入堆栈的行更改为 lua_pushstring(L, "foo");,我得到的只是 Lua 端的一个空表。知道我在这里做错了什么吗?

最佳答案

你在 Lua 中错误地检查了表格:

if #table > 0 then

#table 只是告诉您表中第一个未使用的整数是 0。当您将字符串插入表中时,它不会增加计数,因为它不是一个连续的数组,你也不能用 ipairs 处理它。只需切换到使用 pairs,您应该会看到 foo:

function DumpTable(table)
print("LuaDumpTable")
local out = "\t"
for i, v in pairs(table) do
out = out .. i .. "=" .. v .. " "
end
print(out)
end

较旧的注释,仅供引用:

我怀疑这会导致问题,但对于从 Lua 调用的 API,您通常希望将输入从堆栈中弹出。所以在 lua_tonumber 调用之后:

int32 value = lua_tonumber(L, 1);

lua_pop(L, 1);

rgb_color c = ui_color((color_which)value);

关于c++ - 为什么 Lua 在此代码中返回一个空表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6400433/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com