#include "alist.h"
#include "eval.h"
#include "dynwind.h"
+#include "backtrace.h"
#ifdef DEBUG_EXTENSIONS
#include "debug.h"
#endif
SCM retval;
};
+
+/* scm_internal_catch is the guts of catch. It handles all the
+ mechanics of setting up a catch target, invoking the catch body,
+ and perhaps invoking the handler if the body does a throw.
+
+ The function is designed to be usable from C code, but is general
+ enough to implement all the semantics Guile Scheme expects from
+ throw.
+
+ TAG is the catch tag. Typically, this is a symbol, but this
+ function doesn't actually care about that.
+
+ BODY is a pointer to a C function which runs the body of the catch;
+ this is the code you can throw from. We call it like this:
+ BODY (BODY_DATA, JMPBUF)
+ where:
+ BODY_DATA is just the BODY_DATA argument we received; we pass it
+ through to BODY as its first argument. The caller can make
+ BODY_DATA point to anything useful that BODY might need.
+ JMPBUF is the Scheme jmpbuf object corresponding to this catch,
+ which we have just created and initialized.
+
+ HANDLER is a pointer to a C function to deal with a throw to TAG,
+ should one occur. We call it like this:
+ HANDLER (HANDLER_DATA, TAG, THROW_ARGS)
+ where
+ HANDLER_DATA is the HANDLER_DATA argument we recevied; it's the
+ same idea as BODY_DATA above.
+ TAG is the tag that the user threw to; usually this is TAG, but
+ it could be something else if TAG was #t (i.e., a catch-all),
+ or the user threw to a jmpbuf.
+ THROW_ARGS is the list of arguments the user passed to the THROW
+ function.
+
+ BODY_DATA is just a pointer we pass through to BODY. HANDLER_DATA
+ is just a pointer we pass through to HANDLER. We don't actually
+ use either of those pointers otherwise ourselves. The idea is
+ that, if our caller wants to communicate something to BODY or
+ HANDLER, it can pass a pointer to it as MUMBLE_DATA, which BODY and
+ HANDLER can then use. Think of it as a way to make BODY and
+ HANDLER closures, not just functions; MUMBLE_DATA points to the
+ enclosed variables.
+
+ Of course, it's up to the caller to make sure that any data a
+ MUMBLE_DATA needs is protected from GC. A common way to do this is
+ to make MUMBLE_DATA a pointer to data stored in an automatic
+ structure variable; since the collector must scan the stack for
+ references anyway, this assures that any references in MUMBLE_DATA
+ will be found. */
+
SCM
-scm_catch_apply (tag, proc, a1, args, handler)
+scm_internal_catch (tag, body, body_data, handler, handler_data)
SCM tag;
- SCM proc;
- SCM a1;
- SCM args;
- SCM handler;
+ scm_catch_body_t body;
+ void *body_data;
+ scm_catch_handler_t handler;
+ void *handler_data;
{
struct jmp_buf_and_retval jbr;
SCM jmpbuf;
throw_tag = jbr.throw_tag;
jbr.throw_tag = SCM_EOL;
jbr.retval = SCM_EOL;
- answer = scm_apply (handler, scm_cons (throw_tag, throw_args), SCM_EOL);
+ answer = handler (handler_data, throw_tag, throw_args);
}
else
{
ACTIVATEJB (jmpbuf);
- if (tag == SCM_BOOL_F)
- answer = scm_apply (proc,
- SCM_NULLP (a1)
- ? scm_cons (jmpbuf, SCM_EOL)
- : scm_cons2 (jmpbuf, a1, args),
- SCM_EOL);
- else
- answer = scm_apply (proc, a1, args);
+ answer = body (body_data, jmpbuf);
SCM_REDEFER_INTS;
DEACTIVATEJB (jmpbuf);
scm_dynwinds = SCM_CDR (scm_dynwinds);
return answer;
}
+
+/* This is a body function you can pass to scm_internal_catch if you
+ want the body to be like Scheme's `catch' --- a thunk, or a
+ function of one argument if the tag is #f.
+
+ DATA contains the Scheme procedure to invoke. If the tag being
+ caught is #f, then we pass JMPBUF to the body procedure; otherwise,
+ it gets no arguments. */
+
+SCM
+scm_body_thunk (body_data, jmpbuf)
+ void *body_data;
+ SCM jmpbuf;
+{
+ struct scm_body_thunk_data *c = (struct scm_body_thunk_data *) body_data;
+
+ if (c->tag == SCM_BOOL_F)
+ return scm_apply (c->body_proc, scm_cons (jmpbuf, SCM_EOL), SCM_EOL);
+ else
+ return scm_apply (c->body_proc, SCM_EOL, SCM_EOL);
+}
+
+
+/* If the user does a throw to this catch, this function runs a
+ handler procedure written in Scheme. HANDLER_DATA is a pointer to
+ an SCM variable holding the Scheme procedure object to invoke. It
+ ought to be a pointer to an automatic, or the procedure object
+ should be otherwise protected from GC. */
+SCM
+scm_handle_by_proc (handler_data, tag, throw_args)
+ void *handler_data;
+ SCM tag;
+ SCM throw_args;
+{
+ SCM *handler_proc_p = (SCM *) handler_data;
+
+ return scm_apply (*handler_proc_p, scm_cons (tag, throw_args), SCM_EOL);
+}
+
+
SCM_PROC(s_catch, "catch", 3, 0, 0, scm_catch);
SCM
scm_catch (tag, thunk, handler)
SCM thunk;
SCM handler;
{
+ struct scm_body_thunk_data c;
+
SCM_ASSERT ((tag == SCM_BOOL_F)
|| (SCM_NIMP(tag) && SCM_SYMBOLP(tag))
|| (tag == SCM_BOOL_T),
tag, SCM_ARG1, s_catch);
- return scm_catch_apply (tag, thunk, SCM_EOL, SCM_EOL, handler);
+
+ c.tag = tag;
+ c.body_proc = thunk;
+
+ /* scm_internal_catch takes care of all the mechanics of setting up
+ a catch tag; we tell it to call scm_body_thunk to run the body,
+ and scm_handle_by_proc to deal with any throws to this catch.
+ The former receives a pointer to c, telling it how to behave.
+ The latter receives a pointer to HANDLER, so it knows who to call. */
+ return scm_internal_catch (tag,
+ scm_body_thunk, &c,
+ scm_handle_by_proc, &handler);
+}
+
+
+/* The smob tag for lazy_catch smobs. */
+static long tc16_lazy_catch;
+
+/* This is the structure we put on the wind list for a lazy catch. It
+ stores the handler function to call, and the data pointer to pass
+ through to it. It's not a Scheme closure, but it is a function
+ with data, so the term "closure" is appropriate in its broader
+ sense.
+
+ (We don't need anything like this in the "eager" catch code,
+ because the same C frame runs both the body and the handler.) */
+struct lazy_catch {
+ scm_catch_handler_t handler;
+ void *handler_data;
+};
+
+/* Strictly speaking, we could just pass a zero for our print
+ function, because we don't need to print them. They should never
+ appear in normal data structures, only in the wind list. However,
+ it might be nice for debugging someday... */
+static int
+print_lazy_catch (SCM closure, SCM port, scm_print_state *pstate)
+{
+ struct lazy_catch *c = (struct lazy_catch *) SCM_CDR (closure);
+ char buf[200];
+
+ sprintf (buf, "#<lazy-catch 0x%lx 0x%lx>",
+ (long) c->handler, (long) c->handler_data);
+ scm_gen_puts (scm_regular_string, buf, port);
+
+ return 1;
+}
+
+static scm_smobfuns lazy_catch_funs = {
+ scm_mark0, scm_free0, print_lazy_catch, 0
+};
+
+
+/* Given a pointer to a lazy catch structure, return a smob for it,
+ suitable for inclusion in the wind list. ("Ah yes, a Château
+ Gollombiere '72, no?"). */
+static SCM
+make_lazy_catch (struct lazy_catch *c)
+{
+ SCM smob;
+
+ SCM_NEWCELL (smob);
+ SCM_SETCDR (smob, c);
+ SCM_SETCAR (smob, tc16_lazy_catch);
+
+ return smob;
+}
+
+#define SCM_LAZY_CATCH_P(obj) \
+ (SCM_NIMP (obj) && (SCM_CAR (obj) == tc16_lazy_catch))
+
+
+/* Exactly like scm_internal_catch, except:
+ - It does not unwind the stack (this is the major difference).
+ - If handler returns, its value is returned from the throw.
+ - BODY always receives #f as its JMPBUF argument (since there's no
+ jmpbuf associated with a lazy catch, because we don't unwind the
+ stack.) */
+SCM
+scm_internal_lazy_catch (tag, body, body_data, handler, handler_data)
+ SCM tag;
+ scm_catch_body_t body;
+ void *body_data;
+ scm_catch_handler_t handler;
+ void *handler_data;
+{
+ SCM lazy_catch, answer;
+ struct lazy_catch c;
+
+ c.handler = handler;
+ c.handler_data = handler_data;
+ lazy_catch = make_lazy_catch (&c);
+
+ SCM_REDEFER_INTS;
+ scm_dynwinds = scm_acons (tag, lazy_catch, scm_dynwinds);
+ SCM_REALLOW_INTS;
+
+ answer = (*body) (body_data, SCM_BOOL_F);
+
+ SCM_REDEFER_INTS;
+ scm_dynwinds = SCM_CDR (scm_dynwinds);
+ SCM_REALLOW_INTS;
+
+ return answer;
}
+
SCM_PROC(s_lazy_catch, "lazy-catch", 3, 0, 0, scm_lazy_catch);
SCM
scm_lazy_catch (tag, thunk, handler)
SCM thunk;
SCM handler;
{
+ struct scm_body_thunk_data c;
+
SCM_ASSERT ((SCM_NIMP(tag) && SCM_SYMBOLP(tag))
|| (tag == SCM_BOOL_T),
tag, SCM_ARG1, s_lazy_catch);
- scm_dynwinds = scm_acons (tag, handler, scm_dynwinds);
- return scm_apply (thunk, SCM_EOL, SCM_EOL);
+
+ c.tag = tag;
+ c.body_proc = thunk;
+
+ /* scm_internal_lazy_catch takes care of all the mechanics of
+ setting up a lazy catch tag; we tell it to call scm_body_thunk to
+ run the body, and scm_handle_by_proc to deal with any throws to
+ this catch. The former receives a pointer to c, telling it how
+ to behave. The latter receives a pointer to HANDLER, so it knows
+ who to call. */
+ return scm_internal_lazy_catch (tag,
+ scm_body_thunk, &c,
+ scm_handle_by_proc, &handler);
}
-/* The user has thrown to an uncaught key --- print a message and die.
+
+/* The user has thrown to an uncaught key --- print a message and die.
+ At boot time, we establish a catch-all that uses this as its handler.
1) If the user wants something different, they can use (catch #t
...) to do what they like.
2) Outside the context of a read-eval-print loop, there isn't
anything else good to do; libguile should not assume the existence
of a read-eval-print loop.
3) Given that we shouldn't do anything complex, it's much more
- robust to do it in C code. */
-static SCM uncaught_throw SCM_P ((SCM key, SCM args));
-static SCM
-uncaught_throw (key, args)
- SCM key;
+ robust to do it in C code.
+
+ HANDLER_DATA, if non-zero, is assumed to be a char * pointing to a
+ message header to print; if zero, we use "guile" instead. That
+ text is followed by a colon, then the message described by ARGS. */
+
+SCM
+scm_handle_by_message (handler_data, tag, args)
+ void *handler_data;
+ SCM tag;
SCM args;
{
- SCM p = scm_def_errp;
- scm_gen_puts (scm_regular_string, "guile: uncaught throw to ", p);
- scm_prin1 (key, p, 0);
+ char *prog_name = (char *) handler_data;
+ SCM p = scm_def_errp;
+
+ if (! prog_name)
+ prog_name = "guile";
+
+ scm_gen_puts (scm_regular_string, prog_name, p);
scm_gen_puts (scm_regular_string, ": ", p);
- scm_prin1 (args, p, 1);
- scm_gen_putc ('\n', p);
-
+
+ if (scm_ilength (args) >= 3)
+ {
+ SCM message = SCM_CADR (args);
+ SCM parts = SCM_CADDR (args);
+
+ scm_display_error_message (message, parts, p);
+ }
+ else
+ {
+ scm_gen_puts (scm_regular_string, "uncaught throw to ", p);
+ scm_prin1 (tag, p, 0);
+ scm_gen_puts (scm_regular_string, ": ", p);
+ scm_prin1 (args, p, 1);
+ scm_gen_putc ('\n', p);
+ }
+
exit (2);
}
-static char s_throw[];
+SCM_PROC(s_throw, "throw", 1, 0, 1, scm_throw);
+SCM
+scm_throw (key, args)
+ SCM key;
+ SCM args;
+{
+ /* May return if handled by lazy catch. */
+ return scm_ithrow (key, args, 1);
+}
+
+
SCM
scm_ithrow (key, args, noreturn)
SCM key;
}
else
{
- SCM dynpair;
+ SCM dynpair = SCM_UNDEFINED;
SCM winds;
if (noreturn)
}
}
- /* If we didn't find anything, print a message and exit Guile. */
+ /* If we didn't find anything, abort. scm_boot_guile should
+ have established a catch-all, but obviously things are
+ thoroughly screwed up. */
if (winds == SCM_EOL)
- uncaught_throw (key, args);
+ abort ();
+ /* If the wind list is malformed, bail. */
if (SCM_IMP (winds) || SCM_NCONSP (winds))
abort ();
SCM_CDAR (wind_goal) != jmpbuf;
wind_goal = SCM_CDR (wind_goal))
;
- scm_dowinds (wind_goal, scm_ilength (scm_dynwinds) - scm_ilength (wind_goal));
- if (!SCM_JMPBUFP (jmpbuf))
+
+ /* Is a lazy catch? In wind list entries for lazy catches, the key
+ is bound to a lazy_catch smob, not a jmpbuf. */
+ if (SCM_LAZY_CATCH_P (jmpbuf))
{
+ struct lazy_catch *c = (struct lazy_catch *) SCM_CDR (jmpbuf);
+ SCM oldwinds = scm_dynwinds;
+ SCM handle, answer;
+ scm_dowinds (wind_goal, (scm_ilength (scm_dynwinds)
+ - scm_ilength (wind_goal)));
SCM_REDEFER_INTS;
+ handle = scm_dynwinds;
scm_dynwinds = SCM_CDR (scm_dynwinds);
SCM_REALLOW_INTS;
- return scm_apply (jmpbuf, scm_cons (key, args), SCM_EOL);
+ answer = (c->handler) (c->handler_data, key, args);
+ SCM_REDEFER_INTS;
+ SCM_SETCDR (handle, scm_dynwinds);
+ scm_dynwinds = handle;
+ SCM_REALLOW_INTS;
+ scm_dowinds (oldwinds, (scm_ilength (scm_dynwinds)
+ - scm_ilength (oldwinds)));
+ return answer;
}
- else
+
+ /* Otherwise, it's a normal catch. */
+ else if (SCM_JMPBUFP (jmpbuf))
{
struct jmp_buf_and_retval * jbr;
+ scm_dowinds (wind_goal, (scm_ilength (scm_dynwinds)
+ - scm_ilength (wind_goal)));
jbr = (struct jmp_buf_and_retval *)JBJMPBUF (jmpbuf);
jbr->throw_tag = key;
jbr->retval = args;
}
+
+ /* Otherwise, it's some random piece of junk. */
+ else
+ abort ();
+
#ifdef DEBUG_EXTENSIONS
scm_last_debug_frame = SCM_JBDFRAME (jmpbuf);
#endif
}
-SCM_PROC(s_throw, "throw", 1, 0, 1, scm_throw);
-SCM
-scm_throw (key, args)
- SCM key;
- SCM args;
-{
- /* May return if handled by lazy catch. */
- return scm_ithrow (key, args, 1);
-}
-
-
void
scm_init_throw ()
{
scm_tc16_jmpbuffer = scm_newsmob (&jbsmob);
+ tc16_lazy_catch = scm_newsmob (&lazy_catch_funs);
#include "throw.x"
}