2014-08-25 00:38:09 -04:00
|
|
|
/**
|
|
|
|
* See Copyright Notice in picrin.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "picrin.h"
|
2016-02-18 10:56:56 -05:00
|
|
|
#include "picrin/object.h"
|
2014-08-25 00:38:09 -04:00
|
|
|
|
|
|
|
void
|
2015-05-28 04:06:41 -04:00
|
|
|
pic_panic(pic_state PIC_UNUSED(*pic), const char *msg)
|
2014-08-25 00:38:09 -04:00
|
|
|
{
|
2015-07-19 13:52:47 -04:00
|
|
|
extern PIC_NORETURN void abort();
|
2015-05-30 06:57:32 -04:00
|
|
|
|
2015-05-27 13:12:26 -04:00
|
|
|
#if DEBUG
|
2014-08-25 00:38:09 -04:00
|
|
|
fprintf(stderr, "abort: %s\n", msg);
|
2015-05-27 13:12:26 -04:00
|
|
|
#else
|
2015-05-28 04:06:41 -04:00
|
|
|
(void)msg;
|
2015-05-27 13:12:26 -04:00
|
|
|
#endif
|
|
|
|
PIC_ABORT(pic);
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
pic_warnf(pic_state *pic, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
2016-02-19 13:26:52 -05:00
|
|
|
pic_value err;
|
2014-08-25 00:38:09 -04:00
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2016-02-18 09:49:16 -05:00
|
|
|
err = pic_vstrf_value(pic, fmt, ap);
|
2014-08-25 00:38:09 -04:00
|
|
|
va_end(ap);
|
|
|
|
|
2016-02-20 02:51:24 -05:00
|
|
|
pic_fprintf(pic, pic_stderr(pic), "warn: %s\n", pic_str(pic, err));
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
2014-09-17 08:58:51 -04:00
|
|
|
void
|
|
|
|
pic_errorf(pic_state *pic, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
const char *msg;
|
2016-02-19 13:26:52 -05:00
|
|
|
pic_value err;
|
2014-09-17 08:58:51 -04:00
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2016-02-18 09:49:16 -05:00
|
|
|
err = pic_vstrf_value(pic, fmt, ap);
|
2014-09-17 08:58:51 -04:00
|
|
|
va_end(ap);
|
|
|
|
|
2016-02-18 09:49:16 -05:00
|
|
|
msg = pic_str(pic, err);
|
2014-09-17 08:58:51 -04:00
|
|
|
|
2016-02-18 11:34:13 -05:00
|
|
|
pic_error(pic, "", msg, pic_nil_value(pic));
|
2014-09-17 08:58:51 -04:00
|
|
|
}
|
|
|
|
|
2016-02-20 09:55:40 -05:00
|
|
|
static pic_value
|
|
|
|
native_exception_handler(pic_state *pic)
|
2014-09-18 01:12:18 -04:00
|
|
|
{
|
|
|
|
pic_value err;
|
|
|
|
|
2016-02-19 10:03:16 -05:00
|
|
|
pic_get_args(pic, "o", &err);
|
2014-09-18 01:12:18 -04:00
|
|
|
|
|
|
|
pic->err = err;
|
|
|
|
|
2016-02-19 10:03:16 -05:00
|
|
|
pic_call(pic, pic_closure_ref(pic, 0), 1, pic_false_value(pic));
|
2014-09-18 01:12:18 -04:00
|
|
|
|
2015-01-07 16:11:48 -05:00
|
|
|
PIC_UNREACHABLE();
|
2014-09-18 01:12:18 -04:00
|
|
|
}
|
|
|
|
|
2016-02-20 09:55:40 -05:00
|
|
|
void
|
|
|
|
pic_push_native_handler(pic_state *pic, struct pic_cont *cont)
|
|
|
|
{
|
|
|
|
pic_value handler;
|
|
|
|
|
|
|
|
handler = pic_lambda(pic, native_exception_handler, 1, pic_make_cont(pic, cont));
|
|
|
|
|
|
|
|
pic_push_handler(pic, handler);
|
|
|
|
}
|
|
|
|
|
2014-09-24 07:21:28 -04:00
|
|
|
void
|
2016-02-19 10:03:16 -05:00
|
|
|
pic_push_handler(pic_state *pic, pic_value handler)
|
2014-08-25 00:38:09 -04:00
|
|
|
{
|
2014-09-26 10:24:26 -04:00
|
|
|
size_t xp_len;
|
|
|
|
ptrdiff_t xp_offset;
|
2014-09-18 01:12:18 -04:00
|
|
|
|
2014-09-24 07:21:28 -04:00
|
|
|
if (pic->xp >= pic->xpend) {
|
2014-09-26 10:24:26 -04:00
|
|
|
xp_len = (size_t)(pic->xpend - pic->xpbase) * 2;
|
2014-09-24 07:21:28 -04:00
|
|
|
xp_offset = pic->xp - pic->xpbase;
|
|
|
|
pic->xpbase = pic_realloc(pic, pic->xpbase, sizeof(struct pic_proc *) * xp_len);
|
|
|
|
pic->xp = pic->xpbase + xp_offset;
|
|
|
|
pic->xpend = pic->xpbase + xp_len;
|
2014-09-24 02:35:00 -04:00
|
|
|
}
|
2014-09-24 07:21:28 -04:00
|
|
|
|
2016-02-19 10:03:16 -05:00
|
|
|
*pic->xp++ = pic_proc_ptr(pic, handler);
|
2014-09-18 01:12:18 -04:00
|
|
|
}
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2016-02-19 10:03:16 -05:00
|
|
|
pic_value
|
2015-06-01 18:57:52 -04:00
|
|
|
pic_pop_handler(pic_state *pic)
|
2014-09-18 01:12:18 -04:00
|
|
|
{
|
2015-06-01 18:57:52 -04:00
|
|
|
if (pic->xp == pic->xpbase) {
|
|
|
|
pic_panic(pic, "no exception handler registered");
|
|
|
|
}
|
|
|
|
|
2016-02-19 10:03:16 -05:00
|
|
|
return pic_obj_value(*--pic->xp);
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
2016-02-20 02:59:05 -05:00
|
|
|
pic_value
|
2016-02-18 11:34:13 -05:00
|
|
|
pic_make_error(pic_state *pic, const char *type, const char *msg, pic_value irrs)
|
2014-08-25 00:38:09 -04:00
|
|
|
{
|
|
|
|
struct pic_error *e;
|
2016-02-20 01:31:14 -05:00
|
|
|
pic_value stack, ty = pic_intern_cstr(pic, type);
|
2014-08-25 00:38:09 -04:00
|
|
|
|
|
|
|
stack = pic_get_backtrace(pic);
|
|
|
|
|
2016-02-18 09:25:45 -05:00
|
|
|
e = (struct pic_error *)pic_obj_alloc(pic, sizeof(struct pic_error), PIC_TYPE_ERROR);
|
2016-02-20 01:31:14 -05:00
|
|
|
e->type = pic_sym_ptr(pic, ty);
|
2016-02-19 13:26:52 -05:00
|
|
|
e->msg = pic_str_ptr(pic, pic_cstr_value(pic, msg));
|
2014-08-25 00:38:09 -04:00
|
|
|
e->irrs = irrs;
|
2016-02-19 13:26:52 -05:00
|
|
|
e->stack = pic_str_ptr(pic, stack);
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2016-02-20 02:59:05 -05:00
|
|
|
return pic_obj_value(e);
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
2014-09-17 08:58:51 -04:00
|
|
|
pic_value
|
|
|
|
pic_raise_continuable(pic_state *pic, pic_value err)
|
|
|
|
{
|
2016-02-19 10:03:16 -05:00
|
|
|
pic_value handler, v;
|
2014-09-17 08:58:51 -04:00
|
|
|
|
2015-06-01 18:57:52 -04:00
|
|
|
handler = pic_pop_handler(pic);
|
2014-09-17 09:21:31 -04:00
|
|
|
|
2016-02-19 10:03:16 -05:00
|
|
|
pic_protect(pic, handler);
|
2014-09-17 09:21:31 -04:00
|
|
|
|
2016-02-13 23:46:55 -05:00
|
|
|
v = pic_call(pic, handler, 1, err);
|
2014-09-17 09:21:31 -04:00
|
|
|
|
2015-06-01 18:57:52 -04:00
|
|
|
pic_push_handler(pic, handler);
|
2014-09-17 09:21:31 -04:00
|
|
|
|
2014-09-17 09:34:51 -04:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2015-01-07 16:11:48 -05:00
|
|
|
void
|
2014-09-17 09:34:51 -04:00
|
|
|
pic_raise(pic_state *pic, pic_value err)
|
|
|
|
{
|
|
|
|
pic_value val;
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2014-09-17 09:34:51 -04:00
|
|
|
val = pic_raise_continuable(pic, err);
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2015-06-01 18:57:52 -04:00
|
|
|
pic_pop_handler(pic);
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2014-09-17 09:34:51 -04:00
|
|
|
pic_errorf(pic, "error handler returned with ~s on error ~s", val, err);
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
2015-01-07 16:11:48 -05:00
|
|
|
void
|
2016-02-18 11:34:13 -05:00
|
|
|
pic_error(pic_state *pic, const char *type, const char *msg, pic_value irrs)
|
2014-08-25 00:38:09 -04:00
|
|
|
{
|
2016-02-20 02:59:05 -05:00
|
|
|
pic_raise(pic, pic_make_error(pic, type, msg, irrs));
|
2014-09-16 11:28:55 -04:00
|
|
|
}
|
|
|
|
|
2014-08-25 00:38:09 -04:00
|
|
|
static pic_value
|
|
|
|
pic_error_with_exception_handler(pic_state *pic)
|
|
|
|
{
|
2016-02-19 10:03:16 -05:00
|
|
|
pic_value handler, thunk, val;
|
2014-08-25 00:38:09 -04:00
|
|
|
|
|
|
|
pic_get_args(pic, "ll", &handler, &thunk);
|
|
|
|
|
2015-06-01 18:57:52 -04:00
|
|
|
pic_push_handler(pic, handler);
|
2014-09-17 09:13:00 -04:00
|
|
|
|
2016-02-13 23:46:55 -05:00
|
|
|
val = pic_call(pic, thunk, 0);
|
2014-09-17 09:13:00 -04:00
|
|
|
|
2015-06-01 18:57:52 -04:00
|
|
|
pic_pop_handler(pic);
|
2014-09-17 09:13:00 -04:00
|
|
|
|
2014-09-16 11:28:55 -04:00
|
|
|
return val;
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
2015-01-07 16:11:48 -05:00
|
|
|
static pic_value
|
2014-08-25 00:38:09 -04:00
|
|
|
pic_error_raise(pic_state *pic)
|
|
|
|
{
|
|
|
|
pic_value v;
|
|
|
|
|
|
|
|
pic_get_args(pic, "o", &v);
|
|
|
|
|
2014-09-16 11:28:55 -04:00
|
|
|
pic_raise(pic, v);
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_error_raise_continuable(pic_state *pic)
|
|
|
|
{
|
|
|
|
pic_value v;
|
|
|
|
|
|
|
|
pic_get_args(pic, "o", &v);
|
|
|
|
|
2014-09-17 08:58:51 -04:00
|
|
|
return pic_raise_continuable(pic, v);
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
2015-01-07 16:11:48 -05:00
|
|
|
static pic_value
|
2014-08-25 00:38:09 -04:00
|
|
|
pic_error_error(pic_state *pic)
|
|
|
|
{
|
|
|
|
const char *str;
|
2015-08-26 06:04:27 -04:00
|
|
|
int argc;
|
2014-08-25 00:38:09 -04:00
|
|
|
pic_value *argv;
|
|
|
|
|
|
|
|
pic_get_args(pic, "z*", &str, &argc, &argv);
|
|
|
|
|
2016-02-18 12:29:40 -05:00
|
|
|
pic_error(pic, "", str, pic_make_list(pic, argc, argv));
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_error_error_object_p(pic_state *pic)
|
|
|
|
{
|
|
|
|
pic_value v;
|
|
|
|
|
|
|
|
pic_get_args(pic, "o", &v);
|
|
|
|
|
2016-02-18 06:15:42 -05:00
|
|
|
return pic_bool_value(pic, pic_error_p(pic, v));
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_error_error_object_message(pic_state *pic)
|
|
|
|
{
|
2016-02-20 02:59:05 -05:00
|
|
|
pic_value e;
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2016-02-20 02:59:05 -05:00
|
|
|
pic_get_args(pic, "o", &e);
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2016-02-20 02:59:05 -05:00
|
|
|
pic_assert_type(pic, e, error);
|
|
|
|
|
|
|
|
return pic_obj_value(pic_error_ptr(pic, e)->msg);
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_error_error_object_irritants(pic_state *pic)
|
|
|
|
{
|
2016-02-20 02:59:05 -05:00
|
|
|
pic_value e;
|
|
|
|
|
|
|
|
pic_get_args(pic, "o", &e);
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2016-02-20 02:59:05 -05:00
|
|
|
pic_assert_type(pic, e, error);
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2016-02-20 02:59:05 -05:00
|
|
|
return pic_error_ptr(pic, e)->irrs;
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
2014-09-16 11:49:37 -04:00
|
|
|
pic_error_error_object_type(pic_state *pic)
|
2014-08-25 00:38:09 -04:00
|
|
|
{
|
2016-02-20 02:59:05 -05:00
|
|
|
pic_value e;
|
|
|
|
|
|
|
|
pic_get_args(pic, "o", &e);
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2016-02-20 02:59:05 -05:00
|
|
|
pic_assert_type(pic, e, error);
|
2014-09-16 11:28:55 -04:00
|
|
|
|
2016-02-20 02:59:05 -05:00
|
|
|
return pic_obj_value(pic_error_ptr(pic, e)->type);
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
pic_init_error(pic_state *pic)
|
|
|
|
{
|
|
|
|
pic_defun(pic, "with-exception-handler", pic_error_with_exception_handler);
|
|
|
|
pic_defun(pic, "raise", pic_error_raise);
|
|
|
|
pic_defun(pic, "raise-continuable", pic_error_raise_continuable);
|
|
|
|
pic_defun(pic, "error", pic_error_error);
|
|
|
|
pic_defun(pic, "error-object?", pic_error_error_object_p);
|
|
|
|
pic_defun(pic, "error-object-message", pic_error_error_object_message);
|
|
|
|
pic_defun(pic, "error-object-irritants", pic_error_error_object_irritants);
|
2014-09-16 11:49:37 -04:00
|
|
|
pic_defun(pic, "error-object-type", pic_error_error_object_type);
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|