2

Lua features hook call BEFORE every processed line. What I need is a call AFTER line is processed, so that I can check for encountered errors and so on. Is there a way to make such kind of call?

Otherwise things get a little bit confusing if error is encountered at the last line of the script. I don't get any feedback.

UPDATE #1 We want to catch both Lua errors and 'our' errors asserted via lua_error(*L) C interface, and Lua should throw correct debug info including the line number where the error occurred.

Using return hook we always get error line number -1, which is not what we want. Using any combination of pcall and any hook setup after lua_error(*L) we get either line number -1, or number of the next executed line, never a correct one.

SOLUTION# We managed to make everything work. The thing was that Lua throws a real C exception after it detects an error, so some of our 'cleaning & finalizing' C code called from Lua operation did not execute, which messed up some flags and so on. The solution was to execute 'cleaning code' right before calling lua_error(...). This is correct and desired Lua behavior as we really want to stop executing the function once lua_error(...) is called, it was our mistake to expect any code would be executed after lua_error(...) call.

Tnx Paul Kulchenko, some of this behavior was found while trying to design a simple example script which reproduces the problem.

ahrvoje
  • 41
  • 4

2 Answers2

0

Try setting a return hook: it'll be called after the last line is executed.

lhf
  • 70,581
  • 9
  • 108
  • 149
0

I'm not sure debug hook is the best solution for what you are trying to do (or you need to provide more details). If you just need to check for run-time errors, why use debug hooks at all if you can run your code with pcall and get an error message that points to the line number where the error happened (or use xpcall, which also allows you to get a stack trace)? You can combine this with debug.getinfo(func, "L") to get a table whose indexes are valid line numbers for the function.

Paul Kulchenko
  • 25,884
  • 3
  • 38
  • 56