e$B1sF#$G$9!#e(B
2010e$BG/e(B5e$B7ne(B20e$BF|e(B4:47 Nobuhiro IMAI [email protected]:
e$B$,!"$3$l$@$H!"e(Bcaller(0) e$B$GJV$kG[Ns$N%5%$%:$r1[$($k?t$r0z?t$H$7$FEO$7$Fe(B
e$B$b6uG[Ns$,JV$k$h$&$K$J$C$F$7$^$C$F$$$k$h$&$G$9!#e(B
e$B$0$“$”!“LdBj$r8m2r$7$F$$$^$7$?!#e(Bnil
e$B$,5”$k$3$H$,$"$k$N$O85$+$i$J$s$G$9$M!#e(B
e$B0J2<$N%Q%C%A$G:FD)@o$7$^$9!#$=$l$+$iD:$$$?%Q%C%A$O<h$j9~$s$G$*$-$^$9!#e(B
diff --git a/vm.c b/vm.c
index 084c8c8…ac1d8ed 100644
— a/vm.c
+++ b/vm.c
@@ -706,19 +706,20 @@ rb_vm_get_sourceline(const rb_control_frame_t
*cfp)
}
static int
-vm_backtrace_each(rb_thread_t *th, int lev, rb_backtrace_iter_func
*iter, void *arg)
+vm_backtrace_each(rb_thread_t *th, int lev, void (*init)(void *),
rb_backtrace_iter_func *iter, void *arg)
{
const rb_control_frame_t *limit_cfp = th->cfp;
const rb_control_frame_t *cfp = (void *)(th->stack +
th->stack_size);
- if (++limit_cfp >= cfp) {
- if (++limit_cfp > cfp) {
return FALSE;
}
}
- if (init) (*init)(arg);
limit_cfp = RUBY_VM_NEXT_CONTROL_FRAME(limit_cfp);
if (th->vm->progname) file = th->vm->progname;
while (cfp > limit_cfp) {
@@ -747,15 +748,19 @@ vm_backtrace_each(rb_thread_t *th, int lev,
rb_backtrace_iter_func *iter, void *
return TRUE;
}
+static void
+vm_backtrace_alloc(void *arg)
+{
- VALUE *aryp = arg;
- *aryp = rb_ary_new();
+}
-
static int
vm_backtrace_push(void *arg, VALUE file, int line_no, VALUE name)
{
VALUE *aryp = arg;
VALUE bt;
- if (!*aryp) {
- *aryp = rb_ary_new();
- }
bt = rb_enc_sprintf(rb_enc_compatible(file, name), “%s:%d:in `%s’”,
RSTRING_PTR(file), line_no, RSTRING_PTR(name));
rb_ary_push(*aryp, bt);
@@ -770,7 +775,7 @@ vm_backtrace(rb_thread_t *th, int lev)
if (lev < 0) {
ary = rb_ary_new();
}
- vm_backtrace_each(th, lev, vm_backtrace_push, &ary);
- vm_backtrace_each(th, lev, vm_backtrace_alloc, vm_backtrace_push,
&ary);
if (!ary) return Qnil;
return rb_ary_reverse(ary);
}
diff --git a/vm_eval.c b/vm_eval.c
index d1e6352…b854efc 100644
— a/vm_eval.c
+++ b/vm_eval.c
@@ -16,7 +16,7 @@ static inline VALUE rb_vm_set_finish_env(rb_thread_t *
th);
static inline VALUE vm_yield_with_cref(rb_thread_t *th, int argc,
const VALUE *argv, const NODE *cref);
static inline VALUE vm_yield(rb_thread_t *th, int argc, const VALUE
*argv);
static inline VALUE vm_backtrace(rb_thread_t *th, int lev);
-static int vm_backtrace_each(rb_thread_t *th, int lev,
rb_backtrace_iter_func *iter, void *arg);
+static int vm_backtrace_each(rb_thread_t *th, int lev, void
(*init)(void *), rb_backtrace_iter_func *iter, void *arg);
static NODE *vm_cref_push(rb_thread_t *th, VALUE klass, int noex,
rb_block_t *blockptr);
static VALUE vm_exec(rb_thread_t *th);
static void vm_set_eval_stack(rb_thread_t * th, VALUE iseqval, const
NODE *cref);
@@ -1574,9 +1574,7 @@ rb_f_caller(int argc, VALUE *argv)
if (lev < 0)
rb_raise(rb_eArgError, “negative level (%d)”, lev);
- ary = vm_backtrace(GET_THREAD(), lev);
- if (NIL_P(ary)) ary = rb_ary_new();
- return ary;
- return vm_backtrace(GET_THREAD(), lev);
}
static int
@@ -1598,7 +1596,7 @@ print_backtrace(void *arg, VALUE file, int line,
VALUE method)
void
rb_backtrace(void)
{
- vm_backtrace_each(GET_THREAD(), -1, print_backtrace, stderr);
- vm_backtrace_each(GET_THREAD(), -1, NULL, print_backtrace, stderr);
}
VALUE
@@ -1629,7 +1627,7 @@ rb_thread_backtrace(VALUE thval)
int
rb_backtrace_each(rb_backtrace_iter_func *iter, void *arg)
{
- return vm_backtrace_each(GET_THREAD(), -1, iter, arg);
- return vm_backtrace_each(GET_THREAD(), -1, NULL, iter, arg);
}
/*