1 ! Copyright (C) 2003, 2010 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 ;
16 SYMBOL: error-continuation
22 : catchstack* ( -- catchstack )
23 1 context-object { vector } declare ; inline
25 : >c ( continuation -- ) catchstack* push ;
27 : c> ( -- continuation ) catchstack* pop ;
29 ! We have to defeat some optimizations to make continuations work
30 : dummy-1 ( -- obj ) f ;
31 : dummy-2 ( obj -- obj ) dup drop ;
33 : catchstack ( -- catchstack ) catchstack* clone ; inline
35 : set-catchstack ( catchstack -- )
36 >vector 1 set-context-object ; inline
38 : init-catchstack ( -- ) f set-catchstack ;
42 TUPLE: continuation data call retain name catch ;
44 C: <continuation> continuation
46 : continuation ( -- continuation )
47 datastack callstack retainstack namestack catchstack
52 : >continuation< ( continuation -- data call retain name catch )
53 { [ data>> ] [ call>> ] [ retain>> ] [ name>> ] [ catch>> ] } cleave ;
57 : ifcc ( capture restore -- )
58 [ dummy-1 continuation ] 2dip [ dummy-2 ] prepose ?if ; inline
60 : callcc0 ( quot -- ) [ drop ] ifcc ; inline
62 : callcc1 ( quot -- obj ) [ ] ifcc ; inline
66 : (continue) ( continuation -- * )
74 ] (( continuation -- * )) call-effect-unsafe ;
78 : continue-with ( obj continuation -- * )
80 swap 4 set-special-object
85 [ set-datastack drop 4 special-object f 4 set-special-object f ] dip
87 ] (( obj continuation -- * )) call-effect-unsafe ;
89 : continue ( continuation -- * )
90 f swap continue-with ;
92 SYMBOL: return-continuation
94 : with-return ( quot -- )
95 [ [ return-continuation set ] prepose callcc0 ] with-scope ; inline
98 return-continuation get continue ;
100 GENERIC: compute-restarts ( error -- seq )
104 : save-error ( error -- )
106 compute-restarts restarts set-global ;
110 SYMBOL: thread-error-hook
112 : rethrow ( error -- * )
115 thread-error-hook get-global
116 [ (( error -- * )) call-effect-unsafe ] [ die ] if*
120 : recover ( ..a try: ( ..a -- ..b ) recovery: ( ..a error -- ..b ) -- ..b )
121 [ [ swap >c call c> drop ] curry ] dip ifcc ; inline
123 : ignore-errors ( quot -- )
124 [ drop ] recover ; inline
126 : cleanup ( try cleanup-always cleanup-error -- )
127 [ compose [ dip rethrow ] curry recover ] [ drop ] 2bi call ; inline
129 ERROR: attempt-all-error ;
131 : attempt-all ( ... seq quot: ( ... elt -- ... obj ) -- ... obj )
136 [ [ , f ] compose [ , drop t ] recover ] curry all?
137 ] { } make last swap [ rethrow ] when
140 TUPLE: condition error restarts continuation ;
142 C: <condition> condition ( error restarts cc -- condition )
144 : throw-restarts ( error restarts -- restart )
145 [ <condition> throw ] callcc1 2nip ;
147 : rethrow-restarts ( error restarts -- restart )
148 [ <condition> rethrow ] callcc1 2nip ;
150 : throw-continue ( error -- )
151 { { "Continue" t } } throw-restarts drop ;
153 TUPLE: restart name obj continuation ;
157 : restart ( restart -- * )
158 [ obj>> ] [ continuation>> ] bi continue-with ;
160 M: object compute-restarts drop { } ;
162 M: condition compute-restarts
163 [ error>> compute-restarts ]
166 [ continuation>> [ <restart> ] curry ] bi
172 : init-error-handler ( -- )
177 63 special-object error-thread set-global
178 continuation error-continuation set-global
180 ] 5 set-special-object
181 ! VM adds this to kernel errors, so that user-space
183 "kernel-error" 6 set-special-object ;