Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fe04063

Browse files
committed
Tracebacks recognize metamethods '__close'
1 parent f645d31 commit fe04063

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

ldebug.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,9 @@ static const char *funcnamefromcode (lua_State *L, CallInfo *ci,
651651
case OP_SHRI: case OP_SHLI:
652652
*name = "shift";
653653
return "metamethod";
654+
case OP_CLOSE: case OP_RETURN:
655+
*name = "close";
656+
return "metamethod";
654657
default:
655658
return NULL; /* cannot find a reasonable name */
656659
}

testes/locals.lua

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -316,14 +316,27 @@ do -- errors in __close
316316
assert(log[1] == 5 and log[2] == 4 and log[3] == 4 and log[4] == 4
317317
and #log == 4)
318318

319+
-- error leaving a block
320+
local function foo (...)
321+
do
322+
local x1 <close> = func2close(function () error("Y") end)
323+
local x123 <close> = func2close(function () error("X") end)
324+
end
325+
end
326+
327+
local st, msg = xpcall(foo, debug.traceback)
328+
assert(string.match(msg, "^[^ ]* X"))
329+
assert(string.find(msg, "in metamethod 'close'"))
330+
319331
-- error in toclose in vararg function
320-
function foo (...)
321-
local x123 <close> = 10
332+
local function foo (...)
333+
local x123 <close> = func2close(function () error("X") end)
322334
end
323335

324-
local st, msg = pcall(foo)
325-
assert(string.find(msg, "'x123'"))
336+
local st, msg = xpcall(foo, debug.traceback)
337+
assert(string.match(msg, "^[^ ]* X"))
326338

339+
assert(string.find(msg, "in metamethod 'close'"))
327340
end
328341

329342

0 commit comments

Comments
 (0)