1 ! Copyright (C) 2003, 2011 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors assocs classes combinators combinators.private
4 kernel kernel.private make namespaces sequences vectors ;
7 : with-datastack ( stack quot -- new-stack )
9 [ [ get-datastack ] dip swap [ { } like set-datastack ] dip ] dip
10 swap [ call get-datastack ] dip
11 swap [ set-datastack ] dip
12 ] ( stack quot -- new-stack ) call-effect-unsafe ;
14 SYMBOL: original-error
16 SYMBOL: error-continuation
22 : (get-catchstack) ( -- catchstack )
23 CONTEXT-OBJ-CATCHSTACK context-object { vector } declare ; inline
25 ! We have to defeat some optimizations to make continuations work
26 : dummy-1 ( -- obj ) f ;
27 : dummy-2 ( obj -- obj ) ;
29 : get-catchstack ( -- catchstack ) (get-catchstack) clone ; inline
31 : (set-catchstack) ( catchstack -- )
32 CONTEXT-OBJ-CATCHSTACK set-context-object ; inline
34 : set-catchstack ( catchstack -- )
35 >vector (set-catchstack) ; inline
37 : init-catchstack ( -- )
38 V{ } clone (set-catchstack) ;
42 TUPLE: continuation data call retain name catch ;
44 C: <continuation> continuation
46 : current-continuation ( -- continuation )
47 get-datastack get-callstack get-retainstack get-namestack get-catchstack
52 : >continuation< ( continuation -- data call retain name catch )
53 continuation check-instance {
54 [ data>> ] [ call>> ] [ retain>> ] [ name>> ] [ catch>> ]
59 : ifcc ( capture restore -- )
60 [ dummy-1 current-continuation ] 2dip [ dummy-2 ] prepose ?if ; inline
62 : callcc0 ( quot -- ) [ drop ] ifcc ; inline
64 : callcc1 ( quot -- obj ) [ ] ifcc ; inline
68 : (continue) ( continuation -- * )
76 ] ( continuation -- * ) call-effect-unsafe ;
80 : continue-with ( obj continuation -- * )
82 swap OBJ-CALLCC-1 set-special-object
89 OBJ-CALLCC-1 special-object
90 f OBJ-CALLCC-1 set-special-object
94 ] ( obj continuation -- * ) call-effect-unsafe ;
96 : continue ( continuation -- * )
97 f swap continue-with ;
99 SYMBOL: return-continuation
101 : with-return ( quot -- )
102 [ return-continuation ] dip [ with-variable ] 2curry callcc0 ; inline
105 return-continuation get continue ;
107 GENERIC: compute-restarts ( error -- seq )
111 : save-error ( error -- )
113 [ compute-restarts restarts set-global ] bi ;
117 GENERIC: error-in-thread ( error thread -- * )
119 SYMBOL: thread-error-hook ! ( error thread -- * )
121 M: object error-in-thread
122 thread-error-hook get-global call( error thread -- * ) ;
124 : in-callback? ( -- ? ) CONTEXT-OBJ-IN-CALLBACK-P context-object ;
126 SYMBOL: callback-error-hook ! ( error -- * )
128 : rethrow ( error -- * )
132 [ callback-error-hook get-global call( error -- * ) ]
133 [ OBJ-CURRENT-THREAD special-object error-in-thread ]
135 ] [ pop continue-with ] if-empty ;
137 thread-error-hook [ [ die drop rethrow ] ] initialize
139 callback-error-hook [ [ die rethrow ] ] initialize
141 : recover ( ..a try: ( ..a -- ..b ) recovery: ( ..a error -- ..b ) -- ..b )
144 [ (get-catchstack) push ] dip
146 (get-catchstack) pop*
150 : ignore-errors ( quot -- )
151 [ drop ] recover ; inline
153 : ignore-error ( quot check: ( error -- ? ) -- )
154 '[ dup @ [ drop ] [ rethrow ] if ] recover ; inline
156 : ignore-error/f ( quot check: ( error -- ? ) -- )
157 '[ dup @ [ drop f ] [ rethrow ] if ] recover ; inline
159 : cleanup ( try cleanup-always cleanup-error -- )
160 [ '[ [ @ @ ] dip rethrow ] recover ] [ drop ] 2bi call ; inline
162 : finally ( try cleanup-always -- )
165 ERROR: attempt-all-error ;
167 : attempt-all ( ... seq quot: ( ... elt -- ... obj ) -- ... obj )
172 '[ [ @ , f ] [ , drop t ] recover ] all?
173 ] { } make last swap [ rethrow ] when
176 TUPLE: condition error restarts continuation ;
178 C: <condition> condition
180 : throw-restarts ( error restarts -- restart )
181 [ <condition> throw ] callcc1 2nip ;
183 : rethrow-restarts ( error restarts -- restart )
184 [ <condition> rethrow ] callcc1 2nip ;
186 : throw-continue ( error -- )
187 { { "Continue" t } } throw-restarts drop ;
189 TUPLE: restart name obj continuation ;
193 : continue-restart ( restart -- * )
194 [ obj>> ] [ continuation>> ] bi continue-with ;
196 M: object compute-restarts drop { } ;
198 M: condition compute-restarts
199 [ error>> compute-restarts ]
202 [ continuation>> [ <restart> ] curry ] bi
208 : init-error-handler ( -- )
211 OBJ-CURRENT-THREAD special-object error-thread set-global
212 current-continuation error-continuation set-global
213 [ original-error set-global ] [ rethrow ] bi
214 ] ERROR-HANDLER-QUOT set-special-object ;