]> gitweb.factorcode.org Git - factor.git/blob - core/continuations/continuations.factor
6dde851963442774f3b24cfaf305a43f60ddfb39
[factor.git] / core / continuations / continuations.factor
1 ! Copyright (C) 2003, 2008 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 accessors ;
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     >r >r dummy-1 continuation r> r> [ 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     >continuation<
78     set-catchstack
79     set-namestack
80     set-retainstack
81     >r set-datastack r>
82     set-callstack ;
83
84 : (continue-with) ( obj continuation -- )
85     swap 4 setenv
86     >continuation<
87     set-catchstack
88     set-namestack
89     set-retainstack
90     >r set-datastack drop 4 getenv f 4 setenv f r>
91     set-callstack ;
92
93 PRIVATE>
94
95 : continue-with ( obj continuation -- )
96     [ (continue-with) ] 2 (throw) ;
97
98 : continue ( continuation -- )
99     f swap continue-with ;
100
101 SYMBOL: return-continuation
102
103 : with-return ( quot -- )
104     [ [ return-continuation set ] prepose callcc0 ] with-scope ; inline
105
106 : return ( -- )
107     return-continuation get continue ;
108
109 : with-datastack ( stack quot -- newstack )
110     [
111         [
112             [ [ { } like set-datastack ] dip call datastack ] dip
113             continue-with
114         ] 3 (throw)
115     ] callcc1 2nip ;
116
117 GENERIC: compute-restarts ( error -- seq )
118
119 <PRIVATE
120
121 : save-error ( error -- )
122     dup error set-global
123     compute-restarts restarts set-global ;
124
125 PRIVATE>
126
127 SYMBOL: thread-error-hook
128
129 : rethrow ( error -- * )
130     dup save-error
131     catchstack* empty? [
132         thread-error-hook get-global
133         [ 1 (throw) ] [ die ] if*
134     ] when
135     c> continue-with ;
136
137 : recover ( try recovery -- )
138     >r [ swap >c call c> drop ] curry r> ifcc ; inline
139
140 : ignore-errors ( quot -- )
141     [ drop ] recover ; inline
142
143 : cleanup ( try cleanup-always cleanup-error -- )
144     over >r compose [ dip rethrow ] curry
145     recover r> 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>