summaryrefslogtreecommitdiff
path: root/testes/locals.lua
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2020-12-29 10:23:02 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2020-12-29 10:23:02 -0300
commit59e565d9555c07e82808d8c1db8f4f4d159b5e5c (patch)
tree7dced733d2e01a6060d656afad903539ac2fedc9 /testes/locals.lua
parent6188f3a654c0380db08eb40a5465ce8e71c784f5 (diff)
downloadlua-github-59e565d9555c07e82808d8c1db8f4f4d159b5e5c.tar.gz
No need to recheck close method before calling it
A to-be-closed variable is constant and it must have a close metamethod when it is created. A program has to go out of its way (e.g., by changing the variable's metamethod) to invalidate that check. So, it is not worth to test that again. If the program tampers with the metamethod, Lua will raise a regular error when attempting to call it.
Diffstat (limited to 'testes/locals.lua')
-rw-r--r--testes/locals.lua46
1 files changed, 44 insertions, 2 deletions
diff --git a/testes/locals.lua b/testes/locals.lua
index 84e0b03a..1b43609b 100644
--- a/testes/locals.lua
+++ b/testes/locals.lua
@@ -459,8 +459,50 @@ do -- errors due to non-closable values
getmetatable(xyz).__close = nil -- remove metamethod
end
local stat, msg = pcall(foo)
- assert(not stat and
- string.find(msg, "attempt to close non%-closable variable 'xyz'"))
+ assert(not stat and string.find(msg, "attempt to call a nil value"))
+end
+
+
+do -- tbc inside close methods
+ local track = {}
+ local function foo ()
+ local x <close> = func2close(function ()
+ local xx <close> = func2close(function (_, msg)
+ assert(msg == nil)
+ track[#track + 1] = "xx"
+ end)
+ track[#track + 1] = "x"
+ end)
+ track[#track + 1] = "foo"
+ return 20, 30, 40
+ end
+ local a, b, c, d = foo()
+ assert(a == 20 and b == 30 and c == 40 and d == nil)
+ assert(track[1] == "foo" and track[2] == "x" and track[3] == "xx")
+
+ -- again, with errors
+ local track = {}
+ local function foo ()
+ local x0 <close> = func2close(function (_, msg)
+ assert(msg == 202)
+ track[#track + 1] = "x0"
+ end)
+ local x <close> = func2close(function ()
+ local xx <close> = func2close(function (_, msg)
+ assert(msg == 101)
+ track[#track + 1] = "xx"
+ error(202)
+ end)
+ track[#track + 1] = "x"
+ error(101)
+ end)
+ track[#track + 1] = "foo"
+ return 20, 30, 40
+ end
+ local st, msg = pcall(foo)
+ assert(not st and msg == 202)
+ assert(track[1] == "foo" and track[2] == "x" and track[3] == "xx" and
+ track[4] == "x0")
end