From f8270ee59bfbeb46a267312e49a4d39a236f0e7e Mon Sep 17 00:00:00 2001 From: take_cheeze Date: Thu, 6 Mar 2014 03:40:41 +0900 Subject: fix and add test for Proc#source_location --- mrbgems/mruby-proc-ext/src/proc.c | 12 +++++++----- mrbgems/mruby-proc-ext/test/proc.rb | 7 +++++++ 2 files changed, 14 insertions(+), 5 deletions(-) (limited to 'mrbgems/mruby-proc-ext') diff --git a/mrbgems/mruby-proc-ext/src/proc.c b/mrbgems/mruby-proc-ext/src/proc.c index 3bb0d3570..0bf1204f5 100644 --- a/mrbgems/mruby-proc-ext/src/proc.c +++ b/mrbgems/mruby-proc-ext/src/proc.c @@ -2,6 +2,7 @@ #include "mruby/proc.h" #include "mruby/array.h" #include "mruby/string.h" +#include "mruby/debug.h" static mrb_value mrb_proc_lambda(mrb_state *mrb, mrb_value self) @@ -20,13 +21,14 @@ mrb_proc_source_location(mrb_state *mrb, mrb_value self) } else { mrb_irep *irep = p->body.irep; - mrb_value filename = mrb_nil_value(); - mrb_value lines = mrb_nil_value(); + uint32_t line; + const char *filename; - if (irep->filename) filename = mrb_str_new_cstr(mrb, irep->filename); - if (irep->lines) lines = mrb_fixnum_value(*irep->lines); + filename = mrb_debug_get_filename(irep, 0); + line = mrb_debug_get_line(irep, 0); - return mrb_assoc_new(mrb, filename, lines); + return (!filename && line == -1)? mrb_nil_value() + : mrb_assoc_new(mrb, mrb_str_new_cstr(mrb, filename), mrb_fixnum_value(line)); } } diff --git a/mrbgems/mruby-proc-ext/test/proc.rb b/mrbgems/mruby-proc-ext/test/proc.rb index abbd7a9d9..ef8b7f31c 100644 --- a/mrbgems/mruby-proc-ext/test/proc.rb +++ b/mrbgems/mruby-proc-ext/test/proc.rb @@ -1,6 +1,13 @@ ## # Proc(Ext) Test +assert('Proc#source_location') do + loc = Proc.new {}.source_location + next true if loc.nil? + assert_equal loc[0][-7, 7], 'proc.rb' + assert_equal loc[1], 5 +end + assert('Proc#lambda?') do assert_true lambda{}.lambda? assert_true !Proc.new{}.lambda? -- cgit v1.2.3