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