aboutsummaryrefslogtreecommitdiff
path: root/testes
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2025-05-08 11:08:03 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2025-05-08 11:08:03 -0300
commit3f0ea90aa8b8493485637f6e8d2a070a1ac0d5cb (patch)
treec8f2808286713805ab138a7c4fc1ccf149d94cd7 /testes
parent4365a45d681b4e71e3c39148489bb8eae538ccf7 (diff)
downloadlua-3f0ea90aa8b8493485637f6e8d2a070a1ac0d5cb.tar.gz
lua-3f0ea90aa8b8493485637f6e8d2a070a1ac0d5cb.tar.bz2
lua-3f0ea90aa8b8493485637f6e8d2a070a1ac0d5cb.zip
New syntax 'global function'
Diffstat (limited to 'testes')
-rw-r--r--testes/errors.lua8
-rw-r--r--testes/goto.lua23
2 files changed, 30 insertions, 1 deletions
diff --git a/testes/errors.lua b/testes/errors.lua
index c80051fc..6c76a99a 100644
--- a/testes/errors.lua
+++ b/testes/errors.lua
@@ -489,6 +489,14 @@ if not b then
489 end 489 end
490end]], 5) 490end]], 5)
491 491
492lineerror([[
493_ENV = 1
494global function foo ()
495 local a = 10
496 return a
497end
498]], 2)
499
492 500
493-- bug in 5.4.0 501-- bug in 5.4.0
494lineerror([[ 502lineerror([[
diff --git a/testes/goto.lua b/testes/goto.lua
index fdfddb85..b41399ff 100644
--- a/testes/goto.lua
+++ b/testes/goto.lua
@@ -293,8 +293,9 @@ do
293 assert(not st and string.find(msg, err)) 293 assert(not st and string.find(msg, err))
294 end 294 end
295 295
296 -- globals must be declared after a global declaration 296 -- globals must be declared, after a global declaration
297 checkerr("global none; X = 1", "variable 'X'") 297 checkerr("global none; X = 1", "variable 'X'")
298 checkerr("global none; function XX() end", "variable 'XX'")
298 299
299 -- global variables cannot be to-be-closed 300 -- global variables cannot be to-be-closed
300 checkerr("global X<close>", "cannot be") 301 checkerr("global X<close>", "cannot be")
@@ -321,6 +322,26 @@ do
321 -- "global" reserved, cannot be used as a variable 322 -- "global" reserved, cannot be used as a variable
322 assert(not load("global = 1; return global")) 323 assert(not load("global = 1; return global"))
323 end 324 end
325
326 local foo = 20
327 do
328 global function foo (x)
329 if x == 0 then return 1 else return 2 * foo(x - 1) end
330 end
331 assert(foo == _ENV.foo and foo(4) == 16)
332 end
333 assert(_ENV.foo(4) == 16)
334 assert(foo == 20) -- local one is in context here
335
336 do
337 global foo;
338 function foo (x) return end -- Ok after declaration
339 end
340
341 checkerr([[
342 global foo <const>;
343 function foo (x) return end -- ERROR: foo is read-only
344 ]], "assign to const variable 'foo'")
324 345
325end 346end
326 347