|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H |
|
# include <config.h> |
|
#endif |
|
|
|
#include <stdio.h> |
|
#include <unistdio.h> |
|
|
|
#include "backtrace.h" |
|
#include "boolean.h" |
|
#include "debug.h" |
|
#include "dynwind.h" |
|
#include "eq.h" |
|
#include "eval.h" |
|
#include "exceptions.h" |
|
#include "fluids.h" |
|
#include "gsubr.h" |
|
#include "init.h" |
|
#include "list.h" |
|
#include "modules.h" |
|
#include "numbers.h" |
|
#include "pairs.h" |
|
#include "ports.h" |
|
#include "private-options.h" |
|
#include "smob.h" |
|
#include "stackchk.h" |
|
#include "stacks.h" |
|
#include "strings.h" |
|
#include "symbols.h" |
|
#include "variable.h" |
|
#include "vm.h" |
|
|
|
#include "throw.h" |
|
|
|
|
|
|
|
|
|
static SCM throw_var; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct scm_catch_data |
|
{ |
|
SCM tag; |
|
scm_t_thunk body; |
|
void *body_data; |
|
scm_t_catch_handler handler; |
|
void *handler_data; |
|
scm_t_catch_handler pre_unwind_handler; |
|
void *pre_unwind_handler_data; |
|
SCM pre_unwind_running; |
|
}; |
|
|
|
static SCM |
|
catch_post_unwind_handler (void *data, SCM exn) |
|
{ |
|
struct scm_catch_data *catch_data = data; |
|
return catch_data->handler (catch_data->handler_data, |
|
scm_exception_kind (exn), |
|
scm_exception_args (exn)); |
|
} |
|
|
|
static SCM |
|
catch_pre_unwind_handler (void *data, SCM exn) |
|
{ |
|
struct scm_catch_data *catch_data = data; |
|
SCM kind = scm_exception_kind (exn); |
|
SCM args = scm_exception_args (exn); |
|
if ((scm_is_eq (catch_data->tag, SCM_BOOL_T) |
|
|| scm_is_eq (kind, catch_data->tag)) |
|
&& scm_is_false (scm_fluid_ref (catch_data->pre_unwind_running))) { |
|
scm_dynwind_begin (SCM_F_DYNWIND_REWINDABLE); |
|
scm_dynwind_throw_handler (); |
|
scm_dynwind_fluid (catch_data->pre_unwind_running, SCM_BOOL_T); |
|
catch_data->pre_unwind_handler (catch_data->pre_unwind_handler_data, |
|
kind, args); |
|
scm_dynwind_end (); |
|
} |
|
return scm_raise_exception (exn); |
|
} |
|
|
|
static SCM |
|
catch_body (void *data) |
|
{ |
|
struct scm_catch_data *catch_data = data; |
|
|
|
if (catch_data->pre_unwind_handler) { |
|
SCM thunk = scm_c_make_thunk (catch_data->body, catch_data->body_data); |
|
SCM handler = scm_c_make_exception_handler (catch_pre_unwind_handler, data); |
|
SCM fluid = scm_make_thread_local_fluid (SCM_BOOL_F); |
|
catch_data->pre_unwind_running = fluid; |
|
return scm_with_pre_unwind_exception_handler (handler, thunk); |
|
} |
|
|
|
return catch_data->body (catch_data->body_data); |
|
} |
|
|
|
SCM |
|
scm_c_catch (SCM tag, |
|
scm_t_thunk body, void *body_data, |
|
scm_t_catch_handler handler, void *handler_data, |
|
scm_t_catch_handler pre_unwind_handler, void *pre_unwind_handler_data) |
|
{ |
|
struct scm_catch_data data = |
|
{ tag, body, body_data, handler, handler_data, pre_unwind_handler, |
|
pre_unwind_handler_data, SCM_BOOL_F }; |
|
|
|
return scm_c_with_exception_handler (tag, catch_post_unwind_handler, &data, |
|
catch_body, &data); |
|
} |
|
|
|
SCM |
|
scm_internal_catch (SCM tag, |
|
scm_t_thunk body, void *body_data, |
|
scm_t_catch_handler handler, void *handler_data) |
|
{ |
|
return scm_c_catch (tag, |
|
body, body_data, |
|
handler, handler_data, |
|
NULL, NULL); |
|
} |
|
|
|
|
|
SCM |
|
scm_c_with_throw_handler (SCM tag, |
|
scm_t_thunk body, |
|
void *body_data, |
|
scm_t_catch_handler handler, |
|
void *handler_data, |
|
int lazy_catch_p) |
|
{ |
|
struct scm_catch_data data = |
|
{ tag, body, body_data, NULL, NULL, handler, handler_data, SCM_BOOL_F }; |
|
|
|
if (lazy_catch_p) |
|
|
|
|
|
abort (); |
|
|
|
return catch_body (&data); |
|
} |
|
|
|
static SCM |
|
call_thunk (void* data) |
|
{ |
|
return scm_call_0 (PTR2SCM (data)); |
|
} |
|
|
|
static SCM |
|
call_handler (void* data, SCM a, SCM b) |
|
{ |
|
return scm_call_2 (PTR2SCM (data), a, b); |
|
} |
|
|
|
SCM |
|
scm_catch (SCM key, SCM thunk, SCM handler) |
|
{ |
|
return scm_c_catch (key, call_thunk, SCM2PTR (thunk), |
|
call_handler, SCM2PTR (handler), NULL, NULL); |
|
} |
|
|
|
SCM |
|
scm_catch_with_pre_unwind_handler (SCM key, SCM thunk, SCM handler, |
|
SCM pre_unwind_handler) |
|
{ |
|
if (SCM_UNBNDP (pre_unwind_handler)) |
|
return scm_catch (key, thunk, handler); |
|
|
|
return scm_c_catch (key, call_thunk, SCM2PTR (thunk), |
|
call_handler, SCM2PTR (handler), |
|
call_handler, SCM2PTR (pre_unwind_handler)); |
|
} |
|
|
|
SCM |
|
scm_with_throw_handler (SCM key, SCM thunk, SCM handler) |
|
{ |
|
return scm_c_with_throw_handler (key, call_thunk, SCM2PTR (thunk), |
|
call_handler, SCM2PTR (handler), 0); |
|
} |
|
|
|
SCM |
|
scm_throw (SCM key, SCM args) |
|
{ |
|
SCM throw = scm_variable_ref (throw_var); |
|
if (scm_is_false (throw)) { |
|
static int error_printing_error = 0; |
|
if (error_printing_error++) |
|
{ |
|
fprintf (stderr, "Error while printing pre-boot error: %s\n", |
|
scm_i_symbol_chars (key)); |
|
} |
|
else |
|
{ |
|
SCM port = scm_current_error_port (); |
|
scm_puts ("Pre-boot error; key: ", port); |
|
scm_write (key, port); |
|
scm_puts (", args: ", port); |
|
scm_write (args, port); |
|
} |
|
abort (); |
|
} |
|
scm_apply_1 (throw, key, args); |
|
|
|
abort (); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
static scm_t_bits tc16_catch_handler; |
|
|
|
SCM |
|
scm_i_make_catch_handler (scm_t_catch_handler handler, void *data) |
|
{ |
|
SCM_RETURN_NEWSMOB2 (tc16_catch_handler, handler, data); |
|
} |
|
|
|
static SCM |
|
apply_catch_handler (SCM clo, SCM args) |
|
{ |
|
scm_t_catch_handler handler = (void*)SCM_SMOB_DATA (clo); |
|
void *data = (void*)SCM_SMOB_DATA_2 (clo); |
|
return handler (data, scm_car (args), scm_cdr (args)); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SCM |
|
scm_body_thunk (void *body_data) |
|
{ |
|
struct scm_body_thunk_data *c = (struct scm_body_thunk_data *) body_data; |
|
|
|
return scm_call_0 (c->body_proc); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SCM |
|
scm_handle_by_proc (void *handler_data, SCM tag, SCM throw_args) |
|
{ |
|
SCM *handler_proc_p = (SCM *) handler_data; |
|
|
|
return scm_apply_1 (*handler_proc_p, tag, throw_args); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
struct hbpca_data { |
|
SCM proc; |
|
SCM args; |
|
}; |
|
|
|
static SCM |
|
hbpca_body (void *body_data) |
|
{ |
|
struct hbpca_data *data = (struct hbpca_data *)body_data; |
|
return scm_apply_0 (data->proc, data->args); |
|
} |
|
|
|
SCM |
|
scm_handle_by_proc_catching_all (void *handler_data, SCM tag, SCM throw_args) |
|
{ |
|
SCM *handler_proc_p = (SCM *) handler_data; |
|
struct hbpca_data data; |
|
data.proc = *handler_proc_p; |
|
data.args = scm_cons (tag, throw_args); |
|
|
|
return scm_internal_catch (SCM_BOOL_T, |
|
hbpca_body, &data, |
|
scm_handle_by_message_noexit, NULL); |
|
} |
|
|
|
|
|
static int |
|
should_print_backtrace (SCM tag, SCM stack) |
|
{ |
|
return SCM_BACKTRACE_P |
|
&& scm_is_true (stack) |
|
&& scm_initialized_p |
|
|
|
|
|
&& !scm_is_eq (tag, scm_from_latin1_symbol ("read-error")) |
|
&& !scm_is_eq (tag, scm_from_latin1_symbol ("syntax-error")); |
|
} |
|
|
|
static void |
|
handler_message (void *handler_data, SCM tag, SCM args) |
|
{ |
|
SCM p, stack, frame; |
|
|
|
p = scm_current_error_port (); |
|
|
|
|
|
|
|
|
|
|
|
|
|
stack = scm_make_stack (SCM_BOOL_T, scm_list_1 (scm_from_int (2))); |
|
frame = scm_is_true (stack) ? scm_stack_ref (stack, SCM_INUM0) : SCM_BOOL_F; |
|
|
|
if (should_print_backtrace (tag, stack)) |
|
{ |
|
scm_puts ("Backtrace:\n", p); |
|
scm_display_backtrace_with_highlights (stack, p, |
|
SCM_BOOL_F, SCM_BOOL_F, |
|
SCM_EOL); |
|
scm_newline (p); |
|
} |
|
|
|
scm_print_exception (p, frame, tag, args); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SCM |
|
scm_handle_by_message (void *handler_data, SCM tag, SCM args) |
|
{ |
|
if (scm_is_true (scm_eq_p (tag, scm_from_latin1_symbol ("quit")))) |
|
exit (scm_exit_status (args)); |
|
|
|
handler_message (handler_data, tag, args); |
|
scm_i_pthread_exit (NULL); |
|
|
|
|
|
|
|
|
|
return SCM_BOOL_F; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
SCM |
|
scm_handle_by_message_noexit (void *handler_data, SCM tag, SCM args) |
|
{ |
|
if (scm_is_true (scm_eq_p (tag, scm_from_latin1_symbol ("quit")))) |
|
exit (scm_exit_status (args)); |
|
|
|
handler_message (handler_data, tag, args); |
|
|
|
return SCM_BOOL_F; |
|
} |
|
|
|
|
|
SCM |
|
scm_handle_by_throw (void *handler_data SCM_UNUSED, SCM tag, SCM args) |
|
{ |
|
scm_ithrow (tag, args, 1); |
|
return SCM_UNSPECIFIED; |
|
} |
|
|
|
SCM |
|
scm_ithrow (SCM key, SCM args, int no_return SCM_UNUSED) |
|
{ |
|
scm_throw (key, args); |
|
} |
|
|
|
void |
|
scm_init_throw () |
|
{ |
|
tc16_catch_handler = scm_make_smob_type ("catch-handler", 0); |
|
scm_set_smob_apply (tc16_catch_handler, apply_catch_handler, 0, 0, 1); |
|
|
|
throw_var = scm_c_define ("throw", SCM_BOOL_F); |
|
|
|
#include "throw.x" |
|
} |
|
|