Bump version number for 1.9.8.
[bpt/guile.git] / libguile / dynwind.c
CommitLineData
dbb605f5 1/* Copyright (C) 1995,1996,1998,1999,2000,2001, 2003, 2004, 2006, 2008 Free Software Foundation, Inc.
0f2d19dd 2 *
73be1d9e 3 * This library is free software; you can redistribute it and/or
53befeb7
NJ
4 * modify it under the terms of the GNU Lesser General Public License
5 * as published by the Free Software Foundation; either version 3 of
6 * the License, or (at your option) any later version.
0f2d19dd 7 *
53befeb7
NJ
8 * This library is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
73be1d9e
MV
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
0f2d19dd 12 *
73be1d9e
MV
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
53befeb7
NJ
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * 02110-1301 USA
73be1d9e 17 */
1bbd0b84 18
1bbd0b84 19
0f2d19dd
JB
20\f
21
dbb605f5
LC
22#ifdef HAVE_CONFIG_H
23# include <config.h>
24#endif
25
4845bbae
MV
26#include <assert.h>
27
a0599745
MD
28#include "libguile/_scm.h"
29#include "libguile/eval.h"
30#include "libguile/alist.h"
31#include "libguile/fluids.h"
32#include "libguile/ports.h"
33#include "libguile/smob.h"
0f2d19dd 34
a0599745 35#include "libguile/dynwind.h"
0f2d19dd
JB
36\f
37
38/* {Dynamic wind}
b3460a50
MV
39
40 Things that can be on the wind list:
41
4845bbae
MV
42 #<frame>
43 #<winder>
b3460a50
MV
44 (enter-proc . leave-proc) dynamic-wind
45 (tag . jmpbuf) catch
43e01b1e 46 (tag . pre-unwind-data) throw-handler / lazy-catch
b3460a50
MV
47 tag is either a symbol or a boolean
48
b3460a50 49*/
0f2d19dd
JB
50
51
52
3b3b36dd 53SCM_DEFINE (scm_dynamic_wind, "dynamic-wind", 3, 0, 0,
1e6808ea
MG
54 (SCM in_guard, SCM thunk, SCM out_guard),
55 "All three arguments must be 0-argument procedures.\n"
56 "@var{in_guard} is called, then @var{thunk}, then\n"
57 "@var{out_guard}.\n"
58 "\n"
59 "If, any time during the execution of @var{thunk}, the\n"
60 "continuation of the @code{dynamic_wind} expression is escaped\n"
61 "non-locally, @var{out_guard} is called. If the continuation of\n"
62 "the dynamic-wind is re-entered, @var{in_guard} is called. Thus\n"
63 "@var{in_guard} and @var{out_guard} may be called any number of\n"
64 "times.\n"
65 "@lisp\n"
b380b885 66 "(define x 'normal-binding)\n"
1e6808ea 67 "@result{} x\n"
9401323e 68 "(define a-cont (call-with-current-continuation\n"
b380b885
MD
69 " (lambda (escape)\n"
70 " (let ((old-x x))\n"
71 " (dynamic-wind\n"
72 " ;; in-guard:\n"
73 " ;;\n"
1e6808ea
MG
74 " (lambda () (set! x 'special-binding))\n"
75 "\n"
b380b885
MD
76 " ;; thunk\n"
77 " ;;\n"
78 " (lambda () (display x) (newline)\n"
79 " (call-with-current-continuation escape)\n"
80 " (display x) (newline)\n"
1e6808ea
MG
81 " x)\n"
82 "\n"
b380b885
MD
83 " ;; out-guard:\n"
84 " ;;\n"
1e6808ea
MG
85 " (lambda () (set! x old-x)))))))\n"
86 "\n"
9401323e 87 ";; Prints:\n"
b380b885
MD
88 "special-binding\n"
89 ";; Evaluates to:\n"
1e6808ea 90 "@result{} a-cont\n"
b380b885 91 "x\n"
1e6808ea 92 "@result{} normal-binding\n"
b380b885
MD
93 "(a-cont #f)\n"
94 ";; Prints:\n"
95 "special-binding\n"
96 ";; Evaluates to:\n"
1e6808ea 97 "@result{} a-cont ;; the value of the (define a-cont...)\n"
b380b885 98 "x\n"
1e6808ea 99 "@result{} normal-binding\n"
b380b885
MD
100 "a-cont\n"
101 "@result{} special-binding\n"
1e6808ea 102 "@end lisp")
1bbd0b84 103#define FUNC_NAME s_scm_dynamic_wind
0f2d19dd 104{
9de87eea 105 SCM ans, old_winds;
7888309b 106 SCM_ASSERT (scm_is_true (scm_thunk_p (out_guard)),
1e6808ea 107 out_guard,
1bbd0b84 108 SCM_ARG3, FUNC_NAME);
fdc28395 109 scm_call_0 (in_guard);
9de87eea
MV
110 old_winds = scm_i_dynwinds ();
111 scm_i_set_dynwinds (scm_acons (in_guard, out_guard, old_winds));
fdc28395 112 ans = scm_call_0 (thunk);
9de87eea 113 scm_i_set_dynwinds (old_winds);
fdc28395 114 scm_call_0 (out_guard);
0f2d19dd
JB
115 return ans;
116}
1bbd0b84 117#undef FUNC_NAME
0f2d19dd 118
3346a90f 119SCM
92c2555f
MV
120scm_internal_dynamic_wind (scm_t_guard before,
121 scm_t_inner inner,
122 scm_t_guard after,
3346a90f
MD
123 void *inner_data,
124 void *guard_data)
125{
4845bbae
MV
126 SCM ans;
127
661ae7ab
MV
128 scm_dynwind_begin (SCM_F_DYNWIND_REWINDABLE);
129 scm_dynwind_rewind_handler (before, guard_data, SCM_F_WIND_EXPLICITLY);
130 scm_dynwind_unwind_handler (after, guard_data, SCM_F_WIND_EXPLICITLY);
3346a90f 131 ans = inner (inner_data);
661ae7ab 132 scm_dynwind_end ();
3346a90f
MD
133 return ans;
134}
1cc91f1b 135
4845bbae
MV
136/* Frames and winders. */
137
138static scm_t_bits tc16_frame;
139#define FRAME_P(f) SCM_SMOB_PREDICATE (tc16_frame, (f))
140
f5710d53
MV
141#define FRAME_F_REWINDABLE (1 << 0)
142#define FRAME_REWINDABLE_P(f) (SCM_SMOB_FLAGS(f) & FRAME_F_REWINDABLE)
4845bbae
MV
143
144static scm_t_bits tc16_winder;
14578fa4 145#define WINDER_P(w) SCM_SMOB_PREDICATE (tc16_winder, (w))
f5710d53
MV
146#define WINDER_PROC(w) ((void (*)(void *))SCM_SMOB_DATA (w))
147#define WINDER_DATA(w) ((void *)SCM_SMOB_DATA_2 (w))
4845bbae 148
f5710d53
MV
149#define WINDER_F_EXPLICIT (1 << 0)
150#define WINDER_F_REWIND (1 << 1)
151#define WINDER_F_MARK (1 << 2)
152#define WINDER_EXPLICIT_P(w) (SCM_SMOB_FLAGS(w) & WINDER_F_EXPLICIT)
153#define WINDER_REWIND_P(w) (SCM_SMOB_FLAGS(w) & WINDER_F_REWIND)
154#define WINDER_MARK_P(w) (SCM_SMOB_FLAGS(w) & WINDER_F_MARK)
4845bbae
MV
155
156void
98241dc5 157scm_dynwind_begin (scm_t_dynwind_flags flags)
4845bbae
MV
158{
159 SCM f;
f5710d53 160 SCM_NEWSMOB (f, tc16_frame, 0);
661ae7ab 161 if (flags & SCM_F_DYNWIND_REWINDABLE)
f5710d53 162 SCM_SET_SMOB_FLAGS (f, FRAME_F_REWINDABLE);
9de87eea 163 scm_i_set_dynwinds (scm_cons (f, scm_i_dynwinds ()));
4845bbae
MV
164}
165
166void
661ae7ab 167scm_dynwind_end (void)
4845bbae 168{
9de87eea
MV
169 SCM winds;
170
0888de4f
MV
171 /* Unwind upto and including the next frame entry. We can only
172 encounter #<winder> entries on the way.
4845bbae
MV
173 */
174
9de87eea
MV
175 winds = scm_i_dynwinds ();
176 while (scm_is_pair (winds))
4845bbae 177 {
9de87eea
MV
178 SCM entry = SCM_CAR (winds);
179 winds = SCM_CDR (winds);
180
181 scm_i_set_dynwinds (winds);
0888de4f
MV
182
183 if (FRAME_P (entry))
184 return;
185
186 assert (WINDER_P (entry));
187 if (!WINDER_REWIND_P (entry) && WINDER_EXPLICIT_P (entry))
188 WINDER_PROC(entry) (WINDER_DATA (entry));
4845bbae
MV
189 }
190
191 assert (0);
192}
193
194void
98241dc5
NJ
195scm_dynwind_unwind_handler (void (*proc) (void *), void *data,
196 scm_t_wind_flags flags)
4845bbae
MV
197{
198 SCM w;
f5710d53
MV
199 SCM_NEWSMOB2 (w, tc16_winder, (scm_t_bits) proc, (scm_t_bits) data);
200 if (flags & SCM_F_WIND_EXPLICITLY)
201 SCM_SET_SMOB_FLAGS (w, WINDER_F_EXPLICIT);
9de87eea 202 scm_i_set_dynwinds (scm_cons (w, scm_i_dynwinds ()));
4845bbae
MV
203}
204
205void
98241dc5
NJ
206scm_dynwind_rewind_handler (void (*proc) (void *), void *data,
207 scm_t_wind_flags flags)
4845bbae
MV
208{
209 SCM w;
f5710d53
MV
210 SCM_NEWSMOB2 (w, tc16_winder, (scm_t_bits) proc, (scm_t_bits) data);
211 SCM_SET_SMOB_FLAGS (w, WINDER_F_REWIND);
9de87eea 212 scm_i_set_dynwinds (scm_cons (w, scm_i_dynwinds ()));
a520e4f0
MV
213 if (flags & SCM_F_WIND_EXPLICITLY)
214 proc (data);
215}
216
217void
98241dc5
NJ
218scm_dynwind_unwind_handler_with_scm (void (*proc) (SCM), SCM data,
219 scm_t_wind_flags flags)
a520e4f0
MV
220{
221 SCM w;
222 scm_t_bits fl = ((flags&SCM_F_WIND_EXPLICITLY)? WINDER_F_EXPLICIT : 0);
f5710d53
MV
223 SCM_NEWSMOB2 (w, tc16_winder, (scm_t_bits) proc, SCM_UNPACK (data));
224 SCM_SET_SMOB_FLAGS (w, fl | WINDER_F_MARK);
9de87eea 225 scm_i_set_dynwinds (scm_cons (w, scm_i_dynwinds ()));
a520e4f0
MV
226}
227
228void
98241dc5
NJ
229scm_dynwind_rewind_handler_with_scm (void (*proc) (SCM), SCM data,
230 scm_t_wind_flags flags)
a520e4f0
MV
231{
232 SCM w;
f5710d53
MV
233 SCM_NEWSMOB2 (w, tc16_winder, (scm_t_bits) proc, SCM_UNPACK (data));
234 SCM_SET_SMOB_FLAGS (w, WINDER_F_REWIND | WINDER_F_MARK);
9de87eea 235 scm_i_set_dynwinds (scm_cons (w, scm_i_dynwinds ()));
a520e4f0 236 if (flags & SCM_F_WIND_EXPLICITLY)
4845bbae
MV
237 proc (data);
238}
239
6d5649b7 240void
661ae7ab 241scm_dynwind_free (void *mem)
6d5649b7 242{
661ae7ab 243 scm_dynwind_unwind_handler (free, mem, SCM_F_WIND_EXPLICITLY);
6d5649b7
MV
244}
245
c2654ef0 246#ifdef GUILE_DEBUG
a1ec6916 247SCM_DEFINE (scm_wind_chain, "wind-chain", 0, 0, 0,
1bbd0b84 248 (),
156149ad
MG
249 "Return the current wind chain. The wind chain contains all\n"
250 "information required by @code{dynamic-wind} to call its\n"
251 "argument thunks when entering/exiting its scope.")
1bbd0b84 252#define FUNC_NAME s_scm_wind_chain
c2654ef0 253{
9de87eea 254 return scm_i_dynwinds ();
c2654ef0 255}
1bbd0b84 256#undef FUNC_NAME
c2654ef0
MD
257#endif
258
2e171178 259void
904a077d 260scm_swap_bindings (SCM vars, SCM vals)
6778caf9
MD
261{
262 SCM tmp;
263 while (SCM_NIMP (vals))
264 {
904a077d
MV
265 tmp = SCM_VARIABLE_REF (SCM_CAR (vars));
266 SCM_VARIABLE_SET (SCM_CAR (vars), SCM_CAR (vals));
6778caf9 267 SCM_SETCAR (vals, tmp);
904a077d 268 vars = SCM_CDR (vars);
6778caf9
MD
269 vals = SCM_CDR (vals);
270 }
271}
c2654ef0 272
4845bbae 273void
c014a02e 274scm_dowinds (SCM to, long delta)
4845bbae 275{
14578fa4 276 scm_i_dowinds (to, delta, NULL, NULL);
4845bbae
MV
277}
278
279void
14578fa4 280scm_i_dowinds (SCM to, long delta, void (*turn_func) (void *), void *data)
0f2d19dd
JB
281{
282 tail:
9de87eea 283 if (scm_is_eq (to, scm_i_dynwinds ()))
4845bbae
MV
284 {
285 if (turn_func)
286 turn_func (data);
287 }
1be6b49c 288 else if (delta < 0)
0f2d19dd
JB
289 {
290 SCM wind_elt;
291 SCM wind_key;
292
14578fa4 293 scm_i_dowinds (SCM_CDR (to), 1 + delta, turn_func, data);
0f2d19dd 294 wind_elt = SCM_CAR (to);
4845bbae 295
928e0f42 296 if (FRAME_P (wind_elt))
0f2d19dd 297 {
928e0f42
MV
298 if (!FRAME_REWINDABLE_P (wind_elt))
299 scm_misc_error ("dowinds",
300 "cannot invoke continuation from this context",
301 SCM_EOL);
302 }
303 else if (WINDER_P (wind_elt))
304 {
305 if (WINDER_REWIND_P (wind_elt))
306 WINDER_PROC (wind_elt) (WINDER_DATA (wind_elt));
0f2d19dd
JB
307 }
308 else
0f2d19dd 309 {
928e0f42
MV
310 wind_key = SCM_CAR (wind_elt);
311 /* key = #t | symbol | thunk | list of variables */
312 if (SCM_NIMP (wind_key))
4845bbae 313 {
d2e53ed6 314 if (scm_is_pair (wind_key))
4845bbae 315 {
928e0f42
MV
316 if (SCM_VARIABLEP (SCM_CAR (wind_key)))
317 scm_swap_bindings (wind_key, SCM_CDR (wind_elt));
904a077d 318 }
5a963489 319 else if (scm_is_true (scm_thunk_p (wind_key)))
928e0f42 320 scm_call_0 (wind_key);
b3460a50 321 }
0f2d19dd 322 }
928e0f42 323
9de87eea 324 scm_i_set_dynwinds (to);
0f2d19dd
JB
325 }
326 else
327 {
9de87eea 328 SCM wind;
0f2d19dd
JB
329 SCM wind_elt;
330 SCM wind_key;
331
9de87eea
MV
332 wind = scm_i_dynwinds ();
333 wind_elt = SCM_CAR (wind);
334 scm_i_set_dynwinds (SCM_CDR (wind));
4845bbae 335
928e0f42 336 if (FRAME_P (wind_elt))
0f2d19dd 337 {
928e0f42
MV
338 /* Nothing to do. */
339 }
340 else if (WINDER_P (wind_elt))
341 {
342 if (!WINDER_REWIND_P (wind_elt))
343 WINDER_PROC (wind_elt) (WINDER_DATA (wind_elt));
0f2d19dd
JB
344 }
345 else
0f2d19dd 346 {
928e0f42
MV
347 wind_key = SCM_CAR (wind_elt);
348 if (SCM_NIMP (wind_key))
4845bbae 349 {
d2e53ed6 350 if (scm_is_pair (wind_key))
4845bbae 351 {
928e0f42
MV
352 if (SCM_VARIABLEP (SCM_CAR (wind_key)))
353 scm_swap_bindings (wind_key, SCM_CDR (wind_elt));
904a077d 354 }
5a963489 355 else if (scm_is_true (scm_thunk_p (wind_key)))
928e0f42 356 scm_call_0 (SCM_CDR (wind_elt));
b3460a50 357 }
0f2d19dd 358 }
928e0f42 359
0f2d19dd
JB
360 delta--;
361 goto tail; /* scm_dowinds(to, delta-1); */
362 }
363}
364
0f2d19dd
JB
365void
366scm_init_dynwind ()
0f2d19dd 367{
4845bbae 368 tc16_frame = scm_make_smob_type ("frame", 0);
4845bbae
MV
369
370 tc16_winder = scm_make_smob_type ("winder", 0);
371
a0599745 372#include "libguile/dynwind.x"
0f2d19dd 373}
89e00824
ML
374
375/*
376 Local Variables:
377 c-file-style: "gnu"
378 End:
379*/