Collective declaration for globals ('global *')

This commit is contained in:
Roberto Ierusalimschy
2025-05-13 11:43:10 -03:00
parent 7dc6aae290
commit 3b9dd52be0
14 changed files with 156 additions and 64 deletions

View File

@@ -2,6 +2,10 @@
-- $Id: testes/all.lua $
-- See Copyright Notice in file lua.h
global * <const>
global _soft, _port, _nomsg
global T
local version = "Lua 5.5"
if _VERSION ~= version then
@@ -34,7 +38,7 @@ if usertests then
end
-- tests should require debug when needed
debug = nil
global debug; debug = nil
if usertests then
@@ -71,7 +75,7 @@ do -- (
-- track messages for tests not performed
local msgs = {}
function Message (m)
global function Message (m)
if not _nomsg then
print(m)
msgs[#msgs+1] = string.sub(m, 3, -3)

View File

@@ -1,6 +1,8 @@
-- $Id: testes/calls.lua $
-- See Copyright Notice in file lua.h
global * <const>
print("testing functions and calls")
local debug = require "debug"
@@ -22,7 +24,7 @@ assert(not pcall(type))
-- testing local-function recursion
fact = false
global fact; fact = false
do
local res = 1
local function fact (n)
@@ -63,7 +65,7 @@ a.b.c:f2('k', 12); assert(a.b.c.k == 12)
print('+')
t = nil -- 'declare' t
global t; 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
@@ -75,7 +77,7 @@ assert(t[1] == 1 and t[2] == 2 and t[3] == 3 and t[4] == 'a')
t = nil -- delete 't'
function fat(x)
global function fat(x)
if x <= 1 then return 1
else return x*load("return fat(" .. x-1 .. ")", "")()
end
@@ -107,7 +109,7 @@ end
_G.deep = nil -- "declaration" (used by 'all.lua')
function deep (n)
global function deep (n)
if n>0 then deep(n-1) end
end
deep(10)
@@ -352,7 +354,7 @@ assert(not load(function () return true end))
-- small bug
local t = {nil, "return ", "3"}
f, msg = load(function () return table.remove(t, 1) end)
local f, msg = load(function () return table.remove(t, 1) end)
assert(f() == nil) -- should read the empty chunk
-- another small bug (in 5.2.1)
@@ -388,7 +390,8 @@ assert(load("return _ENV", nil, nil, 123)() == 123)
-- load when _ENV is not first upvalue
local x; XX = 123
global XX; local x
XX = 123
local function h ()
local y=x -- use 'x', so that it becomes 1st upvalue
return XX -- global name

View File

@@ -1,6 +1,8 @@
-- $Id: testes/closure.lua $
-- See Copyright Notice in file lua.h
global * <const>
print "testing closures"
do -- bug in 5.4.7

View File

@@ -1,6 +1,8 @@
-- $Id: testes/code.lua $
-- See Copyright Notice in file lua.h
global * <const>
if T==nil then
(Message or print)('\n >>> testC not active: skipping opcode tests <<<\n')
return
@@ -405,8 +407,8 @@ do -- tests for table access in upvalues
end
-- de morgan
checkequal(function () local a; if not (a or b) then b=a end end,
function () local a; if (not a and not b) then b=a end end)
checkequal(function () local a, b; if not (a or b) then b=a end end,
function () local a, b; if (not a and not b) then b=a end end)
checkequal(function (l) local a; return 0 <= a and a <= l end,
function (l) local a; return not (not(a >= 0) or not(a <= l)) end)

View File

@@ -1,6 +1,8 @@
-- $Id: testes/files.lua $
-- See Copyright Notice in file lua.h
global * <const>
local debug = require "debug"
local maxint = math.maxinteger
@@ -838,13 +840,13 @@ assert(os.date("!\0\0") == "\0\0")
local x = string.rep("a", 10000)
assert(os.date(x) == x)
local t = os.time()
D = os.date("*t", t)
global D; 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))
local function checkDateTable (t)
_G.D = os.date("*t", t)
D = os.date("*t", t)
assert(os.time(D) == t)
load(os.date([[assert(D.year==%Y and D.month==%m and D.day==%d and
D.hour==%H and D.min==%M and D.sec==%S and

View File

@@ -1,6 +1,10 @@
-- $Id: testes/goto.lua $
-- See Copyright Notice in file lua.h
global require
global print, load, assert, string, setmetatable
global collectgarbage, error
print("testing goto and global declarations")
collectgarbage()
@@ -254,6 +258,8 @@ assert(testG(5) == 10)
do -- test goto's around to-be-closed variable
global *
-- set 'var' and return an object that will reset 'var' when
-- it goes out of scope
local function newobj (var)
@@ -265,16 +271,16 @@ do -- test goto's around to-be-closed variable
goto L1
::L4:: assert(not X); goto L5 -- varX dead here
::L4:: assert(not varX); goto L5 -- varX dead here
::L1::
local varX <close> = newobj("X")
assert(X); goto L2 -- varX alive here
assert(varX); goto L2 -- varX alive here
::L3::
assert(X); goto L4 -- varX alive here
assert(varX); goto L4 -- varX alive here
::L2:: assert(X); goto L3 -- varX alive here
::L2:: assert(varX); goto L3 -- varX alive here
::L5:: -- return
end
@@ -285,8 +291,7 @@ foo()
--------------------------------------------------------------------------
do
global print, load, T<const>; global assert<const>
global string
global T<const>
local function checkerr (code, err)
local st, msg = load(code)
@@ -299,6 +304,7 @@ do
-- global variables cannot be to-be-closed
checkerr("global X<close>", "cannot be")
checkerr("global * <close>", "cannot be")
do
local X = 10
@@ -349,6 +355,12 @@ do
return
end
]], "%:2%:") -- correct line in error message
checkerr([[
global * <const>;
print(X) -- Ok to use
Y = 1 -- ERROR
]], "assign to const variable 'Y'")
end

View File

@@ -3,6 +3,8 @@
print('testing scanner')
global * <const>
local debug = require "debug"

View File

@@ -1,6 +1,8 @@
-- $Id: testes/locals.lua $
-- See Copyright Notice in file lua.h
global * <const>
print('testing local variables and environments')
local debug = require"debug"
@@ -39,9 +41,11 @@ f = nil
local f
local x = 1
a = nil
load('local a = {}')()
assert(a == nil)
do
global a; a = nil
load('local a = {}')()
assert(a == nil)
end
function f (a)
local _1, _2, _3, _4, _5
@@ -154,7 +158,7 @@ local _ENV = (function (...) return ... end)(_G, dummy) -- {
do local _ENV = {assert=assert}; assert(true) end
local mt = {_G = _G}
local foo,x
A = false -- "declare" A
global A; A = false -- "declare" A
do local _ENV = mt
function foo (x)
A = x

View File

@@ -1,6 +1,8 @@
-- $Id: testes/nextvar.lua $
-- See Copyright Notice in file lua.h
global * <const>
print('testing tables, next, and for')
local function checkerror (msg, f, ...)
@@ -345,9 +347,6 @@ end
local nofind = {}
a,b,c = 1,2,3
a,b,c = nil
-- next uses always the same iteration function
assert(next{} == next{})
@@ -396,7 +395,7 @@ for i=0,10000 do
end
end
n = {n=0}
local n = {n=0}
for i,v in pairs(a) do
n.n = n.n+1
assert(i and v and a[i] == v)

View File

@@ -6,6 +6,8 @@
print('testing pattern matching')
global * <const>
local function checkerror (msg, f, ...)
local s, err = pcall(f, ...)
assert(not s and string.find(err, msg))

View File

@@ -3,6 +3,7 @@
-- ISO Latin encoding
global * <const>
print('testing strings and string library')

View File

@@ -3,6 +3,8 @@
-- UTF-8 file
global * <const>
print "testing UTF-8 library"
local utf8 = require'utf8'