我使用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"]转换为匹配的代码行?我能得到一个堆栈跟踪吗?
lua代码是通过对文本代码执行string.dump,然后用luaL_loadbuffer加载来“编译”的。加载和调用lua函数的Psuedo代码:
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, 0, LUA_MULTRET, -1);
auto func_to_call = luabridge::getGlobal(L, "foo");
func_to_call(&bar);发布于 2020-08-27 01:18:14
[string "1"]来自您在此处提供的区块名称:std::to_string(current_id++).c_str()
因为它不是以@开头,所以它被视为已加载的字符串,因此使用[string ""]语法。如果希望错误消息将其视为文件名,请将@放在文件名的前面。
我怀疑行号是0的原因是因为您告诉Lua删除行号,将"strip“设置为true。
https://stackoverflow.com/questions/63599864
复制相似问题