lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index] wrote:

I noticed that Lua hooks are usually used for
interrupting running Lua code (with a lua_error

I wonder if it is "safe" (no stack corruption,
etc.) to do something else in a count hook :
for example calling other Lua functions, or
resuming a coroutine.

It's safe to do a lua_call or a lua_resume. However,
hooks are disabled until the hook returns.

The count hook executes inside the current callframe,
which is sometimes convenient, and of course a bit
faster, but it means that you can change the callframe
stack with possibly unpredictable results.

However, doing a lua_call (or lua_pcall or lua_resume)
will create a new callframe, and inside that you're in a
normal Lua call environment, except that hooks are disabled.

If you use the Lua debug library to set the hook, the
supplied hook function is Lua called and it's free to
do whatever it wants.