Docstring updated so that make-soft-port example is correct.
[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
47#include <stdio.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
e71575d9
MV
61/* Define this if you want to try out the stack allocation of cwdr's
62 jumpbuf. It works for me but I'm still worried that the dynwinds
63 might be able to make a mess. */
64
65#undef USE_STACKJMPBUF
66
0f2d19dd 67SCM scm_sys_protects[SCM_NUM_PROTECTS];
d564d753
MD
68
69long scm_tc16_root;
70
71#ifndef USE_THREADS
72struct scm_root_state *scm_root;
73#endif
0f2d19dd
JB
74
75\f
76
d564d753 77static SCM
1bbd0b84 78mark_root (SCM root)
d564d753
MD
79{
80 scm_root_state *s = SCM_ROOT_STATE (root);
dc53f026 81
d564d753
MD
82 scm_gc_mark (s->rootcont);
83 scm_gc_mark (s->dynwinds);
84 scm_gc_mark (s->continuation_stack);
85 scm_gc_mark (s->continuation_stack_ptr);
86 scm_gc_mark (s->progargs);
87 scm_gc_mark (s->exitval);
88 scm_gc_mark (s->cur_inp);
89 scm_gc_mark (s->cur_outp);
90 scm_gc_mark (s->cur_errp);
91 scm_gc_mark (s->def_inp);
92 scm_gc_mark (s->def_outp);
93 scm_gc_mark (s->def_errp);
903073d5 94 /* No need to gc mark def_loadp */
d9dfcf80 95 scm_gc_mark (s->fluids);
d564d753
MD
96 return SCM_ROOT_STATE (root) -> parent;
97}
0f2d19dd 98
d564d753 99
d564d753 100static int
1bbd0b84 101print_root (SCM exp,SCM port,scm_print_state *pstate)
d564d753 102{
b7f3516f 103 scm_puts ("#<root ", port);
d564d753 104 scm_intprint(SCM_SEQ (SCM_ROOT_STATE (exp) -> rootcont), 16, port);
b7f3516f 105 scm_putc('>', port);
d564d753
MD
106 return 1;
107}
108
d564d753
MD
109
110\f
111
112SCM
1bbd0b84 113scm_make_root (SCM parent)
d564d753
MD
114{
115 SCM root;
116 scm_root_state *root_state;
117
118 root_state = (scm_root_state *) scm_must_malloc (sizeof (scm_root_state),
119 "scm_make_root");
0c95b57d 120 if (SCM_ROOTP (parent))
d564d753
MD
121 {
122 memcpy (root_state, SCM_ROOT_STATE (parent), sizeof (scm_root_state));
d9dfcf80 123 scm_copy_fluids (root_state);
d564d753
MD
124 root_state->parent = parent;
125 }
126 else
127 {
128 root_state->parent = SCM_BOOL_F;
4ea1f83d
JB
129
130 /* Initialize everything right now, in case a GC happens early. */
131 root_state->rootcont
132 = root_state->dynwinds
133 = root_state->continuation_stack
134 = root_state->continuation_stack_ptr
135 = root_state->progargs
136 = root_state->exitval
137 = root_state->cur_inp
138 = root_state->cur_outp
139 = root_state->cur_errp
140 = root_state->def_inp
141 = root_state->def_outp
142 = root_state->def_errp
143 = root_state->cur_loadp
144 = root_state->fluids
145 = root_state->system_transformer
146 = root_state->top_level_lookup_closure_var
147 = root_state->handle
148 = root_state->parent
149 = SCM_BOOL_F;
d564d753 150 }
d564d753 151 SCM_REDEFER_INTS;
23a62151 152 SCM_NEWSMOB (root, scm_tc16_root, root_state);
d564d753
MD
153 root_state->handle = root;
154 SCM_REALLOW_INTS;
155 return root;
156}
157
1cc91f1b 158/* {call-with-dynamic-root}
d564d753
MD
159 *
160 * Suspending the current thread to evaluate a thunk on the
161 * same C stack but under a new root.
162 *
1cc91f1b 163 * Calls to call-with-dynamic-root return exactly once (unless
e71575d9 164 * the process is somehow exitted). */
d564d753 165
650fa1ab
JB
166/* Some questions about cwdr:
167
168 Couldn't the body just be a closure? Do we really need to pass
169 args through to it?
170
171 The semantics are a lot like catch's; in fact, we call
172 scm_internal_catch to take care of that part of things. Wouldn't
173 it be cleaner to say that uncaught throws just disappear into the
174 ether (or print a message to stderr), and let the caller use catch
175 themselves if they want to?
176
177 -JimB */
178
d564d753
MD
179#if 0
180SCM scm_exitval; /* INUM with return value */
181#endif
182static int n_dynamic_roots = 0;
183
650fa1ab 184
e71575d9
MV
185/* cwdr fills out both of these structures, and then passes a pointer
186 to them through scm_internal_catch to the cwdr_body and
187 cwdr_handler functions, to tell them how to behave and to get
188 information back from them.
650fa1ab
JB
189
190 A cwdr is a lot like a catch, except there is no tag (all
191 exceptions are caught), and the body procedure takes the arguments
e71575d9
MV
192 passed to cwdr as A1 and ARGS. The handler is also special since
193 it is not directly run from scm_internal_catch. It is executed
194 outside the new dynamic root. */
650fa1ab
JB
195
196struct cwdr_body_data {
650fa1ab
JB
197 /* Arguments to pass to the cwdr body function. */
198 SCM a1, args;
199
200 /* Scheme procedure to use as body of cwdr. */
201 SCM body_proc;
e71575d9
MV
202};
203
204struct cwdr_handler_data {
205 /* Do we need to run the handler? */
206 int run_handler;
f032b8a8 207
e71575d9
MV
208 /* The tag and args to pass it. */
209 SCM tag, args;
650fa1ab
JB
210};
211
212
213/* Invoke the body of a cwdr, assuming that the throw handler has
214 already been set up. DATA points to a struct set up by cwdr that
816a6f06
JB
215 says what proc to call, and what args to apply it to.
216
217 With a little thought, we could replace this with scm_body_thunk,
218 but I don't want to mess with that at the moment. */
650fa1ab 219static SCM
39752bec 220cwdr_body (void *data)
650fa1ab
JB
221{
222 struct cwdr_body_data *c = (struct cwdr_body_data *) data;
223
224 return scm_apply (c->body_proc, c->a1, c->args);
225}
226
e71575d9
MV
227/* Record the fact that the body of the cwdr has thrown. Record
228 enough information to invoke the handler later when the dynamic
229 root has been deestablished. */
650fa1ab 230
f032b8a8 231static SCM
e71575d9 232cwdr_handler (void *data, SCM tag, SCM args)
f032b8a8 233{
e71575d9 234 struct cwdr_handler_data *c = (struct cwdr_handler_data *) data;
f032b8a8 235
e71575d9
MV
236 c->run_handler = 1;
237 c->tag = tag;
238 c->args = args;
239 return SCM_UNSPECIFIED;
f032b8a8 240}
d564d753
MD
241
242/* This is the basic code for new root creation.
243 *
244 * WARNING! The order of actions in this routine is in many ways
245 * critical. E. g., it is essential that an error doesn't leave Guile
650fa1ab 246 * in a messed up state. */
d564d753 247
e71575d9
MV
248SCM
249scm_internal_cwdr (scm_catch_body_t body, void *body_data,
250 scm_catch_handler_t handler, void *handler_data,
251 SCM_STACKITEM *stack_start)
d564d753 252{
e71575d9 253#ifdef USE_STACKJMPBUF
8b3bda20 254 scm_contregs static_contregs;
e71575d9 255#endif
d564d753 256 int old_ints_disabled = scm_ints_disabled;
8938d022 257 SCM old_rootcont, old_winds;
e71575d9 258 struct cwdr_handler_data my_handler_data;
d564d753
MD
259 SCM answer;
260
e71575d9 261 /* Create a fresh root continuation. */
d564d753
MD
262 {
263 SCM new_rootcont;
264 SCM_NEWCELL (new_rootcont);
265 SCM_REDEFER_INTS;
e71575d9 266#ifdef USE_STACKJMPBUF
8b3bda20 267 SCM_SET_CONTREGS (new_rootcont, &static_contregs);
e71575d9 268#else
8b3bda20
DH
269 SCM_SET_CONTREGS (new_rootcont,
270 scm_must_malloc (sizeof (scm_contregs),
271 "inferior root continuation"));
e71575d9 272#endif
54778cd3 273 SCM_SET_CELL_TYPE (new_rootcont, scm_tc7_contin);
d564d753
MD
274 SCM_DYNENV (new_rootcont) = SCM_EOL;
275 SCM_BASE (new_rootcont) = stack_start;
8938d022 276 SCM_SEQ (new_rootcont) = ++n_dynamic_roots;
d564d753
MD
277#ifdef DEBUG_EXTENSIONS
278 SCM_DFRAME (new_rootcont) = 0;
279#endif
8938d022
MD
280 old_rootcont = scm_rootcont;
281 scm_rootcont = new_rootcont;
d564d753
MD
282 SCM_REALLOW_INTS;
283 }
284
8938d022
MD
285 /* Exit caller's dynamic state.
286 */
287 old_winds = scm_dynwinds;
288 scm_dowinds (SCM_EOL, scm_ilength (scm_dynwinds));
d564d753 289#ifdef DEBUG_EXTENSIONS
308277cb 290 SCM_DFRAME (old_rootcont) = scm_last_debug_frame;
8938d022 291 scm_last_debug_frame = 0;
d564d753 292#endif
650fa1ab 293
e71575d9
MV
294 {
295 my_handler_data.run_handler = 0;
816a6f06 296 answer = scm_internal_catch (SCM_BOOL_T,
e71575d9
MV
297 body, body_data,
298 cwdr_handler, &my_handler_data);
650fa1ab 299 }
e71575d9 300
d564d753
MD
301 scm_dowinds (old_winds, - scm_ilength (old_winds));
302 SCM_REDEFER_INTS;
e71575d9 303#ifdef USE_STACKCJMPBUF
8b3bda20 304 SCM_SET_CONTREGS (scm_rootcont, NULL);
e71575d9 305#endif
d564d753 306#ifdef DEBUG_EXTENSIONS
308277cb 307 scm_last_debug_frame = SCM_DFRAME (old_rootcont);
d564d753 308#endif
308277cb 309 scm_rootcont = old_rootcont;
d564d753
MD
310 SCM_REALLOW_INTS;
311 scm_ints_disabled = old_ints_disabled;
e71575d9
MV
312
313 /* Now run the real handler iff the body did a throw. */
314 if (my_handler_data.run_handler)
315 return handler (handler_data, my_handler_data.tag, my_handler_data.args);
316 else
317 return answer;
d564d753
MD
318}
319
e71575d9
MV
320/* The original CWDR for invoking Scheme code with a Scheme handler. */
321
322static SCM
323cwdr (SCM proc, SCM a1, SCM args, SCM handler, SCM_STACKITEM *stack_start)
324{
325 struct cwdr_body_data c;
326
327 c.a1 = a1;
328 c.args = args;
329 c.body_proc = proc;
330
331 return scm_internal_cwdr (cwdr_body, &c,
332 scm_handle_by_proc, &handler,
333 stack_start);
334}
d564d753 335
3b3b36dd 336SCM_DEFINE (scm_call_with_dynamic_root, "call-with-dynamic-root", 2, 0, 0,
1bbd0b84 337 (SCM thunk, SCM handler),
b380b885
MD
338 "Evaluate @var{(thunk)} in a new dynamic context, returning its value.\n\n"
339 "If an error occurs during evaluation, apply @var{handler} to the\n"
340 "arguments to the throw, just as @code{throw} would. If this happens,\n"
341 "@var{handler} is called outside the scope of the new root -- it is\n"
342 "called in the same dynamic context in which\n"
343 "@code{call-with-dynamic-root} was evaluated.\n\n"
344 "If @var{thunk} captures a continuation, the continuation is rooted at\n"
345 "the call to @var{thunk}. In particular, the call to\n"
346 "@code{call-with-dynamic-root} is not captured. Therefore,\n"
347 "@code{call-with-dynamic-root} always returns at most one time.\n\n"
348 "Before calling @var{thunk}, the dynamic-wind chain is un-wound back to\n"
349 "the root and a new chain started for @var{thunk}. Therefore, this call\n"
350 "may not do what you expect:\n\n"
351 "@example\n"
352 ";; Almost certainly a bug:\n"
353 "(with-output-to-port\n"
354 " some-port\n\n"
355 " (lambda ()\n"
356 " (call-with-dynamic-root\n"
357 " (lambda ()\n"
358 " (display 'fnord)\n"
359 " (newline))\n"
360 " (lambda (errcode) errcode))))\n"
361 "@end example\n\n"
362 "The problem is, on what port will @samp{fnord\n"
363 "} be displayed? You\n"
364 "might expect that because of the @code{with-output-to-port} that\n"
365 "it will be displayed on the port bound to @code{some-port}. But it\n"
366 "probably won't -- before evaluating the thunk, dynamic winds are\n"
367 "unwound, including those created by @code{with-output-to-port}.\n"
368 "So, the standard output port will have been re-set to its default value\n"
369 "before @code{display} is evaluated.\n\n"
370 "(This function was added to Guile mostly to help calls to functions in C\n"
371 "libraries that can not tolerate non-local exits or calls that return\n"
372 "multiple times. If such functions call back to the interpreter, it should\n"
373 "be under a new dynamic root.)")
1bbd0b84 374#define FUNC_NAME s_scm_call_with_dynamic_root
d564d753
MD
375{
376 SCM_STACKITEM stack_place;
8938d022 377 return cwdr (thunk, SCM_EOL, SCM_EOL, handler, &stack_place);
d564d753 378}
1bbd0b84 379#undef FUNC_NAME
d564d753 380
3b3b36dd 381SCM_DEFINE (scm_dynamic_root, "dynamic-root", 0, 0, 0,
1bbd0b84 382 (),
b380b885
MD
383 "Return an object representing the current dynamic root.\n\n"
384 "These objects are only useful for comparison using @code{eq?}.\n"
385 "They are currently represented as numbers, but your code should\n"
386 "in no way depend on this.")
1bbd0b84 387#define FUNC_NAME s_scm_dynamic_root
d564d753
MD
388{
389 return scm_ulong2num (SCM_SEQ (scm_root->rootcont));
390}
1bbd0b84 391#undef FUNC_NAME
d564d753 392
d564d753 393SCM
1bbd0b84 394scm_apply_with_dynamic_root (SCM proc, SCM a1, SCM args, SCM handler)
d564d753
MD
395{
396 SCM_STACKITEM stack_place;
8938d022 397 return cwdr (proc, a1, args, handler, &stack_place);
d564d753 398}
0f2d19dd
JB
399
400\f
401
cc4feeca
DH
402#if (SCM_DEBUG_DEPRECATED == 0)
403
0f2d19dd
JB
404/* Call thunk(closure) underneath a top-level error handler.
405 * If an error occurs, pass the exitval through err_filter and return it.
406 * If no error occurs, return the value of thunk.
407 */
408
0f2d19dd
JB
409#ifdef _UNICOS
410typedef int setjmp_type;
411#else
412typedef long setjmp_type;
413#endif
414
415
0f2d19dd 416SCM
1bbd0b84 417scm_call_catching_errors (SCM (*thunk)(), SCM (*err_filter)(), void *closure)
0f2d19dd
JB
418{
419 SCM answer;
420 setjmp_type i;
faa6b3df 421#ifdef DEBUG_EXTENSIONS
8938d022 422 SCM_DFRAME (scm_rootcont) = scm_last_debug_frame;
faa6b3df 423#endif
0f2d19dd 424 i = setjmp (SCM_JMPBUF (scm_rootcont));
faa6b3df 425 scm_stack_checking_enabled_p = SCM_STACK_CHECKING_P;
0f2d19dd
JB
426 if (!i)
427 {
428 scm_gc_heap_lock = 0;
429 answer = thunk (closure);
430 }
431 else
432 {
433 scm_gc_heap_lock = 1;
434 answer = err_filter (scm_exitval, closure);
435 }
436 return answer;
437}
438
cc4feeca
DH
439#endif /* SCM_DEBUG_DEPRECATED == 0 */
440
441
d564d753
MD
442void
443scm_init_root ()
444{
cc4feeca
DH
445 scm_tc16_root = scm_make_smob_type ("root", sizeof (struct scm_root_state));
446 scm_set_smob_mark (scm_tc16_root, mark_root);
447 scm_set_smob_print (scm_tc16_root, print_root);
448
a0599745 449#include "libguile/root.x"
d564d753 450}
89e00824
ML
451
452/*
453 Local Variables:
454 c-file-style: "gnu"
455 End:
456*/