lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


> I wonder if there was a motivation for not letting the line hook execute 
before
> the first line of Lua code (could it have been an oversight?). I also 
wonder
> if one of the two solutions I found ( calling luaG_inithooks() vs.
> lua_state_pointer->hookinit = 1 ) is preferable to the other, and
> whether they are both safe (i.e. won't later cause something 
unexpected). 

I can't speak for the Lua authors, but I think the reasoning goes 
something
like this:

It is likely (or at least possible) that you would want to enable a 
linehook
in an interrupt routine (i.e. a signal handler on Unix, or an alarm 
handler
or whatever your OS comes up with). In this case, the hook should trigger 
at
the next line; however, you don't know what the current line number is 
until
the VM enters traceexec. So it cannot trigger the linehook at that point
because the VM instruction at that point might not be the start of a line;
instead, it figures out which line it is on, remembers that, and triggers
a line hook when the line number changes.

So if you think of a line hook happening *after* each line, instead of 
*before*
each line, it does exactly what it should do, and moreover is 
asychronous-safe.
That is not exactly what the manual says, though.

Now, I gather that what you want to do is have your debugger grab control
after Lua initializes and before it executes even the first line of the
script. That seems like a reasonable thing to want to do. I would think
you could accomplish that with a call hook; presumably you would have
call hooks enabled in a debugger anyway in order to allow "step through".
Since the script is going to be executed by your debugger executing
lua_call or (preferably) lua_pcall, the call hook will trigger *as if*
the script were a function (and in fact, the script *is* a function,
so there is really no *as if* about it.)

Rici