diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2025-05-18 11:43:43 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2025-05-18 11:43:43 -0300 |
| commit | abbae57c7844b1121e7251d56f681394f20c1821 (patch) | |
| tree | 823201f4d2631fcae027117681553de80b2c96f0 /testes/locals.lua | |
| parent | f2c1531e6cacb10926158d8def5fa5841a0f357e (diff) | |
| download | lua-abbae57c7844b1121e7251d56f681394f20c1821.tar.gz lua-abbae57c7844b1121e7251d56f681394f20c1821.tar.bz2 lua-abbae57c7844b1121e7251d56f681394f20c1821.zip | |
Variable attributes can prefix name list
In this format, the attribute applies to all names in the list;
e.g. "global<const> print, require, math".
Diffstat (limited to 'testes/locals.lua')
| -rw-r--r-- | testes/locals.lua | 22 |
1 files changed, 17 insertions, 5 deletions
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 @@ | |||
| 1 | -- $Id: testes/locals.lua $ | 1 | -- $Id: testes/locals.lua $ |
| 2 | -- See Copyright Notice in file lua.h | 2 | -- See Copyright Notice in file lua.h |
| 3 | 3 | ||
| 4 | global * <const> | 4 | global <const> * |
| 5 | 5 | ||
| 6 | print('testing local variables and environments') | 6 | print('testing local variables and environments') |
| 7 | 7 | ||
| @@ -181,23 +181,25 @@ assert(x==20) | |||
| 181 | A = nil | 181 | A = nil |
| 182 | 182 | ||
| 183 | 183 | ||
| 184 | do -- constants | 184 | do print("testing local constants") |
| 185 | global assert<const>, load, string, X | 185 | global assert<const>, load, string, X |
| 186 | X = 1 -- not a constant | 186 | X = 1 -- not a constant |
| 187 | local a<const>, b, c<const> = 10, 20, 30 | 187 | local a<const>, b, c<const> = 10, 20, 30 |
| 188 | b = a + c + b -- 'b' is not constant | 188 | b = a + c + b -- 'b' is not constant |
| 189 | assert(a == 10 and b == 60 and c == 30) | 189 | assert(a == 10 and b == 60 and c == 30) |
| 190 | |||
| 190 | local function checkro (name, code) | 191 | local function checkro (name, code) |
| 191 | local st, msg = load(code) | 192 | local st, msg = load(code) |
| 192 | local gab = string.format("attempt to assign to const variable '%s'", name) | 193 | local gab = string.format("attempt to assign to const variable '%s'", name) |
| 193 | assert(not st and string.find(msg, gab)) | 194 | assert(not st and string.find(msg, gab)) |
| 194 | end | 195 | end |
| 196 | |||
| 195 | checkro("y", "local x, y <const>, z = 10, 20, 30; x = 11; y = 12") | 197 | checkro("y", "local x, y <const>, z = 10, 20, 30; x = 11; y = 12") |
| 196 | checkro("x", "local x <const>, y, z <const> = 10, 20, 30; x = 11") | 198 | checkro("x", "local x <const>, y, z <const> = 10, 20, 30; x = 11") |
| 197 | checkro("z", "local x <const>, y, z <const> = 10, 20, 30; y = 10; z = 11") | 199 | checkro("z", "local x <const>, y, z <const> = 10, 20, 30; y = 10; z = 11") |
| 198 | checkro("foo", "local foo <const> = 10; function foo() end") | 200 | checkro("foo", "local<const> foo = 10; function foo() end") |
| 199 | checkro("foo", "local foo <const> = {}; function foo() end") | 201 | checkro("foo", "local<const> foo <const> = {}; function foo() end") |
| 200 | checkro("foo", "global foo <const>; function foo() end") | 202 | checkro("foo", "global<const> foo <const>; function foo() end") |
| 201 | checkro("XX", "global XX <const>; XX = 10") | 203 | checkro("XX", "global XX <const>; XX = 10") |
| 202 | checkro("XX", "local _ENV; global XX <const>; XX = 10") | 204 | checkro("XX", "local _ENV; global XX <const>; XX = 10") |
| 203 | 205 | ||
| @@ -218,8 +220,18 @@ do -- constants | |||
| 218 | end | 220 | end |
| 219 | 221 | ||
| 220 | 222 | ||
| 223 | |||
| 221 | print"testing to-be-closed variables" | 224 | print"testing to-be-closed variables" |
| 222 | 225 | ||
| 226 | |||
| 227 | do | ||
| 228 | local st, msg = load("local <close> a, b") | ||
| 229 | assert(not st and string.find(msg, "multiple")) | ||
| 230 | |||
| 231 | local st, msg = load("local a<close>, b<close>") | ||
| 232 | assert(not st and string.find(msg, "multiple")) | ||
| 233 | end | ||
| 234 | |||
| 223 | local function stack(n) n = ((n == 0) or stack(n - 1)) end | 235 | local function stack(n) n = ((n == 0) or stack(n - 1)) end |
| 224 | 236 | ||
| 225 | local function func2close (f, x, y) | 237 | local function func2close (f, x, y) |
