1 ! Copyright (C) 2003, 2011 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays vectors kernel kernel.private sequences
4 namespaces make math splitting sorting quotations assocs
5 combinators combinators.private accessors words ;
8 : with-datastack ( stack quot -- new-stack )
10 [ [ datastack ] dip swap [ { } like set-datastack ] dip ] dip
11 swap [ call datastack ] dip
12 swap [ set-datastack ] dip
13 ] ( stack quot -- new-stack ) call-effect-unsafe ;
15 SYMBOL: original-error
17 SYMBOL: error-continuation
23 : catchstack* ( -- catchstack )
24 1 context-object { vector } declare ; inline
26 ! We have to defeat some optimizations to make continuations work
27 : dummy-1 ( -- obj ) f ;
28 : dummy-2 ( obj -- obj ) ;
30 : catchstack ( -- catchstack ) catchstack* clone ; inline
32 : set-catchstack ( catchstack -- )
33 >vector 1 set-context-object ; inline
35 : init-catchstack ( -- ) f set-catchstack ;
39 TUPLE: continuation data call retain name catch ;
41 C: <continuation> continuation
43 : continuation ( -- continuation )
44 datastack callstack retainstack namestack catchstack
49 : >continuation< ( continuation -- data call retain name catch )
50 { [ data>> ] [ call>> ] [ retain>> ] [ name>> ] [ catch>> ] } cleave ;
54 : ifcc ( capture restore -- )
55 [ dummy-1 continuation ] 2dip [ dummy-2 ] prepose ?if ; inline
57 : callcc0 ( quot -- ) [ drop ] ifcc ; inline
59 : callcc1 ( quot -- obj ) [ ] ifcc ; inline
63 : (continue) ( continuation -- * )
71 ] ( continuation -- * ) call-effect-unsafe ;
75 : continue-with ( obj continuation -- * )
77 swap 4 set-special-object
82 [ set-datastack drop 4 special-object f 4 set-special-object f ] dip
84 ] ( obj continuation -- * ) call-effect-unsafe ;
86 : continue ( continuation -- * )
87 f swap continue-with ;
89 SYMBOL: return-continuation
91 : with-return ( quot -- )
92 [ [ return-continuation set ] prepose callcc0 ] with-scope ; inline
95 return-continuation get continue ;
97 GENERIC: compute-restarts ( error -- seq )
101 : save-error ( error -- )
103 [ compute-restarts restarts set-global ] bi ;
107 GENERIC: error-in-thread ( error thread -- * )
109 SYMBOL: thread-error-hook ! ( error thread -- )
111 thread-error-hook [ [ die ] ] initialize
113 M: object error-in-thread ( error thread -- * )
114 thread-error-hook get-global call( error thread -- * ) ;
116 : in-callback? ( -- ? ) 3 context-object ;
118 SYMBOL: callback-error-hook ! ( error -- * )
120 callback-error-hook [ [ die ] ] initialize
122 : rethrow ( error -- * )
126 [ callback-error-hook get-global call( error -- * ) ]
127 [ 63 special-object error-in-thread ]
129 ] [ pop continue-with ] if-empty ;
131 : recover ( ..a try: ( ..a -- ..b ) recovery: ( ..a error -- ..b ) -- ..b )
134 [ catchstack* push ] dip
140 : ignore-errors ( quot -- )
141 [ drop ] recover ; inline
143 : cleanup ( try cleanup-always cleanup-error -- )
144 [ compose [ dip rethrow ] curry recover ] [ drop ] 2bi call ; inline
146 ERROR: attempt-all-error ;
148 : attempt-all ( ... seq quot: ( ... elt -- ... obj ) -- ... obj )
153 [ [ , f ] compose [ , drop t ] recover ] curry all?
154 ] { } make last swap [ rethrow ] when
157 TUPLE: condition error restarts continuation ;
159 C: <condition> condition ( error restarts cc -- condition )
161 : throw-restarts ( error restarts -- restart )
162 [ <condition> throw ] callcc1 2nip ;
164 : rethrow-restarts ( error restarts -- restart )
165 [ <condition> rethrow ] callcc1 2nip ;
167 : throw-continue ( error -- )
168 { { "Continue" t } } throw-restarts drop ;
170 TUPLE: restart name obj continuation ;
174 : restart ( restart -- * )
175 [ obj>> ] [ continuation>> ] bi continue-with ;
177 M: object compute-restarts drop { } ;
179 M: condition compute-restarts
180 [ error>> compute-restarts ]
183 [ continuation>> [ <restart> ] curry ] bi
189 : init-error-handler ( -- )
194 63 special-object error-thread set-global
195 continuation error-continuation set-global
196 [ original-error set-global ] [ rethrow ] bi
197 ] 5 set-special-object
198 ! VM adds this to kernel errors, so that user-space
200 "kernel-error" 6 set-special-object ;