1 ! Copyright (C) 2003, 2008 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 accessors ;
9 SYMBOL: error-continuation
15 : catchstack* ( -- catchstack )
16 1 getenv { vector } declare ; inline
18 : >c ( continuation -- ) catchstack* push ;
20 : c> ( -- continuation ) catchstack* pop ;
22 ! We have to defeat some optimizations to make continuations work
23 : dummy-1 ( -- obj ) f ;
24 : dummy-2 ( obj -- obj ) dup drop ;
26 : init-catchstack ( -- ) V{ } clone 1 setenv ;
30 : catchstack ( -- catchstack ) catchstack* clone ; inline
32 : set-catchstack ( catchstack -- ) >vector 1 setenv ; inline
34 TUPLE: continuation data call retain name catch ;
36 C: <continuation> continuation
38 : continuation ( -- continuation )
39 datastack callstack retainstack namestack catchstack
42 : >continuation< ( continuation -- data call retain name catch )
51 : ifcc ( capture restore -- )
52 #! After continuation is being captured, the stacks looks
54 #! ( f continuation r:capture r:restore )
55 #! so the 'capture' branch is taken.
57 #! Note that the continuation itself is not captured as part
62 #! After the continuation is resumed, (continue-with) pushes
63 #! the given value together with f,
64 #! so now, the stacks looks like:
65 #! ( value f r:capture r:restore )
66 #! Execution begins right after the call to 'continuation'.
67 #! The 'restore' branch is taken.
68 [ dummy-1 continuation ] 2dip [ dummy-2 ] prepose ?if ; inline
70 : callcc0 ( quot -- ) [ drop ] ifcc ; inline
72 : callcc1 ( quot -- obj ) [ ] ifcc ; inline
76 : (continue) ( continuation -- )
84 : (continue-with) ( obj continuation -- )
90 [ set-datastack drop 4 getenv f 4 setenv f ] dip
95 : continue-with ( obj continuation -- )
96 [ (continue-with) ] 2 (throw) ;
98 : continue ( continuation -- )
99 f swap continue-with ;
101 SYMBOL: return-continuation
103 : with-return ( quot -- )
104 [ [ return-continuation set ] prepose callcc0 ] with-scope ; inline
107 return-continuation get continue ;
109 : with-datastack ( stack quot -- newstack )
112 [ [ { } like set-datastack ] dip call datastack ] dip
117 : assert-depth ( quot -- )
118 { } swap with-datastack { } assert= ; inline
120 GENERIC: compute-restarts ( error -- seq )
124 : save-error ( error -- )
126 compute-restarts restarts set-global ;
130 SYMBOL: thread-error-hook
132 : rethrow ( error -- * )
135 thread-error-hook get-global
136 [ 1 (throw) ] [ die ] if*
140 : recover ( try recovery -- )
141 [ [ swap >c call c> drop ] curry ] dip ifcc ; inline
143 : ignore-errors ( quot -- )
144 [ drop ] recover ; inline
146 : cleanup ( try cleanup-always cleanup-error -- )
147 [ compose [ dip rethrow ] curry recover ] [ drop ] 2bi call ; inline
149 ERROR: attempt-all-error ;
151 : attempt-all ( seq quot -- obj )
156 [ [ , f ] compose [ , drop t ] recover ] curry all?
157 ] { } make peek swap [ rethrow ] when
160 : retry ( quot: ( -- ? ) n -- ) swap [ drop ] prepose attempt-all ; inline
162 TUPLE: condition error restarts continuation ;
164 C: <condition> condition ( error restarts cc -- condition )
166 : throw-restarts ( error restarts -- restart )
167 [ <condition> throw ] callcc1 2nip ;
169 : rethrow-restarts ( error restarts -- restart )
170 [ <condition> rethrow ] callcc1 2nip ;
172 TUPLE: restart name obj continuation ;
176 : restart ( restart -- )
177 [ obj>> ] [ continuation>> ] bi continue-with ;
179 M: object compute-restarts drop { } ;
181 M: condition compute-restarts
182 [ error>> compute-restarts ]
185 [ continuation>> [ <restart> ] curry ] bi
191 : init-error-handler ( -- )
192 V{ } clone set-catchstack
196 63 getenv error-thread set-global
197 continuation error-continuation set-global
200 ! VM adds this to kernel errors, so that user-space
202 "kernel-error" 6 setenv ;