diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2021-01-26 16:53:51 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2021-01-26 16:53:51 -0300 |
commit | 58aa09a0b91cf81779d6710d7f9d855bb9d3712f (patch) | |
tree | 56adab5aab1307791b7e485677a41203d7369a6c /testes | |
parent | 1f81baffadad9d955b030a1a29b9b06042a66552 (diff) | |
download | lua-58aa09a0b91cf81779d6710d7f9d855bb9d3712f.tar.gz lua-58aa09a0b91cf81779d6710d7f9d855bb9d3712f.tar.bz2 lua-58aa09a0b91cf81779d6710d7f9d855bb9d3712f.zip |
Small improvements in hooks
- 'L->top' is set once in 'luaD_hook', instead of being set in
'luaD_hookcall' and 'rethook';
- resume discard arguments when returning after an yield inside a hook
(arguments may interfere with the coroutine stack);
- yield inside a hook asserts it has no arguments.
Diffstat (limited to 'testes')
-rw-r--r-- | testes/coroutine.lua | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/testes/coroutine.lua b/testes/coroutine.lua index fbeabd07..b36b76ea 100644 --- a/testes/coroutine.lua +++ b/testes/coroutine.lua | |||
@@ -498,6 +498,28 @@ else | |||
498 | 498 | ||
499 | assert(B // A == 7) -- fact(7) // fact(6) | 499 | assert(B // A == 7) -- fact(7) // fact(6) |
500 | 500 | ||
501 | do -- hooks vs. multiple values | ||
502 | local done | ||
503 | local function test (n) | ||
504 | done = false | ||
505 | return coroutine.wrap(function () | ||
506 | local a = {} | ||
507 | for i = 1, n do a[i] = i end | ||
508 | -- 'pushint' just to perturb the stack | ||
509 | T.sethook("pushint 10; yield 0", "", 1) -- yield at each op. | ||
510 | local a1 = {table.unpack(a)} -- must keep top between ops. | ||
511 | assert(#a1 == n) | ||
512 | for i = 1, n do assert(a[i] == i) end | ||
513 | done = true | ||
514 | end) | ||
515 | end | ||
516 | -- arguments to the coroutine are just to perturb its stack | ||
517 | local co = test(0); while not done do co(30) end | ||
518 | co = test(1); while not done do co(20, 10) end | ||
519 | co = test(3); while not done do co() end | ||
520 | co = test(100); while not done do co() end | ||
521 | end | ||
522 | |||
501 | local line = debug.getinfo(1, "l").currentline + 2 -- get line number | 523 | local line = debug.getinfo(1, "l").currentline + 2 -- get line number |
502 | local function foo () | 524 | local function foo () |
503 | local x = 10 --<< this line is 'line' | 525 | local x = 10 --<< this line is 'line' |