From 511d53a826760dd11cd82947184583e2d094e2d2 Mon Sep 17 00:00:00 2001
From: Roberto Ierusalimschy <roberto@inf.puc-rio.br>
Date: Tue, 9 Mar 2021 11:42:45 -0300
Subject: lua_settop/lua_pop closes to-be-closed variables

The existence of 'lua_closeslot' is no reason for lua_pop not to close
to-be-closed variables too.  It is too error-prone for lua_pop not to
close tbc variables being popped from the stack.
---
 testes/api.lua | 26 +++++++++++++++++++++++++-
 1 file changed, 25 insertions(+), 1 deletion(-)

(limited to 'testes')

diff --git a/testes/api.lua b/testes/api.lua
index fb7e7085..c1bcb4b7 100644
--- a/testes/api.lua
+++ b/testes/api.lua
@@ -1130,7 +1130,7 @@ do
   -- closing resources with 'closeslot'
   _ENV.xxx = true
   local a = T.testC([[
-    pushvalue 2  # stack: S, NR, CH
+    pushvalue 2  # stack: S, NR, CH, NR
     call 0 1   # create resource; stack: S, NR, CH, R
     toclose -1 # mark it to be closed
     pushvalue 2  #  stack: S, NR, CH, R, NR
@@ -1151,6 +1151,30 @@ do
   ]], newresource, check)
   assert(a == 3 and _ENV.xxx == nil)   -- no extra items left in the stack
 
+  -- closing resources with 'pop'
+  local a = T.testC([[
+    pushvalue 2  # stack: S, NR, CH, NR
+    call 0 1   # create resource; stack: S, NR, CH, R
+    toclose -1 # mark it to be closed
+    pushvalue 2  #  stack: S, NR, CH, R, NR
+    call 0 1   # create another resource; stack: S, NR, CH, R, R
+    toclose -1 # mark it to be closed
+    pushvalue 3  # stack: S, NR, CH, R, R, CH
+    pushint 2   # there should be two open resources
+    call 1 0  #  stack: S, NR, CH, R, R
+    pop 1   # pop second resource
+    pushvalue 3  # stack: S, NR, CH, R, CH
+    pushint 1   # there should be one open resource
+    call 1 0  # stack: S, NR, CH, R
+    pop 1       # pop other resource from the stack
+    pushvalue 3  # stack: S, NR, CH, CH
+    pushint 0   # there should be no open resources
+    call 1 0  # stack: S, NR, CH
+    pushint *
+    return 1    # return stack size
+  ]], newresource, check)
+  assert(a == 3)   -- no extra items left in the stack
+
   -- non-closable value
   local a, b = pcall(T.makeCfunc[[
     pushint 32
-- 
cgit v1.2.3-55-g6feb