From abbae57c7844b1121e7251d56f681394f20c1821 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Sun, 18 May 2025 11:43:43 -0300 Subject: Variable attributes can prefix name list In this format, the attribute applies to all names in the list; e.g. "global print, require, math". --- testes/locals.lua | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) (limited to 'testes/locals.lua') diff --git a/testes/locals.lua b/testes/locals.lua index 99ff9edc..02f41980 100644 --- a/testes/locals.lua +++ b/testes/locals.lua @@ -1,7 +1,7 @@ -- $Id: testes/locals.lua $ -- See Copyright Notice in file lua.h -global * +global * print('testing local variables and environments') @@ -181,23 +181,25 @@ assert(x==20) A = nil -do -- constants +do print("testing local constants") global assert, load, string, X X = 1 -- not a constant local a, b, c = 10, 20, 30 b = a + c + b -- 'b' is not constant assert(a == 10 and b == 60 and c == 30) + local function checkro (name, code) local st, msg = load(code) local gab = string.format("attempt to assign to const variable '%s'", name) assert(not st and string.find(msg, gab)) end + checkro("y", "local x, y , z = 10, 20, 30; x = 11; y = 12") checkro("x", "local x , y, z = 10, 20, 30; x = 11") checkro("z", "local x , y, z = 10, 20, 30; y = 10; z = 11") - checkro("foo", "local foo = 10; function foo() end") - checkro("foo", "local foo = {}; function foo() end") - checkro("foo", "global foo ; function foo() end") + checkro("foo", "local foo = 10; function foo() end") + checkro("foo", "local foo = {}; function foo() end") + checkro("foo", "global foo ; function foo() end") checkro("XX", "global XX ; XX = 10") checkro("XX", "local _ENV; global XX ; XX = 10") @@ -218,8 +220,18 @@ do -- constants end + print"testing to-be-closed variables" + +do + local st, msg = load("local a, b") + assert(not st and string.find(msg, "multiple")) + + local st, msg = load("local a, b") + assert(not st and string.find(msg, "multiple")) +end + local function stack(n) n = ((n == 0) or stack(n - 1)) end local function func2close (f, x, y) -- cgit v1.2.3-55-g6feb