]> gitweb.factorcode.org Git - factor.git/blob - core/continuations/continuations.factor
Merge branch 'master' into row-polymorphism
[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 special-object { 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 set-special-object ;
27
28 PRIVATE>
29
30 : catchstack ( -- catchstack ) catchstack* clone ; inline
31
32 : set-catchstack ( catchstack -- ) >vector 1 set-special-object ; 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     [ dummy-1 continuation ] 2dip [ dummy-2 ] prepose ?if ; inline
53
54 : callcc0 ( quot -- ) [ drop ] ifcc ; inline
55
56 : callcc1 ( quot -- obj ) [ ] ifcc ; inline
57
58 <PRIVATE
59
60 : (continue) ( continuation -- * )
61     [
62         >continuation<
63         set-catchstack
64         set-namestack
65         set-retainstack
66         [ set-datastack ] dip
67         set-callstack
68     ] (( continuation -- * )) call-effect-unsafe ;
69
70 PRIVATE>
71
72 : continue-with ( obj continuation -- * )
73     [
74         swap 4 set-special-object
75         >continuation<
76         set-catchstack
77         set-namestack
78         set-retainstack
79         [ set-datastack drop 4 special-object f 4 set-special-object f ] dip
80         set-callstack
81     ] (( obj continuation -- * )) call-effect-unsafe ;
82
83 : continue ( continuation -- * )
84     f swap continue-with ;
85
86 SYMBOL: return-continuation
87
88 : with-return ( quot -- )
89     [ [ return-continuation set ] prepose callcc0 ] with-scope ; inline
90
91 : return ( -- * )
92     return-continuation get continue ;
93
94 : with-datastack ( stack quot -- newstack )
95     [
96         [
97             [ [ { } like set-datastack ] dip call datastack ] dip
98             continue-with
99         ] (( stack quot continuation -- * )) call-effect-unsafe
100     ] callcc1 2nip ;
101
102 GENERIC: compute-restarts ( error -- seq )
103
104 <PRIVATE
105
106 : save-error ( error -- )
107     dup error set-global
108     compute-restarts restarts set-global ;
109
110 PRIVATE>
111
112 SYMBOL: thread-error-hook
113
114 : rethrow ( error -- * )
115     dup save-error
116     catchstack* empty? [
117         thread-error-hook get-global
118         [ (( error -- * )) call-effect-unsafe ] [ die ] if*
119     ] when
120     c> continue-with ;
121
122 : recover ( ..a try: ( ..a -- ..b ) recovery: ( ..a error -- ..b ) -- ..b )
123     [ [ swap >c call c> drop ] curry ] dip ifcc ; inline
124
125 : ignore-errors ( quot -- )
126     [ drop ] recover ; inline
127
128 : cleanup ( try cleanup-always cleanup-error -- )
129     [ compose [ dip rethrow ] curry recover ] [ drop ] 2bi call ; inline
130
131 ERROR: attempt-all-error ;
132
133 : attempt-all ( ... seq quot: ( ... elt -- ... obj ) -- ... obj )
134     over empty? [
135         attempt-all-error
136     ] [
137         [
138             [ [ , f ] compose [ , drop t ] recover ] curry all?
139         ] { } make last swap [ rethrow ] when
140     ] if ; inline
141
142 TUPLE: condition error restarts continuation ;
143
144 C: <condition> condition ( error restarts cc -- condition )
145
146 : throw-restarts ( error restarts -- restart )
147     [ <condition> throw ] callcc1 2nip ;
148
149 : rethrow-restarts ( error restarts -- restart )
150     [ <condition> rethrow ] callcc1 2nip ;
151
152 : throw-continue ( error -- )
153     { { "Continue" t } } throw-restarts drop ;
154
155 TUPLE: restart name obj continuation ;
156
157 C: <restart> restart
158
159 : restart ( restart -- * )
160     [ obj>> ] [ continuation>> ] bi continue-with ;
161
162 M: object compute-restarts drop { } ;
163
164 M: condition compute-restarts
165     [ error>> compute-restarts ]
166     [
167         [ restarts>> ]
168         [ continuation>> [ <restart> ] curry ] bi
169         { } assoc>map
170     ] bi append ;
171
172 <PRIVATE
173
174 : init-error-handler ( -- )
175     V{ } clone set-catchstack
176     ! VM calls on error
177     [
178         ! 63 = self
179         63 special-object error-thread set-global
180         continuation error-continuation set-global
181         rethrow
182     ] 5 set-special-object
183     ! VM adds this to kernel errors, so that user-space
184     ! can identify them
185     "kernel-error" 6 set-special-object ;
186
187 PRIVATE>