* validate.h
[bpt/guile.git] / libguile / root.c
CommitLineData
f2c9fcb0 1/* Copyright (C) 1995,1996,1997,1998, 1999, 2000 Free Software Foundation, Inc.
0f2d19dd
JB
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2, or (at your option)
6 * any later version.
7 *
8 * This program 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
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; see the file COPYING. If not, write to
82892bed
JB
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
0f2d19dd
JB
17 *
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
20 *
21 * The exception is that, if you link the GUILE library with other files
22 * to produce an executable, this does not by itself cause the
23 * resulting executable to be covered by the GNU General Public License.
24 * Your use of that executable is in no way restricted on account of
25 * linking the GUILE library code into it.
26 *
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
29 *
30 * This exception applies only to the code released by the
31 * Free Software Foundation under the name GUILE. If you copy
32 * code from other Free Software Foundation releases into a copy of
33 * GUILE, as the General Public License permits, the exception does
34 * not apply to the code that you add in this way. To avoid misleading
35 * anyone as to the status of such modified files, you must delete
36 * this exception notice from them.
37 *
38 * If you write modifications of your own for GUILE, it is your choice
39 * whether to permit this exception to apply to your modifications.
82892bed 40 * If you do not wish that, delete this exception notice. */
1bbd0b84
GB
41
42/* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
43 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
44
0f2d19dd
JB
45\f
46
783e7774 47#include <string.h>
a0599745
MD
48#include "libguile/_scm.h"
49#include "libguile/stackchk.h"
50#include "libguile/dynwind.h"
51#include "libguile/eval.h"
52#include "libguile/smob.h"
53#include "libguile/pairs.h"
54#include "libguile/throw.h"
55#include "libguile/fluids.h"
56#include "libguile/ports.h"
57
58#include "libguile/root.h"
0f2d19dd
JB
59\f
60
61SCM scm_sys_protects[SCM_NUM_PROTECTS];
d564d753 62
e841c3e0 63scm_bits_t scm_tc16_root;
d564d753
MD
64
65#ifndef USE_THREADS
66struct scm_root_state *scm_root;
67#endif
0f2d19dd
JB
68
69\f
70
d564d753 71static SCM
e841c3e0 72root_mark (SCM root)
d564d753
MD
73{
74 scm_root_state *s = SCM_ROOT_STATE (root);
dc53f026 75
d564d753
MD
76 scm_gc_mark (s->rootcont);
77 scm_gc_mark (s->dynwinds);
78 scm_gc_mark (s->continuation_stack);
79 scm_gc_mark (s->continuation_stack_ptr);
80 scm_gc_mark (s->progargs);
81 scm_gc_mark (s->exitval);
82 scm_gc_mark (s->cur_inp);
83 scm_gc_mark (s->cur_outp);
84 scm_gc_mark (s->cur_errp);
85 scm_gc_mark (s->def_inp);
86 scm_gc_mark (s->def_outp);
87 scm_gc_mark (s->def_errp);
903073d5 88 /* No need to gc mark def_loadp */
d9dfcf80 89 scm_gc_mark (s->fluids);
d564d753
MD
90 return SCM_ROOT_STATE (root) -> parent;
91}
0f2d19dd 92
d564d753 93
d564d753 94static int
e841c3e0 95root_print (SCM exp, SCM port, scm_print_state *pstate)
d564d753 96{
b7f3516f 97 scm_puts ("#<root ", port);
d564d753 98 scm_intprint(SCM_SEQ (SCM_ROOT_STATE (exp) -> rootcont), 16, port);
b7f3516f 99 scm_putc('>', port);
d564d753
MD
100 return 1;
101}
102
d564d753
MD
103
104\f
105
106SCM
1bbd0b84 107scm_make_root (SCM parent)
d564d753
MD
108{
109 SCM root;
110 scm_root_state *root_state;
111
112 root_state = (scm_root_state *) scm_must_malloc (sizeof (scm_root_state),
113 "scm_make_root");
0c95b57d 114 if (SCM_ROOTP (parent))
d564d753
MD
115 {
116 memcpy (root_state, SCM_ROOT_STATE (parent), sizeof (scm_root_state));
d9dfcf80 117 scm_copy_fluids (root_state);
d564d753
MD
118 root_state->parent = parent;
119 }
120 else
121 {
122 root_state->parent = SCM_BOOL_F;
4ea1f83d
JB
123
124 /* Initialize everything right now, in case a GC happens early. */
125 root_state->rootcont
126 = root_state->dynwinds
127 = root_state->continuation_stack
128 = root_state->continuation_stack_ptr
129 = root_state->progargs
130 = root_state->exitval
131 = root_state->cur_inp
132 = root_state->cur_outp
133 = root_state->cur_errp
134 = root_state->def_inp
135 = root_state->def_outp
136 = root_state->def_errp
137 = root_state->cur_loadp
138 = root_state->fluids
4ea1f83d
JB
139 = root_state->handle
140 = root_state->parent
141 = SCM_BOOL_F;
d564d753 142 }
d564d753 143 SCM_REDEFER_INTS;
23a62151 144 SCM_NEWSMOB (root, scm_tc16_root, root_state);
d564d753
MD
145 root_state->handle = root;
146 SCM_REALLOW_INTS;
147 return root;
148}
149
1cc91f1b 150/* {call-with-dynamic-root}
d564d753
MD
151 *
152 * Suspending the current thread to evaluate a thunk on the
153 * same C stack but under a new root.
154 *
1cc91f1b 155 * Calls to call-with-dynamic-root return exactly once (unless
e71575d9 156 * the process is somehow exitted). */
d564d753 157
650fa1ab
JB
158/* Some questions about cwdr:
159
160 Couldn't the body just be a closure? Do we really need to pass
161 args through to it?
162
163 The semantics are a lot like catch's; in fact, we call
164 scm_internal_catch to take care of that part of things. Wouldn't
165 it be cleaner to say that uncaught throws just disappear into the
166 ether (or print a message to stderr), and let the caller use catch
167 themselves if they want to?
168
169 -JimB */
170
d564d753
MD
171#if 0
172SCM scm_exitval; /* INUM with return value */
173#endif
1be6b49c 174static scm_bits_t n_dynamic_roots = 0;
d564d753 175
650fa1ab 176
e71575d9
MV
177/* cwdr fills out both of these structures, and then passes a pointer
178 to them through scm_internal_catch to the cwdr_body and
179 cwdr_handler functions, to tell them how to behave and to get
180 information back from them.
650fa1ab
JB
181
182 A cwdr is a lot like a catch, except there is no tag (all
183 exceptions are caught), and the body procedure takes the arguments
e71575d9
MV
184 passed to cwdr as A1 and ARGS. The handler is also special since
185 it is not directly run from scm_internal_catch. It is executed
186 outside the new dynamic root. */
650fa1ab
JB
187
188struct cwdr_body_data {
650fa1ab
JB
189 /* Arguments to pass to the cwdr body function. */
190 SCM a1, args;
191
192 /* Scheme procedure to use as body of cwdr. */
193 SCM body_proc;
e71575d9
MV
194};
195
196struct cwdr_handler_data {
197 /* Do we need to run the handler? */
198 int run_handler;
f032b8a8 199
e71575d9
MV
200 /* The tag and args to pass it. */
201 SCM tag, args;
650fa1ab
JB
202};
203
204
205/* Invoke the body of a cwdr, assuming that the throw handler has
206 already been set up. DATA points to a struct set up by cwdr that
816a6f06
JB
207 says what proc to call, and what args to apply it to.
208
209 With a little thought, we could replace this with scm_body_thunk,
210 but I don't want to mess with that at the moment. */
650fa1ab 211static SCM
39752bec 212cwdr_body (void *data)
650fa1ab
JB
213{
214 struct cwdr_body_data *c = (struct cwdr_body_data *) data;
215
216 return scm_apply (c->body_proc, c->a1, c->args);
217}
218
e71575d9
MV
219/* Record the fact that the body of the cwdr has thrown. Record
220 enough information to invoke the handler later when the dynamic
221 root has been deestablished. */
650fa1ab 222
f032b8a8 223static SCM
e71575d9 224cwdr_handler (void *data, SCM tag, SCM args)
f032b8a8 225{
e71575d9 226 struct cwdr_handler_data *c = (struct cwdr_handler_data *) data;
f032b8a8 227
e71575d9
MV
228 c->run_handler = 1;
229 c->tag = tag;
230 c->args = args;
231 return SCM_UNSPECIFIED;
f032b8a8 232}
d564d753
MD
233
234/* This is the basic code for new root creation.
235 *
236 * WARNING! The order of actions in this routine is in many ways
237 * critical. E. g., it is essential that an error doesn't leave Guile
650fa1ab 238 * in a messed up state. */
d564d753 239
e71575d9
MV
240SCM
241scm_internal_cwdr (scm_catch_body_t body, void *body_data,
242 scm_catch_handler_t handler, void *handler_data,
243 SCM_STACKITEM *stack_start)
d564d753
MD
244{
245 int old_ints_disabled = scm_ints_disabled;
8938d022 246 SCM old_rootcont, old_winds;
e71575d9 247 struct cwdr_handler_data my_handler_data;
d564d753
MD
248 SCM answer;
249
e71575d9 250 /* Create a fresh root continuation. */
d564d753
MD
251 {
252 SCM new_rootcont;
5f144b10 253
d564d753 254 SCM_REDEFER_INTS;
5f144b10 255 {
1be6b49c 256 scm_contregs_t *contregs = scm_must_malloc (sizeof (scm_contregs_t),
5f144b10
GH
257 "inferior root continuation");
258
259 contregs->num_stack_items = 0;
260 contregs->dynenv = SCM_EOL;
261 contregs->base = stack_start;
262 contregs->seq = ++n_dynamic_roots;
263 contregs->throw_value = SCM_BOOL_F;
d564d753 264#ifdef DEBUG_EXTENSIONS
5f144b10 265 contregs->dframe = 0;
d564d753 266#endif
5f144b10
GH
267 SCM_NEWSMOB (new_rootcont, scm_tc16_continuation, contregs);
268 }
8938d022
MD
269 old_rootcont = scm_rootcont;
270 scm_rootcont = new_rootcont;
d564d753
MD
271 SCM_REALLOW_INTS;
272 }
273
8938d022
MD
274 /* Exit caller's dynamic state.
275 */
276 old_winds = scm_dynwinds;
277 scm_dowinds (SCM_EOL, scm_ilength (scm_dynwinds));
d564d753 278#ifdef DEBUG_EXTENSIONS
308277cb 279 SCM_DFRAME (old_rootcont) = scm_last_debug_frame;
8938d022 280 scm_last_debug_frame = 0;
d564d753 281#endif
650fa1ab 282
e71575d9
MV
283 {
284 my_handler_data.run_handler = 0;
816a6f06 285 answer = scm_internal_catch (SCM_BOOL_T,
e71575d9
MV
286 body, body_data,
287 cwdr_handler, &my_handler_data);
650fa1ab 288 }
e71575d9 289
d564d753
MD
290 scm_dowinds (old_winds, - scm_ilength (old_winds));
291 SCM_REDEFER_INTS;
d564d753 292#ifdef DEBUG_EXTENSIONS
308277cb 293 scm_last_debug_frame = SCM_DFRAME (old_rootcont);
d564d753 294#endif
308277cb 295 scm_rootcont = old_rootcont;
d564d753
MD
296 SCM_REALLOW_INTS;
297 scm_ints_disabled = old_ints_disabled;
e71575d9
MV
298
299 /* Now run the real handler iff the body did a throw. */
300 if (my_handler_data.run_handler)
301 return handler (handler_data, my_handler_data.tag, my_handler_data.args);
302 else
303 return answer;
d564d753
MD
304}
305
e71575d9
MV
306/* The original CWDR for invoking Scheme code with a Scheme handler. */
307
308static SCM
309cwdr (SCM proc, SCM a1, SCM args, SCM handler, SCM_STACKITEM *stack_start)
310{
311 struct cwdr_body_data c;
312
313 c.a1 = a1;
314 c.args = args;
315 c.body_proc = proc;
316
317 return scm_internal_cwdr (cwdr_body, &c,
318 scm_handle_by_proc, &handler,
319 stack_start);
320}
d564d753 321
3b3b36dd 322SCM_DEFINE (scm_call_with_dynamic_root, "call-with-dynamic-root", 2, 0, 0,
1bbd0b84 323 (SCM thunk, SCM handler),
1bee0e70 324 "Evaluate @code{(thunk)} in a new dynamic context, returning its value.\n\n"
b380b885
MD
325 "If an error occurs during evaluation, apply @var{handler} to the\n"
326 "arguments to the throw, just as @code{throw} would. If this happens,\n"
327 "@var{handler} is called outside the scope of the new root -- it is\n"
328 "called in the same dynamic context in which\n"
329 "@code{call-with-dynamic-root} was evaluated.\n\n"
330 "If @var{thunk} captures a continuation, the continuation is rooted at\n"
331 "the call to @var{thunk}. In particular, the call to\n"
332 "@code{call-with-dynamic-root} is not captured. Therefore,\n"
333 "@code{call-with-dynamic-root} always returns at most one time.\n\n"
334 "Before calling @var{thunk}, the dynamic-wind chain is un-wound back to\n"
335 "the root and a new chain started for @var{thunk}. Therefore, this call\n"
336 "may not do what you expect:\n\n"
1e6808ea 337 "@lisp\n"
b380b885
MD
338 ";; Almost certainly a bug:\n"
339 "(with-output-to-port\n"
340 " some-port\n\n"
341 " (lambda ()\n"
342 " (call-with-dynamic-root\n"
343 " (lambda ()\n"
344 " (display 'fnord)\n"
345 " (newline))\n"
346 " (lambda (errcode) errcode))))\n"
1e6808ea 347 "@end lisp\n\n"
2a2a730b 348 "The problem is, on what port will @samp{fnord} be displayed? You\n"
b380b885
MD
349 "might expect that because of the @code{with-output-to-port} that\n"
350 "it will be displayed on the port bound to @code{some-port}. But it\n"
351 "probably won't -- before evaluating the thunk, dynamic winds are\n"
352 "unwound, including those created by @code{with-output-to-port}.\n"
353 "So, the standard output port will have been re-set to its default value\n"
354 "before @code{display} is evaluated.\n\n"
355 "(This function was added to Guile mostly to help calls to functions in C\n"
356 "libraries that can not tolerate non-local exits or calls that return\n"
357 "multiple times. If such functions call back to the interpreter, it should\n"
358 "be under a new dynamic root.)")
1bbd0b84 359#define FUNC_NAME s_scm_call_with_dynamic_root
d564d753
MD
360{
361 SCM_STACKITEM stack_place;
8938d022 362 return cwdr (thunk, SCM_EOL, SCM_EOL, handler, &stack_place);
d564d753 363}
1bbd0b84 364#undef FUNC_NAME
d564d753 365
3b3b36dd 366SCM_DEFINE (scm_dynamic_root, "dynamic-root", 0, 0, 0,
1bbd0b84 367 (),
b380b885
MD
368 "Return an object representing the current dynamic root.\n\n"
369 "These objects are only useful for comparison using @code{eq?}.\n"
370 "They are currently represented as numbers, but your code should\n"
371 "in no way depend on this.")
1bbd0b84 372#define FUNC_NAME s_scm_dynamic_root
d564d753
MD
373{
374 return scm_ulong2num (SCM_SEQ (scm_root->rootcont));
375}
1bbd0b84 376#undef FUNC_NAME
d564d753 377
d564d753 378SCM
1bbd0b84 379scm_apply_with_dynamic_root (SCM proc, SCM a1, SCM args, SCM handler)
d564d753
MD
380{
381 SCM_STACKITEM stack_place;
8938d022 382 return cwdr (proc, a1, args, handler, &stack_place);
d564d753 383}
0f2d19dd
JB
384
385\f
386
cc4feeca
DH
387#if (SCM_DEBUG_DEPRECATED == 0)
388
0f2d19dd
JB
389/* Call thunk(closure) underneath a top-level error handler.
390 * If an error occurs, pass the exitval through err_filter and return it.
391 * If no error occurs, return the value of thunk.
392 */
393
0f2d19dd
JB
394#ifdef _UNICOS
395typedef int setjmp_type;
396#else
397typedef long setjmp_type;
398#endif
399
400
0f2d19dd 401SCM
1bbd0b84 402scm_call_catching_errors (SCM (*thunk)(), SCM (*err_filter)(), void *closure)
0f2d19dd
JB
403{
404 SCM answer;
405 setjmp_type i;
faa6b3df 406#ifdef DEBUG_EXTENSIONS
8938d022 407 SCM_DFRAME (scm_rootcont) = scm_last_debug_frame;
faa6b3df 408#endif
0f2d19dd 409 i = setjmp (SCM_JMPBUF (scm_rootcont));
faa6b3df 410 scm_stack_checking_enabled_p = SCM_STACK_CHECKING_P;
0f2d19dd
JB
411 if (!i)
412 {
413 scm_gc_heap_lock = 0;
414 answer = thunk (closure);
415 }
416 else
417 {
418 scm_gc_heap_lock = 1;
419 answer = err_filter (scm_exitval, closure);
420 }
421 return answer;
422}
423
cc4feeca
DH
424#endif /* SCM_DEBUG_DEPRECATED == 0 */
425
426
d564d753
MD
427void
428scm_init_root ()
429{
cc4feeca 430 scm_tc16_root = scm_make_smob_type ("root", sizeof (struct scm_root_state));
e841c3e0
KN
431 scm_set_smob_mark (scm_tc16_root, root_mark);
432 scm_set_smob_print (scm_tc16_root, root_print);
cc4feeca 433
8dc9439f 434#ifndef SCM_MAGIC_SNARFER
a0599745 435#include "libguile/root.x"
8dc9439f 436#endif
d564d753 437}
89e00824
ML
438
439/*
440 Local Variables:
441 c-file-style: "gnu"
442 End:
443*/