[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Locals by default yet again, sorry :)
- From: pocomane <pocomane_7a@...>
- Date: Tue, 30 Jul 2019 15:29:01 +0200
On Tue, Jul 30, 2019 at 8:45 AM Abhijit Nandy <firstname.lastname@example.org> wrote:
> What I am wondering is, apart from using the static analyzer to catch it, is there any code patch that can make variable declarations local by default?
How do you access globals then?
Have you to use `__ENV.name` everywhere ?
So, could to completely forbid the globals work for you?
If this is the case, I just wrote a patch some day ago. It is for the
last commit on github  but it is very simple, probably you can
adapt it to the official release. I attached the last version.
The patch lets you to mark a do/end block with one of the following:
- <autoglobal>: it works like the common do/end block
- <withupvalue>: a free name inside this block raises an error instead
of being translated to `__ENV.name`
- <localonly>: an error is raised when accessing an upvalue too (so
local variables only are allowed, it is similar to the noupvalues by
- A non-marked do/end block inherits the behaviour from the parent block.
- The main chunk starts in the <autoglobal> mode (as usual).
For your use case, I think you could do one of the following:
1) in the patch, you can change the default for the main chunk to
2) in your lua code, you can write a wrapper for the lua function
"load" that puts any lua code in a
-- paste here the original script
> Or is it too core to the language that changing this could cause issues elsewhere? I am willing to sacrifice performance as we are on PC and its worth doing this for us to prevent erroneous code if it can be done without requiring significant change to syntax.
The patch rises error at compile time, so the performance should not
Please note that the patch is somehow "Compatible" with the regular
lua: if you write a script that works as expected with the patch, you
can remove any mark from the do/end blocks, and the result is valid
for the regular lua too .
 Of course, if no do/end block is marked it is alright valid for a
regular lua. And this is true also if you changed the default mode, or
if you wrapped the "load" function.
Description: Binary data