0

I use LuaJit and LuaBridge to compile lua code and execute it. When the lua code throws an error, it's really hard to debug - I don't know what line it happened or what caused it.

This is an example: [string "1"]:0: bad argument #1 to 'info' (string expected, got table) in the code:

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

How do I translate [string "1"] to the matching code line? Can I get a stacktrace?

The lua code is "compiled" with doing string.dump on the text code, and then loading it with luaL_loadbuffer. Psuedo code that loads and calls the lua function:

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);

1 Answers1

1

[string "1"] comes from the chunk name you provided here: std::to_string(current_id++).c_str()

Since it doesn't start with @ it's treated as the string that was loaded, hence the [string ""] syntax. If you want the error message to treat it as a filename, put @ in front of the filename.

And I suspect that the reason the line number is 0 because you told Lua to delete the line numbers, by setting "strip" to true.

user253751
  • 57,427
  • 7
  • 48
  • 90