]> gitweb.factorcode.org Git - factor.git/blob - core/continuations/continuations.factor
7681c2b089f5543acf06398de31932ba82384906
[factor.git] / core / continuations / continuations.factor
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 ;
6 IN: continuations
7
8 SYMBOL: error
9 SYMBOL: error-continuation
10 SYMBOL: error-thread
11 SYMBOL: restarts
12
13 <PRIVATE
14
15 : catchstack* ( -- catchstack )
16     1 getenv { vector } declare ; inline
17
18 : >c ( continuation -- ) catchstack* push ;
19
20 : c> ( -- continuation ) catchstack* pop ;
21
22 ! We have to defeat some optimizations to make continuations work
23 : dummy-1 ( -- obj ) f ;
24 : dummy-2 ( obj -- obj ) dup drop ;
25
26 : init-catchstack ( -- ) V{ } clone 1 setenv ;
27
28 PRIVATE>
29
30 : catchstack ( -- catchstack ) catchstack* clone ; inline
31
32 : set-catchstack ( catchstack -- ) >vector 1 setenv ; inline
33
34 TUPLE: continuation data call retain name catch ;
35
36 C: <continuation> continuation
37
38 : continuation ( -- continuation )
39     datastack callstack retainstack namestack catchstack
40     <continuation> ;
41
42 : >continuation< ( continuation -- data call retain name catch )
43     {
44         [ data>>   ]
45         [ call>>   ]
46         [ retain>> ]
47         [ name>>   ]
48         [ catch>>  ]
49     } cleave ;
50
51 : ifcc ( capture restore -- )
52     #! After continuation is being captured, the stacks looks
53     #! like:
54     #! ( f continuation r:capture r:restore )
55     #! so the 'capture' branch is taken.
56     #!
57     #! Note that the continuation itself is not captured as part
58     #! of the datastack.
59     #!
60     #! BUT...
61     #!
62     #! After the continuation is resumed, (continue-with) pushes
63     #! the given value together with f,
64     #! so now, the stacks looks like:
65     #! ( value f r:capture r:restore )
66     #! Execution begins right after the call to 'continuation'.
67     #! The 'restore' branch is taken.
68     [ dummy-1 continuation ] 2dip [ dummy-2 ] prepose ?if ; inline
69
70 : callcc0 ( quot -- ) [ drop ] ifcc ; inline
71
72 : callcc1 ( quot -- obj ) [ ] ifcc ; inline
73
74 <PRIVATE
75
76 : (continue) ( continuation -- * )
77     [
78         >continuation<
79         set-catchstack
80         set-namestack
81         set-retainstack
82         [ set-datastack ] dip
83         set-callstack
84     ] (( continuation -- * )) call-effect-unsafe ;
85
86 PRIVATE>
87
88 : continue-with ( obj continuation -- * )
89     [
90         swap 4 setenv
91         >continuation<
92         set-catchstack
93         set-namestack
94         set-retainstack
95         [ set-datastack drop 4 getenv f 4 setenv f ] dip
96         set-callstack
97     ] (( obj continuation -- * )) call-effect-unsafe ;
98
99 : continue ( continuation -- * )
100     f swap continue-with ;
101
102 SYMBOL: return-continuation
103
104 : with-return ( quot -- )
105     [ [ return-continuation set ] prepose callcc0 ] with-scope ; inline
106
107 : return ( -- * )
108     return-continuation get continue ;
109
110 : with-datastack ( stack quot -- newstack )
111     [
112         [
113             [ [ { } like set-datastack ] dip call datastack ] dip
114             continue-with
115         ] (( stack quot continuation -- * )) call-effect-unsafe
116     ] callcc1 2nip ;
117
118 GENERIC: compute-restarts ( error -- seq )
119
120 <PRIVATE
121
122 : save-error ( error -- )
123     dup error set-global
124     compute-restarts restarts set-global ;
125
126 PRIVATE>
127
128 SYMBOL: thread-error-hook
129
130 : rethrow ( error -- * )
131     dup save-error
132     catchstack* empty? [
133         thread-error-hook get-global
134         [ (( error -- * )) call-effect-unsafe ] [ die ] if*
135     ] when
136     c> continue-with ;
137
138 : recover ( try recovery -- )
139     [ [ swap >c call c> drop ] curry ] dip ifcc ; inline
140
141 : ignore-errors ( quot -- )
142     [ drop ] recover ; inline
143
144 : cleanup ( try cleanup-always cleanup-error -- )
145     [ compose [ dip rethrow ] curry recover ] [ drop ] 2bi call ; inline
146
147 ERROR: attempt-all-error ;
148
149 : attempt-all ( seq quot -- obj )
150     over empty? [
151         attempt-all-error
152     ] [
153         [
154             [ [ , f ] compose [ , drop t ] recover ] curry all?
155         ] { } make peek swap [ rethrow ] when
156     ] if ; inline
157
158 TUPLE: condition error restarts continuation ;
159
160 C: <condition> condition ( error restarts cc -- condition )
161
162 : throw-restarts ( error restarts -- restart )
163     [ <condition> throw ] callcc1 2nip ;
164
165 : rethrow-restarts ( error restarts -- restart )
166     [ <condition> rethrow ] callcc1 2nip ;
167
168 TUPLE: restart name obj continuation ;
169
170 C: <restart> restart
171
172 : restart ( restart -- * )
173     [ obj>> ] [ continuation>> ] bi continue-with ;
174
175 M: object compute-restarts drop { } ;
176
177 M: condition compute-restarts
178     [ error>> compute-restarts ]
179     [
180         [ restarts>> ]
181         [ continuation>> [ <restart> ] curry ] bi
182         { } assoc>map
183     ] bi append ;
184
185 <PRIVATE
186
187 : init-error-handler ( -- )
188     V{ } clone set-catchstack
189     ! VM calls on error
190     [
191         ! 63 = self
192         63 getenv error-thread set-global
193         continuation error-continuation set-global
194         rethrow
195     ] 5 setenv
196     ! VM adds this to kernel errors, so that user-space
197     ! can identify them
198     "kernel-error" 6 setenv ;
199
200 PRIVATE>