Skip to content

Commit bd553f2

Browse files
committed
Fixed tail calls being problematic.
1 parent 23fc27d commit bd553f2

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/functions.lua2p

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2033,15 +2033,21 @@ end
20332033
-- context = getContext( [ requiredContextNames=any ] )
20342034
function _G.getContext(ctxName)
20352035
for level = 3, 1/0 do -- Assume the caller is a webgen function, so start at level 3.
2036-
local ctx = getfenv(level) -- We shouldn't reach an invalid level because pretty much the whole program runs in a context.
2036+
if not debug.getinfo(level, "") then break end
2037+
2038+
-- Note: We shouldn't reach an invalid level because pretty much the
2039+
-- whole program runs in a 'none' context. However, if a level is a
2040+
-- tail call then getfenv() will raise an error.
2041+
local ok, ctx = pcall(getfenv, level)
2042+
if not ok then ctx = nil end
20372043

20382044
if isContext(ctx) then
20392045
if ctxName then _assertContext(ctx, ctxName, nil, 2) end
20402046
return ctx
20412047
end
20422048
end
20432049

2044-
error("Internal error: There is no context.", 2) -- We should never get here as getfenv() should raise an error first.
2050+
error("Internal error: There is no context.", 2)
20452051
end
20462052

20472053
function _G.isContext(v)

0 commit comments

Comments
 (0)