lua-users home
lua-l archive

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

> The Lua manual states:

> "The return hook is called when the interpreter returns from a function. The hook is called just before Lua leaves the function."

> I noticed that return hooks are executed *after* locals have been cleared from the function stack, and before the function exits.

That is what the manual says: "just before Lua leaves the function".

> Is there a reason why the locals are cleared before the return hook triggers?

1) If the return were actually a tailcall, there would be no practical way to hook before the locals were cleared.

2) There is nothing in principle stopping a Lua interpreter from overwriting arbitrary locals while computing return values. Defining a return hook the way you apparently think it should be would preclude this optimisation.

3) Related to the above point, conceptually there are no locals at the point where the return is happening (possibly aside from the function parameters). The _expression_ return statement is executed after the return values are computed, and there are no remaining locals in scope after that. This might be clearer if you think of a stack machine implementation.

If you want to examine the locals before the entire return statement executes, you should probably use a line hook.

Just one person's opinion, of course...