Permalink
Browse files

save lastpc on exception and use it in mruby stack trace

  • Loading branch information...
1 parent f63d5c2 commit fd12f87712ee22b0a727f33f0c46c22ae968ec83 @matz matz committed Dec 12, 2012
Showing with 31 additions and 10 deletions.
  1. +5 −2 src/kernel.c
  2. +1 −0 src/vm.c
  3. +25 −8 tools/mruby/mruby.c
View
@@ -920,8 +920,9 @@ mrb_obj_public_methods(mrb_state *mrb, mrb_value self)
mrb_value
mrb_f_raise(mrb_state *mrb, mrb_value self)
{
- mrb_value a[2];
+ mrb_value a[2], exc;
int argc;
+
argc = mrb_get_args(mrb, "|oo", &a[0], &a[1]);
switch (argc) {
@@ -936,7 +937,9 @@ mrb_f_raise(mrb_state *mrb, mrb_value self)
}
/* fall through */
default:
- mrb_exc_raise(mrb, mrb_make_exception(mrb, argc, a));
+ exc = mrb_make_exception(mrb, argc, a);
+ mrb_obj_iv_set(mrb, mrb_obj_ptr(exc), mrb_intern(mrb, "lastpc"), mrb_voidp_value(mrb->ci->pc));
+ mrb_exc_raise(mrb, exc);
}
return mrb_nil_value(); /* not reached */
}
View
@@ -1151,6 +1151,7 @@ mrb_run(mrb_state *mrb, struct RProc *proc, mrb_value self)
int eidx;
L_RAISE:
+ mrb_obj_iv_ifnone(mrb, mrb->exc, mrb_intern(mrb, "lastpc"), mrb_voidp_value(pc));
ci = mrb->ci;
eidx = mrb->ci->eidx;
if (ci == mrb->cibase) goto L_STOP;
View
@@ -188,8 +188,15 @@ showcallinfo(mrb_state *mrb)
mrb_irep *irep = ci->proc->body.irep;
if (irep->filename != NULL)
filename = irep->filename;
- if (irep->lines != NULL && i+1 <= ciidx) {
- mrb_code *pc = mrb->cibase[i+1].pc;
+ if (irep->lines != NULL) {
+ mrb_code *pc;
+
+ if (i+1 <= ciidx) {
+ pc = mrb->cibase[i+1].pc;
+ }
+ else {
+ pc = (mrb_code*)mrb_voidp(mrb_obj_iv_get(mrb, mrb->exc, mrb_intern(mrb, "lastpc")));
+ }
if (irep->iseq <= pc && pc < irep->iseq + irep->ilen) {
line = irep->lines[pc - irep->iseq - 1];
}
@@ -201,12 +208,22 @@ showcallinfo(mrb_state *mrb)
sep = "#";
method = mrb_sym2name(mrb, ci->mid);
- printf("\t[%d] %s:%d%s%s%s%s\n",
- i, filename, line,
- method ? ":in " : "",
- method ? mrb_class_name(mrb, ci->proc->target_class) : "",
- method ? sep : "",
- method ? method : "");
+ if (method) {
+ const char *cn = mrb_class_name(mrb, ci->proc->target_class);
+
+ if (cn) {
+ printf("\t[%d] %s:%d:in %s%s%s\n",
+ i, filename, line, cn, sep, method);
+ }
+ else {
+ printf("\t[%d] %s:%d:in %s\n",
+ i, filename, line, method);
+ }
+ }
+ else {
+ printf("\t[%d] %s:%d\n",
+ i, filename, line);
+ }
}
}

0 comments on commit fd12f87

Please sign in to comment.