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