21 Commits

Author SHA1 Message Date
Roberto I
e44f3a2ffc Global initialization checks name conflict
Initialization "global a = 10" raises an error if global 'a' is already
defined, that is, it has a non-nil value.
2025-11-08 11:43:42 -03:00
Roberto I
d4eff00234 Fixed initialization of global variables
When calling 'luaK_storevar', the 'expdesc' for the variable must be
created before the one for the expression, to satisfy the assumptions
for register allocation. So, in a statement like 'global a = exp', where
'a' is actually '_ENV.a', this variable must be handled before the
initializing expression 'exp'.
2025-10-29 13:14:48 -03:00
Roberto Ierusalimschy
942c10a5e3 Optional initialization for global declarations 2025-07-08 13:33:57 -03:00
Roberto Ierusalimschy
07b009c371 No need to limit variable declarations to 250
Only local variables, which use registers, need this low limit.
2025-06-18 16:45:55 -03:00
Roberto Ierusalimschy
be05c44481 New way to control preambular declaration
Validity of the preambular global declaration in controled together
with all declarations, when checking variable names.
2025-05-20 17:36:05 -03:00
Roberto Ierusalimschy
6d53701c7a Proper error message when jumping into 'global *'
A goto cannot jump into the scope of any variable declaration,
including 'global *'. To report the error, it needs a "name" for
the scope it is entering.
2025-05-18 12:03:54 -03:00
Roberto Ierusalimschy
abbae57c78 Variable attributes can prefix name list
In this format, the attribute applies to all names in the list;
e.g. "global<const> print, require, math".
2025-05-18 11:43:43 -03:00
Roberto Ierusalimschy
3b9dd52be0 Collective declaration for globals ('global *') 2025-05-13 11:43:10 -03:00
Roberto Ierusalimschy
7dc6aae290 Correct line in error message for constant function 2025-05-12 11:42:45 -03:00
Roberto Ierusalimschy
3f0ea90aa8 New syntax 'global function' 2025-05-08 11:08:03 -03:00
Roberto Ierusalimschy
be81209063 First implementation of global declarations 2025-05-05 16:24:59 -03:00
Roberto Ierusalimschy
ab66652b32 Removed copyright notice from 'testes/all.lua'
All test files refer to the main copyright notice in 'lua.h'.
2025-03-12 14:00:58 -03:00
Roberto Ierusalimschy
7ca3c40b50 Another way to compile goto's
The compilation of a goto or a label just create an entry and generate
boilerplate code for the gotos. As we don't know yet whether it needs a
CLOSE, we code a jump followed by a CLOSE, which is then dead code.

When a block ends (and then we know for sure whether there are variables
that need to be closed), we check the goto's against the labels of that
block. When closing a goto against a label, if it needs a CLOSE, the
compiler swaps the order of the jump and the CLOSE, making the CLOSE
active.
2025-01-10 13:54:51 -03:00
Roberto Ierusalimschy
0d52913804 Change in the syntax of attributes
Attributes changed to posfixed ('x <const>', instead of '<const> x'),
and "toclose" renamed to "close". Posfixed attributes seem to make it
clearer that it applies to only one variable when there are multiple
variables.
2019-07-30 12:18:19 -03:00
Roberto Ierusalimschy
d881325c2f Flag for to-be-closed variables changed to '<toclose>'
The flag for to-be-closed variables was changed from '*toclose'
to '<toclose>'. Several people found confusing the old syntax and
the new one has a clear terminator, making it more flexible for
future changes.
2019-05-09 12:10:31 -03:00
Roberto Ierusalimschy
4ace93ca65 No more to-be-closed functions
To-be-closed variables must contain objects with '__toclose'
metamethods (or nil). Functions were removed for several reasons:

* Functions interact badly with sandboxes. If a sandbox raises
an error to interrupt a script, a to-be-closed function still
can hijack control and continue running arbitrary sandboxed code.

* Functions interact badly with coroutines. If a coroutine yields
and is never resumed again, its to-be-closed functions will never
run. To-be-closed objects, on the other hand, will still be closed,
provided they have appropriate finalizers.

* If you really need a function, it is easy to create a dummy
object to run that function in its '__toclose' metamethod.

This comit also adds closing of variables in case of panic.
2019-01-04 13:09:47 -02:00
Roberto Ierusalimschy
b8fed93215 New syntax for to-be-closed variables
The new syntax is <local *toclose x = f()>. The mark '*' allows other
attributes to be added later without the need of new keywords; it
also allows better error messages.  The API function was also renamed
('lua_tobeclosed' -> 'lua_toclose').
2018-11-07 10:03:05 -02:00
Roberto Ierusalimschy
2316ec4c24 Back with optimization for 'if cond then goto'
Statements like 'if cond then goto label' generate code so that the
jump in the 'if' goes directly to the given label. This optimization
cannot be done when the jump is backwards leaving the scope of some
variable, as it cannot add the needed 'close' instruction. (The jumps
were already generated by the 'if'.)

This commit also added 'likely'/'unlikely' for tests for errors in
the parser, and it changed the way breaks outside loops are detected.
(Now they are detected like other goto's with undefined labels.)
2018-10-30 15:04:19 -03:00
Roberto Ierusalimschy
a006514ea1 Big revamp in the implmentation of labels/gotos
Added restriction that, when a label is created, there cannot be
another label with the same name visible. That allows backward goto's
to be resolved when they are read. Backward goto's get a close if
they jump out of the scope of some variable; labels get a close only
if previous goto to it jumps out of the scope of some upvalue.
2018-10-29 14:26:48 -03:00
Roberto Ierusalimschy
aa4c5cf190 Added directory to test file names in '$Id:'
From the point of view of 'git', all names are relative to the root
directory of the project. So, file names in '$Id:' also should be
relative to that directory: the proper name for test file 'all.lua'
is 'testes/all.lua'.
2018-07-25 15:31:04 -03:00
Roberto Ierusalimschy
7c519dfbd0 Added manual and tests for version 5.4-w2 2018-07-09 12:33:01 -03:00