From fdc25a1ebfe9968dcec390dd556375105aa0be40 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 13 Dec 2018 13:07:53 -0200 Subject: 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...) --- testes/coroutine.lua | 45 +++++++++++++++++++++++++++++++++++++++++++++ testes/main.lua | 24 +++++++++++++++++++----- 2 files changed, 64 insertions(+), 5 deletions(-) (limited to 'testes') diff --git a/testes/coroutine.lua b/testes/coroutine.lua index 7d42eadd..5674a4dd 100644 --- a/testes/coroutine.lua +++ b/testes/coroutine.lua @@ -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() diff --git a/testes/main.lua b/testes/main.lua index c7bde0d9..b9dcab1c 100644 --- a/testes/main.lua +++ b/testes/main.lua @@ -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)) -- cgit v1.2.3-55-g6feb