lua-users home
lua-l archive

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


2013/8/24 Thijs Schreijer <thijs@thijsschreijer.nl>


> -----Original Message-----
> From: lua-l-bounces@lists.lua.org [mailto:lua-l-bounces@lists.lua.org] On
> Behalf Of Tim Hill
> Sent: zaterdag 24 augustus 2013 3:39
> To: Lua mailing list
> Cc: SLONIK.AZ@gmail.com
> Subject: Re: Lua strictlessness
>
>
> On Aug 23, 2013, at 5:59 PM, Tangent 128 <tangent128@gmail.com> wrote:
>
>
> function t()
>    local a = {}
>    w[1] = a
>    cg() cg()
>
>    local a = 2
>    cg() cg()
> end
>
> This is really a side-effect of the non-optimizing Lua compiler; a quick
> look at luac output shows that each "a" gets a different stack slot, since
> the compiler does not perform scope analysis (hardly surprising).
>
> However, this does bring up an interesting issue for locals declared at
> the top-level scope in a long-lived script; as the earlier poster noted,
> these shadowed items will indeed live on until the script terminates so
> far as I can see.
>
> local a = { a_very_big_table }
> local a = 10
> -- The table is now inaccessible but reachable by the GC
>
> Of course, the quick answer is "don't do this", so I guess it might be
> worth noting somewhere in the Lua docs or wiki?
>
> --Tim

Theoretical correct, but what code would do this? Define a variable, and then make it inaccessible by shadowing it. Common cases are to do this in inner blocks etc, different scoping, so after the shadowing scope ends the original is accessible again. If you don't use the '{ a_very_big_table }' any more, remove it.

It's not a practical case. Though it might be a source of errors, I doubt it to be worth the extra compiler checks and balances for each local that is created.

Thijs

I thought about run-time checks only, compiler time checks for such task looks odd to me (it can't handle loadstring/loadfile). In case of global scope I can check it through strict.lua (http://lua-users.org/wiki/DetectingUndefinedVariables). But in case of local scope I can't manage it. It would be nice to have some function to enable such check on run-time debug.strict_locals(true). Is it possible to write such function in C using Lua API ABI or do I need to modify Lua core?