lua-users home
lua-l archive

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



On Aug 26, 2013, at 4:16 PM, Andrew Starks <andrew.starks@trms.com> wrote:

On Mon, Aug 26, 2013 at 4:00 PM, Tim Hill <drtimhill@gmail.com> wrote:


When you declare a variable with local local, you make a _new_ variable. You do not overwrite the old one. The second local declaration in your code is not lexically in scope of foo, so why would it overwrite it?

When you set a variable without local, you assign a new value to the existing variable, if it exists, or to the _G/_ENV table, if it does not. 

Of course, as I said it was just a silly typo on my part. My point, however, was that the reuse of stack slots by the compiler involves more than just a quick check to see if the new local name matches any existing one at the same scope.

If memory serves me right, the upvalue in foo() that references the first "a" still references the stack slot (hence it changing to 2 as per your example). it's only when the containing function goes out of scope that Lua has to migrate the upvalue off the stack.

--Tim