1 ! Copyright (C) 2003, 2009 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 ;
9 SYMBOL: error-continuation
15 : catchstack* ( -- catchstack )
16 1 special-object { 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 set-special-object ;
30 : catchstack ( -- catchstack ) catchstack* clone ; inline
32 : set-catchstack ( catchstack -- ) >vector 1 set-special-object ; 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 [ dummy-1 continuation ] 2dip [ dummy-2 ] prepose ?if ; inline
54 : callcc0 ( quot -- ) [ drop ] ifcc ; inline
56 : callcc1 ( quot -- obj ) [ ] ifcc ; inline
60 : (continue) ( continuation -- * )
68 ] (( continuation -- * )) call-effect-unsafe ;
72 : continue-with ( obj continuation -- * )
74 swap 4 set-special-object
79 [ set-datastack drop 4 special-object f 4 set-special-object f ] dip
81 ] (( obj continuation -- * )) call-effect-unsafe ;
83 : continue ( continuation -- * )
84 f swap continue-with ;
86 SYMBOL: return-continuation
88 : with-return ( quot -- )
89 [ [ return-continuation set ] prepose callcc0 ] with-scope ; inline
92 return-continuation get continue ;
94 : with-datastack ( stack quot -- newstack )
97 [ [ { } like set-datastack ] dip call datastack ] dip
99 ] (( stack quot continuation -- * )) call-effect-unsafe
102 GENERIC: compute-restarts ( error -- seq )
106 : save-error ( error -- )
108 compute-restarts restarts set-global ;
112 SYMBOL: thread-error-hook
114 : rethrow ( error -- * )
117 thread-error-hook get-global
118 [ (( error -- * )) call-effect-unsafe ] [ die ] if*
122 : recover ( ..a try: ( ..a -- ..b ) recovery: ( ..a error -- ..b ) -- ..b )
123 [ [ swap >c call c> drop ] curry ] dip ifcc ; inline
125 : ignore-errors ( quot -- )
126 [ drop ] recover ; inline
128 : cleanup ( try cleanup-always cleanup-error -- )
129 [ compose [ dip rethrow ] curry recover ] [ drop ] 2bi call ; inline
131 ERROR: attempt-all-error ;
133 : attempt-all ( ... seq quot: ( ... elt -- ... obj ) -- ... obj )
138 [ [ , f ] compose [ , drop t ] recover ] curry all?
139 ] { } make last swap [ rethrow ] when
142 TUPLE: condition error restarts continuation ;
144 C: <condition> condition ( error restarts cc -- condition )
146 : throw-restarts ( error restarts -- restart )
147 [ <condition> throw ] callcc1 2nip ;
149 : rethrow-restarts ( error restarts -- restart )
150 [ <condition> rethrow ] callcc1 2nip ;
152 : throw-continue ( error -- )
153 { { "Continue" t } } throw-restarts drop ;
155 TUPLE: restart name obj continuation ;
159 : restart ( restart -- * )
160 [ obj>> ] [ continuation>> ] bi continue-with ;
162 M: object compute-restarts drop { } ;
164 M: condition compute-restarts
165 [ error>> compute-restarts ]
168 [ continuation>> [ <restart> ] curry ] bi
174 : init-error-handler ( -- )
175 V{ } clone set-catchstack
179 63 special-object error-thread set-global
180 continuation error-continuation set-global
182 ] 5 set-special-object
183 ! VM adds this to kernel errors, so that user-space
185 "kernel-error" 6 set-special-object ;