[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: HOOK_RET bug?
- From: RLake@...
- Date: Sun, 25 Jan 2004 12:01:54 -0500
> Consider this piece of code:
> k=0;
> function secondcall()
> return 2;
//LUA_HOOKRET *is* called for this line
> end
> function firstcall()
> return secondcall(); //LUA_HOOKRET is *not*
called for this line
This is a tail call, so no stack frame is made for it.
LUA_HOOKTAILRET will be called once for each "omitted" stack
frame.
> end
From page 38 of the manual:
Whenever a hook is called, its ar argument has its field
event set to the specific event that triggered the hook. Moreover, for
line events, the field currentline is also set. To get the value of any
other field in ar, the hook must call lua_getinfo. For return events, event
may be LUA_HOOKRET, the normal value, or LUA_HOOKTAILRET. In the latter
case, Lua is simulating a return from a function that did a tail call;
in this case, it is useless to call lua_getinfo.
You can recognise a tail call easily enough: it has the
form:
return <expr>(<arglist>)
In this case, the "caller" will never be reentered,
and its stack frame can be reused by the "callee" -- in effect,
the call can be replaced with a jump.
Tail calls are extremely useful, for example in implementing
state machines.
HTH,
Rici