aboutsummaryrefslogtreecommitdiff
path: root/testes/goto.lua
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2025-05-05 16:24:59 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2025-05-05 16:24:59 -0300
commitbe8120906304a8658fab998587b969e0e42f5650 (patch)
tree81cf2d38522b10468d09763fc25d261486008197 /testes/goto.lua
parente05590591410a5e007a1e3f1691f6c1cf9d8fe45 (diff)
downloadlua-be8120906304a8658fab998587b969e0e42f5650.tar.gz
lua-be8120906304a8658fab998587b969e0e42f5650.tar.bz2
lua-be8120906304a8658fab998587b969e0e42f5650.zip
First implementation of global declarations
Diffstat (limited to 'testes/goto.lua')
-rw-r--r--testes/goto.lua44
1 files changed, 43 insertions, 1 deletions
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 @@
1-- $Id: testes/goto.lua $ 1-- $Id: testes/goto.lua $
2-- See Copyright Notice in file lua.h 2-- See Copyright Notice in file lua.h
3 3
4print("testing goto and global declarations")
5
4collectgarbage() 6collectgarbage()
5 7
6local function errmsg (code, m) 8local function errmsg (code, m)
@@ -280,7 +282,47 @@ end
280 282
281 283
282foo() 284foo()
283-------------------------------------------------------------------------------- 285--------------------------------------------------------------------------
284 286
287do
288 global print, load, T<const>; global assert<const>
289 global string
290
291 local function checkerr (code, err)
292 local st, msg = load(code)
293 assert(not st and string.find(msg, err))
294 end
295
296 -- globals must be declared after a global declaration
297 checkerr("global none; X = 1", "variable 'X'")
298
299 -- global variables cannot be to-be-closed
300 checkerr("global X<close>", "cannot be")
301
302 do
303 local X = 10
304 do global X; X = 20 end
305 assert(X == 10) -- local X
306 end
307 assert(_ENV.X == 20) -- global X
308
309 -- '_ENV' cannot be global
310 checkerr("global _ENV, a; a = 10", "variable 'a'")
311
312 -- global declarations inside functions
313 checkerr([[
314 global none
315 local function foo () XXX = 1 end --< ERROR]], "variable 'XXX'")
316
317 if not T then -- when not in "test mode", "global" isn't reserved
318 assert(load("global = 1; return global")() == 1)
319 print " ('global' is not a reserved word)"
320 else
321 -- "global" reserved, cannot be used as a variable
322 assert(not load("global = 1; return global"))
323 end
324
325end
285 326
286print'OK' 327print'OK'
328