1 /* Copyright (C) 1995,1996,1997,1998,1999,2000, 2001, 2002 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
22 #include "libguile/_scm.h"
23 #include "libguile/stackchk.h"
24 #include "libguile/dynwind.h"
25 #include "libguile/eval.h"
26 #include "libguile/smob.h"
27 #include "libguile/pairs.h"
28 #include "libguile/throw.h"
29 #include "libguile/fluids.h"
30 #include "libguile/ports.h"
32 #include "libguile/root.h"
35 SCM scm_sys_protects
[SCM_NUM_PROTECTS
];
37 scm_t_bits scm_tc16_root
;
44 scm_root_state
*s
= SCM_ROOT_STATE (root
);
46 scm_gc_mark (s
->rootcont
);
47 scm_gc_mark (s
->dynwinds
);
48 scm_gc_mark (s
->progargs
);
49 scm_gc_mark (s
->exitval
);
50 scm_gc_mark (s
->cur_inp
);
51 scm_gc_mark (s
->cur_outp
);
52 scm_gc_mark (s
->cur_errp
);
53 /* No need to gc mark def_loadp */
54 scm_gc_mark (s
->fluids
);
55 scm_gc_mark (s
->active_asyncs
);
56 scm_gc_mark (s
->signal_asyncs
);
57 return SCM_ROOT_STATE (root
) -> parent
;
62 root_print (SCM exp
, SCM port
, scm_print_state
*pstate SCM_UNUSED
)
64 scm_puts ("#<root ", port
);
65 scm_intprint(SCM_SEQ (SCM_ROOT_STATE (exp
) -> rootcont
), 16, port
);
74 scm_make_root (SCM parent
)
77 scm_root_state
*root_state
;
79 root_state
= (scm_root_state
*) scm_gc_malloc (sizeof (scm_root_state
),
81 if (SCM_ROOTP (parent
))
83 memcpy (root_state
, SCM_ROOT_STATE (parent
), sizeof (scm_root_state
));
84 root_state
->parent
= parent
;
88 root_state
->parent
= SCM_BOOL_F
;
90 /* Initialize everything right now, in case a GC happens early. */
92 = root_state
->dynwinds
93 = root_state
->progargs
96 = root_state
->cur_outp
97 = root_state
->cur_errp
98 = root_state
->cur_loadp
105 root_state
->active_asyncs
= SCM_EOL
;
106 root_state
->signal_asyncs
= SCM_EOL
;
107 root_state
->block_asyncs
= 0;
108 root_state
->pending_asyncs
= 1;
110 SCM_NEWSMOB (root
, scm_tc16_root
, root_state
);
111 root_state
->handle
= root
;
113 if (SCM_ROOTP (parent
))
114 /* Must be done here so that fluids are GC protected */
115 scm_copy_fluids (root_state
);
120 /* {call-with-dynamic-root}
122 * Suspending the current thread to evaluate a thunk on the
123 * same C stack but under a new root.
125 * Calls to call-with-dynamic-root return exactly once (unless
126 * the process is somehow exitted). */
128 /* Some questions about cwdr:
130 Couldn't the body just be a closure? Do we really need to pass
133 The semantics are a lot like catch's; in fact, we call
134 scm_internal_catch to take care of that part of things. Wouldn't
135 it be cleaner to say that uncaught throws just disappear into the
136 ether (or print a message to stderr), and let the caller use catch
137 themselves if they want to?
142 SCM scm_exitval
; /* INUM with return value */
144 static long n_dynamic_roots
= 0;
147 /* cwdr fills out both of these structures, and then passes a pointer
148 to them through scm_internal_catch to the cwdr_body and
149 cwdr_handler functions, to tell them how to behave and to get
150 information back from them.
152 A cwdr is a lot like a catch, except there is no tag (all
153 exceptions are caught), and the body procedure takes the arguments
154 passed to cwdr as A1 and ARGS. The handler is also special since
155 it is not directly run from scm_internal_catch. It is executed
156 outside the new dynamic root. */
158 struct cwdr_body_data
{
159 /* Arguments to pass to the cwdr body function. */
162 /* Scheme procedure to use as body of cwdr. */
166 struct cwdr_handler_data
{
167 /* Do we need to run the handler? */
170 /* The tag and args to pass it. */
175 /* Invoke the body of a cwdr, assuming that the throw handler has
176 already been set up. DATA points to a struct set up by cwdr that
177 says what proc to call, and what args to apply it to.
179 With a little thought, we could replace this with scm_body_thunk,
180 but I don't want to mess with that at the moment. */
182 cwdr_body (void *data
)
184 struct cwdr_body_data
*c
= (struct cwdr_body_data
*) data
;
186 return scm_apply (c
->body_proc
, c
->a1
, c
->args
);
189 /* Record the fact that the body of the cwdr has thrown. Record
190 enough information to invoke the handler later when the dynamic
191 root has been deestablished. */
194 cwdr_handler (void *data
, SCM tag
, SCM args
)
196 struct cwdr_handler_data
*c
= (struct cwdr_handler_data
*) data
;
201 return SCM_UNSPECIFIED
;
204 /* This is the basic code for new root creation.
206 * WARNING! The order of actions in this routine is in many ways
207 * critical. E. g., it is essential that an error doesn't leave Guile
208 * in a messed up state. */
211 scm_internal_cwdr (scm_t_catch_body body
, void *body_data
,
212 scm_t_catch_handler handler
, void *handler_data
,
213 SCM_STACKITEM
*stack_start
)
215 SCM old_rootcont
, old_winds
;
216 struct cwdr_handler_data my_handler_data
;
219 /* Create a fresh root continuation. */
225 scm_t_contregs
*contregs
= scm_gc_malloc (sizeof (scm_t_contregs
),
228 contregs
->num_stack_items
= 0;
229 contregs
->dynenv
= SCM_EOL
;
230 contregs
->base
= stack_start
;
231 contregs
->seq
= ++n_dynamic_roots
;
232 contregs
->throw_value
= SCM_BOOL_F
;
233 #ifdef DEBUG_EXTENSIONS
234 contregs
->dframe
= 0;
236 SCM_NEWSMOB (new_rootcont
, scm_tc16_continuation
, contregs
);
238 old_rootcont
= scm_rootcont
;
239 scm_rootcont
= new_rootcont
;
243 /* Exit caller's dynamic state.
245 old_winds
= scm_dynwinds
;
246 scm_dowinds (SCM_EOL
, scm_ilength (scm_dynwinds
));
247 #ifdef DEBUG_EXTENSIONS
248 SCM_DFRAME (old_rootcont
) = scm_last_debug_frame
;
249 scm_last_debug_frame
= 0;
253 my_handler_data
.run_handler
= 0;
254 answer
= scm_internal_catch (SCM_BOOL_T
,
256 cwdr_handler
, &my_handler_data
);
259 scm_dowinds (old_winds
, - scm_ilength (old_winds
));
261 #ifdef DEBUG_EXTENSIONS
262 scm_last_debug_frame
= SCM_DFRAME (old_rootcont
);
264 scm_rootcont
= old_rootcont
;
267 /* Now run the real handler iff the body did a throw. */
268 if (my_handler_data
.run_handler
)
269 return handler (handler_data
, my_handler_data
.tag
, my_handler_data
.args
);
274 /* The original CWDR for invoking Scheme code with a Scheme handler. */
277 cwdr (SCM proc
, SCM a1
, SCM args
, SCM handler
, SCM_STACKITEM
*stack_start
)
279 struct cwdr_body_data c
;
285 return scm_internal_cwdr (cwdr_body
, &c
,
286 scm_handle_by_proc
, &handler
,
290 SCM_DEFINE (scm_call_with_dynamic_root
, "call-with-dynamic-root", 2, 0, 0,
291 (SCM thunk
, SCM handler
),
292 "Evaluate @code{(thunk)} in a new dynamic context, returning its value.\n\n"
293 "If an error occurs during evaluation, apply @var{handler} to the\n"
294 "arguments to the throw, just as @code{throw} would. If this happens,\n"
295 "@var{handler} is called outside the scope of the new root -- it is\n"
296 "called in the same dynamic context in which\n"
297 "@code{call-with-dynamic-root} was evaluated.\n\n"
298 "If @var{thunk} captures a continuation, the continuation is rooted at\n"
299 "the call to @var{thunk}. In particular, the call to\n"
300 "@code{call-with-dynamic-root} is not captured. Therefore,\n"
301 "@code{call-with-dynamic-root} always returns at most one time.\n\n"
302 "Before calling @var{thunk}, the dynamic-wind chain is un-wound back to\n"
303 "the root and a new chain started for @var{thunk}. Therefore, this call\n"
304 "may not do what you expect:\n\n"
306 ";; Almost certainly a bug:\n"
307 "(with-output-to-port\n"
310 " (call-with-dynamic-root\n"
312 " (display 'fnord)\n"
314 " (lambda (errcode) errcode))))\n"
316 "The problem is, on what port will @samp{fnord} be displayed? You\n"
317 "might expect that because of the @code{with-output-to-port} that\n"
318 "it will be displayed on the port bound to @code{some-port}. But it\n"
319 "probably won't -- before evaluating the thunk, dynamic winds are\n"
320 "unwound, including those created by @code{with-output-to-port}.\n"
321 "So, the standard output port will have been re-set to its default value\n"
322 "before @code{display} is evaluated.\n\n"
323 "(This function was added to Guile mostly to help calls to functions in C\n"
324 "libraries that can not tolerate non-local exits or calls that return\n"
325 "multiple times. If such functions call back to the interpreter, it should\n"
326 "be under a new dynamic root.)")
327 #define FUNC_NAME s_scm_call_with_dynamic_root
329 SCM_STACKITEM stack_place
;
330 return cwdr (thunk
, SCM_EOL
, SCM_EOL
, handler
, &stack_place
);
334 SCM_DEFINE (scm_dynamic_root
, "dynamic-root", 0, 0, 0,
336 "Return an object representing the current dynamic root.\n\n"
337 "These objects are only useful for comparison using @code{eq?}.\n"
338 "They are currently represented as numbers, but your code should\n"
339 "in no way depend on this.")
340 #define FUNC_NAME s_scm_dynamic_root
342 return scm_ulong2num (SCM_SEQ (scm_root
->rootcont
));
347 scm_apply_with_dynamic_root (SCM proc
, SCM a1
, SCM args
, SCM handler
)
349 SCM_STACKITEM stack_place
;
350 return cwdr (proc
, a1
, args
, handler
, &stack_place
);
358 scm_tc16_root
= scm_make_smob_type ("root", sizeof (struct scm_root_state
));
359 scm_set_smob_mark (scm_tc16_root
, root_mark
);
360 scm_set_smob_print (scm_tc16_root
, root_print
);
362 #include "libguile/root.x"