*** empty log message ***
[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
MD
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);
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);
d564d753 65 scm_intprint(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
JB
93 = root_state->progargs
94 = root_state->exitval
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 */
115 scm_copy_fluids (root_state);
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;
d564d753 233#ifdef DEBUG_EXTENSIONS
5f144b10 234 contregs->dframe = 0;
d564d753 235#endif
5f144b10
GH
236 SCM_NEWSMOB (new_rootcont, scm_tc16_continuation, contregs);
237 }
8938d022
MD
238 old_rootcont = scm_rootcont;
239 scm_rootcont = new_rootcont;
d564d753
MD
240 SCM_REALLOW_INTS;
241 }
242
8938d022
MD
243 /* Exit caller's dynamic state.
244 */
245 old_winds = scm_dynwinds;
246 scm_dowinds (SCM_EOL, scm_ilength (scm_dynwinds));
d564d753 247#ifdef DEBUG_EXTENSIONS
308277cb 248 SCM_DFRAME (old_rootcont) = scm_last_debug_frame;
8938d022 249 scm_last_debug_frame = 0;
d564d753 250#endif
650fa1ab 251
e71575d9
MV
252 {
253 my_handler_data.run_handler = 0;
816a6f06 254 answer = scm_internal_catch (SCM_BOOL_T,
e71575d9
MV
255 body, body_data,
256 cwdr_handler, &my_handler_data);
650fa1ab 257 }
e71575d9 258
d564d753
MD
259 scm_dowinds (old_winds, - scm_ilength (old_winds));
260 SCM_REDEFER_INTS;
d564d753 261#ifdef DEBUG_EXTENSIONS
308277cb 262 scm_last_debug_frame = SCM_DFRAME (old_rootcont);
d564d753 263#endif
308277cb 264 scm_rootcont = old_rootcont;
d564d753 265 SCM_REALLOW_INTS;
e71575d9
MV
266
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);
270 else
271 return answer;
d564d753
MD
272}
273
e71575d9
MV
274/* The original CWDR for invoking Scheme code with a Scheme handler. */
275
276static SCM
277cwdr (SCM proc, SCM a1, SCM args, SCM handler, SCM_STACKITEM *stack_start)
278{
279 struct cwdr_body_data c;
280
281 c.a1 = a1;
282 c.args = args;
283 c.body_proc = proc;
284
285 return scm_internal_cwdr (cwdr_body, &c,
286 scm_handle_by_proc, &handler,
287 stack_start);
288}
d564d753 289
3b3b36dd 290SCM_DEFINE (scm_call_with_dynamic_root, "call-with-dynamic-root", 2, 0, 0,
1bbd0b84 291 (SCM thunk, SCM handler),
1bee0e70 292 "Evaluate @code{(thunk)} in a new dynamic context, returning its value.\n\n"
b380b885
MD
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"
1e6808ea 305 "@lisp\n"
b380b885
MD
306 ";; Almost certainly a bug:\n"
307 "(with-output-to-port\n"
308 " some-port\n\n"
309 " (lambda ()\n"
310 " (call-with-dynamic-root\n"
311 " (lambda ()\n"
312 " (display 'fnord)\n"
313 " (newline))\n"
314 " (lambda (errcode) errcode))))\n"
1e6808ea 315 "@end lisp\n\n"
2a2a730b 316 "The problem is, on what port will @samp{fnord} be displayed? You\n"
b380b885
MD
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.)")
1bbd0b84 327#define FUNC_NAME s_scm_call_with_dynamic_root
d564d753
MD
328{
329 SCM_STACKITEM stack_place;
8938d022 330 return cwdr (thunk, SCM_EOL, SCM_EOL, handler, &stack_place);
d564d753 331}
1bbd0b84 332#undef FUNC_NAME
d564d753 333
3b3b36dd 334SCM_DEFINE (scm_dynamic_root, "dynamic-root", 0, 0, 0,
1bbd0b84 335 (),
b380b885
MD
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.")
1bbd0b84 340#define FUNC_NAME s_scm_dynamic_root
d564d753
MD
341{
342 return scm_ulong2num (SCM_SEQ (scm_root->rootcont));
343}
1bbd0b84 344#undef FUNC_NAME
d564d753 345
d564d753 346SCM
1bbd0b84 347scm_apply_with_dynamic_root (SCM proc, SCM a1, SCM args, SCM handler)
d564d753
MD
348{
349 SCM_STACKITEM stack_place;
8938d022 350 return cwdr (proc, a1, args, handler, &stack_place);
d564d753 351}
0f2d19dd
JB
352
353\f
354
d564d753
MD
355void
356scm_init_root ()
357{
cc4feeca 358 scm_tc16_root = scm_make_smob_type ("root", sizeof (struct scm_root_state));
e841c3e0
KN
359 scm_set_smob_mark (scm_tc16_root, root_mark);
360 scm_set_smob_print (scm_tc16_root, root_print);
cc4feeca 361
a0599745 362#include "libguile/root.x"
d564d753 363}
89e00824
ML
364
365/*
366 Local Variables:
367 c-file-style: "gnu"
368 End:
369*/