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 context-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 : catchstack ( -- catchstack ) catchstack* clone ; inline
28 : set-catchstack ( catchstack -- )
29 >vector 1 set-context-object ; inline
31 : init-catchstack ( -- ) f set-catchstack ;
35 TUPLE: continuation data call retain name catch ;
37 C: <continuation> continuation
39 : continuation ( -- continuation )
40 datastack callstack retainstack namestack catchstack
45 : >continuation< ( continuation -- data call retain name catch )
46 { [ data>> ] [ call>> ] [ retain>> ] [ name>> ] [ catch>> ] } cleave ;
50 : ifcc ( capture restore -- )
51 [ dummy-1 continuation ] 2dip [ dummy-2 ] prepose ?if ; inline
53 : callcc0 ( quot -- ) [ drop ] ifcc ; inline
55 : callcc1 ( quot -- obj ) [ ] ifcc ; inline
59 : (continue) ( continuation -- * )
67 ] (( continuation -- * )) call-effect-unsafe ;
71 : continue-with ( obj continuation -- * )
73 swap 4 set-special-object
78 [ set-datastack drop 4 special-object f 4 set-special-object f ] dip
80 ] (( obj continuation -- * )) call-effect-unsafe ;
82 : continue ( continuation -- * )
83 f swap continue-with ;
85 SYMBOL: return-continuation
87 : with-return ( quot -- )
88 [ [ return-continuation set ] prepose callcc0 ] with-scope ; inline
91 return-continuation get continue ;
93 : with-datastack ( stack quot -- newstack )
96 [ [ { } like set-datastack ] dip call datastack ] dip
98 ] (( stack quot continuation -- * )) call-effect-unsafe
101 GENERIC: compute-restarts ( error -- seq )
105 : save-error ( error -- )
107 compute-restarts restarts set-global ;
111 SYMBOL: thread-error-hook
113 : rethrow ( error -- * )
116 thread-error-hook get-global
117 [ (( error -- * )) call-effect-unsafe ] [ die ] if*
121 : recover ( ..a try: ( ..a -- ..b ) recovery: ( ..a error -- ..b ) -- ..b )
122 [ [ swap >c call c> drop ] curry ] dip ifcc ; inline
124 : ignore-errors ( quot -- )
125 [ drop ] recover ; inline
127 : cleanup ( try cleanup-always cleanup-error -- )
128 [ compose [ dip rethrow ] curry recover ] [ drop ] 2bi call ; inline
130 ERROR: attempt-all-error ;
132 : attempt-all ( ... seq quot: ( ... elt -- ... obj ) -- ... obj )
137 [ [ , f ] compose [ , drop t ] recover ] curry all?
138 ] { } make last swap [ rethrow ] when
141 TUPLE: condition error restarts continuation ;
143 C: <condition> condition ( error restarts cc -- condition )
145 : throw-restarts ( error restarts -- restart )
146 [ <condition> throw ] callcc1 2nip ;
148 : rethrow-restarts ( error restarts -- restart )
149 [ <condition> rethrow ] callcc1 2nip ;
151 : throw-continue ( error -- )
152 { { "Continue" t } } throw-restarts drop ;
154 TUPLE: restart name obj continuation ;
158 : restart ( restart -- * )
159 [ obj>> ] [ continuation>> ] bi continue-with ;
161 M: object compute-restarts drop { } ;
163 M: condition compute-restarts
164 [ error>> compute-restarts ]
167 [ continuation>> [ <restart> ] curry ] bi
173 : init-error-handler ( -- )
178 63 special-object error-thread set-global
179 continuation error-continuation set-global
181 ] 5 set-special-object
182 ! VM adds this to kernel errors, so that user-space
184 "kernel-error" 6 set-special-object ;