如何使用重定义的 print() 函数打印 Lua 表?

我从这篇帖子中学到了如何在 C++ 中重新定义 Lua 的 print()。( https://stackoverflow.com/a/4514193/5224286)

以下是重新定义的打印函数,它会将变量打印到我的主机程序的控制台中。 (通过名为 post.. 的函数)

int l_my_print(lua_State *L)
{
    int nargs = lua_gettop(L);
    for (int i = 1; i <= nargs; ++i)
    {
        if (lua_isnil(L, i))
            poststring("nil");
        else if (lua_isboolean(L, i))
            lua_toboolean(L, i) ? poststring("true") : poststring("false");
        else if (lua_isnumber(L, i))
            postfloat(static_cast<t_float>(lua_tonumber(L, i)));
        else if (lua_isstring(L, i))
            poststring(lua_tostring(L, i));
        else if (lua_istable(L, i))
            poststring("table: "); // 怎样像 Lua 内置的 print() 一样打印呢?
    }
    endpost();
    return 0;
}

除了打印表时出现问题(现在只打印 table:),这段代码还是正常工作的。

我想知道如何像 Lua 的 print() 函数一样打印表。

例如,当我在 Lua 中运行以下代码时(重新定义之前):

print({1,2,3});

我得到的结果是:(似乎不断变化)

table: 0x23b8660

这是指向 Lua 表指针的十六进制表示吗?

我应该如何修改 l_my_print() 函数,使其可以像 Lua 的 print() 一样工作呢?

点赞
用户1944004
用户1944004

只需使用luaL_tolstring获取任何东西的字符串表示即可。 这也尊重__tostring元方法。 下面的示例使用C ++ 17的std :: string_view,用于零复制只读字符串参数。

#include <iostream>
#include <string_view>

#include <lua.hpp>

void poststring(std::string_view sv) { std::cout << sv << '\n'; }

void endpost() { std::cout << "---\n"; }

int l_my_print(lua_State *L) {
    int nargs = lua_gettop(L);
    for (int i = 1; i <= nargs; ++i) {
        poststring(luaL_tolstring(L, i, nullptr));
        lua_pop(L, 1); // remove the string
    }
    endpost();
    return 0;
}

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

    lua_pushcfunction(L, l_my_print);
    lua_setglobal(L, "my_print");

    int i = 0;
    lua_pushlightuserdata(L, &i);
    lua_setglobal(L, "udata");

    luaL_dostring(L, "my_print(1, 3.14, \"Hello World\")\n"
                     "my_print(false, udata, {})\n");

    lua_close(L);
}

示例调用:

$ clang++ -Wall -Wextra -Wpedantic -std=c++17 -I/usr/include/lua5.3 test.cpp -llua5.3
$ ./a.out
1
3.14
Hello World
---
false
userdata: 0x7fff4685993c
table: 0x883300
---

2018-09-02 03:08:57
用户9963147
用户9963147
-- 该函数用于将一个 Lua 表转换成一种简洁、清晰的表示形式。递归调用。
-- 来源:http://lua-users.org/wiki/TableUtils
function table.tostring(tbl)
    local result, done = {}, {}
    for k, v in ipairs(tbl) do
        table.insert(result, table.val_to_str(v))
        done[k] = true
    end
    for k, v in pairs(tbl) do
        if not done[k] then
            table.insert(result, table.key_to_str(k) .. "=" .. table.val_to_str(v))
        end
    end
    return "{" .. table.concat(result, ",") .. "}"
end
2018-09-05 14:45:58