Reverted changed from 2005/01/24 19:14:54, which was a commit to the
[bpt/guile.git] / libguile / root.c
CommitLineData
d4719ab8 1/* Copyright (C) 1995,1996,1997,1998,1999,2000, 2001, 2002 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
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 */
1bbd0b84 17
1bbd0b84 18
0f2d19dd
JB
19\f
20
783e7774 21#include <string.h>
a0599745
MD
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"
31
32#include "libguile/root.h"
0f2d19dd
JB
33\f
34
35SCM scm_sys_protects[SCM_NUM_PROTECTS];
d564d753 36
92c2555f 37scm_t_bits scm_tc16_root;
d564d753 38
0f2d19dd
JB
39\f
40
d564d753 41static SCM
e841c3e0 42root_mark (SCM root)
d564d753
MD
43{
44 scm_root_state *s = SCM_ROOT_STATE (root);
dc53f026 45
d564d753
MD
46 scm_gc_mark (s->rootcont);
47 scm_gc_mark (s->dynwinds);
d564d753 48 scm_gc_mark (s->progargs);
76da80e7 49 scm_gc_mark (s->exitval);
d564d753
MD
50 scm_gc_mark (s->cur_inp);
51 scm_gc_mark (s->cur_outp);
52 scm_gc_mark (s->cur_errp);
903073d5 53 /* No need to gc mark def_loadp */
d9dfcf80 54 scm_gc_mark (s->fluids);
a7d3641d 55 scm_gc_mark (s->active_asyncs);
1ceead47 56 scm_gc_mark (s->signal_asyncs);
d564d753
MD
57 return SCM_ROOT_STATE (root) -> parent;
58}
0f2d19dd 59
d564d753 60
d564d753 61static int
e81d98ec 62root_print (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED)
d564d753 63{
b7f3516f 64 scm_puts ("#<root ", port);
0345e278 65 scm_uintprint(SCM_SEQ (SCM_ROOT_STATE (exp) -> rootcont), 16, port);
b7f3516f 66 scm_putc('>', port);
d564d753
MD
67 return 1;
68}
69
d564d753
MD
70
71\f
72
73SCM
1bbd0b84 74scm_make_root (SCM parent)
d564d753
MD
75{
76 SCM root;
77 scm_root_state *root_state;
78
4c9419ac
MV
79 root_state = (scm_root_state *) scm_gc_malloc (sizeof (scm_root_state),
80 "root state");
0c95b57d 81 if (SCM_ROOTP (parent))
d564d753
MD
82 {
83 memcpy (root_state, SCM_ROOT_STATE (parent), sizeof (scm_root_state));
84 root_state->parent = parent;
85 }
86 else
87 {
88 root_state->parent = SCM_BOOL_F;
4ea1f83d
JB
89
90 /* Initialize everything right now, in case a GC happens early. */
91 root_state->rootcont
92 = root_state->dynwinds
4ea1f83d 93 = root_state->progargs
76da80e7 94 = root_state->exitval
4ea1f83d
JB
95 = root_state->cur_inp
96 = root_state->cur_outp
97 = root_state->cur_errp
4ea1f83d
JB
98 = root_state->cur_loadp
99 = root_state->fluids
4ea1f83d
JB
100 = root_state->handle
101 = root_state->parent
102 = SCM_BOOL_F;
d564d753 103 }
d4719ab8
MV
104
105 root_state->active_asyncs = SCM_EOL;
1ceead47 106 root_state->signal_asyncs = SCM_EOL;
8ee25fb9 107 root_state->block_asyncs = 0;
1ceead47 108 root_state->pending_asyncs = 1;
d4719ab8 109
23a62151 110 SCM_NEWSMOB (root, scm_tc16_root, root_state);
d564d753 111 root_state->handle = root;
392d2833
MD
112
113 if (SCM_ROOTP (parent))
114 /* Must be done here so that fluids are GC protected */
a52dbe01 115 scm_i_copy_fluids (root_state);
392d2833 116
d564d753
MD
117 return root;
118}
119
1cc91f1b 120/* {call-with-dynamic-root}
d564d753
MD
121 *
122 * Suspending the current thread to evaluate a thunk on the
123 * same C stack but under a new root.
124 *
1cc91f1b 125 * Calls to call-with-dynamic-root return exactly once (unless
e71575d9 126 * the process is somehow exitted). */
d564d753 127
650fa1ab
JB
128/* Some questions about cwdr:
129
130 Couldn't the body just be a closure? Do we really need to pass
131 args through to it?
132
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?
138
139 -JimB */
140
d564d753
MD
141#if 0
142SCM scm_exitval; /* INUM with return value */
143#endif
c014a02e 144static long n_dynamic_roots = 0;
d564d753 145
650fa1ab 146
e71575d9
MV
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.
650fa1ab
JB
151
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
e71575d9
MV
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. */
650fa1ab
JB
157
158struct cwdr_body_data {
650fa1ab
JB
159 /* Arguments to pass to the cwdr body function. */
160 SCM a1, args;
161
162 /* Scheme procedure to use as body of cwdr. */
163 SCM body_proc;
e71575d9
MV
164};
165
166struct cwdr_handler_data {
167 /* Do we need to run the handler? */
168 int run_handler;
f032b8a8 169
e71575d9
MV
170 /* The tag and args to pass it. */
171 SCM tag, args;
650fa1ab
JB
172};
173
174
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
816a6f06
JB
177 says what proc to call, and what args to apply it to.
178
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. */
650fa1ab 181static SCM
39752bec 182cwdr_body (void *data)
650fa1ab
JB
183{
184 struct cwdr_body_data *c = (struct cwdr_body_data *) data;
185
186 return scm_apply (c->body_proc, c->a1, c->args);
187}
188
e71575d9
MV
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. */
650fa1ab 192
f032b8a8 193static SCM
e71575d9 194cwdr_handler (void *data, SCM tag, SCM args)
f032b8a8 195{
e71575d9 196 struct cwdr_handler_data *c = (struct cwdr_handler_data *) data;
f032b8a8 197
e71575d9
MV
198 c->run_handler = 1;
199 c->tag = tag;
200 c->args = args;
201 return SCM_UNSPECIFIED;
f032b8a8 202}
d564d753
MD
203
204/* This is the basic code for new root creation.
205 *
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
650fa1ab 208 * in a messed up state. */
d564d753 209
e71575d9 210SCM
92c2555f
MV
211scm_internal_cwdr (scm_t_catch_body body, void *body_data,
212 scm_t_catch_handler handler, void *handler_data,
e71575d9 213 SCM_STACKITEM *stack_start)
d564d753 214{
8938d022 215 SCM old_rootcont, old_winds;
e71575d9 216 struct cwdr_handler_data my_handler_data;
d564d753
MD
217 SCM answer;
218
e71575d9 219 /* Create a fresh root continuation. */
d564d753
MD
220 {
221 SCM new_rootcont;
5f144b10 222
d564d753 223 SCM_REDEFER_INTS;
5f144b10 224 {
4c9419ac
MV
225 scm_t_contregs *contregs = scm_gc_malloc (sizeof (scm_t_contregs),
226 "continuation");
5f144b10
GH
227
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;
5f144b10 233 contregs->dframe = 0;
5f144b10
GH
234 SCM_NEWSMOB (new_rootcont, scm_tc16_continuation, contregs);
235 }
8938d022
MD
236 old_rootcont = scm_rootcont;
237 scm_rootcont = new_rootcont;
d564d753
MD
238 SCM_REALLOW_INTS;
239 }
240
8938d022
MD
241 /* Exit caller's dynamic state.
242 */
243 old_winds = scm_dynwinds;
244 scm_dowinds (SCM_EOL, scm_ilength (scm_dynwinds));
308277cb 245 SCM_DFRAME (old_rootcont) = scm_last_debug_frame;
8938d022 246 scm_last_debug_frame = 0;
650fa1ab 247
e71575d9
MV
248 {
249 my_handler_data.run_handler = 0;
816a6f06 250 answer = scm_internal_catch (SCM_BOOL_T,
e71575d9
MV
251 body, body_data,
252 cwdr_handler, &my_handler_data);
650fa1ab 253 }
e71575d9 254
d564d753
MD
255 scm_dowinds (old_winds, - scm_ilength (old_winds));
256 SCM_REDEFER_INTS;
308277cb 257 scm_last_debug_frame = SCM_DFRAME (old_rootcont);
308277cb 258 scm_rootcont = old_rootcont;
d564d753 259 SCM_REALLOW_INTS;
e71575d9
MV
260
261 /* Now run the real handler iff the body did a throw. */
262 if (my_handler_data.run_handler)
263 return handler (handler_data, my_handler_data.tag, my_handler_data.args);
264 else
265 return answer;
d564d753
MD
266}
267
e71575d9
MV
268/* The original CWDR for invoking Scheme code with a Scheme handler. */
269
270static SCM
271cwdr (SCM proc, SCM a1, SCM args, SCM handler, SCM_STACKITEM *stack_start)
272{
273 struct cwdr_body_data c;
274
275 c.a1 = a1;
276 c.args = args;
277 c.body_proc = proc;
278
279 return scm_internal_cwdr (cwdr_body, &c,
280 scm_handle_by_proc, &handler,
281 stack_start);
282}
d564d753 283
3b3b36dd 284SCM_DEFINE (scm_call_with_dynamic_root, "call-with-dynamic-root", 2, 0, 0,
1bbd0b84 285 (SCM thunk, SCM handler),
1bee0e70 286 "Evaluate @code{(thunk)} in a new dynamic context, returning its value.\n\n"
b380b885
MD
287 "If an error occurs during evaluation, apply @var{handler} to the\n"
288 "arguments to the throw, just as @code{throw} would. If this happens,\n"
289 "@var{handler} is called outside the scope of the new root -- it is\n"
290 "called in the same dynamic context in which\n"
291 "@code{call-with-dynamic-root} was evaluated.\n\n"
292 "If @var{thunk} captures a continuation, the continuation is rooted at\n"
293 "the call to @var{thunk}. In particular, the call to\n"
294 "@code{call-with-dynamic-root} is not captured. Therefore,\n"
295 "@code{call-with-dynamic-root} always returns at most one time.\n\n"
296 "Before calling @var{thunk}, the dynamic-wind chain is un-wound back to\n"
297 "the root and a new chain started for @var{thunk}. Therefore, this call\n"
298 "may not do what you expect:\n\n"
1e6808ea 299 "@lisp\n"
b380b885
MD
300 ";; Almost certainly a bug:\n"
301 "(with-output-to-port\n"
302 " some-port\n\n"
303 " (lambda ()\n"
304 " (call-with-dynamic-root\n"
305 " (lambda ()\n"
306 " (display 'fnord)\n"
307 " (newline))\n"
308 " (lambda (errcode) errcode))))\n"
1e6808ea 309 "@end lisp\n\n"
2a2a730b 310 "The problem is, on what port will @samp{fnord} be displayed? You\n"
b380b885
MD
311 "might expect that because of the @code{with-output-to-port} that\n"
312 "it will be displayed on the port bound to @code{some-port}. But it\n"
313 "probably won't -- before evaluating the thunk, dynamic winds are\n"
314 "unwound, including those created by @code{with-output-to-port}.\n"
315 "So, the standard output port will have been re-set to its default value\n"
316 "before @code{display} is evaluated.\n\n"
317 "(This function was added to Guile mostly to help calls to functions in C\n"
318 "libraries that can not tolerate non-local exits or calls that return\n"
319 "multiple times. If such functions call back to the interpreter, it should\n"
320 "be under a new dynamic root.)")
1bbd0b84 321#define FUNC_NAME s_scm_call_with_dynamic_root
d564d753
MD
322{
323 SCM_STACKITEM stack_place;
8938d022 324 return cwdr (thunk, SCM_EOL, SCM_EOL, handler, &stack_place);
d564d753 325}
1bbd0b84 326#undef FUNC_NAME
d564d753 327
3b3b36dd 328SCM_DEFINE (scm_dynamic_root, "dynamic-root", 0, 0, 0,
1bbd0b84 329 (),
b380b885
MD
330 "Return an object representing the current dynamic root.\n\n"
331 "These objects are only useful for comparison using @code{eq?}.\n"
332 "They are currently represented as numbers, but your code should\n"
333 "in no way depend on this.")
1bbd0b84 334#define FUNC_NAME s_scm_dynamic_root
d564d753 335{
b9bd8526 336 return scm_from_ulong (SCM_SEQ (scm_root->rootcont));
d564d753 337}
1bbd0b84 338#undef FUNC_NAME
d564d753 339
d564d753 340SCM
1bbd0b84 341scm_apply_with_dynamic_root (SCM proc, SCM a1, SCM args, SCM handler)
d564d753
MD
342{
343 SCM_STACKITEM stack_place;
8938d022 344 return cwdr (proc, a1, args, handler, &stack_place);
d564d753 345}
0f2d19dd
JB
346
347\f
348
d564d753
MD
349void
350scm_init_root ()
351{
cc4feeca 352 scm_tc16_root = scm_make_smob_type ("root", sizeof (struct scm_root_state));
e841c3e0
KN
353 scm_set_smob_mark (scm_tc16_root, root_mark);
354 scm_set_smob_print (scm_tc16_root, root_print);
cc4feeca 355
a0599745 356#include "libguile/root.x"
d564d753 357}
89e00824
ML
358
359/*
360 Local Variables:
361 c-file-style: "gnu"
362 End:
363*/