*** empty log message ***
[bpt/guile.git] / libguile / dynwind.c
1 /* Copyright (C) 1995, 1996, 1998, 1999 Free Software Foundation, Inc.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2, or (at your option)
6 * any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; see the file COPYING. If not, write to
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
17 *
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
20 *
21 * The exception is that, if you link the GUILE library with other files
22 * to produce an executable, this does not by itself cause the
23 * resulting executable to be covered by the GNU General Public License.
24 * Your use of that executable is in no way restricted on account of
25 * linking the GUILE library code into it.
26 *
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
29 *
30 * This exception applies only to the code released by the
31 * Free Software Foundation under the name GUILE. If you copy
32 * code from other Free Software Foundation releases into a copy of
33 * GUILE, as the General Public License permits, the exception does
34 * not apply to the code that you add in this way. To avoid misleading
35 * anyone as to the status of such modified files, you must delete
36 * this exception notice from them.
37 *
38 * If you write modifications of your own for GUILE, it is your choice
39 * whether to permit this exception to apply to your modifications.
40 * If you do not wish that, delete this exception notice. */
41 \f
42
43 #include <stdio.h>
44 #include "_scm.h"
45 #include "eval.h"
46 #include "alist.h"
47 #include "fluids.h"
48 #include "genio.h"
49 #include "smob.h"
50
51 #include "dynwind.h"
52 \f
53
54 /* {Dynamic wind}
55
56 Things that can be on the wind list:
57
58 (enter-proc . leave-proc) dynamic-wind
59 (tag . jmpbuf) catch
60 (tag . lazy-catch) lazy-catch
61 tag is either a symbol or a boolean
62
63 ((fluid ...) . (value ...)) with-fluids
64
65 */
66
67
68
69 SCM_PROC(s_dynamic_wind, "dynamic-wind", 3, 0, 0, scm_dynamic_wind);
70
71 SCM
72 scm_dynamic_wind (thunk1, thunk2, thunk3)
73 SCM thunk1;
74 SCM thunk2;
75 SCM thunk3;
76 {
77 SCM ans;
78 SCM_ASSERT (SCM_NFALSEP (scm_thunk_p (thunk3)),
79 thunk3,
80 SCM_ARG3, s_dynamic_wind);
81 scm_apply (thunk1, SCM_EOL, SCM_EOL);
82 scm_dynwinds = scm_acons (thunk1, thunk3, scm_dynwinds);
83 ans = scm_apply (thunk2, SCM_EOL, SCM_EOL);
84 scm_dynwinds = SCM_CDR (scm_dynwinds);
85 scm_apply (thunk3, SCM_EOL, SCM_EOL);
86 return ans;
87 }
88
89 /* The implementation of a C-callable dynamic-wind,
90 * scm_internal_dynamic_wind, requires packaging of C pointers in a
91 * smob. Objects of this type are pushed onto the dynwind chain.
92 */
93
94 typedef struct guardsmem {
95 scm_guard_t before;
96 scm_guard_t after;
97 void *data;
98 } guardsmem;
99
100 #define SCM_GUARDSMEM(obj) ((guardsmem *) SCM_CDR (obj))
101 #define SCM_BEFORE_GUARD(obj) (SCM_GUARDSMEM (obj)->before)
102 #define SCM_AFTER_GUARD(obj) (SCM_GUARDSMEM (obj)->after)
103 #define SCM_GUARD_DATA(obj) (SCM_GUARDSMEM (obj)->data)
104 #define SCM_GUARDSP(obj) (SCM_CAR (obj) == tc16_guards)
105
106 static long tc16_guards;
107
108 static scm_sizet
109 freeguards (SCM guards)
110 {
111 scm_must_free ((char *) SCM_CDR (guards));
112 return sizeof (guardsmem);
113 }
114
115 static int
116 printguards (SCM exp, SCM port, scm_print_state *pstate)
117 {
118 scm_puts ("#<guards ", port);
119 scm_intprint (SCM_CDR (exp), 16, port);
120 scm_putc ('>', port);
121 return 1;
122 }
123
124 SCM
125 scm_internal_dynamic_wind (scm_guard_t before,
126 scm_inner_t inner,
127 scm_guard_t after,
128 void *inner_data,
129 void *guard_data)
130 {
131 SCM guards, ans;
132 guardsmem *g;
133 before (guard_data);
134 g = (guardsmem *) scm_must_malloc (sizeof (*g), "guards");
135 g->before = before;
136 g->after = after;
137 g->data = guard_data;
138 SCM_NEWSMOB (guards, tc16_guards, g);
139 scm_dynwinds = scm_acons (guards, SCM_BOOL_F, scm_dynwinds);
140 ans = inner (inner_data);
141 scm_dynwinds = SCM_CDR (scm_dynwinds);
142 after (guard_data);
143 return ans;
144 }
145
146 #ifdef GUILE_DEBUG
147 SCM_PROC (s_wind_chain, "wind-chain", 0, 0, 0, scm_wind_chain);
148
149 SCM
150 scm_wind_chain ()
151 {
152 return scm_dynwinds;
153 }
154 #endif
155
156 static void
157 scm_swap_bindings (SCM glocs, SCM vals)
158 {
159 SCM tmp;
160 while (SCM_NIMP (vals))
161 {
162 tmp = SCM_GLOC_VAL (SCM_CAR (glocs));
163 SCM_SETCDR (SCM_CAR (glocs) - 1L, SCM_CAR (vals));
164 SCM_SETCAR (vals, tmp);
165 glocs = SCM_CDR (glocs);
166 vals = SCM_CDR (vals);
167 }
168 }
169
170 void
171 scm_dowinds (to, delta)
172 SCM to;
173 long delta;
174 {
175 tail:
176 if (scm_dynwinds == to);
177 else if (0 > delta)
178 {
179 SCM wind_elt;
180 SCM wind_key;
181
182 scm_dowinds (SCM_CDR (to), 1 + delta);
183 wind_elt = SCM_CAR (to);
184 #if 0
185 if (SCM_INUMP (wind_elt))
186 {
187 scm_cross_dynwind_binding_scope (wind_elt, 0);
188 }
189 else
190 #endif
191 {
192 wind_key = SCM_CAR (wind_elt);
193 /* key = #t | symbol | thunk | list of glocs | list of fluids */
194 if (SCM_NIMP (wind_key))
195 {
196 if (SCM_TYP3 (wind_key) == scm_tc3_cons_gloc)
197 scm_swap_bindings (wind_key, SCM_CDR (wind_elt));
198 else if (SCM_TYP3 (wind_key) == scm_tc3_cons)
199 scm_swap_fluids (wind_key, SCM_CDR (wind_elt));
200 else if (SCM_GUARDSP (wind_key))
201 SCM_BEFORE_GUARD (wind_key) (SCM_GUARD_DATA (wind_key));
202 else if (SCM_TYP3 (wind_key) == scm_tc3_closure)
203 scm_apply (wind_key, SCM_EOL, SCM_EOL);
204 }
205 }
206 scm_dynwinds = to;
207 }
208 else
209 {
210 SCM from;
211 SCM wind_elt;
212 SCM wind_key;
213
214 from = SCM_CDR (SCM_CAR (scm_dynwinds));
215 wind_elt = SCM_CAR (scm_dynwinds);
216 scm_dynwinds = SCM_CDR (scm_dynwinds);
217 #if 0
218 if (SCM_INUMP (wind_elt))
219 {
220 scm_cross_dynwind_binding_scope (wind_elt, 0);
221 }
222 else
223 #endif
224 {
225 wind_key = SCM_CAR (wind_elt);
226 if (SCM_NIMP (wind_key))
227 {
228 if (SCM_TYP3 (wind_key) == scm_tc3_cons_gloc)
229 scm_swap_bindings (wind_key, from);
230 else if (SCM_TYP3 (wind_key) == scm_tc3_cons)
231 scm_swap_fluids_reverse (wind_key, from);
232 else if (SCM_GUARDSP (wind_key))
233 SCM_AFTER_GUARD (wind_key) (SCM_GUARD_DATA (wind_key));
234 else if (SCM_TYP3 (wind_key) == scm_tc3_closure)
235 scm_apply (from, SCM_EOL, SCM_EOL);
236 }
237 }
238 delta--;
239 goto tail; /* scm_dowinds(to, delta-1); */
240 }
241 }
242
243
244
245 void
246 scm_init_dynwind ()
247 {
248 tc16_guards = scm_make_smob_type_mfpe ("guards", sizeof (struct guardsmem),
249 NULL, freeguards, printguards, NULL);
250 #include "dynwind.x"
251 }