diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-07-03 14:18:07 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-07-03 14:18:07 -0300 |
| commit | 4d46289331395a845c5de1f6c0e0fe873c50db4f (patch) | |
| tree | 96649174d3c13830549ad026af0133e0b789d411 /testes | |
| parent | 8eca21c2e85625390a2a3b08c231e75e315980b0 (diff) | |
| download | lua-4d46289331395a845c5de1f6c0e0fe873c50db4f.tar.gz lua-4d46289331395a845c5de1f6c0e0fe873c50db4f.tar.bz2 lua-4d46289331395a845c5de1f6c0e0fe873c50db4f.zip | |
Local attributes can be used in list of local variables
The syntax for local attributes ('const'/'toclose') was unified with
the regular syntax for local variables, so that we can have variables
with attributes in local definitions with multiple names; for instance:
local <toclose> f, <const> err = io.open(fname)
This new syntax does not implement constant propagation, yet.
This commit also has some small improvements to the manual.
Diffstat (limited to 'testes')
| -rw-r--r-- | testes/locals.lua | 34 |
1 files changed, 28 insertions, 6 deletions
diff --git a/testes/locals.lua b/testes/locals.lua index a41b6f0e..50230a27 100644 --- a/testes/locals.lua +++ b/testes/locals.lua | |||
| @@ -173,12 +173,32 @@ end | |||
| 173 | assert(x==20) | 173 | assert(x==20) |
| 174 | 174 | ||
| 175 | 175 | ||
| 176 | do -- constants | ||
| 177 | local <const> a, b, <const> c = 10, 20, 30 | ||
| 178 | b = a + c + b -- 'b' is not constant | ||
| 179 | assert(a == 10 and b == 60 and c == 30) | ||
| 180 | local function checkro (code, name) | ||
| 181 | local st, msg = load(code) | ||
| 182 | local gab = string.format("attempt to assign to const variable '%s'", name) | ||
| 183 | assert(not st and string.find(msg, gab)) | ||
| 184 | end | ||
| 185 | checkro("local x, <const> y, z = 10, 20, 30; x = 11; y = 12", "y") | ||
| 186 | checkro("local <const> x, y, <const> z = 10, 20, 30; x = 11", "x") | ||
| 187 | checkro("local <const> x, y, <const> z = 10, 20, 30; y = 10; z = 11", "z") | ||
| 188 | end | ||
| 189 | |||
| 190 | |||
| 176 | print"testing to-be-closed variables" | 191 | print"testing to-be-closed variables" |
| 177 | 192 | ||
| 178 | local function stack(n) n = ((n == 0) or stack(n - 1)) end | 193 | local function stack(n) n = ((n == 0) or stack(n - 1)) end |
| 179 | 194 | ||
| 180 | local function func2close (f) | 195 | local function func2close (f, x, y) |
| 181 | return setmetatable({}, {__close = f}) | 196 | local obj = setmetatable({}, {__close = f}) |
| 197 | if x then | ||
| 198 | return x, obj, y | ||
| 199 | else | ||
| 200 | return obj | ||
| 201 | end | ||
| 182 | end | 202 | end |
| 183 | 203 | ||
| 184 | 204 | ||
| @@ -187,10 +207,11 @@ do | |||
| 187 | do | 207 | do |
| 188 | local <toclose> x = setmetatable({"x"}, {__close = function (self) | 208 | local <toclose> x = setmetatable({"x"}, {__close = function (self) |
| 189 | a[#a + 1] = self[1] end}) | 209 | a[#a + 1] = self[1] end}) |
| 190 | local <toclose> y = func2close(function (self, err) | 210 | local w, <toclose> y, z = func2close(function (self, err) |
| 191 | assert(err == nil); a[#a + 1] = "y" | 211 | assert(err == nil); a[#a + 1] = "y" |
| 192 | end) | 212 | end, 10, 20) |
| 193 | a[#a + 1] = "in" | 213 | a[#a + 1] = "in" |
| 214 | assert(w == 10 and z == 20) | ||
| 194 | end | 215 | end |
| 195 | a[#a + 1] = "out" | 216 | a[#a + 1] = "out" |
| 196 | assert(a[1] == "in" and a[2] == "y" and a[3] == "x" and a[4] == "out") | 217 | assert(a[1] == "in" and a[2] == "y" and a[3] == "x" and a[4] == "out") |
| @@ -199,7 +220,8 @@ end | |||
| 199 | do | 220 | do |
| 200 | local X = false | 221 | local X = false |
| 201 | 222 | ||
| 202 | local closescope = func2close(function () stack(10); X = true end) | 223 | local x, closescope = func2close(function () stack(10); X = true end, 100) |
| 224 | assert(x == 100); x = 101; -- 'x' is not read-only | ||
| 203 | 225 | ||
| 204 | -- closing functions do not corrupt returning values | 226 | -- closing functions do not corrupt returning values |
| 205 | local function foo (x) | 227 | local function foo (x) |
