1 /* Copyright (C) 1995,1996,1998,1999,2000,2001, 2003, 2004 Free Software Foundation, Inc.
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.
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.
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
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "libguile/_scm.h"
24 #include "libguile/eval.h"
25 #include "libguile/alist.h"
26 #include "libguile/fluids.h"
27 #include "libguile/ports.h"
28 #include "libguile/smob.h"
30 #include "libguile/dynwind.h"
35 Things that can be on the wind list:
39 (enter-proc . leave-proc) dynamic-wind
41 (tag . lazy-catch) lazy-catch
42 tag is either a symbol or a boolean
48 SCM_DEFINE (scm_dynamic_wind
, "dynamic-wind", 3, 0, 0,
49 (SCM in_guard
, SCM thunk
, SCM out_guard
),
50 "All three arguments must be 0-argument procedures.\n"
51 "@var{in_guard} is called, then @var{thunk}, then\n"
54 "If, any time during the execution of @var{thunk}, the\n"
55 "continuation of the @code{dynamic_wind} expression is escaped\n"
56 "non-locally, @var{out_guard} is called. If the continuation of\n"
57 "the dynamic-wind is re-entered, @var{in_guard} is called. Thus\n"
58 "@var{in_guard} and @var{out_guard} may be called any number of\n"
61 "(define x 'normal-binding)\n"
63 "(define a-cont (call-with-current-continuation\n"
69 " (lambda () (set! x 'special-binding))\n"
73 " (lambda () (display x) (newline)\n"
74 " (call-with-current-continuation escape)\n"
75 " (display x) (newline)\n"
80 " (lambda () (set! x old-x)))))))\n"
87 "@result{} normal-binding\n"
92 "@result{} a-cont ;; the value of the (define a-cont...)\n"
94 "@result{} normal-binding\n"
96 "@result{} special-binding\n"
98 #define FUNC_NAME s_scm_dynamic_wind
101 SCM_ASSERT (scm_is_true (scm_thunk_p (out_guard
)),
103 SCM_ARG3
, FUNC_NAME
);
104 scm_call_0 (in_guard
);
105 old_winds
= scm_i_dynwinds ();
106 scm_i_set_dynwinds (scm_acons (in_guard
, out_guard
, old_winds
));
107 ans
= scm_call_0 (thunk
);
108 scm_i_set_dynwinds (old_winds
);
109 scm_call_0 (out_guard
);
115 scm_internal_dynamic_wind (scm_t_guard before
,
123 scm_dynwind_begin (SCM_F_DYNWIND_REWINDABLE
);
124 scm_dynwind_rewind_handler (before
, guard_data
, SCM_F_WIND_EXPLICITLY
);
125 scm_dynwind_unwind_handler (after
, guard_data
, SCM_F_WIND_EXPLICITLY
);
126 ans
= inner (inner_data
);
131 /* Frames and winders. */
133 static scm_t_bits tc16_frame
;
134 #define FRAME_P(f) SCM_SMOB_PREDICATE (tc16_frame, (f))
136 #define FRAME_F_REWINDABLE (1 << 0)
137 #define FRAME_REWINDABLE_P(f) (SCM_SMOB_FLAGS(f) & FRAME_F_REWINDABLE)
139 static scm_t_bits tc16_winder
;
140 #define WINDER_P(w) SCM_SMOB_PREDICATE (tc16_winder, (w))
141 #define WINDER_PROC(w) ((void (*)(void *))SCM_SMOB_DATA (w))
142 #define WINDER_DATA(w) ((void *)SCM_SMOB_DATA_2 (w))
144 #define WINDER_F_EXPLICIT (1 << 0)
145 #define WINDER_F_REWIND (1 << 1)
146 #define WINDER_F_MARK (1 << 2)
147 #define WINDER_EXPLICIT_P(w) (SCM_SMOB_FLAGS(w) & WINDER_F_EXPLICIT)
148 #define WINDER_REWIND_P(w) (SCM_SMOB_FLAGS(w) & WINDER_F_REWIND)
149 #define WINDER_MARK_P(w) (SCM_SMOB_FLAGS(w) & WINDER_F_MARK)
152 scm_dynwind_begin (scm_t_dynwind_flags flags
)
155 SCM_NEWSMOB (f
, tc16_frame
, 0);
156 if (flags
& SCM_F_DYNWIND_REWINDABLE
)
157 SCM_SET_SMOB_FLAGS (f
, FRAME_F_REWINDABLE
);
158 scm_i_set_dynwinds (scm_cons (f
, scm_i_dynwinds ()));
162 scm_dynwind_end (void)
166 /* Unwind upto and including the next frame entry. We can only
167 encounter #<winder> entries on the way.
170 winds
= scm_i_dynwinds ();
171 while (scm_is_pair (winds
))
173 SCM entry
= SCM_CAR (winds
);
174 winds
= SCM_CDR (winds
);
176 scm_i_set_dynwinds (winds
);
181 assert (WINDER_P (entry
));
182 if (!WINDER_REWIND_P (entry
) && WINDER_EXPLICIT_P (entry
))
183 WINDER_PROC(entry
) (WINDER_DATA (entry
));
192 if (WINDER_MARK_P (w
))
193 return SCM_PACK (WINDER_DATA (w
));
198 scm_dynwind_unwind_handler (void (*proc
) (void *), void *data
,
199 scm_t_wind_flags flags
)
202 SCM_NEWSMOB2 (w
, tc16_winder
, (scm_t_bits
) proc
, (scm_t_bits
) data
);
203 if (flags
& SCM_F_WIND_EXPLICITLY
)
204 SCM_SET_SMOB_FLAGS (w
, WINDER_F_EXPLICIT
);
205 scm_i_set_dynwinds (scm_cons (w
, scm_i_dynwinds ()));
209 scm_dynwind_rewind_handler (void (*proc
) (void *), void *data
,
210 scm_t_wind_flags flags
)
213 SCM_NEWSMOB2 (w
, tc16_winder
, (scm_t_bits
) proc
, (scm_t_bits
) data
);
214 SCM_SET_SMOB_FLAGS (w
, WINDER_F_REWIND
);
215 scm_i_set_dynwinds (scm_cons (w
, scm_i_dynwinds ()));
216 if (flags
& SCM_F_WIND_EXPLICITLY
)
221 scm_dynwind_unwind_handler_with_scm (void (*proc
) (SCM
), SCM data
,
222 scm_t_wind_flags flags
)
225 scm_t_bits fl
= ((flags
&SCM_F_WIND_EXPLICITLY
)? WINDER_F_EXPLICIT
: 0);
226 SCM_NEWSMOB2 (w
, tc16_winder
, (scm_t_bits
) proc
, SCM_UNPACK (data
));
227 SCM_SET_SMOB_FLAGS (w
, fl
| WINDER_F_MARK
);
228 scm_i_set_dynwinds (scm_cons (w
, scm_i_dynwinds ()));
232 scm_dynwind_rewind_handler_with_scm (void (*proc
) (SCM
), SCM data
,
233 scm_t_wind_flags flags
)
236 SCM_NEWSMOB2 (w
, tc16_winder
, (scm_t_bits
) proc
, SCM_UNPACK (data
));
237 SCM_SET_SMOB_FLAGS (w
, WINDER_F_REWIND
| WINDER_F_MARK
);
238 scm_i_set_dynwinds (scm_cons (w
, scm_i_dynwinds ()));
239 if (flags
& SCM_F_WIND_EXPLICITLY
)
244 scm_dynwind_free (void *mem
)
246 scm_dynwind_unwind_handler (free
, mem
, SCM_F_WIND_EXPLICITLY
);
250 SCM_DEFINE (scm_wind_chain
, "wind-chain", 0, 0, 0,
252 "Return the current wind chain. The wind chain contains all\n"
253 "information required by @code{dynamic-wind} to call its\n"
254 "argument thunks when entering/exiting its scope.")
255 #define FUNC_NAME s_scm_wind_chain
257 return scm_i_dynwinds ();
263 scm_swap_bindings (SCM vars
, SCM vals
)
266 while (SCM_NIMP (vals
))
268 tmp
= SCM_VARIABLE_REF (SCM_CAR (vars
));
269 SCM_VARIABLE_SET (SCM_CAR (vars
), SCM_CAR (vals
));
270 SCM_SETCAR (vals
, tmp
);
271 vars
= SCM_CDR (vars
);
272 vals
= SCM_CDR (vals
);
277 scm_dowinds (SCM to
, long delta
)
279 scm_i_dowinds (to
, delta
, NULL
, NULL
);
283 scm_i_dowinds (SCM to
, long delta
, void (*turn_func
) (void *), void *data
)
286 if (scm_is_eq (to
, scm_i_dynwinds ()))
296 scm_i_dowinds (SCM_CDR (to
), 1 + delta
, turn_func
, data
);
297 wind_elt
= SCM_CAR (to
);
299 if (FRAME_P (wind_elt
))
301 if (!FRAME_REWINDABLE_P (wind_elt
))
302 scm_misc_error ("dowinds",
303 "cannot invoke continuation from this context",
306 else if (WINDER_P (wind_elt
))
308 if (WINDER_REWIND_P (wind_elt
))
309 WINDER_PROC (wind_elt
) (WINDER_DATA (wind_elt
));
313 wind_key
= SCM_CAR (wind_elt
);
314 /* key = #t | symbol | thunk | list of variables */
315 if (SCM_NIMP (wind_key
))
317 if (scm_is_pair (wind_key
))
319 if (SCM_VARIABLEP (SCM_CAR (wind_key
)))
320 scm_swap_bindings (wind_key
, SCM_CDR (wind_elt
));
322 else if (SCM_TYP3 (wind_key
) == scm_tc3_closure
)
323 scm_call_0 (wind_key
);
327 scm_i_set_dynwinds (to
);
335 wind
= scm_i_dynwinds ();
336 wind_elt
= SCM_CAR (wind
);
337 scm_i_set_dynwinds (SCM_CDR (wind
));
339 if (FRAME_P (wind_elt
))
343 else if (WINDER_P (wind_elt
))
345 if (!WINDER_REWIND_P (wind_elt
))
346 WINDER_PROC (wind_elt
) (WINDER_DATA (wind_elt
));
350 wind_key
= SCM_CAR (wind_elt
);
351 if (SCM_NIMP (wind_key
))
353 if (scm_is_pair (wind_key
))
355 if (SCM_VARIABLEP (SCM_CAR (wind_key
)))
356 scm_swap_bindings (wind_key
, SCM_CDR (wind_elt
));
358 else if (SCM_TYP3 (wind_key
) == scm_tc3_closure
)
359 scm_call_0 (SCM_CDR (wind_elt
));
364 goto tail
; /* scm_dowinds(to, delta-1); */
371 tc16_frame
= scm_make_smob_type ("frame", 0);
373 tc16_winder
= scm_make_smob_type ("winder", 0);
374 scm_set_smob_mark (tc16_winder
, winder_mark
);
376 #include "libguile/dynwind.x"