@@ -603,31 +603,37 @@ fun get( ref: ref<h,a>) : <read<h>,div> a
603603inline extern unsafe-st (f : () -> < st < global > |e> a ) : ( () -> e a)
604604 inline "#1 "
605605
606- fun protect-prompt ( resumed : ref < global , bool > , k : resume-result < b, r> -> e r, res : r ) : e r
607- val did-resume : bool = (unsafe-st{ ! resumed })()
608- if did-resume then
609- // if resumed, we no longer need to protect
610- res
611- elif ! yielding() then
612- // otherwise, if we are not yielding, resume k with finalization (to run all finally clauses)
613- k(Finalize (res))
614- elif yielding-non-final() then
615- // if we yield non-final to an operation, extend the continuation with this prompt (so we keep protecting after being resumed)
616- yield-cont( fn (cont,x) protect-prompt(pretend-decreasing(resumed),k,cont(x)) )
617- else
618- // if we are in a final yield, capture it, resume k with finalization, and reyield
619- val yld = yield-capture()
620- k(Finalize (res))
621- if yielding() return yield-extend( fn (_x ) unsafe-reyield(yld) ) // yikes, a finally clause is itself yielding...
622- unsafe-reyield(yld)
606+ type protect-state < b, r, e>
607+ NeedsFinalization (k : resume-result < b, r> -> e r)
608+ NoFinalization
609+
610+ fun protect-prompt ( resumed : ref < global , protect-state < b, r, e> > , res : r) : e r
611+ val hdiv =@Hnodiv
612+ val did-resume : some < b, r, e> protect-state < b, r, e> = (unsafe-st{ ! resumed })()
613+ match did-resume
614+ NoFinalization -> // resumed already, no need to protect
615+ res
616+ NeedsFinalization (k) ->
617+ if ! yielding() then
618+ // otherwise, if we are not yielding, resume k with finalization (to run all finally clauses)
619+ k(Finalize (res))
620+ elif yielding-non-final() then
621+ // if we yield non-final to an operation, extend the continuation with this prompt (so we keep protecting after being resumed)
622+ yield-cont( fn (cont,x) protect-prompt(pretend-decreasing(resumed),cont(x)) )
623+ else
624+ // if we are in a final yield, capture it, resume k with finalization, and reyield
625+ val yld = yield-capture()
626+ k(Finalize (res))
627+ if yielding() return yield-extend( fn (_x ) unsafe-reyield(yld) ) // yikes, a finally clause is itself yielding...
628+ unsafe-reyield(yld)
623629
624630fun protect ( x : a, clause : ( x : a, k : b -> e r) -> e r, k : resume-result < b, r> -> e r ) : e r
625- val resumed = (unsafe-st{ref(False )})()
631+ val resumed = (unsafe-st{ref(NeedsFinalization (k) )})()
626632 fun kprotect (ret)
627- (unsafe-st{resumed := True })()
633+ (unsafe-st{resumed := NoFinalization })()
628634 k(Deep (ret))
629635 val res = clause(x,kprotect)
630- protect-prompt(resumed,k, res)
636+ protect-prompt(resumed,res)
631637
632638/*
633639pub fun clause-control1( clause : (x:a, k: b -> e r) -> e r ) : clause1<a,b,e,r>
@@ -715,12 +721,12 @@ fun under2( ev : ev<h>, op : (a1,a2) -> e b, x1 : a1, x2 : a2 ) : e b
715721 z
716722
717723fun protect2 ( x1 : a1, x2 : a2, clause : ( x : a1, x : a2, k : b -> e r) -> e r, k : resume-result < b, r> -> e r ) : e r
718- val resumed = (unsafe-st{ref(False )})()
724+ val resumed = (unsafe-st{ref(NeedsFinalization (k) )})()
719725 fun kprotect (ret)
720- (unsafe-st{ resumed := True })()
726+ (unsafe-st{ resumed := NoFinalization })()
721727 k(Deep (ret))
722728 val res = clause(x1,x2,kprotect)
723- protect-prompt(resumed,k, res)
729+ protect-prompt(resumed,res)
724730
725731pub fun clause-control2 ( clause : ( x1 : a1, x2 : a2, k : b -> e r) -> e r ) : clause2 < a1, a2, b, h, e, r>
726732 Clause2 (fn (m,_ev ,x1,x2){ yield-to(m, fn (k){ protect2(x1,x2,clause,k) }) })
0 commit comments