Optional initialization for global declarations

This commit is contained in:
Roberto Ierusalimschy
2025-07-08 13:33:57 -03:00
parent 8485687908
commit 942c10a5e3
7 changed files with 96 additions and 38 deletions

View File

@@ -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)

View File

@@ -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

View File

@@ -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))

View File

@@ -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<const> a, b, c = 10, 20, 30
assert(_ENV.a == 10 and b == 20 and c == 30)
global<const> 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'

View File

@@ -6,7 +6,7 @@ local M = {}
local setmetatable, stderr, collectgarbage =
setmetatable, io.stderr, collectgarbage
_ENV = nil
global none
local active = false