From be8120906304a8658fab998587b969e0e42f5650 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 5 May 2025 16:24:59 -0300 Subject: First implementation of global declarations --- testes/db.lua | 1 + testes/goto.lua | 44 +++++++++++++++++++++++++++++++++++++++++++- testes/math.lua | 16 +++++++++++++--- 3 files changed, 57 insertions(+), 4 deletions(-) (limited to 'testes') diff --git a/testes/db.lua b/testes/db.lua index e4982c20..ae204c41 100644 --- a/testes/db.lua +++ b/testes/db.lua @@ -349,6 +349,7 @@ end, "crl") function f(a,b) + global collectgarbage, assert, g, string collectgarbage() local _, x = debug.getlocal(1, 1) local _, y = debug.getlocal(1, 2) diff --git a/testes/goto.lua b/testes/goto.lua index eca68516..fdfddb85 100644 --- a/testes/goto.lua +++ b/testes/goto.lua @@ -1,6 +1,8 @@ -- $Id: testes/goto.lua $ -- See Copyright Notice in file lua.h +print("testing goto and global declarations") + collectgarbage() local function errmsg (code, m) @@ -280,7 +282,47 @@ end foo() --------------------------------------------------------------------------------- +-------------------------------------------------------------------------- +do + global print, load, T; global assert + global string + + local function checkerr (code, err) + local st, msg = load(code) + assert(not st and string.find(msg, err)) + end + + -- globals must be declared after a global declaration + checkerr("global none; X = 1", "variable 'X'") + + -- global variables cannot be to-be-closed + checkerr("global X", "cannot be") + + do + local X = 10 + do global X; X = 20 end + assert(X == 10) -- local X + end + assert(_ENV.X == 20) -- global X + + -- '_ENV' cannot be global + checkerr("global _ENV, a; a = 10", "variable 'a'") + + -- global declarations inside functions + checkerr([[ + global none + local function foo () XXX = 1 end --< ERROR]], "variable 'XXX'") + + if not T then -- when not in "test mode", "global" isn't reserved + assert(load("global = 1; return global")() == 1) + print " ('global' is not a reserved word)" + else + -- "global" reserved, cannot be used as a variable + assert(not load("global = 1; return global")) + end + +end print'OK' + diff --git a/testes/math.lua b/testes/math.lua index 88a57ce7..242579b1 100644 --- a/testes/math.lua +++ b/testes/math.lua @@ -3,6 +3,14 @@ print("testing numbers and math lib") +local math = require "math" +local string = require "string" + +global none + +global print, assert, pcall, type, pairs, load +global tonumber, tostring, select + local minint = math.mininteger local maxint = math.maxinteger @@ -184,7 +192,7 @@ do for i = -3, 3 do -- variables avoid constant folding for j = -3, 3 do -- domain errors (0^(-n)) are not portable - if not _port or i ~= 0 or j > 0 then + if not _ENV._port or i ~= 0 or j > 0 then assert(eq(i^j, 1 / i^(-j))) end end @@ -430,7 +438,7 @@ for i = 2,36 do assert(tonumber('\t10000000000\t', i) == i10) end -if not _soft then +if not _ENV._soft then -- tests with very long numerals assert(tonumber("0x"..string.rep("f", 13)..".0") == 2.0^(4*13) - 1) assert(tonumber("0x"..string.rep("f", 150)..".0") == 2.0^(4*150) - 1) @@ -632,7 +640,7 @@ assert(maxint % -2 == -1) -- non-portable tests because Windows C library cannot compute -- fmod(1, huge) correctly -if not _port then +if not _ENV._port then local function anan (x) assert(isNaN(x)) end -- assert Not a Number anan(0.0 % 0) anan(1.3 % 0) @@ -779,6 +787,7 @@ assert(a == '10' and b == '20') do print("testing -0 and NaN") + global rawset, undef local mz = -0.0 local z = 0.0 assert(mz == z) @@ -1074,6 +1083,7 @@ do -- different numbers should print differently. -- check pairs of floats with minimum detectable difference local p = floatbits - 1 + global ipairs for i = 1, maxexp - 1 do for _, i in ipairs{-i, i} do local x = 2^i -- cgit v1.2.3-55-g6feb