C ++ Lua Error Handling

I am working on a program (C ++, with D3D9) and Lua. I have already implemented LUA Api and everything works fine except for error handling. I googled a lot and I found a solution that handles most errors. I am writing this due to other errors.

I will show you the corresponding code in the photos.

Code Part 1 (LUA)

Code Part 2 (LUA)

Part 3 (Console Output)

Now the most important functions: PerformCall () and LuaErrorReport ()

Part 4 (C ++)Part 5 (PerformCall)

However, as I said, some errors are handled. But this is not.

+4
source share
1 answer

Some thoughts on what might be wrong. You pass L for lua_pcall , and LuaErrorReport gets State() . They can work with different lua_State , which can happen, as in the context of a coroutine. If this is intentional, at least indicate why this is being done.

lua_pushvalue inside LuaErrorReport also looks suspicious. How do you know that the bottom value on the stack contains the message you are reporting? This function is called from other functions that can accumulate more values ​​on the stack. I would either change this to

 lua_pushvalue(L, -3); 

or

 lua_pushstring(L, msg); 

Finally, you should also consider passing debug.traceback as an error function in lua_pcall to get a more meaningful stacktrace error message. Sort of:

 void LUA::PerformCall(int return_amount) { lua_getglobal(L, "debug"); lua_getfield(L, -1, "traceback"); lua_remove(L, -2); int errindex = -p_iArgCount - 2; lua_insert(L, errindex); int error = lua_pcall(L, p_iArgCount, return_amount, errindex); // ... 

And get rid of the debug.trace call in LuaErrorReport .

+3
source

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


All Articles