Smob-related creanup.
[bpt/guile.git] / libguile / throw.c
1 /* Copyright (C) 1995, 1996, 1997, 1998, 2000 Free Software Foundation, Inc.
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
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
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.
40 * If you do not wish that, delete this exception notice. */
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
45 \f
46
47 #include <stdio.h>
48 #include "libguile/_scm.h"
49 #include "libguile/smob.h"
50 #include "libguile/alist.h"
51 #include "libguile/eval.h"
52 #include "libguile/eq.h"
53 #include "libguile/dynwind.h"
54 #include "libguile/backtrace.h"
55 #ifdef DEBUG_EXTENSIONS
56 #include "libguile/debug.h"
57 #endif
58 #include "libguile/continuations.h"
59 #include "libguile/stackchk.h"
60 #include "libguile/stacks.h"
61 #include "libguile/fluids.h"
62 #include "libguile/ports.h"
63
64 #include "libguile/validate.h"
65 #include "libguile/throw.h"
66
67 \f
68 /* the jump buffer data structure */
69 static scm_bits_t tc16_jmpbuffer;
70
71 #define SCM_JMPBUFP(OBJ) SCM_TYP16_PREDICATE (tc16_jmpbuffer, OBJ)
72
73 #define JBACTIVE(OBJ) (SCM_CELL_WORD_0 (OBJ) & (1L << 16L))
74 #define ACTIVATEJB(OBJ) (SCM_SETOR_CAR (OBJ, (1L << 16L)))
75 #define DEACTIVATEJB(OBJ) (SCM_SETAND_CAR (OBJ, ~(1L << 16L)))
76
77 #define JBJMPBUF(OBJ) ((jmp_buf *) SCM_CELL_WORD_1 (OBJ))
78 #define SETJBJMPBUF(x,v) (SCM_SET_CELL_WORD_1 ((x), (v)))
79 #ifdef DEBUG_EXTENSIONS
80 #define SCM_JBDFRAME(x) ((scm_debug_frame *) SCM_CELL_WORD_2 (x))
81 #define SCM_SETJBDFRAME(x,v) (SCM_SET_CELL_WORD_2 ((x), (v)))
82 #endif
83
84 static int
85 jmpbuffer_print (SCM exp, SCM port, scm_print_state *pstate)
86 {
87 scm_puts ("#<jmpbuffer ", port);
88 scm_puts (JBACTIVE(exp) ? "(active) " : "(inactive) ", port);
89 scm_intprint((long) JBJMPBUF (exp), 16, port);
90 scm_putc ('>', port);
91 return 1 ;
92 }
93
94 static SCM
95 make_jmpbuf (void)
96 {
97 SCM answer;
98 SCM_REDEFER_INTS;
99 {
100 #ifdef DEBUG_EXTENSIONS
101 SCM_NEWSMOB2 (answer, tc16_jmpbuffer, 0, 0);
102 #else
103 SCM_NEWSMOB (answer, tc16_jmpbuffer, 0);
104 #endif
105 SETJBJMPBUF(answer, (jmp_buf *)0);
106 DEACTIVATEJB(answer);
107 }
108 SCM_REALLOW_INTS;
109 return answer;
110 }
111
112 \f
113 /* scm_internal_catch (the guts of catch) */
114
115 struct jmp_buf_and_retval /* use only on the stack, in scm_catch */
116 {
117 jmp_buf buf; /* must be first */
118 SCM throw_tag;
119 SCM retval;
120 };
121
122
123 /* scm_internal_catch is the guts of catch. It handles all the
124 mechanics of setting up a catch target, invoking the catch body,
125 and perhaps invoking the handler if the body does a throw.
126
127 The function is designed to be usable from C code, but is general
128 enough to implement all the semantics Guile Scheme expects from
129 throw.
130
131 TAG is the catch tag. Typically, this is a symbol, but this
132 function doesn't actually care about that.
133
134 BODY is a pointer to a C function which runs the body of the catch;
135 this is the code you can throw from. We call it like this:
136 BODY (BODY_DATA)
137 where:
138 BODY_DATA is just the BODY_DATA argument we received; we pass it
139 through to BODY as its first argument. The caller can make
140 BODY_DATA point to anything useful that BODY might need.
141
142 HANDLER is a pointer to a C function to deal with a throw to TAG,
143 should one occur. We call it like this:
144 HANDLER (HANDLER_DATA, THROWN_TAG, THROW_ARGS)
145 where
146 HANDLER_DATA is the HANDLER_DATA argument we recevied; it's the
147 same idea as BODY_DATA above.
148 THROWN_TAG is the tag that the user threw to; usually this is
149 TAG, but it could be something else if TAG was #t (i.e., a
150 catch-all), or the user threw to a jmpbuf.
151 THROW_ARGS is the list of arguments the user passed to the THROW
152 function, after the tag.
153
154 BODY_DATA is just a pointer we pass through to BODY. HANDLER_DATA
155 is just a pointer we pass through to HANDLER. We don't actually
156 use either of those pointers otherwise ourselves. The idea is
157 that, if our caller wants to communicate something to BODY or
158 HANDLER, it can pass a pointer to it as MUMBLE_DATA, which BODY and
159 HANDLER can then use. Think of it as a way to make BODY and
160 HANDLER closures, not just functions; MUMBLE_DATA points to the
161 enclosed variables.
162
163 Of course, it's up to the caller to make sure that any data a
164 MUMBLE_DATA needs is protected from GC. A common way to do this is
165 to make MUMBLE_DATA a pointer to data stored in an automatic
166 structure variable; since the collector must scan the stack for
167 references anyway, this assures that any references in MUMBLE_DATA
168 will be found. */
169
170 SCM
171 scm_internal_catch (SCM tag, scm_catch_body_t body, void *body_data, scm_catch_handler_t handler, void *handler_data)
172 {
173 struct jmp_buf_and_retval jbr;
174 SCM jmpbuf;
175 SCM answer;
176
177 jmpbuf = make_jmpbuf ();
178 answer = SCM_EOL;
179 scm_dynwinds = scm_acons (tag, jmpbuf, scm_dynwinds);
180 SETJBJMPBUF(jmpbuf, &jbr.buf);
181 #ifdef DEBUG_EXTENSIONS
182 SCM_SETJBDFRAME(jmpbuf, scm_last_debug_frame);
183 #endif
184 if (setjmp (jbr.buf))
185 {
186 SCM throw_tag;
187 SCM throw_args;
188
189 #ifdef STACK_CHECKING
190 scm_stack_checking_enabled_p = SCM_STACK_CHECKING_P;
191 #endif
192 SCM_REDEFER_INTS;
193 DEACTIVATEJB (jmpbuf);
194 scm_dynwinds = SCM_CDR (scm_dynwinds);
195 SCM_REALLOW_INTS;
196 throw_args = jbr.retval;
197 throw_tag = jbr.throw_tag;
198 jbr.throw_tag = SCM_EOL;
199 jbr.retval = SCM_EOL;
200 answer = handler (handler_data, throw_tag, throw_args);
201 }
202 else
203 {
204 ACTIVATEJB (jmpbuf);
205 answer = body (body_data);
206 SCM_REDEFER_INTS;
207 DEACTIVATEJB (jmpbuf);
208 scm_dynwinds = SCM_CDR (scm_dynwinds);
209 SCM_REALLOW_INTS;
210 }
211 return answer;
212 }
213
214
215 \f
216 /* scm_internal_lazy_catch (the guts of lazy catching) */
217
218 /* The smob tag for lazy_catch smobs. */
219 static scm_bits_t tc16_lazy_catch;
220
221 /* This is the structure we put on the wind list for a lazy catch. It
222 stores the handler function to call, and the data pointer to pass
223 through to it. It's not a Scheme closure, but it is a function
224 with data, so the term "closure" is appropriate in its broader
225 sense.
226
227 (We don't need anything like this in the "eager" catch code,
228 because the same C frame runs both the body and the handler.) */
229 struct lazy_catch {
230 scm_catch_handler_t handler;
231 void *handler_data;
232 };
233
234 /* Strictly speaking, we could just pass a zero for our print
235 function, because we don't need to print them. They should never
236 appear in normal data structures, only in the wind list. However,
237 it might be nice for debugging someday... */
238 static int
239 lazy_catch_print (SCM closure, SCM port, scm_print_state *pstate)
240 {
241 struct lazy_catch *c = (struct lazy_catch *) SCM_CELL_WORD_1 (closure);
242 char buf[200];
243
244 sprintf (buf, "#<lazy-catch 0x%lx 0x%lx>",
245 (long) c->handler, (long) c->handler_data);
246 scm_puts (buf, port);
247
248 return 1;
249 }
250
251
252 /* Given a pointer to a lazy catch structure, return a smob for it,
253 suitable for inclusion in the wind list. ("Ah yes, a Château
254 Gollombiere '72, non?"). */
255 static SCM
256 make_lazy_catch (struct lazy_catch *c)
257 {
258 SCM_RETURN_NEWSMOB (tc16_lazy_catch, c);
259 }
260
261 #define SCM_LAZY_CATCH_P(obj) (SCM_TYP16_PREDICATE (tc16_lazy_catch, obj))
262
263
264 /* Exactly like scm_internal_catch, except:
265 - It does not unwind the stack (this is the major difference).
266 - If handler returns, its value is returned from the throw. */
267 SCM
268 scm_internal_lazy_catch (SCM tag, scm_catch_body_t body, void *body_data, scm_catch_handler_t handler, void *handler_data)
269 {
270 SCM lazy_catch, answer;
271 struct lazy_catch c;
272
273 c.handler = handler;
274 c.handler_data = handler_data;
275 lazy_catch = make_lazy_catch (&c);
276
277 SCM_REDEFER_INTS;
278 scm_dynwinds = scm_acons (tag, lazy_catch, scm_dynwinds);
279 SCM_REALLOW_INTS;
280
281 answer = (*body) (body_data);
282
283 SCM_REDEFER_INTS;
284 scm_dynwinds = SCM_CDR (scm_dynwinds);
285 SCM_REALLOW_INTS;
286
287 return answer;
288 }
289
290 \f
291 /* scm_internal_stack_catch
292 Use this one if you want debugging information to be stored in
293 scm_the_last_stack_fluid on error. */
294
295 static SCM
296 ss_handler (void *data, SCM tag, SCM throw_args)
297 {
298 /* Save the stack */
299 scm_fluid_set_x (SCM_CDR (scm_the_last_stack_fluid),
300 scm_make_stack (SCM_BOOL_T, SCM_EOL));
301 /* Throw the error */
302 return scm_throw (tag, throw_args);
303 }
304
305 struct cwss_data
306 {
307 SCM tag;
308 scm_catch_body_t body;
309 void *data;
310 };
311
312 static SCM
313 cwss_body (void *data)
314 {
315 struct cwss_data *d = data;
316 return scm_internal_lazy_catch (d->tag, d->body, d->data, ss_handler, NULL);
317 }
318
319 SCM
320 scm_internal_stack_catch (SCM tag,
321 scm_catch_body_t body,
322 void *body_data,
323 scm_catch_handler_t handler,
324 void *handler_data)
325 {
326 struct cwss_data d;
327 d.tag = tag;
328 d.body = body;
329 d.data = body_data;
330 return scm_internal_catch (tag, cwss_body, &d, handler, handler_data);
331 }
332
333
334 \f
335 /* body and handler functions for use with any of the above catch variants */
336
337 /* This is a body function you can pass to scm_internal_catch if you
338 want the body to be like Scheme's `catch' --- a thunk.
339
340 BODY_DATA is a pointer to a scm_body_thunk_data structure, which
341 contains the Scheme procedure to invoke as the body, and the tag
342 we're catching. */
343
344 SCM
345 scm_body_thunk (void *body_data)
346 {
347 struct scm_body_thunk_data *c = (struct scm_body_thunk_data *) body_data;
348
349 return scm_apply (c->body_proc, SCM_EOL, SCM_EOL);
350 }
351
352
353 /* This is a handler function you can pass to scm_internal_catch if
354 you want the handler to act like Scheme's catch: (throw TAG ARGS ...)
355 applies a handler procedure to (TAG ARGS ...).
356
357 If the user does a throw to this catch, this function runs a
358 handler procedure written in Scheme. HANDLER_DATA is a pointer to
359 an SCM variable holding the Scheme procedure object to invoke. It
360 ought to be a pointer to an automatic variable (i.e., one living on
361 the stack), or the procedure object should be otherwise protected
362 from GC. */
363 SCM
364 scm_handle_by_proc (void *handler_data, SCM tag, SCM throw_args)
365 {
366 SCM *handler_proc_p = (SCM *) handler_data;
367
368 return scm_apply (*handler_proc_p, scm_cons (tag, throw_args), SCM_EOL);
369 }
370
371 /* SCM_HANDLE_BY_PROC_CATCHING_ALL is like SCM_HANDLE_BY_PROC but
372 catches all throws that the handler might emit itself. The handler
373 used for these `secondary' throws is SCM_HANDLE_BY_MESSAGE_NO_EXIT. */
374
375 struct hbpca_data {
376 SCM proc;
377 SCM args;
378 };
379
380 static SCM
381 hbpca_body (void *body_data)
382 {
383 struct hbpca_data *data = (struct hbpca_data *)body_data;
384 return scm_apply (data->proc, data->args, SCM_EOL);
385 }
386
387 SCM
388 scm_handle_by_proc_catching_all (void *handler_data, SCM tag, SCM throw_args)
389 {
390 SCM *handler_proc_p = (SCM *) handler_data;
391 struct hbpca_data data;
392 data.proc = *handler_proc_p;
393 data.args = scm_cons (tag, throw_args);
394
395 return scm_internal_catch (SCM_BOOL_T,
396 hbpca_body, &data,
397 scm_handle_by_message_noexit, NULL);
398 }
399
400 /* Derive the an exit status from the arguments to (quit ...). */
401 int
402 scm_exit_status (SCM args)
403 {
404 if (SCM_NNULLP (args))
405 {
406 SCM cqa = SCM_CAR (args);
407
408 if (SCM_INUMP (cqa))
409 return (SCM_INUM (cqa));
410 else if (SCM_FALSEP (cqa))
411 return 1;
412 }
413 return 0;
414 }
415
416
417 static void
418 handler_message (void *handler_data, SCM tag, SCM args)
419 {
420 char *prog_name = (char *) handler_data;
421 SCM p = scm_cur_errp;
422
423 if (scm_ilength (args) >= 3)
424 {
425 SCM stack = scm_make_stack (SCM_BOOL_T, SCM_EOL);
426 SCM subr = SCM_CAR (args);
427 SCM message = SCM_CADR (args);
428 SCM parts = SCM_CADDR (args);
429 SCM rest = SCM_CDDDR (args);
430
431 if (SCM_BACKTRACE_P && SCM_NFALSEP (stack))
432 {
433 scm_puts ("Backtrace:\n", p);
434 scm_display_backtrace (stack, p, SCM_UNDEFINED, SCM_UNDEFINED);
435 scm_newline (p);
436 }
437 scm_display_error (stack, p, subr, message, parts, rest);
438 }
439 else
440 {
441 if (! prog_name)
442 prog_name = "guile";
443
444 scm_puts (prog_name, p);
445 scm_puts (": ", p);
446
447 scm_puts ("uncaught throw to ", p);
448 scm_prin1 (tag, p, 0);
449 scm_puts (": ", p);
450 scm_prin1 (args, p, 1);
451 scm_putc ('\n', p);
452 }
453 }
454
455
456 /* This is a handler function to use if you want scheme to print a
457 message and die. Useful for dealing with throws to uncaught keys
458 at the top level.
459
460 At boot time, we establish a catch-all that uses this as its handler.
461 1) If the user wants something different, they can use (catch #t
462 ...) to do what they like.
463 2) Outside the context of a read-eval-print loop, there isn't
464 anything else good to do; libguile should not assume the existence
465 of a read-eval-print loop.
466 3) Given that we shouldn't do anything complex, it's much more
467 robust to do it in C code.
468
469 HANDLER_DATA, if non-zero, is assumed to be a char * pointing to a
470 message header to print; if zero, we use "guile" instead. That
471 text is followed by a colon, then the message described by ARGS. */
472
473 /* Dirk:FIXME:: The name of the function should make clear that the
474 * application gets terminated.
475 */
476
477 SCM
478 scm_handle_by_message (void *handler_data, SCM tag, SCM args)
479 {
480 if (SCM_NFALSEP (scm_eq_p (tag, scm_str2symbol ("quit"))))
481 {
482 exit (scm_exit_status (args));
483 }
484
485 handler_message (handler_data, tag, args);
486 exit (2);
487 }
488
489
490 /* This is just like scm_handle_by_message, but it doesn't exit; it
491 just returns #f. It's useful in cases where you don't really know
492 enough about the body to handle things in a better way, but don't
493 want to let throws fall off the bottom of the wind list. */
494 SCM
495 scm_handle_by_message_noexit (void *handler_data, SCM tag, SCM args)
496 {
497 handler_message (handler_data, tag, args);
498
499 return SCM_BOOL_F;
500 }
501
502
503 SCM
504 scm_handle_by_throw (void *handler_data, SCM tag, SCM args)
505 {
506 scm_ithrow (tag, args, 1);
507 return SCM_UNSPECIFIED; /* never returns */
508 }
509
510
511 \f
512 /* the Scheme-visible CATCH and LAZY-CATCH functions */
513
514 SCM_DEFINE (scm_catch, "catch", 3, 0, 0,
515 (SCM tag, SCM thunk, SCM handler),
516 "Invoke @var{thunk} in the dynamic context of @var{handler} for\n"
517 "exceptions matching @var{key}. If thunk throws to the symbol @var{key},\n"
518 "then @var{handler} is invoked this way:\n\n"
519 "@example\n"
520 "(handler key args ...)\n"
521 "@end example\n\n"
522 "@var{key} is a symbol or #t.\n\n"
523 "@var{thunk} takes no arguments. If @var{thunk} returns normally, that\n"
524 "is the return value of @code{catch}.\n\n"
525 "Handler is invoked outside the scope of its own @code{catch}. If\n"
526 "@var{handler} again throws to the same key, a new handler from further\n"
527 "up the call chain is invoked.\n\n"
528 "If the key is @code{#t}, then a throw to @emph{any} symbol will match\n"
529 "this call to @code{catch}.")
530 #define FUNC_NAME s_scm_catch
531 {
532 struct scm_body_thunk_data c;
533
534 SCM_ASSERT (SCM_SYMBOLP (tag) || SCM_EQ_P (tag, SCM_BOOL_T),
535 tag, SCM_ARG1, FUNC_NAME);
536
537 c.tag = tag;
538 c.body_proc = thunk;
539
540 /* scm_internal_catch takes care of all the mechanics of setting up
541 a catch tag; we tell it to call scm_body_thunk to run the body,
542 and scm_handle_by_proc to deal with any throws to this catch.
543 The former receives a pointer to c, telling it how to behave.
544 The latter receives a pointer to HANDLER, so it knows who to call. */
545 return scm_internal_catch (tag,
546 scm_body_thunk, &c,
547 scm_handle_by_proc, &handler);
548 }
549 #undef FUNC_NAME
550
551
552 SCM_DEFINE (scm_lazy_catch, "lazy-catch", 3, 0, 0,
553 (SCM tag, SCM thunk, SCM handler),
554 "")
555 #define FUNC_NAME s_scm_lazy_catch
556 {
557 struct scm_body_thunk_data c;
558
559 SCM_ASSERT (SCM_SYMBOLP (tag) || SCM_EQ_P (tag, SCM_BOOL_T),
560 tag, SCM_ARG1, FUNC_NAME);
561
562 c.tag = tag;
563 c.body_proc = thunk;
564
565 /* scm_internal_lazy_catch takes care of all the mechanics of
566 setting up a lazy catch tag; we tell it to call scm_body_thunk to
567 run the body, and scm_handle_by_proc to deal with any throws to
568 this catch. The former receives a pointer to c, telling it how
569 to behave. The latter receives a pointer to HANDLER, so it knows
570 who to call. */
571 return scm_internal_lazy_catch (tag,
572 scm_body_thunk, &c,
573 scm_handle_by_proc, &handler);
574 }
575 #undef FUNC_NAME
576
577
578 \f
579 /* throwing */
580
581 SCM_DEFINE (scm_throw, "throw", 1, 0, 1,
582 (SCM key, SCM args),
583 "Invoke the catch form matching @var{key}, passing @var{args} to the\n"
584 "@var{handler}. \n\n"
585 "@var{key} is a symbol. It will match catches of the same symbol or of\n"
586 "#t.\n\n"
587 "If there is no handler at all, an error is signaled.")
588 #define FUNC_NAME s_scm_throw
589 {
590 SCM_VALIDATE_SYMBOL (1,key);
591 /* May return if handled by lazy catch. */
592 return scm_ithrow (key, args, 1);
593 }
594 #undef FUNC_NAME
595
596 SCM
597 scm_ithrow (SCM key, SCM args, int noreturn)
598 {
599 SCM jmpbuf = SCM_UNDEFINED;
600 SCM wind_goal;
601
602 SCM dynpair = SCM_UNDEFINED;
603 SCM winds;
604
605 /* Search the wind list for an appropriate catch.
606 "Waiter, please bring us the wind list." */
607 for (winds = scm_dynwinds; SCM_CONSP (winds); winds = SCM_CDR (winds))
608 {
609 dynpair = SCM_CAR (winds);
610 if (SCM_CONSP (dynpair))
611 {
612 SCM this_key = SCM_CAR (dynpair);
613
614 if (SCM_EQ_P (this_key, SCM_BOOL_T) || SCM_EQ_P (this_key, key))
615 break;
616 }
617 }
618
619 #ifdef __GNUC__
620 /* Dirk:FIXME:: This bugfix should be removed some time. */
621 /* GCC 2.95.2 has a bug in its optimizer that makes it generate
622 incorrect code sometimes. This barrier stops it from being too
623 clever. */
624 asm volatile ("" : "=g" (winds));
625 #endif
626
627 /* If we didn't find anything, print a message and abort the process
628 right here. If you don't want this, establish a catch-all around
629 any code that might throw up. */
630 if (SCM_NULLP (winds))
631 {
632 scm_handle_by_message (NULL, key, args);
633 abort ();
634 }
635
636 /* If the wind list is malformed, bail. */
637 if (!SCM_CONSP (winds))
638 abort ();
639
640 jmpbuf = SCM_CDR (dynpair);
641
642 for (wind_goal = scm_dynwinds;
643 !SCM_EQ_P (SCM_CDAR (wind_goal), jmpbuf);
644 wind_goal = SCM_CDR (wind_goal))
645 ;
646
647 /* Is a lazy catch? In wind list entries for lazy catches, the key
648 is bound to a lazy_catch smob, not a jmpbuf. */
649 if (SCM_LAZY_CATCH_P (jmpbuf))
650 {
651 struct lazy_catch *c = (struct lazy_catch *) SCM_CELL_WORD_1 (jmpbuf);
652 SCM oldwinds = scm_dynwinds;
653 SCM handle, answer;
654 scm_dowinds (wind_goal, (scm_ilength (scm_dynwinds)
655 - scm_ilength (wind_goal)));
656 SCM_REDEFER_INTS;
657 handle = scm_dynwinds;
658 scm_dynwinds = SCM_CDR (scm_dynwinds);
659 SCM_REALLOW_INTS;
660 answer = (c->handler) (c->handler_data, key, args);
661 SCM_REDEFER_INTS;
662 SCM_SETCDR (handle, scm_dynwinds);
663 scm_dynwinds = handle;
664 SCM_REALLOW_INTS;
665 scm_dowinds (oldwinds, (scm_ilength (scm_dynwinds)
666 - scm_ilength (oldwinds)));
667 return answer;
668 }
669
670 /* Otherwise, it's a normal catch. */
671 else if (SCM_JMPBUFP (jmpbuf))
672 {
673 struct jmp_buf_and_retval * jbr;
674 scm_dowinds (wind_goal, (scm_ilength (scm_dynwinds)
675 - scm_ilength (wind_goal)));
676 jbr = (struct jmp_buf_and_retval *)JBJMPBUF (jmpbuf);
677 jbr->throw_tag = key;
678 jbr->retval = args;
679 }
680
681 /* Otherwise, it's some random piece of junk. */
682 else
683 abort ();
684
685 #ifdef DEBUG_EXTENSIONS
686 scm_last_debug_frame = SCM_JBDFRAME (jmpbuf);
687 #endif
688 longjmp (*JBJMPBUF (jmpbuf), 1);
689 }
690
691
692 void
693 scm_init_throw ()
694 {
695 tc16_jmpbuffer = scm_make_smob_type ("jmpbuffer", 0);
696 scm_set_smob_print (tc16_jmpbuffer, jmpbuffer_print);
697
698 tc16_lazy_catch = scm_make_smob_type ("lazy-catch", 0);
699 scm_set_smob_print (tc16_lazy_catch, lazy_catch_print);
700
701 #ifndef SCM_MAGIC_SNARFER
702 #include "libguile/throw.x"
703 #endif
704 }
705
706 /*
707 Local Variables:
708 c-file-style: "gnu"
709 End:
710 */