From 3f0ea90aa8b8493485637f6e8d2a070a1ac0d5cb Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 8 May 2025 11:08:03 -0300 Subject: New syntax 'global function' --- testes/goto.lua | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) (limited to 'testes/goto.lua') 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 assert(not st and string.find(msg, err)) end - -- globals must be declared after a global declaration + -- globals must be declared, after a global declaration checkerr("global none; X = 1", "variable 'X'") + checkerr("global none; function XX() end", "variable 'XX'") -- global variables cannot be to-be-closed checkerr("global X", "cannot be") @@ -321,6 +322,26 @@ do -- "global" reserved, cannot be used as a variable assert(not load("global = 1; return global")) end + + local foo = 20 + do + global function foo (x) + if x == 0 then return 1 else return 2 * foo(x - 1) end + end + assert(foo == _ENV.foo and foo(4) == 16) + end + assert(_ENV.foo(4) == 16) + assert(foo == 20) -- local one is in context here + + do + global foo; + function foo (x) return end -- Ok after declaration + end + + checkerr([[ + global foo ; + function foo (x) return end -- ERROR: foo is read-only + ]], "assign to const variable 'foo'") end -- cgit v1.2.3-55-g6feb