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

标签 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 端的一个空表。知道我在这里做错了什么吗?

最佳答案

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

相关文章:

java - Java中有goto语句吗?

c++ - gdk_screen_get_default() 返回 null

c++ - 通过 copy 和 decltype 捕获 Lambda 引用

string - Lua bool 值在 Redis 中没有预期的行为

c - lua中的简单加密

lua - 声明 Lua 函数并使用正确的名称进行引用

oop - 确保表是类的对象?

c++ - 如何在 Visual Basic 中使用矩形函数 OpenGL 绘制矩形

c++ - OpenCV 的 Python 或 C++ 编码之间的性能是否不同?

callback - luajit qsort回调示例内存泄漏