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