1 ! Copyright (C) 2003, 2011 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs combinators combinators.private
4 kernel kernel.private make namespaces sequences vectors ;
7 : with-datastack ( stack quot -- new-stack )
9 [ [ datastack ] dip swap [ { } like set-datastack ] dip ] dip
10 swap [ call datastack ] dip
11 swap [ set-datastack ] dip
12 ] ( stack quot -- new-stack ) call-effect-unsafe ;
14 SYMBOL: original-error
16 SYMBOL: error-continuation
22 : 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 : catchstack ( -- catchstack ) catchstack* clone ; inline
31 : set-catchstack ( catchstack -- )
32 >vector CONTEXT-OBJ-CATCHSTACK set-context-object ; inline
34 : init-catchstack ( -- ) f set-catchstack ;
45 C: <continuation> continuation
47 : current-continuation ( -- continuation )
48 datastack callstack retainstack namestack catchstack
53 : >continuation< ( continuation -- data call retain name catch )
54 { [ data>> ] [ call>> ] [ retain>> ] [ name>> ] [ catch>> ] } cleave ;
58 : ifcc ( capture restore -- )
59 [ dummy-1 current-continuation ] 2dip [ dummy-2 ] prepose ?if ; inline
61 : callcc0 ( quot -- ) [ drop ] ifcc ; inline
63 : callcc1 ( quot -- obj ) [ ] ifcc ; inline
67 : (continue) ( continuation -- * )
75 ] ( continuation -- * ) call-effect-unsafe ;
79 : continue-with ( obj continuation -- * )
81 swap OBJ-CALLCC-1 set-special-object
88 OBJ-CALLCC-1 special-object
89 f OBJ-CALLCC-1 set-special-object
93 ] ( obj continuation -- * ) call-effect-unsafe ;
95 : continue ( continuation -- * )
96 f swap continue-with ;
98 SYMBOL: return-continuation
100 : with-return ( quot -- )
101 [ [ return-continuation set ] prepose callcc0 ] with-scope ; inline
104 return-continuation get continue ;
106 GENERIC: compute-restarts ( error -- seq )
110 : save-error ( error -- )
112 [ compute-restarts restarts set-global ] bi ;
116 GENERIC: error-in-thread ( error thread -- * )
118 SYMBOL: thread-error-hook ! ( error thread -- )
120 thread-error-hook [ [ die ] ] initialize
122 M: object error-in-thread ( error thread -- * )
123 thread-error-hook get-global call( error thread -- * ) ;
125 : in-callback? ( -- ? ) CONTEXT-OBJ-IN-CALLBACK-P context-object ;
127 SYMBOL: callback-error-hook ! ( error -- * )
129 callback-error-hook [ [ die ] ] initialize
131 : rethrow ( error -- * )
135 [ callback-error-hook get-global call( error -- * ) ]
136 [ OBJ-CURRENT-THREAD special-object error-in-thread ]
138 ] [ pop continue-with ] if-empty ;
140 : recover ( ..a try: ( ..a -- ..b ) recovery: ( ..a error -- ..b ) -- ..b )
143 [ catchstack* push ] dip
149 : ignore-errors ( quot -- )
150 [ drop ] recover ; inline
152 : cleanup ( try cleanup-always cleanup-error -- )
153 [ compose [ dip rethrow ] curry recover ] [ drop ] 2bi call ; inline
155 ERROR: attempt-all-error ;
157 : attempt-all ( ... seq quot: ( ... elt -- ... obj ) -- ... obj )
162 [ [ , f ] compose [ , drop t ] recover ] curry all?
163 ] { } make last swap [ rethrow ] when
166 TUPLE: condition error restarts continuation ;
168 C: <condition> condition ( error restarts cc -- condition )
170 : throw-restarts ( error restarts -- restart )
171 [ <condition> throw ] callcc1 2nip ;
173 : rethrow-restarts ( error restarts -- restart )
174 [ <condition> rethrow ] callcc1 2nip ;
176 : throw-continue ( error -- )
177 { { "Continue" t } } throw-restarts drop ;
179 TUPLE: restart name obj continuation ;
183 : restart ( restart -- * )
184 [ obj>> ] [ continuation>> ] bi continue-with ;
186 M: object compute-restarts drop { } ;
188 M: condition compute-restarts
189 [ error>> compute-restarts ]
192 [ continuation>> [ <restart> ] curry ] bi
198 : init-error-handler ( -- )
203 OBJ-CURRENT-THREAD special-object error-thread set-global
204 current-continuation error-continuation set-global
205 [ original-error set-global ] [ rethrow ] bi
206 ] ERROR-HANDLER-QUOT set-special-object
207 ! VM adds this to kernel errors, so that user-space
209 "kernel-error" OBJ-ERROR set-special-object ;