// Handler-stack mappings are reusable only when no continuation can restore // their old addresses (issue #329). Exercise one-shot return/cancellation, // multi-shot copies and final cancellation with fresh handlers in between; // the last stale resume must still panic rather than reach a new // occupant of its old mapping. import std/io import std/list import std/process.{type Subject, type proc} effect ask { ctl ask :: -> Int } effect amb { multi ctl choose :: -> Bool } // Every iteration installs and completes a fresh one-shot handler. fn one_shots :: left: Int total: Int -> Int { if left == 0 { total } else { let answer = handle { ask () + 1 } with { ask -> resume 40 } one_shots (left - 1) (total + answer) } } // Not resuming abandons the body. Its suspended stack is not finished until // cancellation has run the cleanup and driven the coroutine to Return. fn cancel_once :: -> Int / { handle { defer io.println "one-shot cancelled" ask () } with { ask -> 5 } } // A non-local return leaves the resumed handler while unwinding is active. fn return_from_once :: -> Int { let answer = handle { let answer = ask () return answer + 2 } with { ask -> resume 40 } answer } // The first call copies the capture and the second is its last use, so this // case exercises both resume modes and ends with no saved capture left. fn choose_both :: -> List { handle { if choose () { 10 } else { 20 } } with { choose -> list.append (resume True) (resume False), return n -> [n] } } fn many_multi :: left: Int total: Int -> Int { if left == 0 { total } else { many_multi (left - 1) (total + list.sum (choose_both ())) } } // No resume at all: the live capture is cancelled at final finish. fn cancel_multi :: -> Int / { handle { defer io.println "multi-shot cancelled" if choose () { 1 } else { 2 } } with { choose -> 9 } } fn first_of :: ks: List List / > value: Bool -> List / { match ks { [k, ..] -> k value, [] -> [] } } // After the first copied resume, the original capture is Saved. Complete // fresh one-shot handlers before the second, last-use resume materialises // that capture and marks it Gone. Final-discard coverage stays in // multishot.lse's all_kept; this case checks intervening-stack restoration. fn saved_with_handlers_between :: -> List { handle { if choose () { 10 } else { 20 } } with { choose -> { let kept = [resume] let first = first_of kept True let between = one_shots 2 0 let second = first_of kept False list.append (list.append first [between]) second } , return n -> [n] } } // The copied first resume leaves the original capture Saved. Park this actor // with that capture live; a peer then completes fresh handlers before waking // it. In particular, generic actor hibernation is not being mistaken for // coverage of a parked, saved handler. The resume closure stays local: only // subjects and unit/int messages cross actor boundaries. Each peer creates // and offers its own `ready` subject, whose owner is its only receiver. fn saved_while_parked :: ready: Subject<()> wake: Subject -> List / { handle { if choose () { 10 } else { 20 } } with { choose -> { let kept = [resume] let first = first_of kept True process.send ready () // Park the coordinating actor itself (main), not just a handler's // parent, while the copied resume leaves its original capture Saved. let between = process.receive_forever wake let second = first_of kept False list.append (list.append first [between]) second } , return n -> [n] } } fn parked_capture_rounds :: left: Int offers: Subject> wake: Subject total: Int -> Int / { if left == 0 { total } else { let _ = process.spawn fn { let ready = process.subject () process.send offers ready process.receive_forever ready // Both actors park here. LOUISE_PARKED_EPOCH_MS=0 therefore hibernates // the actor whose multi-shot capture is Saved before the fresh handlers. process.sleep 1 let between = one_shots 64 0 process.send wake between } let ready = process.receive_forever offers let answer = list.sum (saved_while_parked ready wake) parked_capture_rounds (left - 1) offers wake (total + answer) } } type Escaped { Done(Int), Later(fn(Bool) -> Escaped / ) } // Escaping the closure retains the frame after final finish, not the right // to restore its old stack. Later handlers are free to reuse that mapping. fn escape_multi :: -> Escaped / { handle { defer io.println "escaped capture cancelled" Done(if choose () { 11 } else { 22 }) } with { choose -> Later(fn value { resume value }) } } pub fn main :: -> () / { io.println ("one-shot total " <> show (one_shots 1024 0)) io.println ("cancelled " <> show (cancel_once ()) <> ", " <> show (cancel_once ())) io.println ("returned " <> show (return_from_once ())) io.println ("multi-shot total " <> show (many_multi 128 0)) io.println ("cancelled multi " <> show (cancel_multi ())) io.println ("saved between " <> show (saved_with_handlers_between ())) let offers = process.subject () let wake = process.subject () io.println ("parked captures " <> show (parked_capture_rounds 4 offers wake 0)) match escape_multi () { Done(n) -> io.println ("unexpected " <> show n) , Later(k) -> { io.println ("after final finish " <> show (one_shots 64 0)) match k True { Done(n) -> io.println ("stale resume ran " <> show n) , Later(_) -> io.println "stale resume returned a closure" } } } } //> one-shot total 41984 //> one-shot cancelled //> one-shot cancelled //> cancelled 5, 5 //> returned 42 //> multi-shot total 3840 //> multi-shot cancelled //> cancelled multi 9 //> saved between [10, 82, 20] //> parked captures 10616 //> escaped capture cancelled //> after final finish 2624 //! panic: `resume` called after its handler finished