add pic_throw function

This commit is contained in:
Yuichi Nishiwaki 2014-03-24 14:09:28 +09:00
parent 2246213a74
commit daa7513be5
4 changed files with 17 additions and 14 deletions

View File

@ -27,6 +27,8 @@ struct pic_jmpbuf {
void pic_push_try(pic_state *); void pic_push_try(pic_state *);
void pic_pop_try(pic_state *); void pic_pop_try(pic_state *);
noreturn void pic_throw(pic_state *, struct pic_error *);
struct pic_error { struct pic_error {
PIC_OBJECT_HEADER PIC_OBJECT_HEADER
enum pic_error_kind { enum pic_error_kind {

View File

@ -39,16 +39,8 @@ pic_pop_try(pic_state *pic)
pic->try_jmps = prev; pic->try_jmps = prev;
} }
const char * noreturn void
pic_errmsg(pic_state *pic) pic_throw(pic_state *pic, struct pic_error *e)
{
assert(pic->err != NULL);
return pic_str_cstr(pic->err->msg);
}
noreturn static void
raise(pic_state *pic, struct pic_error *e)
{ {
pic->err = e; pic->err = e;
if (! pic->jmp) { if (! pic->jmp) {
@ -58,6 +50,14 @@ raise(pic_state *pic, struct pic_error *e)
longjmp(*pic->jmp, 1); longjmp(*pic->jmp, 1);
} }
const char *
pic_errmsg(pic_state *pic)
{
assert(pic->err != NULL);
return pic_str_cstr(pic->err->msg);
}
noreturn static void noreturn static void
error(pic_state *pic, pic_str *msg, pic_value irrs) error(pic_state *pic, pic_str *msg, pic_value irrs)
{ {
@ -68,7 +68,7 @@ error(pic_state *pic, pic_str *msg, pic_value irrs)
e->msg = msg; e->msg = msg;
e->irrs = irrs; e->irrs = irrs;
raise(pic, e); pic_throw(pic, e);
} }
void void
@ -115,7 +115,7 @@ pic_raise(pic_state *pic, struct pic_error *e)
struct pic_proc *handler; struct pic_proc *handler;
if (pic->ridx == 0) { if (pic->ridx == 0) {
raise(pic, e); pic_throw(pic, e);
} }
handler = pic->rescue[--pic->ridx]; handler = pic->rescue[--pic->ridx];

View File

@ -243,7 +243,7 @@ macroexpand(pic_state *pic, pic_value expr, struct pic_senv *senv)
pic_catch { pic_catch {
/* restores pic->lib even if an error occurs */ /* restores pic->lib even if an error occurs */
pic_in_library(pic, prev->name); pic_in_library(pic, prev->name);
longjmp(*pic->jmp, 1); pic_throw(pic, pic->err);
} }
return pic_none_value(); return pic_none_value();

View File

@ -18,6 +18,7 @@
#include "picrin/var.h" #include "picrin/var.h"
#include "picrin/lib.h" #include "picrin/lib.h"
#include "picrin/macro.h" #include "picrin/macro.h"
#include "picrin/error.h"
#define GET_OPERAND(pic,n) ((pic)->ci->fp[(n)]) #define GET_OPERAND(pic,n) ((pic)->ci->fp[(n)])
@ -935,7 +936,7 @@ pic_apply(pic_state *pic, struct pic_proc *proc, pic_value argv)
pic->jmp = prev_jmp; pic->jmp = prev_jmp;
if (pic->err) { if (pic->err) {
longjmp(*pic->jmp, 1); pic_throw(pic, pic->err);
} }
#if VM_DEBUG #if VM_DEBUG