From 6ccd24eff58340c00db2877c4558a63c6b859442 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 19 Jan 2021 10:03:13 -0300 Subject: Simpler handling of errors when creating tbc variables New field 'lua_State.ptbc' keeps to-be-closed variable until its upvalue is created, so that it can be closed in case of a memory-allocation error. --- testes/locals.lua | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) (limited to 'testes') diff --git a/testes/locals.lua b/testes/locals.lua index 8506195e..24a95d18 100644 --- a/testes/locals.lua +++ b/testes/locals.lua @@ -539,15 +539,17 @@ if rawget(_G, "T") then local _, msg = pcall(foo) assert(msg == "not enough memory") + local closemsg local close = func2close(function (self, msg) T.alloccount() - assert(msg == "not enough memory") + closemsg = msg end) -- set a memory limit and return a closing object to remove the limit local function enter (count) stack(10) -- reserve some stack space T.alloccount(count) + closemsg = nil return close end @@ -558,12 +560,7 @@ if rawget(_G, "T") then end local _, msg = pcall(test) - assert(msg == "not enough memory") - - -- now use metamethod for closing - close = setmetatable({}, {__close = function () - T.alloccount() - end}) + assert(msg == "not enough memory" and closemsg == "not enough memory") -- repeat test with extra closing upvalues local function test () @@ -580,7 +577,7 @@ if rawget(_G, "T") then end local _, msg = pcall(test) - assert(msg == 1000) + assert(msg == 1000 and closemsg == "not enough memory") do -- testing 'toclose' in C string buffer collectgarbage() -- cgit v1.2.3-55-g6feb