factor/core/continuations/continuations.factor

210 lines
5.2 KiB
Factor
Raw Normal View History

! Copyright (C) 2003, 2011 Slava Pestov.
2007-09-20 18:09:08 -04:00
! See http://factorcode.org/license.txt for BSD license.
USING: accessors assocs combinators combinators.private kernel
kernel.private make namespaces sequences vectors ;
2007-09-20 18:09:08 -04:00
IN: continuations
2010-04-03 19:10:21 -04:00
: with-datastack ( stack quot -- new-stack )
[
[ [ get-datastack ] dip swap [ { } like set-datastack ] dip ] dip
swap [ call get-datastack ] dip
2010-04-03 19:10:21 -04:00
swap [ set-datastack ] dip
] ( stack quot -- new-stack ) call-effect-unsafe ;
2010-04-03 19:10:21 -04:00
SYMBOL: original-error
2007-09-20 18:09:08 -04:00
SYMBOL: error
SYMBOL: error-continuation
2008-02-27 20:23:22 -05:00
SYMBOL: error-thread
2007-09-20 18:09:08 -04:00
SYMBOL: restarts
<PRIVATE
: (get-catchstack) ( -- catchstack )
CONTEXT-OBJ-CATCHSTACK context-object { vector } declare ; inline
2007-09-20 18:09:08 -04:00
2008-08-22 04:12:15 -04:00
! We have to defeat some optimizations to make continuations work
: dummy-1 ( -- obj ) f ;
: dummy-2 ( obj -- obj ) ;
2007-10-03 17:35:48 -04:00
: get-catchstack ( -- catchstack ) (get-catchstack) clone ; inline
2008-02-27 20:23:22 -05:00
: (set-catchstack) ( catchstack -- )
CONTEXT-OBJ-CATCHSTACK set-context-object ; inline
: set-catchstack ( catchstack -- )
>vector (set-catchstack) ; inline
2007-09-20 18:09:08 -04:00
: init-catchstack ( -- )
V{ } clone (set-catchstack) ;
2007-09-20 18:09:08 -04:00
PRIVATE>
2007-09-20 18:09:08 -04:00
TUPLE: continuation data call retain name catch ;
2007-09-20 18:09:08 -04:00
C: <continuation> continuation
: current-continuation ( -- continuation )
get-datastack get-callstack get-retainstack get-namestack get-catchstack
2007-09-20 18:09:08 -04:00
<continuation> ;
<PRIVATE
ERROR: not-a-continuation object ;
2012-09-14 17:59:38 -04:00
2007-09-20 18:09:08 -04:00
: >continuation< ( continuation -- data call retain name catch )
dup continuation? [ not-a-continuation ] unless
2012-09-14 17:59:38 -04:00
{ [ data>> ] [ call>> ] [ retain>> ] [ name>> ] [ catch>> ] } cleave ; inline
PRIVATE>
2007-09-20 18:09:08 -04:00
: ifcc ( capture restore -- )
[ dummy-1 current-continuation ] 2dip [ dummy-2 ] prepose ?if ; inline
2007-09-20 18:09:08 -04:00
: callcc0 ( quot -- ) [ drop ] ifcc ; inline
2007-09-20 18:09:08 -04:00
: callcc1 ( quot -- obj ) [ ] ifcc ; inline
2007-09-20 18:09:08 -04:00
<PRIVATE
2009-03-16 21:11:36 -04:00
: (continue) ( continuation -- * )
2009-04-17 00:14:11 -04:00
[
>continuation<
set-catchstack
set-namestack
set-retainstack
[ set-datastack ] dip
set-callstack
] ( continuation -- * ) call-effect-unsafe ;
2009-04-13 00:01:14 -04:00
2007-09-20 18:09:08 -04:00
PRIVATE>
: continue-with ( obj continuation -- * )
2009-03-16 21:11:36 -04:00
[
swap OBJ-CALLCC-1 set-special-object
2009-03-16 21:11:36 -04:00
>continuation<
set-catchstack
set-namestack
set-retainstack
[
set-datastack drop
OBJ-CALLCC-1 special-object
f OBJ-CALLCC-1 set-special-object
f
] dip
2009-03-16 21:11:36 -04:00
set-callstack
] ( obj continuation -- * ) call-effect-unsafe ;
2007-09-20 18:09:08 -04:00
: continue ( continuation -- * )
f swap continue-with ;
2008-05-07 08:49:29 -04:00
SYMBOL: return-continuation
: with-return ( quot -- )
[ return-continuation ] dip [ with-variable ] 2curry callcc0 ; inline
2008-05-07 08:49:29 -04:00
: return ( -- * )
2008-05-07 08:49:29 -04:00
return-continuation get continue ;
2007-09-20 18:09:08 -04:00
GENERIC: compute-restarts ( error -- seq )
<PRIVATE
: save-error ( error -- )
[ error set-global ]
[ compute-restarts restarts set-global ] bi ;
2007-09-20 18:09:08 -04:00
PRIVATE>
GENERIC: error-in-thread ( error thread -- * )
SYMBOL: thread-error-hook ! ( error thread -- * )
M: object error-in-thread
thread-error-hook get-global call( error thread -- * ) ;
: in-callback? ( -- ? ) CONTEXT-OBJ-IN-CALLBACK-P context-object ;
SYMBOL: callback-error-hook ! ( error -- * )
2007-09-20 18:09:08 -04:00
: rethrow ( error -- * )
2008-02-21 02:24:24 -05:00
dup save-error
(get-catchstack) [
in-callback?
[ callback-error-hook get-global call( error -- * ) ]
[ OBJ-CURRENT-THREAD special-object error-in-thread ]
if
] [ pop continue-with ] if-empty ;
2007-09-20 18:09:08 -04:00
thread-error-hook [ [ die drop rethrow ] ] initialize
callback-error-hook [ [ die rethrow ] ] initialize
: recover ( ..a try: ( ..a -- ..b ) recovery: ( ..a error -- ..b ) -- ..b )
[
[
[ (get-catchstack) push ] dip
call
(get-catchstack) pop*
] curry
] dip ifcc ; inline
2007-09-20 18:09:08 -04:00
2008-02-26 15:58:02 -05:00
: ignore-errors ( quot -- )
[ drop ] recover ; inline
2007-09-20 18:09:08 -04:00
: cleanup ( try cleanup-always cleanup-error -- )
[ compose [ dip rethrow ] curry recover ] [ drop ] 2bi call ; inline
2007-09-20 18:09:08 -04:00
ERROR: attempt-all-error ;
: attempt-all ( ... seq quot: ( ... elt -- ... obj ) -- ... obj )
over empty? [
attempt-all-error
] [
[
[ [ , f ] compose [ , drop t ] recover ] curry all?
] { } make last swap [ rethrow ] when
] if ; inline
2007-09-20 18:09:08 -04:00
2008-04-04 01:33:06 -04:00
TUPLE: condition error restarts continuation ;
2007-09-20 18:09:08 -04:00
2012-09-14 17:59:38 -04:00
C: <condition> condition
2007-09-20 18:09:08 -04:00
: throw-restarts ( error restarts -- restart )
[ <condition> throw ] callcc1 2nip ;
: rethrow-restarts ( error restarts -- restart )
[ <condition> rethrow ] callcc1 2nip ;
: throw-continue ( error -- )
{ { "Continue" t } } throw-restarts drop ;
2007-09-20 18:09:08 -04:00
TUPLE: restart name obj continuation ;
C: <restart> restart
: continue-restart ( restart -- * )
2008-04-04 01:33:06 -04:00
[ obj>> ] [ continuation>> ] bi continue-with ;
2007-09-20 18:09:08 -04:00
M: object compute-restarts drop { } ;
M: condition compute-restarts
2008-04-04 01:33:06 -04:00
[ error>> compute-restarts ]
[
[ restarts>> ]
[ continuation>> [ <restart> ] curry ] bi
2008-04-04 01:33:06 -04:00
{ } assoc>map
] bi append ;
2008-07-28 23:03:13 -04:00
<PRIVATE
: init-error-handler ( -- )
init-catchstack
2008-07-28 23:03:13 -04:00
! VM calls on error
[
OBJ-CURRENT-THREAD special-object error-thread set-global
current-continuation error-continuation set-global
[ original-error set-global ] [ rethrow ] bi
] ERROR-HANDLER-QUOT set-special-object ;
2008-07-28 23:03:13 -04:00
PRIVATE>