From 942c10a5e33811a08a290ec15031c950a6d17c99 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 8 Jul 2025 13:33:57 -0300 Subject: Optional initialization for global declarations --- testes/bwcoercion.lua | 2 +- testes/calls.lua | 4 ++-- testes/files.lua | 4 ++-- testes/goto.lua | 23 ++++++++++++++++++++++- testes/tracegc.lua | 2 +- 5 files changed, 28 insertions(+), 7 deletions(-) (limited to 'testes') diff --git a/testes/bwcoercion.lua b/testes/bwcoercion.lua index cd735ab0..0544944d 100644 --- a/testes/bwcoercion.lua +++ b/testes/bwcoercion.lua @@ -4,7 +4,7 @@ local strsub = string.sub local print = print -_ENV = nil +global none -- Try to convert a value to an integer, without assuming any coercion. local function toint (x) diff --git a/testes/calls.lua b/testes/calls.lua index 21441701..0dacb85a 100644 --- a/testes/calls.lua +++ b/testes/calls.lua @@ -24,7 +24,7 @@ assert(not pcall(type)) -- testing local-function recursion -global fact; fact = false +global fact = false do local res = 1 local function fact (n) @@ -65,7 +65,7 @@ a.b.c:f2('k', 12); assert(a.b.c.k == 12) print('+') -global t; t = nil -- 'declare' t +global t = nil -- 'declare' t function f(a,b,c) local d = 'a'; t={a,b,c,d} end f( -- this line change must be valid diff --git a/testes/files.lua b/testes/files.lua index d4e327b7..7146ac7c 100644 --- a/testes/files.lua +++ b/testes/files.lua @@ -715,7 +715,7 @@ do end -if T and T.nonblock then +if T and T.nonblock and not _port then print("testing failed write") -- unable to write anything to /dev/full @@ -840,7 +840,7 @@ assert(os.date("!\0\0") == "\0\0") local x = string.rep("a", 10000) assert(os.date(x) == x) local t = os.time() -global D; D = os.date("*t", t) +global D = os.date("*t", t) assert(os.date(string.rep("%d", 1000), t) == string.rep(os.date("%d", t), 1000)) assert(os.date(string.rep("%", 200)) == string.rep("%", 100)) diff --git a/testes/goto.lua b/testes/goto.lua index 3519e75d..3310314d 100644 --- a/testes/goto.lua +++ b/testes/goto.lua @@ -380,7 +380,7 @@ do global * Y = x + Y assert(_ENV.Y == 20) - + Y = nil end @@ -411,5 +411,26 @@ do -- mixing lots of global/local declarations _ENV.x200 = nil end +do print "testing initialization in global declarations" + global a, b, c = 10, 20, 30 + assert(_ENV.a == 10 and b == 20 and c == 30) + + global a, b, c = 10 + assert(_ENV.a == 10 and b == nil and c == nil) + + global table + global a, b, c, d = table.unpack{1, 2, 3, 6, 5} + assert(_ENV.a == 1 and b == 2 and c == 3 and d == 6) + + local a, b = 100, 200 + do + global a, b = a, b + end + assert(_ENV.a == 100 and _ENV.b == 200) + + + _ENV.a, _ENV.b, _ENV.c, _ENV.d = nil -- erase these globals +end + print'OK' diff --git a/testes/tracegc.lua b/testes/tracegc.lua index 9c5c1b3f..a8c929df 100644 --- a/testes/tracegc.lua +++ b/testes/tracegc.lua @@ -6,7 +6,7 @@ local M = {} local setmetatable, stderr, collectgarbage = setmetatable, io.stderr, collectgarbage -_ENV = nil +global none local active = false -- cgit v1.2.3-55-g6feb