]> gitweb.factorcode.org Git - factor.git/blob - core/continuations/continuations.factor
core/basis: trim down using lists
[factor.git] / core / continuations / continuations.factor
1 ! Copyright (C) 2003, 2011 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors assocs combinators combinators.private kernel
4 kernel.private make namespaces sequences vectors ;
5 IN: continuations
6
7 : with-datastack ( stack quot -- new-stack )
8     [
9         [ [ datastack ] dip swap [ { } like set-datastack ] dip ] dip
10         swap [ call datastack ] dip
11         swap [ set-datastack ] dip
12     ] ( stack quot -- new-stack ) call-effect-unsafe ;
13
14 SYMBOL: original-error
15 SYMBOL: error
16 SYMBOL: error-continuation
17 SYMBOL: error-thread
18 SYMBOL: restarts
19
20 <PRIVATE
21
22 : catchstack* ( -- catchstack )
23     CONTEXT-OBJ-CATCHSTACK context-object { vector } declare ; inline
24
25 ! We have to defeat some optimizations to make continuations work
26 : dummy-1 ( -- obj ) f ;
27 : dummy-2 ( obj -- obj ) ;
28
29 : catchstack ( -- catchstack ) catchstack* clone ; inline
30
31 : set-catchstack ( catchstack -- )
32     >vector CONTEXT-OBJ-CATCHSTACK set-context-object ; inline
33
34 : init-catchstack ( -- ) f set-catchstack ;
35
36 PRIVATE>
37
38 TUPLE: continuation data call retain name catch ;
39
40 C: <continuation> continuation
41
42 : continuation ( -- continuation )
43     datastack callstack retainstack namestack catchstack
44     <continuation> ;
45
46 <PRIVATE
47
48 : >continuation< ( continuation -- data call retain name catch )
49     { [ data>> ] [ call>> ] [ retain>> ] [ name>> ] [ catch>> ] } cleave ;
50
51 PRIVATE>
52
53 : ifcc ( capture restore -- )
54     [ dummy-1 continuation ] 2dip [ dummy-2 ] prepose ?if ; inline
55
56 : callcc0 ( quot -- ) [ drop ] ifcc ; inline
57
58 : callcc1 ( quot -- obj ) [ ] ifcc ; inline
59
60 <PRIVATE
61
62 : (continue) ( continuation -- * )
63     [
64         >continuation<
65         set-catchstack
66         set-namestack
67         set-retainstack
68         [ set-datastack ] dip
69         set-callstack
70     ] ( continuation -- * ) call-effect-unsafe ;
71
72 PRIVATE>
73
74 : continue-with ( obj continuation -- * )
75     [
76         swap OBJ-CALLCC-1 set-special-object
77         >continuation<
78         set-catchstack
79         set-namestack
80         set-retainstack
81         [
82             set-datastack drop
83             OBJ-CALLCC-1 special-object
84             f OBJ-CALLCC-1 set-special-object
85             f
86         ] dip
87         set-callstack
88     ] ( obj continuation -- * ) call-effect-unsafe ;
89
90 : continue ( continuation -- * )
91     f swap continue-with ;
92
93 SYMBOL: return-continuation
94
95 : with-return ( quot -- )
96     [ [ return-continuation set ] prepose callcc0 ] with-scope ; inline
97
98 : return ( -- * )
99     return-continuation get continue ;
100
101 GENERIC: compute-restarts ( error -- seq )
102
103 <PRIVATE
104
105 : save-error ( error -- )
106     [ error set-global ]
107     [ compute-restarts restarts set-global ] bi ;
108
109 PRIVATE>
110
111 GENERIC: error-in-thread ( error thread -- * )
112
113 SYMBOL: thread-error-hook ! ( error thread -- )
114
115 thread-error-hook [ [ die ] ] initialize
116
117 M: object error-in-thread ( error thread -- * )
118     thread-error-hook get-global call( error thread -- * ) ;
119
120 : in-callback? ( -- ? ) CONTEXT-OBJ-IN-CALLBACK-P context-object ;
121
122 SYMBOL: callback-error-hook ! ( error -- * )
123
124 callback-error-hook [ [ die ] ] initialize
125
126 : rethrow ( error -- * )
127     dup save-error
128     catchstack* [
129         in-callback?
130         [ callback-error-hook get-global call( error -- * ) ]
131         [ OBJ-CURRENT-THREAD special-object error-in-thread ]
132         if
133     ] [ pop continue-with ] if-empty ;
134
135 : recover ( ..a try: ( ..a -- ..b ) recovery: ( ..a error -- ..b ) -- ..b )
136     [
137         [
138             [ catchstack* push ] dip
139             call
140             catchstack* pop*
141         ] curry
142     ] dip ifcc ; inline
143
144 : ignore-errors ( quot -- )
145     [ drop ] recover ; inline
146
147 : cleanup ( try cleanup-always cleanup-error -- )
148     [ compose [ dip rethrow ] curry recover ] [ drop ] 2bi call ; inline
149
150 ERROR: attempt-all-error ;
151
152 : attempt-all ( ... seq quot: ( ... elt -- ... obj ) -- ... obj )
153     over empty? [
154         attempt-all-error
155     ] [
156         [
157             [ [ , f ] compose [ , drop t ] recover ] curry all?
158         ] { } make last swap [ rethrow ] when
159     ] if ; inline
160
161 TUPLE: condition error restarts continuation ;
162
163 C: <condition> condition ( error restarts cc -- condition )
164
165 : throw-restarts ( error restarts -- restart )
166     [ <condition> throw ] callcc1 2nip ;
167
168 : rethrow-restarts ( error restarts -- restart )
169     [ <condition> rethrow ] callcc1 2nip ;
170
171 : throw-continue ( error -- )
172     { { "Continue" t } } throw-restarts drop ;
173
174 TUPLE: restart name obj continuation ;
175
176 C: <restart> restart
177
178 : restart ( restart -- * )
179     [ obj>> ] [ continuation>> ] bi continue-with ;
180
181 M: object compute-restarts drop { } ;
182
183 M: condition compute-restarts
184     [ error>> compute-restarts ]
185     [
186         [ restarts>> ]
187         [ continuation>> [ <restart> ] curry ] bi
188         { } assoc>map
189     ] bi append ;
190
191 <PRIVATE
192
193 : init-error-handler ( -- )
194     init-catchstack
195     ! VM calls on error
196     [
197         ! 65 = self
198         OBJ-CURRENT-THREAD special-object error-thread set-global
199         continuation error-continuation set-global
200         [ original-error set-global ] [ rethrow ] bi
201     ] ERROR-HANDLER-QUOT set-special-object
202     ! VM adds this to kernel errors, so that user-space
203     ! can identify them
204     "kernel-error" OBJ-ERROR set-special-object ;
205
206 PRIVATE>