为什么Lua在这段代码中返回一个空表?

时间:2011-06-19 03:58:04

标签: c++ lua

编辑: 正如我之前评论过的,我的问题是我无法在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一侧的空表。我在这里做错了什么想法?

1 个答案:

答案 0 :(得分:5)

你在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

旧笔记,留下作为fyi:

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

int32 value = lua_tonumber(L, 1);

lua_pop(L, 1);

rgb_color c = ui_color((color_which)value);