2014-08-25 00:38:09 -04:00
|
|
|
/**
|
|
|
|
* See Copyright Notice in picrin.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
|
|
|
#include "picrin.h"
|
|
|
|
#include "picrin/pair.h"
|
2014-09-18 01:12:18 -04:00
|
|
|
#include "picrin/proc.h"
|
|
|
|
#include "picrin/cont.h"
|
2014-09-24 02:59:23 -04:00
|
|
|
#include "picrin/data.h"
|
2014-08-25 00:38:09 -04:00
|
|
|
#include "picrin/string.h"
|
|
|
|
#include "picrin/error.h"
|
|
|
|
|
|
|
|
void
|
2014-09-16 12:02:27 -04:00
|
|
|
pic_panic(pic_state *pic, const char *msg)
|
2014-08-25 00:38:09 -04:00
|
|
|
{
|
2015-01-07 16:11:48 -05:00
|
|
|
PIC_UNUSED(pic);
|
2014-08-25 00:38:09 -04:00
|
|
|
|
|
|
|
fprintf(stderr, "abort: %s\n", msg);
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
pic_warnf(pic_state *pic, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
pic_value err_line;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2014-08-26 00:30:08 -04:00
|
|
|
err_line = pic_xvformat(pic, fmt, ap);
|
2014-08-25 00:38:09 -04:00
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
fprintf(stderr, "warn: %s\n", pic_str_cstr(pic_str_ptr(pic_car(pic, err_line))));
|
|
|
|
}
|
|
|
|
|
2014-09-17 08:58:51 -04:00
|
|
|
void
|
|
|
|
pic_errorf(pic_state *pic, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
pic_value err_line, irrs;
|
|
|
|
const char *msg;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
err_line = pic_xvformat(pic, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
msg = pic_str_cstr(pic_str_ptr(pic_car(pic, err_line)));
|
|
|
|
irrs = pic_cdr(pic, err_line);
|
|
|
|
|
|
|
|
pic_error(pic, msg, irrs);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
pic_errmsg(pic_state *pic)
|
|
|
|
{
|
|
|
|
pic_str *str;
|
|
|
|
|
|
|
|
assert(! pic_undef_p(pic->err));
|
|
|
|
|
|
|
|
if (! pic_error_p(pic->err)) {
|
|
|
|
str = pic_format(pic, "~s", pic->err);
|
|
|
|
} else {
|
|
|
|
str = pic_error_ptr(pic->err)->msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
return pic_str_cstr(str);
|
|
|
|
}
|
|
|
|
|
2015-01-07 16:11:48 -05:00
|
|
|
static pic_value
|
2014-09-18 01:12:18 -04:00
|
|
|
native_exception_handler(pic_state *pic)
|
|
|
|
{
|
|
|
|
pic_value err;
|
|
|
|
struct pic_proc *cont;
|
|
|
|
|
|
|
|
pic_get_args(pic, "o", &err);
|
|
|
|
|
|
|
|
pic->err = err;
|
|
|
|
|
2014-09-24 20:44:21 -04:00
|
|
|
cont = pic_proc_ptr(pic_attr_ref(pic, pic_obj_value(pic_get_proc(pic)), "@@escape"));
|
2014-09-18 01:12:18 -04:00
|
|
|
|
|
|
|
pic_apply1(pic, cont, pic_false_value());
|
|
|
|
|
2015-01-07 16:11:48 -05:00
|
|
|
PIC_UNREACHABLE();
|
2014-09-18 01:12:18 -04:00
|
|
|
}
|
|
|
|
|
2014-09-24 07:21:28 -04:00
|
|
|
void
|
|
|
|
pic_push_try(pic_state *pic, struct pic_escape *escape)
|
2014-08-25 00:38:09 -04:00
|
|
|
{
|
2014-09-24 07:21:28 -04:00
|
|
|
struct pic_proc *cont, *handler;
|
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
|
|
|
cont = pic_make_econt(pic, escape);
|
2014-09-18 01:12:18 -04:00
|
|
|
|
2014-09-24 07:21:28 -04:00
|
|
|
handler = pic_make_proc(pic, native_exception_handler, "(native-exception-handler)");
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2014-09-24 20:44:21 -04:00
|
|
|
pic_attr_set(pic, pic_obj_value(handler), "@@escape", pic_obj_value(cont));
|
2014-08-25 00:38:09 -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
|
|
|
|
|
|
|
*pic->xp++ = handler;
|
2014-09-18 01:12:18 -04:00
|
|
|
}
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2014-09-18 01:12:18 -04:00
|
|
|
void
|
|
|
|
pic_pop_try(pic_state *pic)
|
|
|
|
{
|
2014-09-24 06:24:02 -04:00
|
|
|
pic_value cont, escape;
|
2014-09-24 02:59:23 -04:00
|
|
|
|
|
|
|
assert(pic->xp > pic->xpbase);
|
|
|
|
|
2014-09-24 20:44:21 -04:00
|
|
|
cont = pic_attr_ref(pic, pic_obj_value(*--pic->xp), "@@escape");
|
2014-09-24 02:59:23 -04:00
|
|
|
|
2014-09-24 06:24:02 -04:00
|
|
|
assert(pic_proc_p(cont));
|
|
|
|
|
2014-09-24 20:44:21 -04:00
|
|
|
escape = pic_attr_ref(pic, cont, "@@escape");
|
2014-09-24 06:24:02 -04:00
|
|
|
|
|
|
|
assert(pic_data_p(escape));
|
|
|
|
|
|
|
|
((struct pic_escape *)pic_data_ptr(escape)->data)->valid = false;
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
2014-09-16 11:43:03 -04:00
|
|
|
struct pic_error *
|
|
|
|
pic_make_error(pic_state *pic, pic_sym type, const char *msg, pic_value irrs)
|
2014-08-25 00:38:09 -04:00
|
|
|
{
|
|
|
|
struct pic_error *e;
|
|
|
|
pic_str *stack;
|
|
|
|
|
|
|
|
stack = pic_get_backtrace(pic);
|
|
|
|
|
|
|
|
e = (struct pic_error *)pic_obj_alloc(pic, sizeof(struct pic_error), PIC_TT_ERROR);
|
|
|
|
e->type = type;
|
2014-09-16 11:43:03 -04:00
|
|
|
e->msg = pic_make_str_cstr(pic, msg);
|
2014-08-25 00:38:09 -04:00
|
|
|
e->irrs = irrs;
|
|
|
|
e->stack = stack;
|
|
|
|
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2014-09-17 08:58:51 -04:00
|
|
|
pic_value
|
|
|
|
pic_raise_continuable(pic_state *pic, pic_value err)
|
|
|
|
{
|
2014-09-18 01:12:18 -04:00
|
|
|
struct pic_proc *handler;
|
2014-09-17 08:58:51 -04:00
|
|
|
pic_value v;
|
|
|
|
|
2014-09-18 01:50:01 -04:00
|
|
|
if (pic->xp == pic->xpbase) {
|
2014-09-18 01:12:18 -04:00
|
|
|
pic_panic(pic, "no exception handler registered");
|
2014-09-17 08:58:51 -04:00
|
|
|
}
|
|
|
|
|
2014-09-18 01:50:01 -04:00
|
|
|
handler = *--pic->xp;
|
2014-09-17 09:21:31 -04:00
|
|
|
|
2014-09-17 09:34:51 -04:00
|
|
|
pic_gc_protect(pic, pic_obj_value(handler));
|
2014-09-17 09:21:31 -04:00
|
|
|
|
2014-09-18 01:50:01 -04:00
|
|
|
v = pic_apply1(pic, handler, err);
|
2014-09-17 09:21:31 -04:00
|
|
|
|
2014-09-18 01:50:01 -04:00
|
|
|
*pic->xp++ = 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
|
|
|
|
2014-09-17 09:34:51 -04:00
|
|
|
pic_pop_try(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
|
2014-09-16 11:28:55 -04:00
|
|
|
pic_throw(pic_state *pic, pic_sym type, const char *msg, pic_value irrs)
|
2014-08-25 00:38:09 -04:00
|
|
|
{
|
|
|
|
struct pic_error *e;
|
|
|
|
|
2014-09-16 11:43:03 -04:00
|
|
|
e = pic_make_error(pic, type, msg, irrs);
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2014-09-16 11:28:55 -04:00
|
|
|
pic_raise(pic, pic_obj_value(e));
|
|
|
|
}
|
|
|
|
|
2015-01-07 16:11:48 -05:00
|
|
|
void
|
2014-09-16 11:28:55 -04:00
|
|
|
pic_error(pic_state *pic, const char *msg, pic_value irrs)
|
|
|
|
{
|
|
|
|
pic_throw(pic, pic_intern_cstr(pic, ""), msg, irrs);
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_error_with_exception_handler(pic_state *pic)
|
|
|
|
{
|
|
|
|
struct pic_proc *handler, *thunk;
|
2014-09-16 11:28:55 -04:00
|
|
|
pic_value val;
|
2014-09-26 10:24:26 -04:00
|
|
|
size_t xp_len;
|
|
|
|
ptrdiff_t xp_offset;
|
2014-08-25 00:38:09 -04:00
|
|
|
|
|
|
|
pic_get_args(pic, "ll", &handler, &thunk);
|
|
|
|
|
2014-09-18 01:50:01 -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-18 01:50:01 -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-18 01:12:18 -04:00
|
|
|
}
|
|
|
|
|
2014-09-18 01:50:01 -04:00
|
|
|
*pic->xp++ = handler;
|
2014-09-17 09:13:00 -04:00
|
|
|
|
2014-09-17 09:21:31 -04:00
|
|
|
val = pic_apply0(pic, thunk);
|
2014-09-17 09:13:00 -04:00
|
|
|
|
2014-09-18 01:50:01 -04:00
|
|
|
--pic->xp;
|
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;
|
2014-09-27 07:17:02 -04:00
|
|
|
size_t argc;
|
2014-08-25 00:38:09 -04:00
|
|
|
pic_value *argv;
|
|
|
|
|
|
|
|
pic_get_args(pic, "z*", &str, &argc, &argv);
|
|
|
|
|
2014-09-16 11:28:55 -04:00
|
|
|
pic_error(pic, str, pic_list_by_array(pic, argc, argv));
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
2014-09-16 11:43:12 -04:00
|
|
|
static pic_value
|
|
|
|
pic_error_make_error_object(pic_state *pic)
|
|
|
|
{
|
|
|
|
struct pic_error *e;
|
|
|
|
pic_sym type;
|
|
|
|
pic_str *msg;
|
2014-09-27 07:17:02 -04:00
|
|
|
size_t argc;
|
2014-09-16 11:43:12 -04:00
|
|
|
pic_value *argv;
|
|
|
|
|
|
|
|
pic_get_args(pic, "ms*", &type, &msg, &argc, &argv);
|
|
|
|
|
|
|
|
e = pic_make_error(pic, type, pic_str_cstr(msg), pic_list_by_array(pic, argc, argv));
|
|
|
|
|
|
|
|
return pic_obj_value(e);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
return pic_bool_value(pic_error_p(v));
|
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_error_error_object_message(pic_state *pic)
|
|
|
|
{
|
|
|
|
struct pic_error *e;
|
|
|
|
|
|
|
|
pic_get_args(pic, "e", &e);
|
|
|
|
|
|
|
|
return pic_obj_value(e->msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_error_error_object_irritants(pic_state *pic)
|
|
|
|
{
|
|
|
|
struct pic_error *e;
|
|
|
|
|
|
|
|
pic_get_args(pic, "e", &e);
|
|
|
|
|
|
|
|
return e->irrs;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
struct pic_error *e;
|
|
|
|
|
2014-09-16 11:49:37 -04:00
|
|
|
pic_get_args(pic, "e", &e);
|
2014-09-16 11:28:55 -04:00
|
|
|
|
2014-09-16 11:49:37 -04:00
|
|
|
return pic_sym_value(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);
|
2014-09-16 11:43:12 -04:00
|
|
|
pic_defun(pic, "make-error-object", pic_error_make_error_object);
|
2014-08-25 00:38:09 -04:00
|
|
|
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
|
|
|
}
|