LuaBridge的描述性错误

问题描述

我使用LuaJit和LuaBridge编译并执行lua代码。 当lua代码抛出错误时,调试起来真的很困难-我不知道它发生在哪一行或什么原因。

这是一个示例: [string "1"]:0: bad argument #1 to 'info' (string expected,got table)代码中:

"function foo(bar)\n"
        "   logger:info(bar.moo);" 

如何将[string "1"]转换为匹配的代码行? 我可以获得堆栈跟踪信息吗?

在文本代码上执行string.dump,然后用luaL_loadbuffer加载lua代码,然后对其进行“编译”。 伪代码加载并调用lua函数

int result = luaL_loadstring(L,script.c_str());
auto script_function = luabridge::LuaRef::fromStack(L);
auto string_module = luabridge::getGlobal(L,"string");
auto string_dump = string_module["dump"];
auto code = luabridge::LuaRef_cast<std::string>(string_dump(script_function,strip));
luaL_loadbuffer(L,code.data(),code.size(),std::to_string(current_id++).c_str());
lua_pcall(L,LUA_MULTRET,-1);

auto func_to_call = luabridge::getGlobal(L,"foo");
func_to_call(&bar);

解决方法

[string "1"]来自您在此处提供的数据块名称:std::to_string(current_id++).c_str()

由于它不是以@开头,因此被视为已加载的字符串,因此使用[string ""]语法。如果希望错误消息将其视为文件名,请将@放在文件名前面。

我怀疑行号为0的原因是因为您告诉Lua通过将“ strip”设置为true来删除行号。