lua-users home
lua-l archive

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


Rici < RLake@oxfam.org.pe > and I had a long discussion about the behavior of the syntacic sugar for local functions when a line hook is enabled. 

Consider:

----------- test.lua ------------------
var = "global"
local foo; foo = function() end
return -- added so line hook would execute after line 2 (before line 3)
----------- test.lua ------------------

When the line hook is triggered before line 2, and I try to retrieve and display all local variables using lua_getlocal(), nothing is displayed, because no local variables are yet defined.

But, if

----------- test2.lua ------------------
var = "global"
local function foo() end
return
----------- test2.lua ------------------

Before line 2, 'foo' is already retrieved by lua_getlocal() (with value "global"), even though it is not yet defined. 

It is my understanding that lua_getlocal() can retrieve a local only *after* it has been defined. That seems to be the case with every local definition except for the syntactic sugar of "local function func_name". 

Now, since section 2.5.8 of the Lua 5.0 manual explained that 'local function f()... end' translates to 'local f; f = function()... end', I had expected both scripts to run in exactly the same way.

However, lua_getlocal() behaved differently in the two cases. And after long discussions, Rici and I narrowed the problem domain and Rici provided the theory that (this is the way I understood it), after 'local function foo() end' is translated into 'local foo; foo = function() end', the 'local foo;' part is executed *before* the line hook. 

In the translation, 'local foo' is not equal to 'local foo = nil' (which is another syntactic sugar, I think); 'local foo' records in the debugging info that 'foo' is already in scope (retrievable by lua_getlocal()), but does not assign foo to anything.  This seems to explain the different behaviors of the two scripts. 

I wonder if someone can confirm the theory or offer an alternate explanation?

If the theory is indeed confirmed, is there a reason why the line hook is not triggered *before* the entire translation?

Tai