diff --git a/extra/coroutines/coroutines-docs.factor b/extra/coroutines/coroutines-docs.factor index 327c60e017..3b2c5033e0 100644 --- a/extra/coroutines/coroutines-docs.factor +++ b/extra/coroutines/coroutines-docs.factor @@ -46,7 +46,13 @@ HELP: coyield* HELP: coterminate { $values { "v" "an object" } } { $description "Terminate the current coroutine, leaving the value v on the stack when control is passed to the " { $link coresume } " caller. Resuming a terminated coroutine is a no-op." } -{ $see-also coyield } +{ $see-also coyield coreset } +; + +HELP: coreset +{ $values { "v" "an object" } } +{ $description "Reset the current coroutine, leaving the value v on the stack when control is passed to the " { $link coresume } " caller. When the coroutine is resumed, it will continue at the beginning of the coroutine." } +{ $see-also coyield coterminate } ; HELP: current-coro diff --git a/extra/coroutines/coroutines-tests.factor b/extra/coroutines/coroutines-tests.factor index 6710452b22..5c443891cb 100644 --- a/extra/coroutines/coroutines-tests.factor +++ b/extra/coroutines/coroutines-tests.factor @@ -17,3 +17,5 @@ test1 dup *coresume . dup *coresume . dup *coresume . dup *coresume 2drop [ [ coyield* ] each ] cocreate ; { "c" "b" "a" } [ test3 { "a" "b" "c" } over coresume >r dup *coresume >r *coresume r> r> ] unit-test + +{ 4+2/3 } [ [ 1+ coyield 2 * coyield 3 / coreset ] cocreate 1 5 [ over coresume ] times nip ] unit-test \ No newline at end of file diff --git a/extra/coroutines/coroutines.factor b/extra/coroutines/coroutines.factor index dc594abd2d..59b703600c 100644 --- a/extra/coroutines/coroutines.factor +++ b/extra/coroutines/coroutines.factor @@ -6,7 +6,7 @@ IN: coroutines SYMBOL: current-coro -TUPLE: coroutine resumecc exitcc ; +TUPLE: coroutine resumecc exitcc originalcc ; : cocreate ( quot -- co ) coroutine new @@ -14,7 +14,7 @@ TUPLE: coroutine resumecc exitcc ; [ swapd , , \ bind , "Coroutine has terminated illegally." , \ throw , ] [ ] make - >>resumecc ; + [ >>resumecc ] [ >>originalcc ] bi ; : coresume ( v co -- result ) [ @@ -43,3 +43,8 @@ TUPLE: coroutine resumecc exitcc ; current-coro get [ ] >>resumecc exitcc>> continue-with ; + +: coreset ( v -- ) + current-coro get dup + originalcc>> >>resumecc + exitcc>> continue-with ; \ No newline at end of file