aboutsummaryrefslogtreecommitdiff
path: root/testes/api.lua
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2019-07-31 10:43:51 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2019-07-31 10:43:51 -0300
commitf645d3157372c73573dff221c5b26691cb0e7d56 (patch)
tree61adb1f332bbd8c0c0365b81cef8de47fa2ea06a /testes/api.lua
parent35b4efc270db2418bc2cac6671575a45028061c3 (diff)
downloadlua-f645d3157372c73573dff221c5b26691cb0e7d56.tar.gz
lua-f645d3157372c73573dff221c5b26691cb0e7d56.tar.bz2
lua-f645d3157372c73573dff221c5b26691cb0e7d56.zip
To-be-closed variables must be closed on initialization
When initializing a to-be-closed variable, check whether it has a '__close' metamethod (or is a false value) and raise an error if if it hasn't. This produces more accurate error messages. (The check before closing still need to be done: in the C API, the value is not constant; and the object may lose its '__close' metamethod during the block.)
Diffstat (limited to 'testes/api.lua')
-rw-r--r--testes/api.lua11
1 files changed, 10 insertions, 1 deletions
diff --git a/testes/api.lua b/testes/api.lua
index 3f7f7596..f6915c3e 100644
--- a/testes/api.lua
+++ b/testes/api.lua
@@ -1096,7 +1096,7 @@ do
1096 assert(type(a[1]) == "string" and a[2][1] == 11) 1096 assert(type(a[1]) == "string" and a[2][1] == 11)
1097 assert(#openresource == 0) -- was closed 1097 assert(#openresource == 0) -- was closed
1098 1098
1099 -- error 1099 -- closing by error
1100 local a, b = pcall(T.makeCfunc[[ 1100 local a, b = pcall(T.makeCfunc[[
1101 call 0 1 # create resource 1101 call 0 1 # create resource
1102 toclose -1 # mark it to be closed 1102 toclose -1 # mark it to be closed
@@ -1105,6 +1105,15 @@ do
1105 assert(a == false and b[1] == 11) 1105 assert(a == false and b[1] == 11)
1106 assert(#openresource == 0) -- was closed 1106 assert(#openresource == 0) -- was closed
1107 1107
1108 -- non-closable value
1109 local a, b = pcall(T.makeCfunc[[
1110 newtable # create non-closable object
1111 toclose -1 # mark it to be closed (shoud raise an error)
1112 abort # will not be executed
1113 ]])
1114 assert(a == false and
1115 string.find(b, "non%-closable value"))
1116
1108 local function check (n) 1117 local function check (n)
1109 assert(#openresource == n) 1118 assert(#openresource == n)
1110 end 1119 end