Coroutine Concepts
Completion Status
After a coroutine finishes, its status becomes complete rather than suspended.
finished state
Lua reports a completed coroutine as `dead`; this page models the final label without creating a thread object.
Completion Status
completion_status.lua
Replay: real traced execution (multi-file project)
local target = 3
local step = 0
local status = "suspended"
while step < target do
step = step + 1
status = "yielded"
end
if step == target then
status = "dead"
end
print("target=" .. target)
print("step=" .. step)
print("status=" .. status)
local target = 1
local step = 0
local status = "suspended"
while step < target do
step = step + 1
status = "yielded"
end
if step == target then
status = "dead"
end
print("target=" .. target)
print("step=" .. step)
print("status=" .. status)
local target = 2
local step = 0
local status = "suspended"
while step < target do
step = step + 1
status = "yielded"
end
if step == target then
status = "dead"
end
print("target=" .. target)
print("step=" .. step)
print("status=" .. status)
target ← 3, step ← 0, status ← suspended
1local target→ 3 = 3 --@target=1, 22local step→ 0 = 03local status→ suspended = "suspended"step ← 1, status ← yielded
pass 1 of 35while step0 < target3 do6 step→ 1 = step + 17 status→ yielded = "yielded"8endAll 3 passes — pass 1 is the card above pass stepstatus1 0 → 1 yielded 2 1 → 2 yielded 3 2 → 3 yielded status ← dead
10if step3 == target3 then11 status→ dead = "dead"12endprint("target=" .. target)
14print("target=" .. target3)15print("step=" .. step3)16print("status=" .. statusdead)outputtarget=3 step=3 status=dead
target ← 1, step ← 0, status ← suspended
1local target→ 1 = 12local step→ 0 = 03local status→ suspended = "suspended"step ← 1, status ← yielded
5while step0 < target1 do6 step→ 1 = step + 17 status→ yielded = "yielded"8endstatus ← dead
10if step1 == target1 then11 status→ dead = "dead"12endprint("target=" .. target)
14print("target=" .. target1)15print("step=" .. step1)16print("status=" .. statusdead)outputtarget=1 step=1 status=dead
target ← 2, step ← 0, status ← suspended
1local target→ 2 = 22local step→ 0 = 03local status→ suspended = "suspended"step ← 1, status ← yielded
pass 1 of 25while step0 < target2 do6 step→ 1 = step + 17 status→ yielded = "yielded"8endstep ← 2, status ← yielded
pass 2 of 25while step1 < target2 do6 step→ 2 = step + 17 status→ yielded = "yielded"8endstatus ← dead
10if step2 == target2 then11 status→ dead = "dead"12endprint("target=" .. target)
14print("target=" .. target2)15print("step=" .. step2)16print("status=" .. statusdead)outputtarget=2 step=2 status=dead