Get all errors from lua_pcall (L, 0, 0, 0)

Is it possible to get all the errors in the lua stack from C / C ++? that's what i 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 also tried a loop, even if the stack size is 0 or negative, but I don’t understand how the stack can be negative, and the program crashes after several attempts.

+6
source share
2 answers

To complete my comments in response:

According to Lua docs on lua_pcall , pcall returns either success (end of fragment) or the first error. Thus, in the latter case, it will push only one message onto the stack. It will never continue execution after the first error.

What you are trying to achieve is checking for possible errors in the file. In statically typed languages ​​such as C, each variable must be defined at compile time, so the compiler can determine when a function that does not exist is called.

Lua, however, is a dynamically typed language in which variables have no types, but rather are placeholders for values ​​(which have types). This means that Lua cannot tell in advance whether printMessage function, string, value, or if it does not exist (nil). It is only at runtime when a variable needs to be called that Lua checks its type.

Therefore, it is impossible to achieve what you want. Executing code outside the first unhandled error is also meaningless, since an error can make assumptions in subsequent fragments invalid (for example, about global variables that a non-existent function should have set) - this will be a mess.

As for syntax errors, they usually happen when compiling the source file, i.e. at boot time. However, the Lua parser stops at the first syntax error. This is due to the fact that in many cases, syntax errors in one place invalidate everything that follows it. As Ethan noted in his comment, many parsers report subsequent errors that disappear or change after fixing errors that precede them. This is true for even strong weight parsers such as MSVS.

+3
source

(transition to the issue that you specified in the comments) It is impossible to report several syntax errors (only the first is reported); therefore, the following snippet reports only the first error on line 1:

 if true the --<-- first syntax error end if false the --<-- second syntax error end 

However, you can write your own parser (or modify one of the existing Lua parsers) that will continue processing even after an error is detected. For example, you can see how David Manura free parser works.

Unable to report multiple errors at run time (only the first is reported); therefore, in the following snippet, pcall will only report the first error:

 pcall(function() nonExistingFunction1() --<-- only this error will be reported nonExistingFunction2() end) 
+1
source

Source: https://habr.com/ru/post/973753/


All Articles