Is it possible to get all the errors in the lua stack from C/C++? here is what I've tried
c++
int main()
{
lua_State* L = luaL_newstate();
luaL_openlibs(L);
if (luaL_loadfile(L, "LuaBridgeScript.lua"))
{
throw std::runtime_error("Unable to find lua file");
}
int error = lua_pcall(L, 0, 0, 0);
while (error && lua_gettop(L))
{
std::cout << "stack = " << lua_gettop(L) << "\n";
std::cout << "error = " << error << "\n";
std::cout << "message = " << lua_tostring(L, -1) << "\n";
lua_pop(L, 1);
error = lua_pcall(L, 0, 0, 0);
}
}
lua:
printMessage("hi")
printMessage2("hi2")
output:
stack = 1
error = 2
message = LuaBridgeScript.lua:1: attempt to call global 'printMessage' <a nil value>
I've also tried looping even if the stack size is 0 or negative, but I don't understand how the stack could be negative, and the program crashes after a few attempts.
To wrap up my comments into an answer:
According to Lua docs on lua_pcall
, pcall returns on either success (end of chunk) or first error thrown. So in the latter case, it will push only one message to the stack. It will never continue execution after the first error.
What you're trying to achieve is checking for possible errors in the file. In statically typed languages like C, every variable has to be defined at compile time, so the compiler can spot instances of calling non existing function.
Lua, however, is a dynamically typed language, in which variables have no types but rather are placeholders for values (which do have types). This means, that Lua cannot tell in advance whether printMessage
is a function, a string, a value or if it doesn't exist (nil). It is only at run time when the variable is about to be called that Lua checks its type.
It is therefore not possible to achieve what you want that way. Running the code beyond the first unhandled error is pointless as well, as the error may render assumptions in subsequent fragments invalid (for instance about global variables that the non existing function should have set) - it would be a mess.
As to syntax errors, these are generally caught when compiling the source file, that is while loading. However, Lua parser stops at first encountered syntax error. This is due to the fact that many times syntax errors in one place invalidate everything that follows it. As Etan pointed out in his comment, many parsers report subsequent errors that disappear or change once you've fixed errors that precede them. It is also true for even heavy weight parsers like those in MSVS.
(going by the question you clarified in comments) It's not possible to report multiple syntax errors (only the first one is reported); so in the following fragment only the first error on line 1 is reported:
if true the --<-- first syntax error
end
if false the --<-- second syntax error
end
You may however write your own parser (or modify one of the existing Lua parsers) that will continue processing even after finding the error. For example, you may review how David Manura's loose parser works.
It's not possible to report multiple run-time errors (only the first one is reported); so in the following fragment pcall will report only the first error:
pcall(function()
nonExistingFunction1() --<-- only this error will be reported
nonExistingFunction2()
end)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With