1 /* Copyright (C) 1995,1996,1998,1999,2000,2001, 2003, 2004, 2006, 2008, 2010 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 License
5 * as published by the Free Software Foundation; either version 3 of
6 * the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful, but
9 * 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
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"
35 #include "libguile/dynwind.h"
40 Things that can be on the wind list:
44 (enter-proc . leave-proc) dynamic-wind
46 (tag . pre-unwind-data) throw-handler / lazy-catch
47 tag is either a symbol or a boolean
54 scm_dynamic_wind (SCM in_guard
, SCM thunk
, SCM out_guard
)
55 #define FUNC_NAME "dynamic-wind"
58 SCM_ASSERT (scm_is_true (scm_thunk_p (out_guard
)),
61 scm_call_0 (in_guard
);
62 old_winds
= scm_i_dynwinds ();
63 scm_i_set_dynwinds (scm_acons (in_guard
, out_guard
, old_winds
));
64 ans
= scm_call_0 (thunk
);
65 scm_i_set_dynwinds (old_winds
);
66 scm_call_0 (out_guard
);
72 scm_internal_dynamic_wind (scm_t_guard before
,
80 scm_dynwind_begin (SCM_F_DYNWIND_REWINDABLE
);
81 scm_dynwind_rewind_handler (before
, guard_data
, SCM_F_WIND_EXPLICITLY
);
82 scm_dynwind_unwind_handler (after
, guard_data
, SCM_F_WIND_EXPLICITLY
);
83 ans
= inner (inner_data
);
88 /* Frames and winders. */
90 static scm_t_bits tc16_frame
;
91 #define FRAME_P(f) SCM_SMOB_PREDICATE (tc16_frame, (f))
93 #define FRAME_F_REWINDABLE (1 << 0)
94 #define FRAME_REWINDABLE_P(f) (SCM_SMOB_FLAGS(f) & FRAME_F_REWINDABLE)
96 static scm_t_bits tc16_winder
;
97 #define WINDER_P(w) SCM_SMOB_PREDICATE (tc16_winder, (w))
98 #define WINDER_PROC(w) ((void (*)(void *))SCM_SMOB_DATA (w))
99 #define WINDER_DATA(w) ((void *)SCM_SMOB_DATA_2 (w))
101 #define WINDER_F_EXPLICIT (1 << 0)
102 #define WINDER_F_REWIND (1 << 1)
103 #define WINDER_F_MARK (1 << 2)
104 #define WINDER_EXPLICIT_P(w) (SCM_SMOB_FLAGS(w) & WINDER_F_EXPLICIT)
105 #define WINDER_REWIND_P(w) (SCM_SMOB_FLAGS(w) & WINDER_F_REWIND)
106 #define WINDER_MARK_P(w) (SCM_SMOB_FLAGS(w) & WINDER_F_MARK)
109 scm_dynwind_begin (scm_t_dynwind_flags flags
)
112 SCM_NEWSMOB (f
, tc16_frame
, 0);
113 if (flags
& SCM_F_DYNWIND_REWINDABLE
)
114 SCM_SET_SMOB_FLAGS (f
, FRAME_F_REWINDABLE
);
115 scm_i_set_dynwinds (scm_cons (f
, scm_i_dynwinds ()));
119 scm_dynwind_end (void)
123 /* Unwind upto and including the next frame entry. We can only
124 encounter #<winder> entries on the way.
127 winds
= scm_i_dynwinds ();
128 while (scm_is_pair (winds
))
130 SCM entry
= SCM_CAR (winds
);
131 winds
= SCM_CDR (winds
);
133 scm_i_set_dynwinds (winds
);
138 assert (WINDER_P (entry
));
139 if (!WINDER_REWIND_P (entry
) && WINDER_EXPLICIT_P (entry
))
140 WINDER_PROC(entry
) (WINDER_DATA (entry
));
147 scm_dynwind_unwind_handler (void (*proc
) (void *), void *data
,
148 scm_t_wind_flags flags
)
151 SCM_NEWSMOB2 (w
, tc16_winder
, (scm_t_bits
) proc
, (scm_t_bits
) data
);
152 if (flags
& SCM_F_WIND_EXPLICITLY
)
153 SCM_SET_SMOB_FLAGS (w
, WINDER_F_EXPLICIT
);
154 scm_i_set_dynwinds (scm_cons (w
, scm_i_dynwinds ()));
158 scm_dynwind_rewind_handler (void (*proc
) (void *), void *data
,
159 scm_t_wind_flags flags
)
162 SCM_NEWSMOB2 (w
, tc16_winder
, (scm_t_bits
) proc
, (scm_t_bits
) data
);
163 SCM_SET_SMOB_FLAGS (w
, WINDER_F_REWIND
);
164 scm_i_set_dynwinds (scm_cons (w
, scm_i_dynwinds ()));
165 if (flags
& SCM_F_WIND_EXPLICITLY
)
170 scm_dynwind_unwind_handler_with_scm (void (*proc
) (SCM
), SCM data
,
171 scm_t_wind_flags flags
)
174 scm_t_bits fl
= ((flags
&SCM_F_WIND_EXPLICITLY
)? WINDER_F_EXPLICIT
: 0);
175 SCM_NEWSMOB2 (w
, tc16_winder
, (scm_t_bits
) proc
, SCM_UNPACK (data
));
176 SCM_SET_SMOB_FLAGS (w
, fl
| WINDER_F_MARK
);
177 scm_i_set_dynwinds (scm_cons (w
, scm_i_dynwinds ()));
181 scm_dynwind_rewind_handler_with_scm (void (*proc
) (SCM
), SCM data
,
182 scm_t_wind_flags flags
)
185 SCM_NEWSMOB2 (w
, tc16_winder
, (scm_t_bits
) proc
, SCM_UNPACK (data
));
186 SCM_SET_SMOB_FLAGS (w
, WINDER_F_REWIND
| WINDER_F_MARK
);
187 scm_i_set_dynwinds (scm_cons (w
, scm_i_dynwinds ()));
188 if (flags
& SCM_F_WIND_EXPLICITLY
)
193 scm_dynwind_free (void *mem
)
195 scm_dynwind_unwind_handler (free
, mem
, SCM_F_WIND_EXPLICITLY
);
199 SCM_DEFINE (scm_wind_chain
, "wind-chain", 0, 0, 0,
201 "Return the current wind chain. The wind chain contains all\n"
202 "information required by @code{dynamic-wind} to call its\n"
203 "argument thunks when entering/exiting its scope.")
204 #define FUNC_NAME s_scm_wind_chain
206 return scm_i_dynwinds ();
212 scm_swap_bindings (SCM vars
, SCM vals
)
215 while (SCM_NIMP (vals
))
217 tmp
= SCM_VARIABLE_REF (SCM_CAR (vars
));
218 SCM_VARIABLE_SET (SCM_CAR (vars
), SCM_CAR (vals
));
219 SCM_SETCAR (vals
, tmp
);
220 vars
= SCM_CDR (vars
);
221 vals
= SCM_CDR (vals
);
226 scm_dowinds (SCM to
, long delta
)
228 scm_i_dowinds (to
, delta
, NULL
, NULL
);
232 scm_i_dowinds (SCM to
, long delta
, void (*turn_func
) (void *), void *data
)
235 if (scm_is_eq (to
, scm_i_dynwinds ()))
245 scm_i_dowinds (SCM_CDR (to
), 1 + delta
, turn_func
, data
);
246 wind_elt
= SCM_CAR (to
);
248 if (FRAME_P (wind_elt
))
250 if (!FRAME_REWINDABLE_P (wind_elt
))
251 scm_misc_error ("dowinds",
252 "cannot invoke continuation from this context",
255 else if (WINDER_P (wind_elt
))
257 if (WINDER_REWIND_P (wind_elt
))
258 WINDER_PROC (wind_elt
) (WINDER_DATA (wind_elt
));
260 else if (SCM_WITH_FLUIDS_P (wind_elt
))
262 scm_i_swap_with_fluids (wind_elt
,
263 SCM_I_CURRENT_THREAD
->dynamic_state
);
267 wind_key
= SCM_CAR (wind_elt
);
268 /* key = #t | symbol | thunk | list of variables */
269 if (SCM_NIMP (wind_key
))
271 if (scm_is_pair (wind_key
))
273 if (SCM_VARIABLEP (SCM_CAR (wind_key
)))
274 scm_swap_bindings (wind_key
, SCM_CDR (wind_elt
));
276 else if (scm_is_true (scm_thunk_p (wind_key
)))
277 scm_call_0 (wind_key
);
281 scm_i_set_dynwinds (to
);
289 wind
= scm_i_dynwinds ();
290 wind_elt
= SCM_CAR (wind
);
291 scm_i_set_dynwinds (SCM_CDR (wind
));
293 if (FRAME_P (wind_elt
))
297 else if (WINDER_P (wind_elt
))
299 if (!WINDER_REWIND_P (wind_elt
))
300 WINDER_PROC (wind_elt
) (WINDER_DATA (wind_elt
));
302 else if (SCM_WITH_FLUIDS_P (wind_elt
))
304 scm_i_swap_with_fluids (wind_elt
,
305 SCM_I_CURRENT_THREAD
->dynamic_state
);
309 wind_key
= SCM_CAR (wind_elt
);
310 if (SCM_NIMP (wind_key
))
312 if (scm_is_pair (wind_key
))
314 if (SCM_VARIABLEP (SCM_CAR (wind_key
)))
315 scm_swap_bindings (wind_key
, SCM_CDR (wind_elt
));
317 else if (scm_is_true (scm_thunk_p (wind_key
)))
318 scm_call_0 (SCM_CDR (wind_elt
));
323 goto tail
; /* scm_dowinds(to, delta-1); */
330 tc16_frame
= scm_make_smob_type ("frame", 0);
332 tc16_winder
= scm_make_smob_type ("winder", 0);
334 #include "libguile/dynwind.x"