Optional initialization for global declarations · lua/lua@942c10a · GitHub
Skip to content

Commit 942c10a

Browse files
committed
Optional initialization for global declarations
1 parent 8485687 commit 942c10a

7 files changed

Lines changed: 96 additions & 38 deletions

File tree

‎lparser.c‎

Lines changed: 58 additions & 23 deletions

‎manual/manual.of‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ as the following example illustrates:
229229
@verbatim{
230230
X = 1 -- Ok, global by default
231231
do
232-
global Y -- voids implicit initial declaration
232+
global Y -- voids the implicit initial declaration
233233
Y = 1 -- Ok, Y declared as global
234234
X = 1 -- ERROR, X not declared
235235
end
@@ -269,7 +269,7 @@ print(x) --> 10 (the global one)
269269

270270
Notice that, in a declaration like @T{local x = x},
271271
the new @id{x} being declared is not in scope yet,
272-
and so the @id{x} in the left-hand side refers to the outside variable.
272+
and so the @id{x} in the right-hand side refers to the outside variable.
273273

274274
Because of the @x{lexical scoping} rules,
275275
local variables can be freely accessed by functions
@@ -1651,11 +1651,12 @@ Function calls are explained in @See{functioncall}.
16511651

16521652
@sect3{localvar| @title{Variable Declarations}
16531653
Local and global variables can be declared anywhere inside a block.
1654-
The declaration for locals can include an initialization:
1654+
The declaration can include an initialization:
16551655
@Produc{
16561656
@producname{stat}@producbody{@Rw{local}
16571657
attnamelist @bnfopt{@bnfter{=} explist}}
1658-
@producname{stat}@producbody{@Rw{global} attnamelist}
1658+
@producname{stat}@producbody{@Rw{global}
1659+
attnamelist @bnfopt{@bnfter{=} explist}}
16591660
}
16601661
If present, an initial assignment has the same semantics
16611662
of a multiple assignment @see{assignment}.
@@ -1712,7 +1713,8 @@ and a program that starts with any other global declaration
17121713
(e.g., @T{global none}) can only refer to declared variables.
17131714

17141715
Note that, for global variables,
1715-
the effect of any declaration is only syntactical:
1716+
the effect of any declaration is only syntactical
1717+
(except for the optional assignment):
17161718
@verbatim{
17171719
global X <const>, _G
17181720
X = 1 -- ERROR
@@ -3924,8 +3926,8 @@ This macro may evaluate its arguments more than once.
39243926

39253927
}
39263928

3927-
@APIEntry{unsigned (lua_numbertocstring) (lua_State *L, int idx,
3928-
char *buff);|
3929+
@APIEntry{unsigned lua_numbertocstring (lua_State *L, int idx,
3930+
char *buff);|
39293931
@apii{0,0,-}
39303932

39313933
Converts the number at acceptable index @id{idx} to a string
@@ -4050,7 +4052,7 @@ This function is equivalent to @Lid{lua_pushcclosure} with no upvalues.
40504052

40514053
}
40524054

4053-
@APIEntry{const char *(lua_pushexternalstring) (lua_State *L,
4055+
@APIEntry{const char *lua_pushexternalstring (lua_State *L,
40544056
const char *s, size_t len, lua_Alloc falloc, void *ud);|
40554057
@apii{0,1,m}
40564058

‎testes/bwcoercion.lua‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ local strsub = string.sub
44

55
local print = print
66

7-
_ENV = nil
7+
global none
88

99
-- Try to convert a value to an integer, without assuming any coercion.
1010
local function toint (x)

‎testes/calls.lua‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ assert(not pcall(type))
2424

2525

2626
-- testing local-function recursion
27-
global fact; fact = false
27+
global fact = false
2828
do
2929
local res = 1
3030
local function fact (n)
@@ -65,7 +65,7 @@ a.b.c:f2('k', 12); assert(a.b.c.k == 12)
6565

6666
print('+')
6767

68-
global t; t = nil -- 'declare' t
68+
global t = nil -- 'declare' t
6969
function f(a,b,c) local d = 'a'; t={a,b,c,d} end
7070

7171
f( -- this line change must be valid

‎testes/files.lua‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,7 @@ do
715715
end
716716

717717

718-
if T and T.nonblock then
718+
if T and T.nonblock and not _port then
719719
print("testing failed write")
720720

721721
-- unable to write anything to /dev/full
@@ -840,7 +840,7 @@ assert(os.date("!\0\0") == "\0\0")
840840
local x = string.rep("a", 10000)
841841
assert(os.date(x) == x)
842842
local t = os.time()
843-
global D; D = os.date("*t", t)
843+
global D = os.date("*t", t)
844844
assert(os.date(string.rep("%d", 1000), t) ==
845845
string.rep(os.date("%d", t), 1000))
846846
assert(os.date(string.rep("%", 200)) == string.rep("%", 100))

‎testes/goto.lua‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ do
380380
global *
381381
Y = x + Y
382382
assert(_ENV.Y == 20)
383-
383+
Y = nil
384384
end
385385

386386

@@ -411,5 +411,26 @@ do -- mixing lots of global/local declarations
411411
_ENV.x200 = nil
412412
end
413413

414+
do print "testing initialization in global declarations"
415+
global<const> a, b, c = 10, 20, 30
416+
assert(_ENV.a == 10 and b == 20 and c == 30)
417+
418+
global<const> a, b, c = 10
419+
assert(_ENV.a == 10 and b == nil and c == nil)
420+
421+
global table
422+
global a, b, c, d = table.unpack{1, 2, 3, 6, 5}
423+
assert(_ENV.a == 1 and b == 2 and c == 3 and d == 6)
424+
425+
local a, b = 100, 200
426+
do
427+
global a, b = a, b
428+
end
429+
assert(_ENV.a == 100 and _ENV.b == 200)
430+
431+
432+
_ENV.a, _ENV.b, _ENV.c, _ENV.d = nil -- erase these globals
433+
end
434+
414435
print'OK'
415436

‎testes/tracegc.lua‎

Lines changed: 1 addition & 1 deletion

0 commit comments

Comments
 (0)