diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2018-11-07 10:03:05 -0200 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2018-11-07 10:03:05 -0200 |
| commit | b8fed93215a23a3f443c5b0126f0de1725771b44 (patch) | |
| tree | 1d7d29388b8a20fb7d0920d94b02a8040612314d /testes/locals.lua | |
| parent | 5e76a4fd313a8690d300085c4e8fcb9dca50c01a (diff) | |
| download | lua-b8fed93215a23a3f443c5b0126f0de1725771b44.tar.gz lua-b8fed93215a23a3f443c5b0126f0de1725771b44.tar.bz2 lua-b8fed93215a23a3f443c5b0126f0de1725771b44.zip | |
New syntax for to-be-closed variables
The new syntax is <local *toclose x = f()>. The mark '*' allows other
attributes to be added later without the need of new keywords; it
also allows better error messages. The API function was also renamed
('lua_tobeclosed' -> 'lua_toclose').
Diffstat (limited to 'testes/locals.lua')
| -rw-r--r-- | testes/locals.lua | 42 |
1 files changed, 21 insertions, 21 deletions
diff --git a/testes/locals.lua b/testes/locals.lua index 1e0f525b..869ac1ff 100644 --- a/testes/locals.lua +++ b/testes/locals.lua | |||
| @@ -181,9 +181,9 @@ local function stack(n) n = ((n == 0) or stack(n - 1)) end | |||
| 181 | do | 181 | do |
| 182 | local a = {} | 182 | local a = {} |
| 183 | do | 183 | do |
| 184 | local scoped x = setmetatable({"x"}, {__close = function (self) | 184 | local *toclose x = setmetatable({"x"}, {__close = function (self) |
| 185 | a[#a + 1] = self[1] end}) | 185 | a[#a + 1] = self[1] end}) |
| 186 | local scoped y = function (x) assert(x == nil); a[#a + 1] = "y" end | 186 | local *toclose y = function (x) assert(x == nil); a[#a + 1] = "y" end |
| 187 | a[#a + 1] = "in" | 187 | a[#a + 1] = "in" |
| 188 | end | 188 | end |
| 189 | a[#a + 1] = "out" | 189 | a[#a + 1] = "out" |
| @@ -197,7 +197,7 @@ do | |||
| 197 | 197 | ||
| 198 | -- closing functions do not corrupt returning values | 198 | -- closing functions do not corrupt returning values |
| 199 | local function foo (x) | 199 | local function foo (x) |
| 200 | local scoped _ = closescope | 200 | local *toclose _ = closescope |
| 201 | return x, X, 23 | 201 | return x, X, 23 |
| 202 | end | 202 | end |
| 203 | 203 | ||
| @@ -206,7 +206,7 @@ do | |||
| 206 | 206 | ||
| 207 | X = false | 207 | X = false |
| 208 | foo = function (x) | 208 | foo = function (x) |
| 209 | local scoped _ = closescope | 209 | local *toclose _ = closescope |
| 210 | local y = 15 | 210 | local y = 15 |
| 211 | return y | 211 | return y |
| 212 | end | 212 | end |
| @@ -215,7 +215,7 @@ do | |||
| 215 | 215 | ||
| 216 | X = false | 216 | X = false |
| 217 | foo = function () | 217 | foo = function () |
| 218 | local scoped x = closescope | 218 | local *toclose x = closescope |
| 219 | return x | 219 | return x |
| 220 | end | 220 | end |
| 221 | 221 | ||
| @@ -228,13 +228,13 @@ do | |||
| 228 | -- to-be-closed variables must be closed in tail calls | 228 | -- to-be-closed variables must be closed in tail calls |
| 229 | local X, Y | 229 | local X, Y |
| 230 | local function foo () | 230 | local function foo () |
| 231 | local scoped _ = function () Y = 10 end | 231 | local *toclose _ = function () Y = 10 end |
| 232 | assert(X == 20 and Y == nil) | 232 | assert(X == 20 and Y == nil) |
| 233 | return 1,2,3 | 233 | return 1,2,3 |
| 234 | end | 234 | end |
| 235 | 235 | ||
| 236 | local function bar () | 236 | local function bar () |
| 237 | local scoped _ = function () X = 20 end | 237 | local *toclose _ = function () X = 20 end |
| 238 | return foo() | 238 | return foo() |
| 239 | end | 239 | end |
| 240 | 240 | ||
| @@ -245,11 +245,11 @@ end | |||
| 245 | do -- errors in __close | 245 | do -- errors in __close |
| 246 | local log = {} | 246 | local log = {} |
| 247 | local function foo (err) | 247 | local function foo (err) |
| 248 | local scoped x = function (msg) log[#log + 1] = msg; error(1) end | 248 | local *toclose x = function (msg) log[#log + 1] = msg; error(1) end |
| 249 | local scoped x1 = function (msg) log[#log + 1] = msg; end | 249 | local *toclose x1 = function (msg) log[#log + 1] = msg; end |
| 250 | local scoped gc = function () collectgarbage() end | 250 | local *toclose gc = function () collectgarbage() end |
| 251 | local scoped y = function (msg) log[#log + 1] = msg; error(2) end | 251 | local *toclose y = function (msg) log[#log + 1] = msg; error(2) end |
| 252 | local scoped z = function (msg) log[#log + 1] = msg or 10; error(3) end | 252 | local *toclose z = function (msg) log[#log + 1] = msg or 10; error(3) end |
| 253 | if err then error(4) end | 253 | if err then error(4) end |
| 254 | end | 254 | end |
| 255 | local stat, msg = pcall(foo, false) | 255 | local stat, msg = pcall(foo, false) |
| @@ -267,8 +267,8 @@ end | |||
| 267 | if rawget(_G, "T") then | 267 | if rawget(_G, "T") then |
| 268 | -- memory error inside closing function | 268 | -- memory error inside closing function |
| 269 | local function foo () | 269 | local function foo () |
| 270 | local scoped y = function () T.alloccount() end | 270 | local *toclose y = function () T.alloccount() end |
| 271 | local scoped x = setmetatable({}, {__close = function () | 271 | local *toclose x = setmetatable({}, {__close = function () |
| 272 | T.alloccount(0); local x = {} -- force a memory error | 272 | T.alloccount(0); local x = {} -- force a memory error |
| 273 | end}) | 273 | end}) |
| 274 | error("a") -- common error inside the function's body | 274 | error("a") -- common error inside the function's body |
| @@ -294,7 +294,7 @@ if rawget(_G, "T") then | |||
| 294 | end | 294 | end |
| 295 | 295 | ||
| 296 | local function test () | 296 | local function test () |
| 297 | local scoped x = enter(0) -- set a memory limit | 297 | local *toclose x = enter(0) -- set a memory limit |
| 298 | -- creation of previous upvalue will raise a memory error | 298 | -- creation of previous upvalue will raise a memory error |
| 299 | os.exit(false) -- should not run | 299 | os.exit(false) -- should not run |
| 300 | end | 300 | end |
| @@ -309,14 +309,14 @@ if rawget(_G, "T") then | |||
| 309 | 309 | ||
| 310 | -- repeat test with extra closing upvalues | 310 | -- repeat test with extra closing upvalues |
| 311 | local function test () | 311 | local function test () |
| 312 | local scoped xxx = function (msg) | 312 | local *toclose xxx = function (msg) |
| 313 | assert(msg == "not enough memory"); | 313 | assert(msg == "not enough memory"); |
| 314 | error(1000) -- raise another error | 314 | error(1000) -- raise another error |
| 315 | end | 315 | end |
| 316 | local scoped xx = function (msg) | 316 | local *toclose xx = function (msg) |
| 317 | assert(msg == "not enough memory"); | 317 | assert(msg == "not enough memory"); |
| 318 | end | 318 | end |
| 319 | local scoped x = enter(0) -- set a memory limit | 319 | local *toclose x = enter(0) -- set a memory limit |
| 320 | -- creation of previous upvalue will raise a memory error | 320 | -- creation of previous upvalue will raise a memory error |
| 321 | os.exit(false) -- should not run | 321 | os.exit(false) -- should not run |
| 322 | end | 322 | end |
| @@ -333,9 +333,9 @@ do | |||
| 333 | local x = false | 333 | local x = false |
| 334 | local y = false | 334 | local y = false |
| 335 | local co = coroutine.create(function () | 335 | local co = coroutine.create(function () |
| 336 | local scoped xv = function () x = true end | 336 | local *toclose xv = function () x = true end |
| 337 | do | 337 | do |
| 338 | local scoped yv = function () y = true end | 338 | local *toclose yv = function () y = true end |
| 339 | coroutine.yield(100) -- yield doesn't close variable | 339 | coroutine.yield(100) -- yield doesn't close variable |
| 340 | end | 340 | end |
| 341 | coroutine.yield(200) -- yield doesn't close variable | 341 | coroutine.yield(200) -- yield doesn't close variable |
| @@ -353,7 +353,7 @@ end | |||
| 353 | -- a suspended coroutine should not close its variables when collected | 353 | -- a suspended coroutine should not close its variables when collected |
| 354 | local co | 354 | local co |
| 355 | co = coroutine.wrap(function() | 355 | co = coroutine.wrap(function() |
| 356 | local scoped x = function () os.exit(false) end -- should not run | 356 | local *toclose x = function () os.exit(false) end -- should not run |
| 357 | co = nil | 357 | co = nil |
| 358 | coroutine.yield() | 358 | coroutine.yield() |
| 359 | end) | 359 | end) |
