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 ;
15 SYMBOL: original-error
17 SYMBOL: error-continuation
23 : catchstack* ( -- catchstack )
24 1 context-object { vector } declare ; inline
26 : >c ( continuation -- ) catchstack* push ;
28 : c> ( -- continuation ) catchstack* pop ;
30 ! We have to defeat some optimizations to make continuations work
31 : dummy-1 ( -- obj ) f ;
32 : dummy-2 ( obj -- obj ) dup drop ;
34 : catchstack ( -- catchstack ) catchstack* clone ; inline
36 : set-catchstack ( catchstack -- )
37 >vector 1 set-context-object ; inline
39 : init-catchstack ( -- ) f set-catchstack ;
43 TUPLE: continuation data call retain name catch ;
45 C: <continuation> continuation
47 : continuation ( -- continuation )
48 datastack callstack retainstack namestack catchstack
53 : >continuation< ( continuation -- data call retain name catch )
54 { [ data>> ] [ call>> ] [ retain>> ] [ name>> ] [ catch>> ] } cleave ;
58 : ifcc ( capture restore -- )
59 [ dummy-1 continuation ] 2dip [ dummy-2 ] prepose ?if ; inline
61 : callcc0 ( quot -- ) [ drop ] ifcc ; inline
63 : callcc1 ( quot -- obj ) [ ] ifcc ; inline
67 : (continue) ( continuation -- * )
75 ] (( continuation -- * )) call-effect-unsafe ;
79 : continue-with ( obj continuation -- * )
81 swap 4 set-special-object
86 [ set-datastack drop 4 special-object f 4 set-special-object f ] dip
88 ] (( obj continuation -- * )) call-effect-unsafe ;
90 : continue ( continuation -- * )
91 f swap continue-with ;
93 SYMBOL: return-continuation
95 : with-return ( quot -- )
96 [ [ return-continuation set ] prepose callcc0 ] with-scope ; inline
99 return-continuation get continue ;
101 GENERIC: compute-restarts ( error -- seq )
105 : save-error ( error -- )
107 [ compute-restarts restarts set-global ] bi ;
111 SYMBOL: thread-error-hook
113 : rethrow ( error -- * )
116 thread-error-hook get-global
117 [ original-error get-global die ] or
118 (( error -- * )) call-effect-unsafe
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 ( -- )
179 63 special-object error-thread set-global
180 continuation error-continuation set-global
181 [ original-error set-global ] [ rethrow ] bi
182 ] 5 set-special-object
183 ! VM adds this to kernel errors, so that user-space
185 "kernel-error" 6 set-special-object ;