From 1402a973544011d70dcf48bf8b61376dc3719469 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 4 Jul 2014 13:32:45 +0900 Subject: [PATCH] change pic_throw API --- include/picrin/error.h | 3 ++- src/error.c | 28 +++++++++++++++++++++------- src/macro.c | 2 +- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/include/picrin/error.h b/include/picrin/error.h index 024d5d29..75361c1a 100644 --- a/include/picrin/error.h +++ b/include/picrin/error.h @@ -32,7 +32,8 @@ struct pic_jmpbuf { void pic_push_try(pic_state *); void pic_pop_try(pic_state *); -noreturn void pic_throw(pic_state *, struct pic_error *); +noreturn void pic_throw(pic_state *, short, const char *, pic_value); +noreturn void pic_throw_error(pic_state *, struct pic_error *); struct pic_error { PIC_OBJECT_HEADER diff --git a/src/error.c b/src/error.c index 773b5d2c..21f6d487 100644 --- a/src/error.c +++ b/src/error.c @@ -87,7 +87,7 @@ error_new(pic_state *pic, short type, pic_str *msg, pic_value irrs) } noreturn void -pic_throw(pic_state *pic, struct pic_error *e) +pic_throw_error(pic_state *pic, struct pic_error *e) { pic->err = e; if (! pic->jmp) { @@ -97,6 +97,16 @@ pic_throw(pic_state *pic, struct pic_error *e) longjmp(*pic->jmp, 1); } +noreturn void +pic_throw(pic_state *pic, short type, const char *msg, pic_value irrs) +{ + struct pic_error *e; + + e = error_new(pic, type, pic_str_new_cstr(pic, msg), irrs); + + pic_throw_error(pic, e); +} + const char * pic_errmsg(pic_state *pic) { @@ -109,13 +119,17 @@ void pic_errorf(pic_state *pic, const char *fmt, ...) { va_list ap; - pic_value err_line; + pic_value err_line, irrs; + const char *msg; va_start(ap, fmt); err_line = pic_vformat(pic, fmt, ap); va_end(ap); - pic_throw(pic, error_new(pic, PIC_ERROR_OTHER, pic_str_ptr(pic_car(pic, err_line)), pic_cdr(pic, err_line))); + msg = pic_str_cstr(pic_str_ptr(pic_car(pic, err_line))); + irrs = pic_cdr(pic, err_line); + + pic_throw(pic, PIC_ERROR_OTHER, msg, irrs); } static pic_value @@ -146,19 +160,19 @@ pic_error_raise(pic_state *pic) pic_get_args(pic, "o", &v); - pic_throw(pic, error_new(pic, PIC_ERROR_RAISED, pic_str_new_cstr(pic, "object is raised"), pic_list1(pic, v))); + pic_throw(pic, PIC_ERROR_RAISED, "object is raised", pic_list1(pic, v)); } noreturn static pic_value pic_error_error(pic_state *pic) { - pic_str *str; + const char *str; size_t argc; pic_value *argv; - pic_get_args(pic, "s*", &str, &argc, &argv); + pic_get_args(pic, "z*", &str, &argc, &argv); - pic_throw(pic, error_new(pic, PIC_ERROR_OTHER, str, pic_list_by_array(pic, argc, argv))); + pic_throw(pic, PIC_ERROR_OTHER, str, pic_list_by_array(pic, argc, argv)); } static pic_value diff --git a/src/macro.c b/src/macro.c index 1a0ed192..ea200e7d 100644 --- a/src/macro.c +++ b/src/macro.c @@ -246,7 +246,7 @@ macroexpand_deflibrary(pic_state *pic, pic_value expr) pic_catch { /* restores pic->lib even if an error occurs */ pic_in_library(pic, prev->name); - pic_throw(pic, pic->err); + pic_throw_error(pic, pic->err); } return pic_none_value();