ruby-changes:49365
From: mame <ko1@a...>
Date: Tue, 26 Dec 2017 17:38:41 +0900 (JST)
Subject: [ruby-changes:49365] mame:r61480 (trunk): proc.c (bind_location): Add Binding#source_location
mame 2017-12-26 17:38:35 +0900 (Tue, 26 Dec 2017) New Revision: 61480 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=61480 Log: proc.c (bind_location): Add Binding#source_location Fixes #14230 Modified files: trunk/lib/irb/workspace.rb trunk/proc.c Index: proc.c =================================================================== --- proc.c (revision 61479) +++ proc.c (revision 61480) @@ -12,6 +12,7 @@ https://github.com/ruby/ruby/blob/trunk/proc.c#L12 #include "eval_intern.h" #include "internal.h" #include "gc.h" +#include "vm_core.h" #include "iseq.h" /* Proc.new with no block will raise an exception in the future @@ -611,6 +612,24 @@ bind_receiver(VALUE bindval) https://github.com/ruby/ruby/blob/trunk/proc.c#L612 return vm_block_self(&bind->block); } +/* + * call-seq: + * binding.source_location -> [String, Integer] + * + * Returns the Ruby source filename and line number of the binding object. + */ +static VALUE +bind_location(VALUE bindval) +{ + VALUE loc[2]; + const rb_binding_t *bind; + GetBindingPtr(bindval, bind); + loc[0] = pathobj_path(bind->pathobj); + loc[1] = INT2FIX(bind->first_lineno); + + return rb_ary_new4(2, loc); +} + static VALUE cfunc_proc_new(VALUE klass, VALUE ifunc, int8_t is_lambda) { @@ -3223,5 +3242,6 @@ Init_Binding(void) https://github.com/ruby/ruby/blob/trunk/proc.c#L3242 rb_define_method(rb_cBinding, "local_variable_set", bind_local_variable_set, 2); rb_define_method(rb_cBinding, "local_variable_defined?", bind_local_variable_defined_p, 1); rb_define_method(rb_cBinding, "receiver", bind_receiver, 0); + rb_define_method(rb_cBinding, "source_location", bind_location, 0); rb_define_global_function("binding", rb_f_binding, 0); } Index: lib/irb/workspace.rb =================================================================== --- lib/irb/workspace.rb (revision 61479) +++ lib/irb/workspace.rb (revision 61480) @@ -108,7 +108,7 @@ EOF https://github.com/ruby/ruby/blob/trunk/lib/irb/workspace.rb#L108 end def code_around_binding - file, pos = @binding.eval('[__FILE__, __LINE__]') + file, pos = @binding.source_location unless defined?(::SCRIPT_LINES__[file]) && lines = ::SCRIPT_LINES__[file] begin -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/