diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2018-12-13 13:07:53 -0200 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2018-12-13 13:07:53 -0200 |
| commit | fdc25a1ebfe9968dcec390dd556375105aa0be40 (patch) | |
| tree | 43759131636a501ec92475d453fd1a1c73bc8090 /testes | |
| parent | 3b06f983ae0e57b90cdeb500c84bb524e5c3635b (diff) | |
| download | lua-fdc25a1ebfe9968dcec390dd556375105aa0be40.tar.gz lua-fdc25a1ebfe9968dcec390dd556375105aa0be40.tar.bz2 lua-fdc25a1ebfe9968dcec390dd556375105aa0be40.zip | |
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...)
Diffstat (limited to 'testes')
| -rw-r--r-- | testes/coroutine.lua | 45 | ||||
| -rw-r--r-- | testes/main.lua | 24 |
2 files changed, 64 insertions, 5 deletions
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 | |||
| 119 | assert(#a == 25 and a[#a] == 97) | 119 | assert(#a == 25 and a[#a] == 97) |
| 120 | x, a = nil | 120 | x, a = nil |
| 121 | 121 | ||
| 122 | |||
| 123 | -- coroutine kill | ||
| 124 | do | ||
| 125 | -- ok to kill a dead coroutine | ||
| 126 | local co = coroutine.create(print) | ||
| 127 | assert(coroutine.resume(co, "testing 'coroutine.kill'")) | ||
| 128 | assert(coroutine.status(co) == "dead") | ||
| 129 | assert(coroutine.kill(co)) | ||
| 130 | |||
| 131 | -- cannot kill the running coroutine | ||
| 132 | local st, msg = pcall(coroutine.kill, coroutine.running()) | ||
| 133 | assert(not st and string.find(msg, "running")) | ||
| 134 | |||
| 135 | local main = coroutine.running() | ||
| 136 | |||
| 137 | -- cannot kill a "normal" coroutine | ||
| 138 | ;(coroutine.wrap(function () | ||
| 139 | local st, msg = pcall(coroutine.kill, main) | ||
| 140 | assert(not st and string.find(msg, "normal")) | ||
| 141 | end))() | ||
| 142 | |||
| 143 | -- to-be-closed variables in coroutines | ||
| 144 | local X | ||
| 145 | co = coroutine.create(function () | ||
| 146 | local *toclose x = function (err) assert(err == nil); X = false end | ||
| 147 | X = true | ||
| 148 | coroutine.yield() | ||
| 149 | end) | ||
| 150 | coroutine.resume(co) | ||
| 151 | assert(X) | ||
| 152 | assert(coroutine.kill(co)) | ||
| 153 | assert(not X and coroutine.status(co) == "dead") | ||
| 154 | |||
| 155 | -- error killing a coroutine | ||
| 156 | co = coroutine.create(function() | ||
| 157 | local *toclose x = function (err) assert(err == nil); error(111) end | ||
| 158 | coroutine.yield() | ||
| 159 | end) | ||
| 160 | coroutine.resume(co) | ||
| 161 | local st, msg = coroutine.kill(co) | ||
| 162 | assert(not st and coroutine.status(co) == "dead" and msg == 111) | ||
| 163 | |||
| 164 | end | ||
| 165 | |||
| 166 | |||
| 122 | -- yielding across C boundaries | 167 | -- yielding across C boundaries |
| 123 | 168 | ||
| 124 | co = coroutine.wrap(function() | 169 | 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) | |||
| 254 | 254 | ||
| 255 | 255 | ||
| 256 | -- chunk broken in many lines | 256 | -- chunk broken in many lines |
| 257 | s = [=[ -- | 257 | s = [=[ -- |
| 258 | function f ( x ) | 258 | function f ( x ) |
| 259 | local a = [[ | 259 | local a = [[ |
| 260 | xuxu | 260 | xuxu |
| 261 | ]] | 261 | ]] |
| 262 | local b = "\ | 262 | local b = "\ |
| 263 | xuxu\n" | 263 | xuxu\n" |
| 264 | if x == 11 then return 1 + 12 , 2 + 20 end --[[ test multiple returns ]] | 264 | if x == 11 then return 1 + 12 , 2 + 20 end --[[ test multiple returns ]] |
| 265 | return x + 1 | 265 | return x + 1 |
| 266 | --\\ | 266 | --\\ |
| 267 | end | 267 | end |
| 268 | return( f( 100 ) ) | 268 | return( f( 100 ) ) |
| @@ -272,10 +272,10 @@ s = string.gsub(s, ' ', '\n\n') -- change all spaces for newlines | |||
| 272 | prepfile(s) | 272 | prepfile(s) |
| 273 | RUN([[lua -e"_PROMPT='' _PROMPT2=''" -i < %s > %s]], prog, out) | 273 | RUN([[lua -e"_PROMPT='' _PROMPT2=''" -i < %s > %s]], prog, out) |
| 274 | checkprogout("101\n13\t22\n\n") | 274 | checkprogout("101\n13\t22\n\n") |
| 275 | 275 | ||
| 276 | prepfile[[#comment in 1st line without \n at the end]] | 276 | prepfile[[#comment in 1st line without \n at the end]] |
| 277 | RUN('lua %s', prog) | 277 | RUN('lua %s', prog) |
| 278 | 278 | ||
| 279 | prepfile[[#test line number when file starts with comment line | 279 | prepfile[[#test line number when file starts with comment line |
| 280 | debug = require"debug" | 280 | debug = require"debug" |
| 281 | print(debug.getinfo(1).currentline) | 281 | print(debug.getinfo(1).currentline) |
| @@ -306,6 +306,20 @@ NoRun("", "lua %s", prog) -- no message | |||
| 306 | prepfile("os.exit(false, true)") | 306 | prepfile("os.exit(false, true)") |
| 307 | NoRun("", "lua %s", prog) -- no message | 307 | NoRun("", "lua %s", prog) -- no message |
| 308 | 308 | ||
| 309 | |||
| 310 | -- to-be-closed variables in main chunk | ||
| 311 | prepfile[[ | ||
| 312 | local *toclose x = function (err) | ||
| 313 | assert(err == 120) | ||
| 314 | print("Ok") | ||
| 315 | end | ||
| 316 | local *toclose e1 = function () error(120) end | ||
| 317 | os.exit(true, true) | ||
| 318 | ]] | ||
| 319 | RUN('lua %s > %s', prog, out) | ||
| 320 | checkprogout("Ok") | ||
| 321 | |||
| 322 | |||
| 309 | -- remove temporary files | 323 | -- remove temporary files |
| 310 | assert(os.remove(prog)) | 324 | assert(os.remove(prog)) |
| 311 | assert(os.remove(otherprog)) | 325 | assert(os.remove(otherprog)) |
