diff options
Diffstat (limited to 'src/error.c')
| -rw-r--r-- | src/error.c | 88 |
1 files changed, 44 insertions, 44 deletions
diff --git a/src/error.c b/src/error.c index df4bbc866..64851bb56 100644 --- a/src/error.c +++ b/src/error.c @@ -7,15 +7,15 @@ #include <errno.h> #include <stdarg.h> #include <stdlib.h> -#include <string.h> #include "mruby.h" #include "mruby/array.h" -#include "mruby/class.h" #include "mruby/irep.h" #include "mruby/proc.h" #include "mruby/string.h" #include "mruby/variable.h" -#include "error.h" +#include "mruby/debug.h" +#include "mruby/error.h" +#include "mrb_throw.h" mrb_value mrb_exc_new(mrb_state *mrb, struct RClass *c, const char *ptr, long len) @@ -24,7 +24,7 @@ mrb_exc_new(mrb_state *mrb, struct RClass *c, const char *ptr, long len) } mrb_value -mrb_exc_new3(mrb_state *mrb, struct RClass* c, mrb_value str) +mrb_exc_new_str(mrb_state *mrb, struct RClass* c, mrb_value str) { str = mrb_str_to_str(mrb, str); return mrb_funcall(mrb, mrb_obj_value(c), "new", 1, str); @@ -44,7 +44,7 @@ exc_initialize(mrb_state *mrb, mrb_value exc) mrb_value mesg; if (mrb_get_args(mrb, "|o", &mesg) == 1) { - mrb_iv_set(mrb, exc, mrb_intern2(mrb, "mesg", 4), mesg); + mrb_iv_set(mrb, exc, mrb_intern_lit(mrb, "mesg"), mesg); } return exc; } @@ -73,7 +73,7 @@ exc_exception(mrb_state *mrb, mrb_value self) if (argc == 0) return self; if (mrb_obj_equal(mrb, self, a)) return self; exc = mrb_obj_clone(mrb, self); - mrb_iv_set(mrb, exc, mrb_intern2(mrb, "mesg", 4), a); + mrb_iv_set(mrb, exc, mrb_intern_lit(mrb, "mesg"), a); return exc; } @@ -89,7 +89,7 @@ exc_exception(mrb_state *mrb, mrb_value self) static mrb_value exc_to_s(mrb_state *mrb, mrb_value exc) { - mrb_value mesg = mrb_attr_get(mrb, exc, mrb_intern2(mrb, "mesg", 4)); + mrb_value mesg = mrb_attr_get(mrb, exc, mrb_intern_lit(mrb, "mesg")); if (mrb_nil_p(mesg)) return mrb_str_new_cstr(mrb, mrb_obj_classname(mrb, exc)); return mesg; @@ -122,33 +122,35 @@ static mrb_value exc_inspect(mrb_state *mrb, mrb_value exc) { mrb_value str, mesg, file, line; + mrb_bool append_mesg; - mesg = mrb_attr_get(mrb, exc, mrb_intern2(mrb, "mesg", 4)); - file = mrb_attr_get(mrb, exc, mrb_intern2(mrb, "file", 4)); - line = mrb_attr_get(mrb, exc, mrb_intern2(mrb, "line", 4)); + mesg = mrb_attr_get(mrb, exc, mrb_intern_lit(mrb, "mesg")); + file = mrb_attr_get(mrb, exc, mrb_intern_lit(mrb, "file")); + line = mrb_attr_get(mrb, exc, mrb_intern_lit(mrb, "line")); + append_mesg = !mrb_nil_p(mesg) && RSTRING_LEN(mesg) > 0; if (!mrb_nil_p(file) && !mrb_nil_p(line)) { - str = file; - mrb_str_cat(mrb, str, ":", 1); + str = mrb_str_dup(mrb, file); + mrb_str_cat_lit(mrb, str, ":"); mrb_str_append(mrb, str, line); - mrb_str_cat(mrb, str, ": ", 2); - if (!mrb_nil_p(mesg) && RSTRING_LEN(mesg) > 0) { + mrb_str_cat_lit(mrb, str, ": "); + if (append_mesg) { mrb_str_append(mrb, str, mesg); - mrb_str_cat(mrb, str, " (", 2); + mrb_str_cat_lit(mrb, str, " ("); } mrb_str_cat_cstr(mrb, str, mrb_obj_classname(mrb, exc)); - if (!mrb_nil_p(mesg) && RSTRING_LEN(mesg) > 0) { - mrb_str_cat(mrb, str, ")", 1); + if (append_mesg) { + mrb_str_cat_lit(mrb, str, ")"); } } else { str = mrb_str_new_cstr(mrb, mrb_obj_classname(mrb, exc)); - if (!mrb_nil_p(mesg) && RSTRING_LEN(mesg) > 0) { - mrb_str_cat(mrb, str, ": ", 2); + if (append_mesg) { + mrb_str_cat_lit(mrb, str, ": "); mrb_str_append(mrb, str, mesg); } else { - mrb_str_cat(mrb, str, ": ", 2); + mrb_str_cat_lit(mrb, str, ": "); mrb_str_cat_cstr(mrb, str, mrb_obj_classname(mrb, exc)); } } @@ -162,7 +164,7 @@ exc_equal(mrb_state *mrb, mrb_value exc) mrb_value obj; mrb_value mesg; mrb_bool equal_p; - mrb_sym id_mesg = mrb_intern2(mrb, "mesg", 4); + mrb_sym id_mesg = mrb_intern_lit(mrb, "mesg"); mrb_get_args(mrb, "o", &obj); if (mrb_obj_equal(mrb, exc, obj)) { @@ -170,7 +172,7 @@ exc_equal(mrb_state *mrb, mrb_value exc) } else { if (mrb_obj_class(mrb, exc) != mrb_obj_class(mrb, obj)) { - if (mrb_respond_to(mrb, obj, mrb_intern2(mrb, "message", 7))) { + if (mrb_respond_to(mrb, obj, mrb_intern_lit(mrb, "message"))) { mesg = mrb_funcall(mrb, obj, "message", 0); } else @@ -192,15 +194,19 @@ exc_debug_info(mrb_state *mrb, struct RObject *exc) mrb_callinfo *ci = mrb->c->ci; mrb_code *pc = ci->pc; - mrb_obj_iv_set(mrb, exc, mrb_intern2(mrb, "ciidx", 5), mrb_fixnum_value(ci - mrb->c->cibase)); - ci--; + mrb_obj_iv_set(mrb, exc, mrb_intern_lit(mrb, "ciidx"), mrb_fixnum_value(ci - mrb->c->cibase)); while (ci >= mrb->c->cibase) { - if (ci->proc && !MRB_PROC_CFUNC_P(ci->proc)) { + mrb_code *err = ci->err; + + if (!err && pc) err = pc - 1; + if (err && ci->proc && !MRB_PROC_CFUNC_P(ci->proc)) { mrb_irep *irep = ci->proc->body.irep; - if (irep->filename && irep->lines && irep->iseq <= pc && pc < irep->iseq + irep->ilen) { - mrb_obj_iv_set(mrb, exc, mrb_intern2(mrb, "file", 4), mrb_str_new_cstr(mrb, irep->filename)); - mrb_obj_iv_set(mrb, exc, mrb_intern2(mrb, "line", 4), mrb_fixnum_value(irep->lines[pc - irep->iseq - 1])); + int32_t const line = mrb_debug_get_line(irep, err - irep->iseq); + char const* file = mrb_debug_get_filename(irep, err - irep->iseq); + if (line != -1 && file) { + mrb_obj_iv_set(mrb, exc, mrb_intern_lit(mrb, "file"), mrb_str_new_cstr(mrb, file)); + mrb_obj_iv_set(mrb, exc, mrb_intern_lit(mrb, "line"), mrb_fixnum_value(line)); return; } } @@ -218,7 +224,7 @@ mrb_exc_raise(mrb_state *mrb, mrb_value exc) mrb_p(mrb, exc); abort(); } - mrb_longjmp(mrb); + MRB_THROW(mrb->jmp); } void @@ -226,7 +232,7 @@ mrb_raise(mrb_state *mrb, struct RClass *c, const char *msg) { mrb_value mesg; mesg = mrb_str_new_cstr(mrb, msg); - mrb_exc_raise(mrb, mrb_exc_new3(mrb, c, mesg)); + mrb_exc_raise(mrb, mrb_exc_new_str(mrb, c, mesg)); } mrb_value @@ -292,7 +298,7 @@ mrb_raisef(mrb_state *mrb, struct RClass *c, const char *fmt, ...) va_start(args, fmt); mesg = mrb_vformat(mrb, fmt, args); va_end(args); - mrb_exc_raise(mrb, mrb_exc_new3(mrb, c, mesg)); + mrb_exc_raise(mrb, mrb_exc_new_str(mrb, c, mesg)); } void @@ -307,7 +313,7 @@ mrb_name_error(mrb_state *mrb, mrb_sym id, const char *fmt, ...) va_end(args); argv[1] = mrb_symbol_value(id); - exc = mrb_class_new_instance(mrb, 2, argv, E_NAME_ERROR); + exc = mrb_obj_new(mrb, E_NAME_ERROR, 2, argv); mrb_exc_raise(mrb, exc); } @@ -342,21 +348,14 @@ mrb_bug(mrb_state *mrb, const char *fmt, ...) exit(EXIT_FAILURE); } -int -sysexit_status(mrb_state *mrb, mrb_value err) -{ - mrb_value st = mrb_iv_get(mrb, err, mrb_intern2(mrb, "status", 6)); - return mrb_fixnum(st); -} - static void set_backtrace(mrb_state *mrb, mrb_value info, mrb_value bt) { mrb_funcall(mrb, info, "set_backtrace", 1, bt); } -mrb_value -make_exception(mrb_state *mrb, int argc, mrb_value *argv, int isstr) +static mrb_value +make_exception(mrb_state *mrb, int argc, const mrb_value *argv, mrb_bool isstr) { mrb_value mesg; int n; @@ -371,7 +370,7 @@ make_exception(mrb_state *mrb, int argc, mrb_value *argv, int isstr) if (isstr) { mesg = mrb_check_string_type(mrb, argv[0]); if (!mrb_nil_p(mesg)) { - mesg = mrb_exc_new3(mrb, E_RUNTIME_ERROR, mesg); + mesg = mrb_exc_new_str(mrb, E_RUNTIME_ERROR, mesg); break; } } @@ -383,7 +382,7 @@ make_exception(mrb_state *mrb, int argc, mrb_value *argv, int isstr) n = 1; exception_call: { - mrb_sym exc = mrb_intern2(mrb, "exception", 9); + mrb_sym exc = mrb_intern_lit(mrb, "exception"); if (mrb_respond_to(mrb, argv[0], exc)) { mesg = mrb_funcall_argv(mrb, argv[0], exc, n, argv+1); } @@ -409,7 +408,7 @@ exception_call: } mrb_value -mrb_make_exception(mrb_state *mrb, int argc, mrb_value *argv) +mrb_make_exception(mrb_state *mrb, int argc, const mrb_value *argv) { return make_exception(mrb, argc, argv, TRUE); } @@ -448,6 +447,7 @@ mrb_init_exception(mrb_state *mrb) mrb_define_method(mrb, e, "to_s", exc_to_s, MRB_ARGS_NONE()); mrb_define_method(mrb, e, "message", exc_message, MRB_ARGS_NONE()); mrb_define_method(mrb, e, "inspect", exc_inspect, MRB_ARGS_NONE()); + mrb_define_method(mrb, e, "backtrace", mrb_exc_backtrace, MRB_ARGS_NONE()); mrb->eStandardError_class = mrb_define_class(mrb, "StandardError", mrb->eException_class); /* 15.2.23 */ mrb_define_class(mrb, "RuntimeError", mrb->eStandardError_class); /* 15.2.28 */ |
