First implementation of global declarations

This commit is contained in:
Roberto Ierusalimschy
2025-05-05 16:24:59 -03:00
parent e055905914
commit be81209063
10 changed files with 272 additions and 117 deletions

View File

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

View File

@@ -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<const>; global assert<const>
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<close>", "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'

View File

@@ -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 <const> = math.mininteger
local maxint <const> = 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 <const> = -0.0
local z <const> = 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