* backtrace.c, debug.c, debug.h, deprecation.c, eq.c, eval.c
[bpt/guile.git] / libguile / throw.c
1 /* Copyright (C) 1995,1996,1997,1998,2000,2001, 2003, 2004, 2006 Free Software Foundation, Inc.
2 *
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.
7 *
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.
12 *
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17
18
19 \f
20
21 #include <stdio.h>
22 #include "libguile/_scm.h"
23 #include "libguile/async.h"
24 #include "libguile/smob.h"
25 #include "libguile/alist.h"
26 #include "libguile/eval.h"
27 #include "libguile/eq.h"
28 #include "libguile/dynwind.h"
29 #include "libguile/backtrace.h"
30 #include "libguile/debug.h"
31 #include "libguile/continuations.h"
32 #include "libguile/stackchk.h"
33 #include "libguile/stacks.h"
34 #include "libguile/fluids.h"
35 #include "libguile/ports.h"
36 #include "libguile/lang.h"
37 #include "libguile/validate.h"
38 #include "libguile/throw.h"
39 #include "libguile/init.h"
40 #include "libguile/strings.h"
41
42 #include "libguile/private-options.h"
43
44
45 \f
46 /* the jump buffer data structure */
47 static scm_t_bits tc16_jmpbuffer;
48
49 #define SCM_JMPBUFP(OBJ) SCM_TYP16_PREDICATE (tc16_jmpbuffer, OBJ)
50
51 #define JBACTIVE(OBJ) (SCM_CELL_WORD_0 (OBJ) & (1L << 16L))
52 #define ACTIVATEJB(x) \
53 (SCM_SET_CELL_WORD_0 ((x), (SCM_CELL_WORD_0 (x) | (1L << 16L))))
54 #define DEACTIVATEJB(x) \
55 (SCM_SET_CELL_WORD_0 ((x), (SCM_CELL_WORD_0 (x) & ~(1L << 16L))))
56
57 #define JBJMPBUF(OBJ) ((jmp_buf *) SCM_CELL_WORD_1 (OBJ))
58 #define SETJBJMPBUF(x, v) (SCM_SET_CELL_WORD_1 ((x), (scm_t_bits) (v)))
59 #define SCM_JBDFRAME(x) ((scm_t_debug_frame *) SCM_CELL_WORD_2 (x))
60 #define SCM_SETJBDFRAME(x, v) (SCM_SET_CELL_WORD_2 ((x), (scm_t_bits) (v)))
61 #define SCM_JBPREUNWIND(x) ((struct pre_unwind_data *) SCM_CELL_WORD_3 (x))
62 #define SCM_SETJBPREUNWIND(x, v) (SCM_SET_CELL_WORD_3 ((x), (scm_t_bits) (v)))
63
64 static int
65 jmpbuffer_print (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED)
66 {
67 scm_puts ("#<jmpbuffer ", port);
68 scm_puts (JBACTIVE(exp) ? "(active) " : "(inactive) ", port);
69 scm_uintprint((scm_t_bits) JBJMPBUF (exp), 16, port);
70 scm_putc ('>', port);
71 return 1 ;
72 }
73
74 static SCM
75 make_jmpbuf (void)
76 {
77 SCM answer;
78 SCM_CRITICAL_SECTION_START;
79 {
80 SCM_NEWSMOB2 (answer, tc16_jmpbuffer, 0, 0);
81 SETJBJMPBUF(answer, (jmp_buf *)0);
82 DEACTIVATEJB(answer);
83 }
84 SCM_CRITICAL_SECTION_END;
85 return answer;
86 }
87
88 \f
89 /* scm_c_catch (the guts of catch) */
90
91 struct jmp_buf_and_retval /* use only on the stack, in scm_catch */
92 {
93 jmp_buf buf; /* must be first */
94 SCM throw_tag;
95 SCM retval;
96 };
97
98 /* These are the structures we use to store pre-unwind handling (aka
99 "lazy") information for a regular catch, and put on the wind list
100 for a "lazy" catch. They store the pre-unwind handler function to
101 call, and the data pointer to pass through to it. It's not a
102 Scheme closure, but it is a function with data, so the term
103 "closure" is appropriate in its broader sense.
104
105 (We don't need anything like this to run the normal (post-unwind)
106 catch handler, because the same C frame runs both the body and the
107 handler.) */
108
109 struct pre_unwind_data {
110 scm_t_catch_handler handler;
111 void *handler_data;
112 int running;
113 int lazy_catch_p;
114 };
115
116
117 /* scm_c_catch is the guts of catch. It handles all the mechanics of
118 setting up a catch target, invoking the catch body, and perhaps
119 invoking the handler if the body does a throw.
120
121 The function is designed to be usable from C code, but is general
122 enough to implement all the semantics Guile Scheme expects from
123 throw.
124
125 TAG is the catch tag. Typically, this is a symbol, but this
126 function doesn't actually care about that.
127
128 BODY is a pointer to a C function which runs the body of the catch;
129 this is the code you can throw from. We call it like this:
130 BODY (BODY_DATA)
131 where:
132 BODY_DATA is just the BODY_DATA argument we received; we pass it
133 through to BODY as its first argument. The caller can make
134 BODY_DATA point to anything useful that BODY might need.
135
136 HANDLER is a pointer to a C function to deal with a throw to TAG,
137 should one occur. We call it like this:
138 HANDLER (HANDLER_DATA, THROWN_TAG, THROW_ARGS)
139 where
140 HANDLER_DATA is the HANDLER_DATA argument we recevied; it's the
141 same idea as BODY_DATA above.
142 THROWN_TAG is the tag that the user threw to; usually this is
143 TAG, but it could be something else if TAG was #t (i.e., a
144 catch-all), or the user threw to a jmpbuf.
145 THROW_ARGS is the list of arguments the user passed to the THROW
146 function, after the tag.
147
148 BODY_DATA is just a pointer we pass through to BODY. HANDLER_DATA
149 is just a pointer we pass through to HANDLER. We don't actually
150 use either of those pointers otherwise ourselves. The idea is
151 that, if our caller wants to communicate something to BODY or
152 HANDLER, it can pass a pointer to it as MUMBLE_DATA, which BODY and
153 HANDLER can then use. Think of it as a way to make BODY and
154 HANDLER closures, not just functions; MUMBLE_DATA points to the
155 enclosed variables.
156
157 Of course, it's up to the caller to make sure that any data a
158 MUMBLE_DATA needs is protected from GC. A common way to do this is
159 to make MUMBLE_DATA a pointer to data stored in an automatic
160 structure variable; since the collector must scan the stack for
161 references anyway, this assures that any references in MUMBLE_DATA
162 will be found. */
163
164 SCM
165 scm_c_catch (SCM tag,
166 scm_t_catch_body body, void *body_data,
167 scm_t_catch_handler handler, void *handler_data,
168 scm_t_catch_handler pre_unwind_handler, void *pre_unwind_handler_data)
169 {
170 struct jmp_buf_and_retval jbr;
171 SCM jmpbuf;
172 SCM answer;
173 struct pre_unwind_data pre_unwind;
174
175 jmpbuf = make_jmpbuf ();
176 answer = SCM_EOL;
177 scm_i_set_dynwinds (scm_acons (tag, jmpbuf, scm_i_dynwinds ()));
178 SETJBJMPBUF(jmpbuf, &jbr.buf);
179 SCM_SETJBDFRAME(jmpbuf, scm_i_last_debug_frame ());
180
181 pre_unwind.handler = pre_unwind_handler;
182 pre_unwind.handler_data = pre_unwind_handler_data;
183 pre_unwind.running = 0;
184 pre_unwind.lazy_catch_p = 0;
185 SCM_SETJBPREUNWIND(jmpbuf, &pre_unwind);
186
187 if (setjmp (jbr.buf))
188 {
189 SCM throw_tag;
190 SCM throw_args;
191
192 #ifdef STACK_CHECKING
193 scm_stack_checking_enabled_p = SCM_STACK_CHECKING_P;
194 #endif
195 SCM_CRITICAL_SECTION_START;
196 DEACTIVATEJB (jmpbuf);
197 scm_i_set_dynwinds (SCM_CDR (scm_i_dynwinds ()));
198 SCM_CRITICAL_SECTION_END;
199 throw_args = jbr.retval;
200 throw_tag = jbr.throw_tag;
201 jbr.throw_tag = SCM_EOL;
202 jbr.retval = SCM_EOL;
203 answer = handler (handler_data, throw_tag, throw_args);
204 }
205 else
206 {
207 ACTIVATEJB (jmpbuf);
208 answer = body (body_data);
209 SCM_CRITICAL_SECTION_START;
210 DEACTIVATEJB (jmpbuf);
211 scm_i_set_dynwinds (SCM_CDR (scm_i_dynwinds ()));
212 SCM_CRITICAL_SECTION_END;
213 }
214 return answer;
215 }
216
217 SCM
218 scm_internal_catch (SCM tag,
219 scm_t_catch_body body, void *body_data,
220 scm_t_catch_handler handler, void *handler_data)
221 {
222 return scm_c_catch(tag,
223 body, body_data,
224 handler, handler_data,
225 NULL, NULL);
226 }
227
228
229 \f
230 /* The smob tag for pre_unwind_data smobs. */
231 static scm_t_bits tc16_pre_unwind_data;
232
233 /* Strictly speaking, we could just pass a zero for our print
234 function, because we don't need to print them. They should never
235 appear in normal data structures, only in the wind list. However,
236 it might be nice for debugging someday... */
237 static int
238 pre_unwind_data_print (SCM closure, SCM port, scm_print_state *pstate SCM_UNUSED)
239 {
240 struct pre_unwind_data *c = (struct pre_unwind_data *) SCM_CELL_WORD_1 (closure);
241 char buf[200];
242
243 sprintf (buf, "#<pre-unwind-data 0x%lx 0x%lx>",
244 (long) c->handler, (long) c->handler_data);
245 scm_puts (buf, port);
246
247 return 1;
248 }
249
250
251 /* Given a pointer to a pre_unwind_data structure, return a smob for it,
252 suitable for inclusion in the wind list. ("Ah yes, a Château
253 Gollombiere '72, non?"). */
254 static SCM
255 make_pre_unwind_data (struct pre_unwind_data *c)
256 {
257 SCM_RETURN_NEWSMOB (tc16_pre_unwind_data, c);
258 }
259
260 #define SCM_PRE_UNWIND_DATA_P(obj) (SCM_TYP16_PREDICATE (tc16_pre_unwind_data, obj))
261
262 SCM
263 scm_c_with_throw_handler (SCM tag,
264 scm_t_catch_body body,
265 void *body_data,
266 scm_t_catch_handler handler,
267 void *handler_data,
268 int lazy_catch_p)
269 {
270 SCM pre_unwind, answer;
271 struct pre_unwind_data c;
272
273 c.handler = handler;
274 c.handler_data = handler_data;
275 c.running = 0;
276 c.lazy_catch_p = lazy_catch_p;
277 pre_unwind = make_pre_unwind_data (&c);
278
279 SCM_CRITICAL_SECTION_START;
280 scm_i_set_dynwinds (scm_acons (tag, pre_unwind, scm_i_dynwinds ()));
281 SCM_CRITICAL_SECTION_END;
282
283 answer = (*body) (body_data);
284
285 SCM_CRITICAL_SECTION_START;
286 scm_i_set_dynwinds (SCM_CDR (scm_i_dynwinds ()));
287 SCM_CRITICAL_SECTION_END;
288
289 return answer;
290 }
291
292 /* Exactly like scm_internal_catch, except:
293 - It does not unwind the stack (this is the major difference).
294 - The handler is not allowed to return. */
295 SCM
296 scm_internal_lazy_catch (SCM tag, scm_t_catch_body body, void *body_data, scm_t_catch_handler handler, void *handler_data)
297 {
298 return scm_c_with_throw_handler (tag, body, body_data, handler, handler_data, 1);
299 }
300
301 \f
302 /* scm_internal_stack_catch
303 Use this one if you want debugging information to be stored in
304 scm_the_last_stack_fluid_var on error. */
305
306 static SCM
307 ss_handler (void *data SCM_UNUSED, SCM tag, SCM throw_args)
308 {
309 /* Save the stack */
310 scm_fluid_set_x (SCM_VARIABLE_REF (scm_the_last_stack_fluid_var),
311 scm_make_stack (SCM_BOOL_T, SCM_EOL));
312 /* Throw the error */
313 return scm_throw (tag, throw_args);
314 }
315
316 struct cwss_data
317 {
318 SCM tag;
319 scm_t_catch_body body;
320 void *data;
321 };
322
323 static SCM
324 cwss_body (void *data)
325 {
326 struct cwss_data *d = data;
327 return scm_internal_lazy_catch (d->tag, d->body, d->data, ss_handler, NULL);
328 }
329
330 SCM
331 scm_internal_stack_catch (SCM tag,
332 scm_t_catch_body body,
333 void *body_data,
334 scm_t_catch_handler handler,
335 void *handler_data)
336 {
337 struct cwss_data d;
338 d.tag = tag;
339 d.body = body;
340 d.data = body_data;
341 return scm_internal_catch (tag, cwss_body, &d, handler, handler_data);
342 }
343
344
345 \f
346 /* body and handler functions for use with any of the above catch variants */
347
348 /* This is a body function you can pass to scm_internal_catch if you
349 want the body to be like Scheme's `catch' --- a thunk.
350
351 BODY_DATA is a pointer to a scm_body_thunk_data structure, which
352 contains the Scheme procedure to invoke as the body, and the tag
353 we're catching. */
354
355 SCM
356 scm_body_thunk (void *body_data)
357 {
358 struct scm_body_thunk_data *c = (struct scm_body_thunk_data *) body_data;
359
360 return scm_call_0 (c->body_proc);
361 }
362
363
364 /* This is a handler function you can pass to scm_internal_catch if
365 you want the handler to act like Scheme's catch: (throw TAG ARGS ...)
366 applies a handler procedure to (TAG ARGS ...).
367
368 If the user does a throw to this catch, this function runs a
369 handler procedure written in Scheme. HANDLER_DATA is a pointer to
370 an SCM variable holding the Scheme procedure object to invoke. It
371 ought to be a pointer to an automatic variable (i.e., one living on
372 the stack), or the procedure object should be otherwise protected
373 from GC. */
374 SCM
375 scm_handle_by_proc (void *handler_data, SCM tag, SCM throw_args)
376 {
377 SCM *handler_proc_p = (SCM *) handler_data;
378
379 return scm_apply_1 (*handler_proc_p, tag, throw_args);
380 }
381
382 /* SCM_HANDLE_BY_PROC_CATCHING_ALL is like SCM_HANDLE_BY_PROC but
383 catches all throws that the handler might emit itself. The handler
384 used for these `secondary' throws is SCM_HANDLE_BY_MESSAGE_NO_EXIT. */
385
386 struct hbpca_data {
387 SCM proc;
388 SCM args;
389 };
390
391 static SCM
392 hbpca_body (void *body_data)
393 {
394 struct hbpca_data *data = (struct hbpca_data *)body_data;
395 return scm_apply_0 (data->proc, data->args);
396 }
397
398 SCM
399 scm_handle_by_proc_catching_all (void *handler_data, SCM tag, SCM throw_args)
400 {
401 SCM *handler_proc_p = (SCM *) handler_data;
402 struct hbpca_data data;
403 data.proc = *handler_proc_p;
404 data.args = scm_cons (tag, throw_args);
405
406 return scm_internal_catch (SCM_BOOL_T,
407 hbpca_body, &data,
408 scm_handle_by_message_noexit, NULL);
409 }
410
411 /* Derive the an exit status from the arguments to (quit ...). */
412 int
413 scm_exit_status (SCM args)
414 {
415 if (!SCM_NULL_OR_NIL_P (args))
416 {
417 SCM cqa = SCM_CAR (args);
418
419 if (scm_is_integer (cqa))
420 return (scm_to_int (cqa));
421 else if (scm_is_false (cqa))
422 return 1;
423 }
424 return 0;
425 }
426
427
428 static void
429 handler_message (void *handler_data, SCM tag, SCM args)
430 {
431 char *prog_name = (char *) handler_data;
432 SCM p = scm_current_error_port ();
433
434 if (scm_ilength (args) == 4)
435 {
436 SCM stack = scm_make_stack (SCM_BOOL_T, SCM_EOL);
437 SCM subr = SCM_CAR (args);
438 SCM message = SCM_CADR (args);
439 SCM parts = SCM_CADDR (args);
440 SCM rest = SCM_CADDDR (args);
441
442 if (SCM_BACKTRACE_P && scm_is_true (stack))
443 {
444 SCM highlights;
445
446 if (scm_is_eq (tag, scm_arg_type_key)
447 || scm_is_eq (tag, scm_out_of_range_key))
448 highlights = rest;
449 else
450 highlights = SCM_EOL;
451
452 scm_puts ("Backtrace:\n", p);
453 scm_display_backtrace_with_highlights (stack, p,
454 SCM_BOOL_F, SCM_BOOL_F,
455 highlights);
456 scm_newline (p);
457 }
458 scm_i_display_error (stack, p, subr, message, parts, rest);
459 }
460 else
461 {
462 if (! prog_name)
463 prog_name = "guile";
464
465 scm_puts (prog_name, p);
466 scm_puts (": ", p);
467
468 scm_puts ("uncaught throw to ", p);
469 scm_prin1 (tag, p, 0);
470 scm_puts (": ", p);
471 scm_prin1 (args, p, 1);
472 scm_putc ('\n', p);
473 }
474 }
475
476
477 /* This is a handler function to use if you want scheme to print a
478 message and die. Useful for dealing with throws to uncaught keys
479 at the top level.
480
481 At boot time, we establish a catch-all that uses this as its handler.
482 1) If the user wants something different, they can use (catch #t
483 ...) to do what they like.
484 2) Outside the context of a read-eval-print loop, there isn't
485 anything else good to do; libguile should not assume the existence
486 of a read-eval-print loop.
487 3) Given that we shouldn't do anything complex, it's much more
488 robust to do it in C code.
489
490 HANDLER_DATA, if non-zero, is assumed to be a char * pointing to a
491 message header to print; if zero, we use "guile" instead. That
492 text is followed by a colon, then the message described by ARGS. */
493
494 /* Dirk:FIXME:: The name of the function should make clear that the
495 * application gets terminated.
496 */
497
498 SCM
499 scm_handle_by_message (void *handler_data, SCM tag, SCM args)
500 {
501 if (scm_is_true (scm_eq_p (tag, scm_from_locale_symbol ("quit"))))
502 exit (scm_exit_status (args));
503
504 handler_message (handler_data, tag, args);
505 scm_i_pthread_exit (NULL);
506
507 /* this point not reached, but suppress gcc warning about no return value
508 in case scm_i_pthread_exit isn't marked as "noreturn" (which seemed not
509 to be the case on cygwin for instance) */
510 return SCM_BOOL_F;
511 }
512
513
514 /* This is just like scm_handle_by_message, but it doesn't exit; it
515 just returns #f. It's useful in cases where you don't really know
516 enough about the body to handle things in a better way, but don't
517 want to let throws fall off the bottom of the wind list. */
518 SCM
519 scm_handle_by_message_noexit (void *handler_data, SCM tag, SCM args)
520 {
521 if (scm_is_true (scm_eq_p (tag, scm_from_locale_symbol ("quit"))))
522 exit (scm_exit_status (args));
523
524 handler_message (handler_data, tag, args);
525
526 return SCM_BOOL_F;
527 }
528
529
530 SCM
531 scm_handle_by_throw (void *handler_data SCM_UNUSED, SCM tag, SCM args)
532 {
533 scm_ithrow (tag, args, 1);
534 return SCM_UNSPECIFIED; /* never returns */
535 }
536
537
538 \f
539 /* the Scheme-visible CATCH, WITH-THROW-HANDLER and LAZY-CATCH functions */
540
541 SCM_DEFINE (scm_catch_with_pre_unwind_handler, "catch", 3, 1, 0,
542 (SCM key, SCM thunk, SCM handler, SCM pre_unwind_handler),
543 "Invoke @var{thunk} in the dynamic context of @var{handler} for\n"
544 "exceptions matching @var{key}. If thunk throws to the symbol\n"
545 "@var{key}, then @var{handler} is invoked this way:\n"
546 "@lisp\n"
547 "(handler key args ...)\n"
548 "@end lisp\n"
549 "\n"
550 "@var{key} is a symbol or @code{#t}.\n"
551 "\n"
552 "@var{thunk} takes no arguments. If @var{thunk} returns\n"
553 "normally, that is the return value of @code{catch}.\n"
554 "\n"
555 "Handler is invoked outside the scope of its own @code{catch}.\n"
556 "If @var{handler} again throws to the same key, a new handler\n"
557 "from further up the call chain is invoked.\n"
558 "\n"
559 "If the key is @code{#t}, then a throw to @emph{any} symbol will\n"
560 "match this call to @code{catch}.\n"
561 "\n"
562 "If a @var{pre-unwind-handler} is given and @var{thunk} throws\n"
563 "an exception that matches @var{key}, Guile calls the\n"
564 "@var{pre-unwind-handler} before unwinding the dynamic state and\n"
565 "invoking the main @var{handler}. @var{pre-unwind-handler} should\n"
566 "be a procedure with the same signature as @var{handler}, that\n"
567 "is @code{(lambda (key . args))}. It is typically used to save\n"
568 "the stack at the point where the exception occurred, but can also\n"
569 "query other parts of the dynamic state at that point, such as\n"
570 "fluid values.\n"
571 "\n"
572 "A @var{pre-unwind-handler} can exit either normally or non-locally.\n"
573 "If it exits normally, Guile unwinds the stack and dynamic context\n"
574 "and then calls the normal (third argument) handler. If it exits\n"
575 "non-locally, that exit determines the continuation.")
576 #define FUNC_NAME s_scm_catch_with_pre_unwind_handler
577 {
578 struct scm_body_thunk_data c;
579
580 SCM_ASSERT (scm_is_symbol (key) || scm_is_eq (key, SCM_BOOL_T),
581 key, SCM_ARG1, FUNC_NAME);
582
583 c.tag = key;
584 c.body_proc = thunk;
585
586 /* scm_c_catch takes care of all the mechanics of setting up a catch
587 key; we tell it to call scm_body_thunk to run the body, and
588 scm_handle_by_proc to deal with any throws to this catch. The
589 former receives a pointer to c, telling it how to behave. The
590 latter receives a pointer to HANDLER, so it knows who to
591 call. */
592 return scm_c_catch (key,
593 scm_body_thunk, &c,
594 scm_handle_by_proc, &handler,
595 SCM_UNBNDP (pre_unwind_handler) ? NULL : scm_handle_by_proc,
596 &pre_unwind_handler);
597 }
598 #undef FUNC_NAME
599
600 /* The following function exists to provide backwards compatibility
601 for the C scm_catch API. Otherwise we could just change
602 "scm_catch_with_pre_unwind_handler" above to "scm_catch". */
603 SCM
604 scm_catch (SCM key, SCM thunk, SCM handler)
605 {
606 return scm_catch_with_pre_unwind_handler (key, thunk, handler, SCM_UNDEFINED);
607 }
608
609
610 SCM_DEFINE (scm_with_throw_handler, "with-throw-handler", 3, 0, 0,
611 (SCM key, SCM thunk, SCM handler),
612 "Add @var{handler} to the dynamic context as a throw handler\n"
613 "for key @var{key}, then invoke @var{thunk}.")
614 #define FUNC_NAME s_scm_with_throw_handler
615 {
616 struct scm_body_thunk_data c;
617
618 SCM_ASSERT (scm_is_symbol (key) || scm_is_eq (key, SCM_BOOL_T),
619 key, SCM_ARG1, FUNC_NAME);
620
621 c.tag = key;
622 c.body_proc = thunk;
623
624 /* scm_c_with_throw_handler takes care of the mechanics of setting
625 up a throw handler; we tell it to call scm_body_thunk to run the
626 body, and scm_handle_by_proc to deal with any throws to this
627 handler. The former receives a pointer to c, telling it how to
628 behave. The latter receives a pointer to HANDLER, so it knows
629 who to call. */
630 return scm_c_with_throw_handler (key,
631 scm_body_thunk, &c,
632 scm_handle_by_proc, &handler,
633 0);
634 }
635 #undef FUNC_NAME
636
637 SCM_DEFINE (scm_lazy_catch, "lazy-catch", 3, 0, 0,
638 (SCM key, SCM thunk, SCM handler),
639 "This behaves exactly like @code{catch}, except that it does\n"
640 "not unwind the stack before invoking @var{handler}.\n"
641 "If the @var{handler} procedure returns normally, Guile\n"
642 "rethrows the same exception again to the next innermost catch,\n"
643 "lazy-catch or throw handler. If the @var{handler} exits\n"
644 "non-locally, that exit determines the continuation.")
645 #define FUNC_NAME s_scm_lazy_catch
646 {
647 struct scm_body_thunk_data c;
648
649 SCM_ASSERT (scm_is_symbol (key) || scm_is_eq (key, SCM_BOOL_T),
650 key, SCM_ARG1, FUNC_NAME);
651
652 c.tag = key;
653 c.body_proc = thunk;
654
655 /* scm_internal_lazy_catch takes care of all the mechanics of
656 setting up a lazy catch key; we tell it to call scm_body_thunk to
657 run the body, and scm_handle_by_proc to deal with any throws to
658 this catch. The former receives a pointer to c, telling it how
659 to behave. The latter receives a pointer to HANDLER, so it knows
660 who to call. */
661 return scm_internal_lazy_catch (key,
662 scm_body_thunk, &c,
663 scm_handle_by_proc, &handler);
664 }
665 #undef FUNC_NAME
666
667
668 \f
669 /* throwing */
670
671 static void toggle_pre_unwind_running (void *data)
672 {
673 struct pre_unwind_data *pre_unwind = (struct pre_unwind_data *)data;
674 pre_unwind->running = !pre_unwind->running;
675 }
676
677 SCM_DEFINE (scm_throw, "throw", 1, 0, 1,
678 (SCM key, SCM args),
679 "Invoke the catch form matching @var{key}, passing @var{args} to the\n"
680 "@var{handler}. \n\n"
681 "@var{key} is a symbol. It will match catches of the same symbol or of\n"
682 "@code{#t}.\n\n"
683 "If there is no handler at all, Guile prints an error and then exits.")
684 #define FUNC_NAME s_scm_throw
685 {
686 SCM_VALIDATE_SYMBOL (1, key);
687 return scm_ithrow (key, args, 1);
688 }
689 #undef FUNC_NAME
690
691 SCM
692 scm_ithrow (SCM key, SCM args, int noreturn SCM_UNUSED)
693 {
694 SCM jmpbuf = SCM_UNDEFINED;
695 SCM wind_goal;
696
697 SCM dynpair = SCM_UNDEFINED;
698 SCM winds;
699
700 if (scm_i_critical_section_level)
701 {
702 SCM s = args;
703 int i = 0;
704
705 /*
706 We have much better routines for displaying Scheme, but we're
707 already inside a pernicious error, and it's unlikely that they
708 are available to us. We try to print something useful anyway,
709 so users don't need a debugger to find out what went wrong.
710 */
711 fprintf (stderr, "throw from within critical section.\n");
712 if (scm_is_symbol (key))
713 fprintf (stderr, "error key: %s\n", scm_i_symbol_chars (key));
714
715
716 for (; scm_is_pair (s); s = scm_cdr (s), i++)
717 {
718 char const *str = NULL;
719 if (scm_is_string (scm_car (s)))
720 str = scm_i_string_chars (scm_car (s));
721 else if (scm_is_symbol (scm_car (s)))
722 str = scm_i_symbol_chars (scm_car (s));
723
724 if (str != NULL)
725 fprintf (stderr, "argument %d: %s\n", i, str);
726 }
727 abort ();
728 }
729
730 rethrow:
731
732 /* Search the wind list for an appropriate catch.
733 "Waiter, please bring us the wind list." */
734 for (winds = scm_i_dynwinds (); scm_is_pair (winds); winds = SCM_CDR (winds))
735 {
736 dynpair = SCM_CAR (winds);
737 if (scm_is_pair (dynpair))
738 {
739 SCM this_key = SCM_CAR (dynpair);
740
741 if (scm_is_eq (this_key, SCM_BOOL_T) || scm_is_eq (this_key, key))
742 {
743 jmpbuf = SCM_CDR (dynpair);
744
745 if (!SCM_PRE_UNWIND_DATA_P (jmpbuf))
746 break;
747 else
748 {
749 struct pre_unwind_data *c =
750 (struct pre_unwind_data *) SCM_CELL_WORD_1 (jmpbuf);
751 if (!c->running)
752 break;
753 }
754 }
755 }
756 }
757
758 /* If we didn't find anything, print a message and abort the process
759 right here. If you don't want this, establish a catch-all around
760 any code that might throw up. */
761 if (scm_is_null (winds))
762 {
763 scm_handle_by_message (NULL, key, args);
764 abort ();
765 }
766
767 /* If the wind list is malformed, bail. */
768 if (!scm_is_pair (winds))
769 abort ();
770
771 for (wind_goal = scm_i_dynwinds ();
772 (!scm_is_pair (SCM_CAR (wind_goal))
773 || !scm_is_eq (SCM_CDAR (wind_goal), jmpbuf));
774 wind_goal = SCM_CDR (wind_goal))
775 ;
776
777 /* Is this a throw handler (or lazy catch)? In a wind list entry
778 for a throw handler or lazy catch, the key is bound to a
779 pre_unwind_data smob, not a jmpbuf. */
780 if (SCM_PRE_UNWIND_DATA_P (jmpbuf))
781 {
782 struct pre_unwind_data *c =
783 (struct pre_unwind_data *) SCM_CELL_WORD_1 (jmpbuf);
784 SCM handle, answer;
785
786 /* For old-style lazy-catch behaviour, we unwind the dynamic
787 context before invoking the handler. */
788 if (c->lazy_catch_p)
789 {
790 scm_dowinds (wind_goal, (scm_ilength (scm_i_dynwinds ())
791 - scm_ilength (wind_goal)));
792 SCM_CRITICAL_SECTION_START;
793 handle = scm_i_dynwinds ();
794 scm_i_set_dynwinds (SCM_CDR (handle));
795 SCM_CRITICAL_SECTION_END;
796 }
797
798 /* Call the handler, with framing to set the pre-unwind
799 structure's running field while the handler is running, so we
800 can avoid recursing into the same handler again. Note that
801 if the handler returns normally, the running flag stays
802 set until some kind of non-local jump occurs. */
803 scm_dynwind_begin (SCM_F_DYNWIND_REWINDABLE);
804 scm_dynwind_rewind_handler (toggle_pre_unwind_running,
805 c,
806 SCM_F_WIND_EXPLICITLY);
807 scm_dynwind_unwind_handler (toggle_pre_unwind_running, c, 0);
808 answer = (c->handler) (c->handler_data, key, args);
809
810 /* There is deliberately no scm_dynwind_end call here. This
811 means that the unwind handler (toggle_pre_unwind_running)
812 stays in place until a non-local exit occurs, and will then
813 reset the pre-unwind structure's running flag. For sample
814 code where this makes a difference, see the "again but with
815 two chained throw handlers" test case in exceptions.test. */
816
817 /* If the handler returns, rethrow the same key and args. */
818 goto rethrow;
819 }
820
821 /* Otherwise, it's a normal catch. */
822 else if (SCM_JMPBUFP (jmpbuf))
823 {
824 struct pre_unwind_data * pre_unwind;
825 struct jmp_buf_and_retval * jbr;
826
827 /* Before unwinding anything, run the pre-unwind handler if
828 there is one, and if it isn't already running. */
829 pre_unwind = SCM_JBPREUNWIND (jmpbuf);
830 if (pre_unwind->handler && !pre_unwind->running)
831 {
832 /* Use framing to detect and avoid possible reentry into
833 this handler, which could otherwise cause an infinite
834 loop. */
835 scm_dynwind_begin (SCM_F_DYNWIND_REWINDABLE);
836 scm_dynwind_rewind_handler (toggle_pre_unwind_running,
837 pre_unwind,
838 SCM_F_WIND_EXPLICITLY);
839 scm_dynwind_unwind_handler (toggle_pre_unwind_running,
840 pre_unwind,
841 SCM_F_WIND_EXPLICITLY);
842 (pre_unwind->handler) (pre_unwind->handler_data, key, args);
843 scm_dynwind_end ();
844 }
845
846 /* Now unwind and jump. */
847 scm_dowinds (wind_goal, (scm_ilength (scm_i_dynwinds ())
848 - scm_ilength (wind_goal)));
849 jbr = (struct jmp_buf_and_retval *)JBJMPBUF (jmpbuf);
850 jbr->throw_tag = key;
851 jbr->retval = args;
852 scm_i_set_last_debug_frame (SCM_JBDFRAME (jmpbuf));
853 longjmp (*JBJMPBUF (jmpbuf), 1);
854 }
855
856 /* Otherwise, it's some random piece of junk. */
857 else
858 abort ();
859 }
860
861
862 void
863 scm_init_throw ()
864 {
865 tc16_jmpbuffer = scm_make_smob_type ("jmpbuffer", 0);
866 scm_set_smob_print (tc16_jmpbuffer, jmpbuffer_print);
867
868 tc16_pre_unwind_data = scm_make_smob_type ("pre-unwind-data", 0);
869 scm_set_smob_print (tc16_pre_unwind_data, pre_unwind_data_print);
870
871 #include "libguile/throw.x"
872 }
873
874 /*
875 Local Variables:
876 c-file-style: "gnu"
877 End:
878 */