aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilipp Janda <siffiejoe@gmx.net>2015-04-20 13:41:20 +0200
committerPhilipp Janda <siffiejoe@gmx.net>2015-04-20 13:41:20 +0200
commita7d337f0d1ccfde974a003993387a42b2daabfed (patch)
treeb5077d49ecb86a488697f533bb05ef76676fd343
parentba8686d6af119fc7ecf734a2d2b6e319c8d74df5 (diff)
downloadlua-compat-5.3-a7d337f0d1ccfde974a003993387a42b2daabfed.tar.gz
lua-compat-5.3-a7d337f0d1ccfde974a003993387a42b2daabfed.tar.bz2
lua-compat-5.3-a7d337f0d1ccfde974a003993387a42b2daabfed.zip
Make coroutine mappings weak (and document them).
(x)pcall uses coroutines internally to allow yielding. The internal mapping tables for those coroutines (and the initial coroutine that started the pcall) should not keep the threads alive if the (x)pcall yields and is never resumed again.
-rw-r--r--compat53.lua25
1 files changed, 18 insertions, 7 deletions
diff --git a/compat53.lua b/compat53.lua
index 84ef101..919ab33 100644
--- a/compat53.lua
+++ b/compat53.lua
@@ -328,12 +328,21 @@ if lua_version < "5.3" then
328 #setmetatable({}, { __len = function() return 1 end }) == 1 328 #setmetatable({}, { __len = function() return 1 end }) == 1
329 329
330 330
331 -- table that maps each running coroutine to the coroutine that resumed it 331 -- the (x)pcall implementations start a new coroutine internally
332 -- this is used to build complete tracebacks when "coroutine-friendly" pcall 332 -- to allow yielding even in Lua 5.1. to allow for accurate
333 -- is used. 333 -- stack traces we keep track of the nested coroutine activations
334 local pcall_previous = {} 334 -- in the weak tables below:
335 local pcall_callOf = {} 335 local weak_meta = { __mode = "kv" }
336 local xpcall_running = {} 336 -- table that maps each running coroutine started by pcall to
337 -- the coroutine that resumed it (user coroutine *or* pcall
338 -- coroutine!)
339 local pcall_previous = setmetatable({}, weak_meta)
340 -- reverse of `pcall_mainOf`. maps a user coroutine to the
341 -- currently active pcall coroutine started within it
342 local pcall_callOf = setmetatable({}, weak_meta)
343 -- similar to `pcall_mainOf` but is used only while executing
344 -- the error handler of xpcall (thus no nesting is necessary!)
345 local xpcall_running = setmetatable({}, weak_meta)
337 local coroutine_running = coroutine.running 346 local coroutine_running = coroutine.running
338 347
339 -- handle debug functions 348 -- handle debug functions
@@ -618,7 +627,9 @@ if lua_version < "5.3" then
618 return result 627 return result
619 end 628 end
620 629
621 local pcall_mainOf = {} 630 -- maps the internal pcall coroutines to the user coroutine that
631 -- *should* be running if pcall didn't use coroutines internally
632 local pcall_mainOf = setmetatable({}, weak_meta)
622 633
623 if not is_luajit52 then 634 if not is_luajit52 then
624 function coroutine.running() 635 function coroutine.running()