[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: HOOK_RET bug?
- From: "Jan-Eric Duden" <jeduden@...>
- Date: Sun, 25 Jan 2004 19:19:34 +0100
Thank you for your explanation.
The behaviour of the hook callbacks makes a lot more sense to me, now. :)
So its not a bug, but a feature. :)
Jan-Eric Duden
----- Original Message -----
From: RLake@oxfam.org.pe
To: Lua list
Sent: Sunday, January 25, 2004 6:01 PM
Subject: Re: HOOK_RET bug?
> 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