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