2014-01-17 06:58:31 -05:00
|
|
|
/**
|
|
|
|
* See Copyright Notice in picrin.h
|
|
|
|
*/
|
|
|
|
|
2013-10-20 04:26:18 -04:00
|
|
|
#include <stdlib.h>
|
2013-11-17 04:16:03 -05:00
|
|
|
#include <string.h>
|
2014-02-10 23:47:11 -05:00
|
|
|
#include <stdarg.h>
|
2013-10-20 04:26:18 -04:00
|
|
|
|
|
|
|
#include "picrin.h"
|
2013-11-17 04:16:03 -05:00
|
|
|
#include "picrin/pair.h"
|
2014-02-22 01:20:53 -05:00
|
|
|
#include "picrin/string.h"
|
2013-11-17 03:25:26 -05:00
|
|
|
#include "picrin/error.h"
|
2013-10-20 04:26:18 -04:00
|
|
|
|
2014-03-24 00:24:37 -04:00
|
|
|
void
|
|
|
|
pic_push_try(pic_state *pic)
|
|
|
|
{
|
|
|
|
struct pic_jmpbuf *try_jmp;
|
|
|
|
|
|
|
|
try_jmp = pic_alloc(pic, sizeof(struct pic_jmpbuf));
|
|
|
|
|
|
|
|
try_jmp->prev_jmp = pic->jmp;
|
|
|
|
pic->jmp = &try_jmp->here;
|
|
|
|
|
|
|
|
try_jmp->prev = pic->try_jmps;
|
|
|
|
pic->try_jmps = try_jmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
pic_pop_try(pic_state *pic)
|
|
|
|
{
|
|
|
|
struct pic_jmpbuf *prev;
|
|
|
|
|
|
|
|
assert(pic->jmp == &pic->try_jmps->here);
|
|
|
|
|
|
|
|
pic->jmp = pic->try_jmps->prev_jmp;
|
|
|
|
|
|
|
|
prev = pic->try_jmps->prev;
|
|
|
|
pic_free(pic, pic->try_jmps);
|
|
|
|
pic->try_jmps = prev;
|
|
|
|
}
|
|
|
|
|
2014-02-10 23:21:00 -05:00
|
|
|
const char *
|
|
|
|
pic_errmsg(pic_state *pic)
|
|
|
|
{
|
|
|
|
assert(pic->err != NULL);
|
|
|
|
|
2014-02-27 07:54:37 -05:00
|
|
|
return pic_str_cstr(pic->err->msg);
|
2014-02-10 23:21:00 -05:00
|
|
|
}
|
|
|
|
|
2014-03-19 04:37:06 -04:00
|
|
|
noreturn static void
|
2014-02-18 12:11:29 -05:00
|
|
|
raise(pic_state *pic, struct pic_error *e)
|
|
|
|
{
|
|
|
|
pic->err = e;
|
|
|
|
if (! pic->jmp) {
|
|
|
|
puts(pic_errmsg(pic));
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
longjmp(*pic->jmp, 1);
|
|
|
|
}
|
|
|
|
|
2014-03-19 04:37:06 -04:00
|
|
|
noreturn static void
|
2014-02-22 01:20:53 -05:00
|
|
|
error(pic_state *pic, pic_str *msg, pic_value irrs)
|
2013-10-20 05:17:12 -04:00
|
|
|
{
|
2014-02-10 23:21:00 -05:00
|
|
|
struct pic_error *e;
|
|
|
|
|
|
|
|
e = (struct pic_error *)pic_obj_alloc(pic, sizeof(struct pic_error), PIC_TT_ERROR);
|
|
|
|
e->type = PIC_ERROR_OTHER;
|
2014-02-10 23:47:11 -05:00
|
|
|
e->msg = msg;
|
|
|
|
e->irrs = irrs;
|
2014-02-10 23:21:00 -05:00
|
|
|
|
2014-02-18 12:11:29 -05:00
|
|
|
raise(pic, e);
|
2013-10-20 05:17:12 -04:00
|
|
|
}
|
|
|
|
|
2013-11-17 03:23:13 -05:00
|
|
|
void
|
2014-02-10 23:47:11 -05:00
|
|
|
pic_error(pic_state *pic, const char *msg)
|
|
|
|
{
|
|
|
|
pic_errorf(pic, msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
pic_errorf(pic_state *pic, const char *fmt, ...)
|
2013-11-17 03:23:13 -05:00
|
|
|
{
|
2014-02-10 23:47:11 -05:00
|
|
|
va_list ap;
|
|
|
|
pic_value err_line;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
err_line = pic_vformat(pic, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
error(pic, pic_str_ptr(pic_car(pic, err_line)), pic_cdr(pic, err_line));
|
2013-11-17 03:23:13 -05:00
|
|
|
}
|
|
|
|
|
2013-10-20 04:26:18 -04:00
|
|
|
void
|
2013-10-20 04:41:48 -04:00
|
|
|
pic_abort(pic_state *pic, const char *msg)
|
2013-10-20 04:26:18 -04:00
|
|
|
{
|
2014-01-30 04:15:59 -05:00
|
|
|
UNUSED(pic);
|
|
|
|
|
2013-11-17 04:16:03 -05:00
|
|
|
fprintf(stderr, "abort: %s\n", msg);
|
|
|
|
fflush(stderr);
|
2013-10-20 04:26:18 -04:00
|
|
|
abort();
|
|
|
|
}
|
2013-10-24 09:29:40 -04:00
|
|
|
|
|
|
|
void
|
|
|
|
pic_warn(pic_state *pic, const char *msg)
|
|
|
|
{
|
2014-01-30 04:15:59 -05:00
|
|
|
UNUSED(pic);
|
|
|
|
|
2013-10-24 09:29:40 -04:00
|
|
|
fprintf(stderr, "warn: %s\n", msg);
|
|
|
|
}
|
2013-11-17 03:25:26 -05:00
|
|
|
|
|
|
|
void
|
2014-02-18 12:22:34 -05:00
|
|
|
pic_raise(pic_state *pic, struct pic_error *e)
|
2013-11-17 03:25:26 -05:00
|
|
|
{
|
|
|
|
pic_value a;
|
|
|
|
struct pic_proc *handler;
|
|
|
|
|
|
|
|
if (pic->ridx == 0) {
|
2014-02-18 12:22:34 -05:00
|
|
|
raise(pic, e);
|
2013-11-17 03:25:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
handler = pic->rescue[--pic->ridx];
|
|
|
|
pic_gc_protect(pic, pic_obj_value(handler));
|
|
|
|
|
2014-02-18 12:22:34 -05:00
|
|
|
a = pic_apply_argv(pic, handler, 1, pic_obj_value(e));
|
2013-11-17 03:25:26 -05:00
|
|
|
/* when the handler returns */
|
|
|
|
pic_errorf(pic, "handler returned", 2, pic_obj_value(handler), a);
|
|
|
|
}
|
|
|
|
|
2014-02-18 02:58:51 -05:00
|
|
|
pic_value
|
|
|
|
pic_raise_continuable(pic_state *pic, pic_value obj)
|
|
|
|
{
|
|
|
|
struct pic_proc *handler;
|
|
|
|
|
|
|
|
if (pic->ridx == 0) {
|
|
|
|
pic_abort(pic, "logic flaw: no exception handler remains");
|
|
|
|
}
|
|
|
|
|
|
|
|
handler = pic->rescue[--pic->ridx];
|
|
|
|
obj = pic_apply_argv(pic, handler, 1, obj);
|
|
|
|
pic->rescue[pic->ridx++] = handler;
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2013-11-17 03:25:26 -05:00
|
|
|
static pic_value
|
|
|
|
pic_error_with_exception_handler(pic_state *pic)
|
|
|
|
{
|
|
|
|
struct pic_proc *handler, *thunk;
|
2014-01-08 06:53:28 -05:00
|
|
|
pic_value v;
|
2013-11-17 03:25:26 -05:00
|
|
|
|
2014-01-08 06:53:28 -05:00
|
|
|
pic_get_args(pic, "ll", &handler, &thunk);
|
2013-11-17 03:25:26 -05:00
|
|
|
|
|
|
|
if (pic->ridx >= pic->rlen) {
|
2013-11-21 09:28:06 -05:00
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
puts("rescue realloced");
|
|
|
|
#endif
|
|
|
|
|
2013-11-17 03:25:26 -05:00
|
|
|
pic->rlen *= 2;
|
2014-01-18 09:01:00 -05:00
|
|
|
pic->rescue = (struct pic_proc **)pic_realloc(pic, pic->rescue, sizeof(struct pic_proc *) * pic->rlen);
|
2013-11-17 03:25:26 -05:00
|
|
|
}
|
|
|
|
pic->rescue[pic->ridx++] = handler;
|
|
|
|
|
|
|
|
v = pic_apply_argv(pic, thunk, 0);
|
|
|
|
pic->ridx--;
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2014-03-19 04:37:06 -04:00
|
|
|
noreturn static pic_value
|
2013-11-17 03:25:26 -05:00
|
|
|
pic_error_raise(pic_state *pic)
|
|
|
|
{
|
|
|
|
pic_value v;
|
2014-02-18 12:22:34 -05:00
|
|
|
struct pic_error *e;
|
2013-11-17 03:25:26 -05:00
|
|
|
|
|
|
|
pic_get_args(pic, "o", &v);
|
|
|
|
|
2014-02-18 12:22:34 -05:00
|
|
|
e = (struct pic_error *)pic_obj_alloc(pic, sizeof(struct pic_error), PIC_TT_ERROR);
|
|
|
|
e->type = PIC_ERROR_RAISED;
|
|
|
|
e->msg = pic_str_new_cstr(pic, "raised");
|
2014-03-01 06:46:08 -05:00
|
|
|
e->irrs = pic_list1(pic, v);
|
2014-02-18 12:22:34 -05:00
|
|
|
|
|
|
|
pic_raise(pic, e);
|
2013-11-17 03:25:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_error_raise_continuable(pic_state *pic)
|
|
|
|
{
|
2014-02-18 02:58:51 -05:00
|
|
|
pic_value obj;
|
2013-11-17 03:25:26 -05:00
|
|
|
|
2014-02-18 02:58:51 -05:00
|
|
|
pic_get_args(pic, "o", &obj);
|
2013-11-17 03:25:26 -05:00
|
|
|
|
2014-02-18 02:58:51 -05:00
|
|
|
return pic_raise_continuable(pic, obj);
|
2013-11-17 03:25:26 -05:00
|
|
|
}
|
|
|
|
|
2014-03-19 04:37:06 -04:00
|
|
|
noreturn static pic_value
|
2013-11-17 04:16:03 -05:00
|
|
|
pic_error_error(pic_state *pic)
|
|
|
|
{
|
2014-02-22 21:52:15 -05:00
|
|
|
pic_str *str;
|
|
|
|
size_t argc;
|
2013-11-17 04:16:03 -05:00
|
|
|
pic_value *argv;
|
|
|
|
struct pic_error *e;
|
|
|
|
|
2014-02-22 21:52:15 -05:00
|
|
|
pic_get_args(pic, "s*", &str, &argc, &argv);
|
2013-11-17 04:16:03 -05:00
|
|
|
|
|
|
|
e = (struct pic_error *)pic_obj_alloc(pic, sizeof(struct pic_error), PIC_TT_ERROR);
|
|
|
|
e->type = PIC_ERROR_OTHER;
|
2014-02-22 21:52:15 -05:00
|
|
|
e->msg = str;
|
2014-02-01 22:25:34 -05:00
|
|
|
e->irrs = pic_list_by_array(pic, argc, argv);
|
2013-11-17 04:16:03 -05:00
|
|
|
|
2014-02-18 12:22:34 -05:00
|
|
|
pic_raise(pic, e);
|
2013-11-17 04:16:03 -05:00
|
|
|
}
|
2014-01-30 00:08:36 -05:00
|
|
|
|
2013-11-17 03:25:26 -05: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);
|
|
|
|
|
2014-02-10 23:21:00 -05:00
|
|
|
return pic_obj_value(e->msg);
|
2013-11-17 03:25:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
pic_error_read_error_p(pic_state *pic)
|
|
|
|
{
|
|
|
|
pic_value v;
|
|
|
|
struct pic_error *e;
|
|
|
|
|
|
|
|
pic_get_args(pic, "o", &v);
|
|
|
|
|
|
|
|
if (! pic_error_p(v)) {
|
|
|
|
return pic_false_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
e = pic_error_ptr(v);
|
|
|
|
return pic_bool_value(e->type == PIC_ERROR_READ);
|
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_error_file_error_p(pic_state *pic)
|
|
|
|
{
|
|
|
|
pic_value v;
|
|
|
|
struct pic_error *e;
|
|
|
|
|
|
|
|
pic_get_args(pic, "o", &v);
|
|
|
|
|
|
|
|
if (! pic_error_p(v)) {
|
|
|
|
return pic_false_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
e = pic_error_ptr(v);
|
|
|
|
return pic_bool_value(e->type == PIC_ERROR_FILE);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2013-11-17 04:16:03 -05:00
|
|
|
pic_defun(pic, "error", pic_error_error);
|
2013-11-17 03:25:26 -05: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);
|
|
|
|
pic_defun(pic, "read-error?", pic_error_read_error_p);
|
|
|
|
pic_defun(pic, "file-error?", pic_error_file_error_p);
|
|
|
|
}
|