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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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
44 ((fluid ...) . (value ...)) with-fluids
50 SCM_DEFINE (scm_dynamic_wind
, "dynamic-wind", 3, 0, 0,
51 (SCM in_guard
, SCM thunk
, SCM out_guard
),
52 "All three arguments must be 0-argument procedures.\n"
53 "@var{in_guard} is called, then @var{thunk}, then\n"
56 "If, any time during the execution of @var{thunk}, the\n"
57 "continuation of the @code{dynamic_wind} expression is escaped\n"
58 "non-locally, @var{out_guard} is called. If the continuation of\n"
59 "the dynamic-wind is re-entered, @var{in_guard} is called. Thus\n"
60 "@var{in_guard} and @var{out_guard} may be called any number of\n"
63 "(define x 'normal-binding)\n"
65 "(define a-cont (call-with-current-continuation\n"
71 " (lambda () (set! x 'special-binding))\n"
75 " (lambda () (display x) (newline)\n"
76 " (call-with-current-continuation escape)\n"
77 " (display x) (newline)\n"
82 " (lambda () (set! x old-x)))))))\n"
89 "@result{} normal-binding\n"
94 "@result{} a-cont ;; the value of the (define a-cont...)\n"
96 "@result{} normal-binding\n"
98 "@result{} special-binding\n"
100 #define FUNC_NAME s_scm_dynamic_wind
103 SCM_ASSERT (SCM_NFALSEP (scm_thunk_p (out_guard
)),
105 SCM_ARG3
, FUNC_NAME
);
106 scm_call_0 (in_guard
);
107 scm_dynwinds
= scm_acons (in_guard
, out_guard
, scm_dynwinds
);
108 ans
= scm_call_0 (thunk
);
109 scm_dynwinds
= SCM_CDR (scm_dynwinds
);
110 scm_call_0 (out_guard
);
116 scm_internal_dynamic_wind (scm_t_guard before
,
124 scm_begin_frame (SCM_F_FRAME_REWINDABLE
);
125 scm_on_rewind (before
, guard_data
, SCM_F_WIND_EXPLICITLY
);
126 scm_on_unwind (after
, guard_data
, SCM_F_WIND_EXPLICITLY
);
127 ans
= inner (inner_data
);
132 /* Frames and winders. */
134 static scm_t_bits tc16_frame
;
135 #define FRAME_P(f) SCM_SMOB_PREDICATE (tc16_frame, (f))
137 #define FRAME_F_REWINDABLE (1 << 16)
138 #define FRAME_REWINDABLE_P(f) (SCM_CELL_WORD_0(f) & FRAME_F_REWINDABLE)
140 static scm_t_bits tc16_winder
;
141 #define WINDER_P(w) SCM_SMOB_PREDICATE (tc16_winder, (w))
142 #define WINDER_PROC(w) ((void (*)(void *))SCM_CELL_WORD_1 (w))
143 #define WINDER_DATA(w) ((void *)SCM_CELL_WORD_2 (w))
145 #define WINDER_F_EXPLICIT (1 << 16)
146 #define WINDER_F_REWIND (1 << 17)
147 #define WINDER_F_MARK (1 << 18)
148 #define WINDER_EXPLICIT_P(w) (SCM_CELL_WORD_0(w) & WINDER_F_EXPLICIT)
149 #define WINDER_REWIND_P(w) (SCM_CELL_WORD_0(w) & WINDER_F_REWIND)
150 #define WINDER_MARK_P(w) (SCM_CELL_WORD_0(w) & WINDER_F_MARK)
153 scm_begin_frame (scm_t_frame_flags flags
)
156 scm_t_bits fl
= ((flags
&SCM_F_FRAME_REWINDABLE
)? FRAME_F_REWINDABLE
: 0);
157 SCM_NEWSMOB (f
, tc16_frame
| fl
, 0);
158 scm_dynwinds
= scm_cons (f
, scm_dynwinds
);
167 /* Unwind upto and including the next frame entry.
170 for (to
= scm_dynwinds
, delta
= 1;
172 to
= SCM_CDR (to
), delta
++)
174 if (FRAME_P (SCM_CAR (to
)))
176 scm_i_dowinds (SCM_CDR (to
), delta
, 1, NULL
, NULL
);
187 if (WINDER_MARK_P (w
))
188 return WINDER_DATA (w
);
193 scm_on_unwind (void (*proc
) (void *), void *data
,
194 scm_t_wind_flags flags
)
197 scm_t_bits fl
= ((flags
&SCM_F_WIND_EXPLICITLY
)? WINDER_F_EXPLICIT
: 0);
198 SCM_NEWSMOB2 (w
, tc16_winder
| fl
,
199 (scm_t_bits
) proc
, (scm_t_bits
) data
);
200 scm_dynwinds
= scm_cons (w
, scm_dynwinds
);
204 scm_on_rewind (void (*proc
) (void *), void *data
,
205 scm_t_wind_flags flags
)
208 SCM_NEWSMOB2 (w
, tc16_winder
| WINDER_F_REWIND
,
209 (scm_t_bits
) proc
, (scm_t_bits
) data
);
210 scm_dynwinds
= scm_cons (w
, scm_dynwinds
);
211 if (flags
& SCM_F_WIND_EXPLICITLY
)
216 scm_on_unwind_with_scm (void (*proc
) (SCM
), SCM data
,
217 scm_t_wind_flags flags
)
220 scm_t_bits fl
= ((flags
&SCM_F_WIND_EXPLICITLY
)? WINDER_F_EXPLICIT
: 0);
221 SCM_NEWSMOB2 (w
, tc16_winder
| fl
| WINDER_F_MARK
,
222 (scm_t_bits
) proc
, SCM_UNPACK (data
));
223 scm_dynwinds
= scm_cons (w
, scm_dynwinds
);
227 scm_on_rewind_with_scm (void (*proc
) (SCM
), SCM data
,
228 scm_t_wind_flags flags
)
231 SCM_NEWSMOB2 (w
, tc16_winder
| WINDER_F_REWIND
| WINDER_F_MARK
,
232 (scm_t_bits
) proc
, SCM_UNPACK (data
));
233 scm_dynwinds
= scm_cons (w
, scm_dynwinds
);
234 if (flags
& SCM_F_WIND_EXPLICITLY
)
239 SCM_DEFINE (scm_wind_chain
, "wind-chain", 0, 0, 0,
241 "Return the current wind chain. The wind chain contains all\n"
242 "information required by @code{dynamic-wind} to call its\n"
243 "argument thunks when entering/exiting its scope.")
244 #define FUNC_NAME s_scm_wind_chain
252 scm_swap_bindings (SCM vars
, SCM vals
)
255 while (SCM_NIMP (vals
))
257 tmp
= SCM_VARIABLE_REF (SCM_CAR (vars
));
258 SCM_VARIABLE_SET (SCM_CAR (vars
), SCM_CAR (vals
));
259 SCM_SETCAR (vals
, tmp
);
260 vars
= SCM_CDR (vars
);
261 vals
= SCM_CDR (vals
);
266 scm_dowinds (SCM to
, long delta
)
268 scm_i_dowinds (to
, delta
, 0, NULL
, NULL
);
272 scm_i_dowinds (SCM to
, long delta
, int explicit,
273 void (*turn_func
) (void *), void *data
)
276 if (SCM_EQ_P (to
, scm_dynwinds
))
286 scm_i_dowinds (SCM_CDR (to
), 1 + delta
, explicit,
288 wind_elt
= SCM_CAR (to
);
291 if (SCM_INUMP (wind_elt
))
293 scm_cross_dynwind_binding_scope (wind_elt
, 0);
298 if (FRAME_P (wind_elt
))
300 if (!FRAME_REWINDABLE_P (wind_elt
))
301 scm_misc_error ("dowinds",
302 "cannot invoke continuation from this context",
305 else if (WINDER_P (wind_elt
))
307 if (WINDER_REWIND_P (wind_elt
))
309 void (*proc
) (void *) = WINDER_PROC (wind_elt
);
310 void *data
= WINDER_DATA (wind_elt
);
316 wind_key
= SCM_CAR (wind_elt
);
317 /* key = #t | symbol | thunk | list of variables | list of fluids */
318 if (SCM_NIMP (wind_key
))
320 if (SCM_CONSP (wind_key
))
322 if (SCM_VARIABLEP (SCM_CAR (wind_key
)))
323 scm_swap_bindings (wind_key
, SCM_CDR (wind_elt
));
324 else if (SCM_FLUIDP (SCM_CAR (wind_key
)))
325 scm_i_swap_fluids (wind_key
, SCM_CDR (wind_elt
));
327 else if (SCM_TYP3 (wind_key
) == scm_tc3_closure
)
328 scm_call_0 (wind_key
);
339 wind_elt
= SCM_CAR (scm_dynwinds
);
340 scm_dynwinds
= SCM_CDR (scm_dynwinds
);
343 if (SCM_INUMP (wind_elt
))
345 scm_cross_dynwind_binding_scope (wind_elt
, 0);
350 if (FRAME_P (wind_elt
))
354 else if (WINDER_P (wind_elt
))
356 if (!WINDER_REWIND_P (wind_elt
)
357 && (!explicit || WINDER_EXPLICIT_P (wind_elt
)))
359 void (*proc
) (void *) = WINDER_PROC (wind_elt
);
360 void *data
= WINDER_DATA (wind_elt
);
366 wind_key
= SCM_CAR (wind_elt
);
367 if (SCM_NIMP (wind_key
))
369 if (SCM_CONSP (wind_key
))
371 if (SCM_VARIABLEP (SCM_CAR (wind_key
)))
372 scm_swap_bindings (wind_key
, SCM_CDR (wind_elt
));
373 else if (SCM_FLUIDP (SCM_CAR (wind_key
)))
374 scm_i_swap_fluids_reverse (wind_key
,
377 else if (SCM_TYP3 (wind_key
) == scm_tc3_closure
)
378 scm_call_0 (SCM_CDR (wind_elt
));
383 goto tail
; /* scm_dowinds(to, delta-1); */
390 tc16_frame
= scm_make_smob_type ("frame", 0);
392 tc16_winder
= scm_make_smob_type ("winder", 0);
393 scm_set_smob_mark (tc16_winder
, winder_mark
);
395 #include "libguile/dynwind.x"