New functions 'lua_resetthread' and 'coroutine.kill'
New functions to reset/kill a thread/coroutine, mainly (only?) to
close any pending to-be-closed variable. ('lua_resetthread' also
allows a thread to be reused...)
This commit is contained in:
@@ -119,6 +119,51 @@ end
|
||||
assert(#a == 25 and a[#a] == 97)
|
||||
x, a = nil
|
||||
|
||||
|
||||
-- coroutine kill
|
||||
do
|
||||
-- ok to kill a dead coroutine
|
||||
local co = coroutine.create(print)
|
||||
assert(coroutine.resume(co, "testing 'coroutine.kill'"))
|
||||
assert(coroutine.status(co) == "dead")
|
||||
assert(coroutine.kill(co))
|
||||
|
||||
-- cannot kill the running coroutine
|
||||
local st, msg = pcall(coroutine.kill, coroutine.running())
|
||||
assert(not st and string.find(msg, "running"))
|
||||
|
||||
local main = coroutine.running()
|
||||
|
||||
-- cannot kill a "normal" coroutine
|
||||
;(coroutine.wrap(function ()
|
||||
local st, msg = pcall(coroutine.kill, main)
|
||||
assert(not st and string.find(msg, "normal"))
|
||||
end))()
|
||||
|
||||
-- to-be-closed variables in coroutines
|
||||
local X
|
||||
co = coroutine.create(function ()
|
||||
local *toclose x = function (err) assert(err == nil); X = false end
|
||||
X = true
|
||||
coroutine.yield()
|
||||
end)
|
||||
coroutine.resume(co)
|
||||
assert(X)
|
||||
assert(coroutine.kill(co))
|
||||
assert(not X and coroutine.status(co) == "dead")
|
||||
|
||||
-- error killing a coroutine
|
||||
co = coroutine.create(function()
|
||||
local *toclose x = function (err) assert(err == nil); error(111) end
|
||||
coroutine.yield()
|
||||
end)
|
||||
coroutine.resume(co)
|
||||
local st, msg = coroutine.kill(co)
|
||||
assert(not st and coroutine.status(co) == "dead" and msg == 111)
|
||||
|
||||
end
|
||||
|
||||
|
||||
-- yielding across C boundaries
|
||||
|
||||
co = coroutine.wrap(function()
|
||||
|
||||
@@ -254,15 +254,15 @@ NoRun("error object is a table value", [[lua %s]], prog)
|
||||
|
||||
|
||||
-- chunk broken in many lines
|
||||
s = [=[ --
|
||||
function f ( x )
|
||||
s = [=[ --
|
||||
function f ( x )
|
||||
local a = [[
|
||||
xuxu
|
||||
]]
|
||||
local b = "\
|
||||
xuxu\n"
|
||||
if x == 11 then return 1 + 12 , 2 + 20 end --[[ test multiple returns ]]
|
||||
return x + 1
|
||||
return x + 1
|
||||
--\\
|
||||
end
|
||||
return( f( 100 ) )
|
||||
@@ -272,10 +272,10 @@ s = string.gsub(s, ' ', '\n\n') -- change all spaces for newlines
|
||||
prepfile(s)
|
||||
RUN([[lua -e"_PROMPT='' _PROMPT2=''" -i < %s > %s]], prog, out)
|
||||
checkprogout("101\n13\t22\n\n")
|
||||
|
||||
|
||||
prepfile[[#comment in 1st line without \n at the end]]
|
||||
RUN('lua %s', prog)
|
||||
|
||||
|
||||
prepfile[[#test line number when file starts with comment line
|
||||
debug = require"debug"
|
||||
print(debug.getinfo(1).currentline)
|
||||
@@ -306,6 +306,20 @@ NoRun("", "lua %s", prog) -- no message
|
||||
prepfile("os.exit(false, true)")
|
||||
NoRun("", "lua %s", prog) -- no message
|
||||
|
||||
|
||||
-- to-be-closed variables in main chunk
|
||||
prepfile[[
|
||||
local *toclose x = function (err)
|
||||
assert(err == 120)
|
||||
print("Ok")
|
||||
end
|
||||
local *toclose e1 = function () error(120) end
|
||||
os.exit(true, true)
|
||||
]]
|
||||
RUN('lua %s > %s', prog, out)
|
||||
checkprogout("Ok")
|
||||
|
||||
|
||||
-- remove temporary files
|
||||
assert(os.remove(prog))
|
||||
assert(os.remove(otherprog))
|
||||
|
||||
Reference in New Issue
Block a user