[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: What is "safe" to do in a hook function?
- From: Rici Lake <lua@...>
- Date: Mon, 02 Apr 2007 11:50:11 -0500
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.