* [ruby-core:18452] [ANN] Ruby 1.9.1 feature freeze [not found] <966599840809022308q2d6f4370l1de6340c9f1fee8a@mail.gmail.com> @ 2008-09-04 16:13 ` Roger Pack 2008-09-04 17:34 ` [ruby-core:18455] " hemant 2008-09-06 19:53 ` [ruby-core:18471] " Yukihiro Matsumoto 0 siblings, 2 replies; 27+ messages in thread From: Roger Pack @ 2008-09-04 16:13 UTC (permalink / raw To: ruby-core Would it be possible to have a few patches applied before freeze [if possible]-I know I would use them [for being able to run racc against the original method definitions, etc.] :) http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/12598 and/or http://groups.google.com/group/ruby-core-google/browse_thread/thread/932c0ea1078e294a I'd be happy to submit a more formal patch for them, as well, if desired. Thanks! -=R ^ permalink raw reply [flat|nested] 27+ messages in thread
* [ruby-core:18455] Re: [ANN] Ruby 1.9.1 feature freeze 2008-09-04 16:13 ` [ruby-core:18452] [ANN] Ruby 1.9.1 feature freeze Roger Pack @ 2008-09-04 17:34 ` hemant 2008-09-06 18:38 ` [ruby-core:18469] " Charles Oliver Nutter 2008-09-06 19:53 ` [ruby-core:18471] " Yukihiro Matsumoto 1 sibling, 1 reply; 27+ messages in thread From: hemant @ 2008-09-04 17:34 UTC (permalink / raw To: ruby-core On Thu, Sep 4, 2008 at 9:43 PM, Roger Pack <rogerpack2005@gmail.com> wrote: > Would it be possible to have a few patches applied before freeze [if > possible]-I know I would use them [for being able to run racc against > the original method definitions, etc.] :) > > http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/12598 > > and/or > > http://groups.google.com/group/ruby-core-google/browse_thread/thread/932c0ea1078e294a > > I'd be happy to submit a more formal patch for them, as well, if desired. This was a very wrong subject line to choose. :) ^ permalink raw reply [flat|nested] 27+ messages in thread
* [ruby-core:18469] Re: [ANN] Ruby 1.9.1 feature freeze 2008-09-04 17:34 ` [ruby-core:18455] " hemant @ 2008-09-06 18:38 ` Charles Oliver Nutter 0 siblings, 0 replies; 27+ messages in thread From: Charles Oliver Nutter @ 2008-09-06 18:38 UTC (permalink / raw To: ruby-core hemant wrote: > On Thu, Sep 4, 2008 at 9:43 PM, Roger Pack <rogerpack2005@gmail.com> wrote: >> Would it be possible to have a few patches applied before freeze [if >> possible]-I know I would use them [for being able to run racc against >> the original method definitions, etc.] :) >> >> http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/12598 >> >> and/or >> >> http://groups.google.com/group/ruby-core-google/browse_thread/thread/932c0ea1078e294a >> >> I'd be happy to submit a more formal patch for them, as well, if desired. > > This was a very wrong subject line to choose. :) Definitely confused me! ^ permalink raw reply [flat|nested] 27+ messages in thread
* [ruby-core:18471] Re: [ANN] Ruby 1.9.1 feature freeze 2008-09-04 16:13 ` [ruby-core:18452] [ANN] Ruby 1.9.1 feature freeze Roger Pack 2008-09-04 17:34 ` [ruby-core:18455] " hemant @ 2008-09-06 19:53 ` Yukihiro Matsumoto 2008-09-06 21:23 ` [ruby-core:18474] " Wilson Bilkovich ` (2 more replies) 1 sibling, 3 replies; 27+ messages in thread From: Yukihiro Matsumoto @ 2008-09-06 19:53 UTC (permalink / raw To: ruby-core Hi, In message "Re: [ruby-core:18452] [ANN] Ruby 1.9.1 feature freeze" on Fri, 5 Sep 2008 01:13:56 +0900, "Roger Pack" <rogerpack2005@gmail.com> writes: |Would it be possible to have a few patches applied before freeze [if |possible]-I know I would use them [for being able to run racc against |the original method definitions, etc.] :) | |http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/12598 This is a proposal to add __file__ and __line__ methods to Method and Proc objects. Issues are: * the method names. I don't think proposed names that are surrounded by underscores are appropriate. * non-Ruby defined methods/procs. the patch raises TypeError, but is it really appropriate? Should they return nil for such cases? * use-case. the proposal comes with use-case sourceref.rb, but any other use case? matz. ^ permalink raw reply [flat|nested] 27+ messages in thread
* [ruby-core:18474] Re: [ANN] Ruby 1.9.1 feature freeze 2008-09-06 19:53 ` [ruby-core:18471] " Yukihiro Matsumoto @ 2008-09-06 21:23 ` Wilson Bilkovich 2008-09-08 4:02 ` [ruby-core:18487] " Roger Pack 2008-09-08 9:05 ` [ruby-core:18490] " Nobuyoshi Nakada 2 siblings, 0 replies; 27+ messages in thread From: Wilson Bilkovich @ 2008-09-06 21:23 UTC (permalink / raw To: ruby-core On 9/6/08, Yukihiro Matsumoto <matz@ruby-lang.org> wrote: > Hi, > > In message "Re: [ruby-core:18452] [ANN] Ruby 1.9.1 feature freeze" > > on Fri, 5 Sep 2008 01:13:56 +0900, "Roger Pack" <rogerpack2005@gmail.com> writes: > > |Would it be possible to have a few patches applied before freeze [if > |possible]-I know I would use them [for being able to run racc against > |the original method definitions, etc.] :) > | > |http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/12598 > > > This is a proposal to add __file__ and __line__ methods to Method and > Proc objects. Issues are: > > * the method names. I don't think proposed names that are > surrounded by underscores are appropriate. > * non-Ruby defined methods/procs. the patch raises TypeError, but > is it really appropriate? Should they return nil for such cases? > * use-case. the proposal comes with use-case sourceref.rb, but any > other use case? > RSpec currently uses eval("caller(0)", @a_proc_instance) to simulate this behavior. I had not seen this proposal before, but I like it. I looked into this while working on RSpec for Rubinius, and this seems to be the only way to determine where a particular block_pass or proc was created. RSpec cares about this because (as one example) it allows users to specify 'examples' to run by regexp against their description. These 'examples' could be in any spec file, so it uses what I have taken to calling the 'declaration trace' .. in other words, what you get when you eval Kernel#caller with a proc as binding. Using eval for this is slow and ugly; it would be nice to have a clean feature to implement. I considered proposing Proc#caller when I first encountered this, but __file__ and __line__ are nice. While I am on the subject, here is the bug ticket I filed related to this: http://redmine.ruby-lang.org/issues/show/146 ^ permalink raw reply [flat|nested] 27+ messages in thread
* [ruby-core:18487] Re: [ANN] Ruby 1.9.1 feature freeze 2008-09-06 19:53 ` [ruby-core:18471] " Yukihiro Matsumoto 2008-09-06 21:23 ` [ruby-core:18474] " Wilson Bilkovich @ 2008-09-08 4:02 ` Roger Pack 2008-09-08 9:05 ` [ruby-core:18490] " Nobuyoshi Nakada 2 siblings, 0 replies; 27+ messages in thread From: Roger Pack @ 2008-09-08 4:02 UTC (permalink / raw To: ruby-core Sorry for the original subject line being wrong--my attempt to post on the right thread went very awry :) Some comments in-line: > This is a proposal to add __file__ and __line__ methods to Method and > Proc objects. Issues are: > > * the method names. I don't think proposed names that are > surrounded by underscores are appropriate. Perhaps #definition_file/ #definition_line or #declaration_file/#declaration_line? > * non-Ruby defined methods/procs. the patch raises TypeError, but > is it really appropriate? Should they return nil for such cases? Nil is probably better, since it already returns nil in some cases. > * use-case. the proposal comes with use-case sourceref.rb, but any > other use case? For me I'll use it to lookup where methods are defined so that I can <gulp> hate to admit it--re-parse the methods [using racc or what not], see if they can be optimized, combine them with sub methods, add pseudo named parameter wrappers, that type of thing. On second thought I'm wondering if the following would be more useful: Method#iseq Proc#iseq and then add the file/line methods to iseq itself: RubyVM::InstructionSequence#declaration_file RubyVM::InstructionSequence#declaration_line So looking up a method's declaration line would be like klass.instance_method(:name).iseq.declaration_file Then that would give us access to the iseq as well as to the file/line positions. That would be even better for me [since I would then have access to the iseq for proc's as well as method's--there seems to currently be no way to disassemble procs, that I know of, so this would also conveniently overcome this difficulty as well]. I'd be happy to submit a modified patch. Thanks! -=R ^ permalink raw reply [flat|nested] 27+ messages in thread
* [ruby-core:18490] Re: [ANN] Ruby 1.9.1 feature freeze 2008-09-06 19:53 ` [ruby-core:18471] " Yukihiro Matsumoto 2008-09-06 21:23 ` [ruby-core:18474] " Wilson Bilkovich 2008-09-08 4:02 ` [ruby-core:18487] " Roger Pack @ 2008-09-08 9:05 ` Nobuyoshi Nakada 2008-09-08 9:36 ` [ruby-core:18491] " Yukihiro Matsumoto ` (2 more replies) 2 siblings, 3 replies; 27+ messages in thread From: Nobuyoshi Nakada @ 2008-09-08 9:05 UTC (permalink / raw To: ruby-core Hi, At Sun, 7 Sep 2008 04:53:43 +0900, Yukihiro Matsumoto wrote in [ruby-core:18471]: > This is a proposal to add __file__ and __line__ methods to Method and > Proc objects. Issues are: > > * the method names. I don't think proposed names that are > surrounded by underscores are appropriate. > * non-Ruby defined methods/procs. the patch raises TypeError, but > is it really appropriate? Should they return nil for such cases? > * use-case. the proposal comes with use-case sourceref.rb, but any > other use case? I propose a new method, #location than those two new methods. \f Index: proc.c =================================================================== --- proc.c (revision 19215) +++ proc.c (working copy) @@ -510,9 +510,9 @@ proc_call(int argc, VALUE *argv, VALUE p rb_proc_t *proc; rb_block_t *blockptr = 0; + rb_iseq_t *iseq; GetProcPtr(procval, proc); - if (BUILTIN_TYPE(proc->block.iseq) == T_NODE || - proc->block.iseq->arg_block != -1) { - + iseq = proc->block.iseq; + if (BUILTIN_TYPE(iseq) == T_NODE || iseq->arg_block != -1) { if (rb_block_given_p()) { rb_proc_t *proc; @@ -621,8 +621,7 @@ get_proc_iseq(VALUE self) } -VALUE -rb_proc_location(VALUE self) +static VALUE +iseq_location(rb_iseq_t *iseq) { - rb_iseq_t *iseq = get_proc_iseq(self); VALUE loc[2]; @@ -640,4 +639,18 @@ rb_proc_location(VALUE self) /* * call-seq: + * prc.location => [String, Fixnum] + * + * returns the ruby source filename and line number containing this proc + * or nil if this proc was not defined in ruby (i.e. native) + */ + +VALUE +rb_proc_location(VALUE self) +{ + return iseq_location(get_proc_iseq(self)); +} + +/* + * call-seq: * prc == other_proc => true or false * @@ -1438,4 +1451,37 @@ rb_obj_method_arity(VALUE obj, ID id) } +static rb_iseq_t * +get_method_iseq(VALUE method) +{ + struct METHOD *data; + NODE *body; + rb_iseq_t *iseq; + + Data_Get_Struct(method, struct METHOD, data); + body = data->body; + switch (nd_type(body)) { + case RUBY_VM_METHOD_NODE: + GetISeqPtr((VALUE)body->nd_body, iseq); + if (RUBY_VM_NORMAL_ISEQ_P(iseq)) break; + default: + return 0; + } + return iseq; +} + +/* + * call-seq: + * meth.location => [String, Fixnum] + * + * returns the ruby source filename and line number containing this method + * or nil if this method was not defined in ruby (i.e. native) + */ + +VALUE +rb_method_location(VALUE method) +{ + return iseq_location(get_method_iseq(method)); +} + /* * call-seq: @@ -1769,4 +1815,5 @@ Init_Proc(void) rb_define_method(rb_cProc, "binding", proc_binding, 0); rb_define_method(rb_cProc, "curry", proc_curry, -1); + rb_define_method(rb_cProc, "location", rb_proc_location, 0); /* Exceptions */ @@ -1803,4 +1850,5 @@ Init_Proc(void) rb_define_method(rb_cMethod, "owner", method_owner, 0); rb_define_method(rb_cMethod, "unbind", method_unbind, 0); + rb_define_method(rb_cMethod, "location", rb_method_location, 0); rb_define_method(rb_mKernel, "method", rb_obj_method, 1); rb_define_method(rb_mKernel, "public_method", rb_obj_public_method, 1); @@ -1820,4 +1868,5 @@ Init_Proc(void) rb_define_method(rb_cUnboundMethod, "owner", method_owner, 0); rb_define_method(rb_cUnboundMethod, "bind", umethod_bind, 1); + rb_define_method(rb_cUnboundMethod, "location", rb_method_location, 0); /* Module#*_method */ \f -- Nobu Nakada ^ permalink raw reply [flat|nested] 27+ messages in thread
* [ruby-core:18491] Re: [ANN] Ruby 1.9.1 feature freeze 2008-09-08 9:05 ` [ruby-core:18490] " Nobuyoshi Nakada @ 2008-09-08 9:36 ` Yukihiro Matsumoto 2008-09-08 11:22 ` [ruby-core:18493] " Trans ` (3 more replies) 2008-09-20 18:49 ` [ruby-core:18763] " Roger Pack 2008-12-19 21:55 ` [ruby-core:20708] " Roger Pack 2 siblings, 4 replies; 27+ messages in thread From: Yukihiro Matsumoto @ 2008-09-08 9:36 UTC (permalink / raw To: ruby-core Hi, In message "Re: [ruby-core:18490] Re: [ANN] Ruby 1.9.1 feature freeze" on Mon, 8 Sep 2008 18:05:05 +0900, Nobuyoshi Nakada <nobu@ruby-lang.org> writes: |At Sun, 7 Sep 2008 04:53:43 +0900, |Yukihiro Matsumoto wrote in [ruby-core:18471]: |> This is a proposal to add __file__ and __line__ methods to Method and |> Proc objects. |I propose a new method, #location than those two new methods. I like the idea. How others think? matz. ^ permalink raw reply [flat|nested] 27+ messages in thread
* [ruby-core:18493] Re: [ANN] Ruby 1.9.1 feature freeze 2008-09-08 9:36 ` [ruby-core:18491] " Yukihiro Matsumoto @ 2008-09-08 11:22 ` Trans 2008-09-09 8:30 ` [ruby-core:18519] " Yukihiro Matsumoto 2008-09-08 14:41 ` [ruby-core:18496] " Wilson Bilkovich ` (2 subsequent siblings) 3 siblings, 1 reply; 27+ messages in thread From: Trans @ 2008-09-08 11:22 UTC (permalink / raw To: ruby-core On Sep 8, 5:36 am, Yukihiro Matsumoto <m...@ruby-lang.org> wrote: > Hi, > > In message "Re: [ruby-core:18490] Re: [ANN] Ruby 1.9.1 feature freeze" > on Mon, 8 Sep 2008 18:05:05 +0900, Nobuyoshi Nakada <n...@ruby-lang.org> writes: > > |At Sun, 7 Sep 2008 04:53:43 +0900, > |Yukihiro Matsumoto wrote in [ruby-core:18471]: > |> This is a proposal to add __file__ and __line__ methods to Method and > |> Proc objects. > > |I propose a new method, #location than those two new methods. > > I like the idea. How others think? Seems reasonable if __FILE__ and __LINE__ can't do the job. But, will it lead to wanting a general #location method to use in place of __FILE__ and __LINE__? If so, a more specialized name, eg. #source_location, would be better. Also, should the method be defined for Binding too? T. P.S. A bit aside, but could we also have a method for binding.eval{ self } too (eg. Binding#context)? ^ permalink raw reply [flat|nested] 27+ messages in thread
* [ruby-core:18519] Re: [ANN] Ruby 1.9.1 feature freeze 2008-09-08 11:22 ` [ruby-core:18493] " Trans @ 2008-09-09 8:30 ` Yukihiro Matsumoto 2008-09-09 23:39 ` [ruby-core:18528] " Trans 0 siblings, 1 reply; 27+ messages in thread From: Yukihiro Matsumoto @ 2008-09-09 8:30 UTC (permalink / raw To: ruby-core Hi, In message "Re: [ruby-core:18493] Re: [ANN] Ruby 1.9.1 feature freeze" on Mon, 8 Sep 2008 20:22:58 +0900, Trans <transfire@gmail.com> writes: |But, will it lead to wanting a general #location method to use in |place of __FILE__ and __LINE__? If so, a more specialized name, eg. |#source_location, would be better. I like #source_location best. |Also, should the method be defined for Binding too? Probably a good idea. |P.S. A bit aside, but could we also have a method for | | binding.eval{ self } | |too (eg. Binding#context)? Is self really a context? matz. ^ permalink raw reply [flat|nested] 27+ messages in thread
* [ruby-core:18528] Re: [ANN] Ruby 1.9.1 feature freeze 2008-09-09 8:30 ` [ruby-core:18519] " Yukihiro Matsumoto @ 2008-09-09 23:39 ` Trans 0 siblings, 0 replies; 27+ messages in thread From: Trans @ 2008-09-09 23:39 UTC (permalink / raw To: ruby-core On Sep 9, 4:30 am, Yukihiro Matsumoto <m...@ruby-lang.org> wrote: > |P.S. A bit aside, but could we also have a method for > | > | binding.eval{ self } > | > |too (eg. Binding#context)? > > Is self really a context? For lack of a better word. T. ^ permalink raw reply [flat|nested] 27+ messages in thread
* [ruby-core:18496] Re: [ANN] Ruby 1.9.1 feature freeze 2008-09-08 9:36 ` [ruby-core:18491] " Yukihiro Matsumoto 2008-09-08 11:22 ` [ruby-core:18493] " Trans @ 2008-09-08 14:41 ` Wilson Bilkovich [not found] ` <966599840809080824t69da9db3saec127e89bb6069@mail.gmail.com> 2008-09-08 15:24 ` [ruby-core:18498] " Robert Klemme 3 siblings, 0 replies; 27+ messages in thread From: Wilson Bilkovich @ 2008-09-08 14:41 UTC (permalink / raw To: ruby-core On 9/8/08, Yukihiro Matsumoto <matz@ruby-lang.org> wrote: > Hi, > > In message "Re: [ruby-core:18490] Re: [ANN] Ruby 1.9.1 feature freeze" > > on Mon, 8 Sep 2008 18:05:05 +0900, Nobuyoshi Nakada <nobu@ruby-lang.org> writes: > > |At Sun, 7 Sep 2008 04:53:43 +0900, > |Yukihiro Matsumoto wrote in [ruby-core:18471]: > |> This is a proposal to add __file__ and __line__ methods to Method and > |> Proc objects. > > > |I propose a new method, #location than those two new methods. > > > I like the idea. How others think? > This is a specialized feature. Having a specialized name appeals to me; 'location' is good. ^ permalink raw reply [flat|nested] 27+ messages in thread
[parent not found: <966599840809080824t69da9db3saec127e89bb6069@mail.gmail.com>]
* [ruby-core:18497] Re: [ANN] Ruby 1.9.1 feature freeze [not found] ` <966599840809080824t69da9db3saec127e89bb6069@mail.gmail.com> @ 2008-09-08 15:20 ` Roger Pack 2008-09-08 18:26 ` [ruby-core:18501] " Wilson Bilkovich 0 siblings, 1 reply; 27+ messages in thread From: Roger Pack @ 2008-09-08 15:20 UTC (permalink / raw To: ruby-core I think Trans' #source_location is nice. I would prefer to rewrite this patch to become RubyVM::InstructionSequence#source_location # or whatever name we use And then also provide Method#iseq and Proc#iseq methods To also provide the added capability of accessing Proc's iseq's [and disassembling it, etc.--currently unavailable] except I am having trouble creating Proc#iseq [Method#iseq is here[1]]. Any pointers on how to accomplish this? :) Thanks for your help. -=R [1] http://groups.google.com/group/ruby-core-google/browse_thread/thread/932c0ea1078e294a On Mon, Sep 8, 2008 at 3:36 AM, Yukihiro Matsumoto <matz@ruby-lang.org> wrote: > Hi, > > In message "Re: [ruby-core:18490] Re: [ANN] Ruby 1.9.1 feature freeze" > on Mon, 8 Sep 2008 18:05:05 +0900, Nobuyoshi Nakada <nobu@ruby-lang.org> writes: > > |At Sun, 7 Sep 2008 04:53:43 +0900, > |Yukihiro Matsumoto wrote in [ruby-core:18471]: > |> This is a proposal to add __file__ and __line__ methods to Method and > |> Proc objects. > > |I propose a new method, #location than those two new methods. > > I like the idea. How others think? > > matz. > > -- Thanks! -=R -- Thanks! -=R ^ permalink raw reply [flat|nested] 27+ messages in thread
* [ruby-core:18501] Re: [ANN] Ruby 1.9.1 feature freeze 2008-09-08 15:20 ` [ruby-core:18497] " Roger Pack @ 2008-09-08 18:26 ` Wilson Bilkovich 2008-09-09 1:40 ` [ruby-core:18509] " Charles Oliver Nutter [not found] ` <966599840809091113k4c738de3kcfdb74bd747ac1d4@mail.gmail.com> 0 siblings, 2 replies; 27+ messages in thread From: Wilson Bilkovich @ 2008-09-08 18:26 UTC (permalink / raw To: ruby-core On 9/8/08, Roger Pack <rogerpack2005@gmail.com> wrote: > I think Trans' #source_location is nice. > > I would prefer to rewrite this patch to become > > RubyVM::InstructionSequence#source_location # or whatever name we use > > And then also provide > Method#iseq > and > Proc#iseq > methods > > To also provide the added capability of accessing Proc's iseq's [and > disassembling it, etc.--currently unavailable] > > except I am having trouble creating Proc#iseq [Method#iseq is here[1]]. > I much prefer 'location', because it is something that every Ruby implementation can provide. iseq is super super low-level, and we should think hard about the API before calling it public. ^ permalink raw reply [flat|nested] 27+ messages in thread
* [ruby-core:18509] Re: [ANN] Ruby 1.9.1 feature freeze 2008-09-08 18:26 ` [ruby-core:18501] " Wilson Bilkovich @ 2008-09-09 1:40 ` Charles Oliver Nutter [not found] ` <966599840809091113k4c738de3kcfdb74bd747ac1d4@mail.gmail.com> 1 sibling, 0 replies; 27+ messages in thread From: Charles Oliver Nutter @ 2008-09-09 1:40 UTC (permalink / raw To: ruby-core Wilson Bilkovich wrote: > I much prefer 'location', because it is something that every Ruby > implementation can provide. iseq is super super low-level, and we > should think hard about the API before calling it public. I would also hope any "location" methods added will refer only to static information, like the place the Proc was created, rather than adding yet another method with "special" knowledge of the caller's context (requiring the ability to look across invocation frames). - Charlie ^ permalink raw reply [flat|nested] 27+ messages in thread
[parent not found: <966599840809091113k4c738de3kcfdb74bd747ac1d4@mail.gmail.com>]
* [ruby-core:18521] Re: [ANN] Ruby 1.9.1 feature freeze [not found] ` <966599840809091113k4c738de3kcfdb74bd747ac1d4@mail.gmail.com> @ 2008-09-09 18:21 ` Roger Pack 2008-09-10 17:01 ` [ruby-core:18546] " Paul Brannan 2008-09-16 20:10 ` [ruby-core:18636] " Roger Pack 0 siblings, 2 replies; 27+ messages in thread From: Roger Pack @ 2008-09-09 18:21 UTC (permalink / raw To: ruby-core > I much prefer 'location', because it is something that every Ruby > implementation can provide. iseq is super super low-level, and we > should think hard about the API before calling it public. True. I suppose my suggestion was a little bit MRI-centric after all :) Could I then also make a request for a few MRI specific functions, either >> p = proc { |n| n + 2 } => #<Proc:...> >> RubyVM::InstructionSequence.disassemble p # this doesn't currently work or the before mentioned Proc#iseq (Unbound)Method#iseq [see [1]] These would yield some very welcome functionality for projects like [2,3,4] since it would allow users to potentially parse blocks [and rewrite them] before executing them, which has some strong potential. This is available in 1.8.x because the AST is made available to c functions, but not (yet) for 1.9. Thanks for your help. -=R [1] http://groups.google.com/group/ruby-core-google/browse_thread/thread/932c0ea1078e294a [2] http://rewrite.rubyforge.org/ [3] http://parsetree.rubyforge.org/ [4] http://code.google.com/p/ruby-roger-useful-functions/wiki/NamedParameters ^ permalink raw reply [flat|nested] 27+ messages in thread
* [ruby-core:18546] Re: [ANN] Ruby 1.9.1 feature freeze 2008-09-09 18:21 ` [ruby-core:18521] " Roger Pack @ 2008-09-10 17:01 ` Paul Brannan 2008-09-16 20:10 ` [ruby-core:18636] " Roger Pack 1 sibling, 0 replies; 27+ messages in thread From: Paul Brannan @ 2008-09-10 17:01 UTC (permalink / raw To: ruby-core On Wed, Sep 10, 2008 at 03:21:20AM +0900, Roger Pack wrote: > > I much prefer 'location', because it is something that every Ruby > > implementation can provide. iseq is super super low-level, and we > > should think hard about the API before calling it public. > > True. I suppose my suggestion was a little bit MRI-centric after all :) > > Could I then also make a request for a few MRI specific functions, either > > >> p = proc { |n| n + 2 } > => #<Proc:...> > >> RubyVM::InstructionSequence.disassemble p # this doesn't currently work With ruby-internal (http://github.com/cout/ruby-internal) you can do this: irb(main):001:0> require 'internal/proc' => true irb(main):002:0> p = proc { |n| n + 2 } => #<Proc:0x40275ed0@(irb):2> irb(main):003:0> puts p.body.disasm == disasm: <ISeq:block (3 levels) in irb_binding@(irb)>================= == catch table | catch type: redo st: 0000 ed: 0006 sp: 0000 cont: 0000 | catch type: next st: 0000 ed: 0006 sp: 0000 cont: 0006 |------------------------------------------------------------------------ local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1] s3) [ 1] n<Arg> 0000 getdynamic n, 0 ( 2) 0003 putobject 2 0005 opt_plus 0006 leave => nil > > or the before mentioned > Proc#iseq > (Unbound)Method#iseq [see [1]] > > These would yield some very welcome functionality for projects like > [2,3,4] since it would allow users to potentially parse blocks [and > rewrite them] before executing them, which has some strong potential. > This is available in 1.8.x because the AST is made available to c > functions, but not (yet) for 1.9. AFAICT the AST is discarded once the method is compiled to bytecode. Paul ^ permalink raw reply [flat|nested] 27+ messages in thread
* [ruby-core:18636] Re: [ANN] Ruby 1.9.1 feature freeze 2008-09-09 18:21 ` [ruby-core:18521] " Roger Pack 2008-09-10 17:01 ` [ruby-core:18546] " Paul Brannan @ 2008-09-16 20:10 ` Roger Pack 1 sibling, 0 replies; 27+ messages in thread From: Roger Pack @ 2008-09-16 20:10 UTC (permalink / raw To: ruby-core > To also provide the added capability of accessing Proc's iseq's [and > disassembling it, etc.--currently unavailable] > > except I am having trouble creating Proc#iseq [Method#iseq is here[1]]. > > Any pointers on how to accomplish this? :) Thanks to Paul Brannan for his pointers. I have included here a patch with a prototype VM::InstructionSequence.disassemble_proc method with the functionality that I was hoping could be added. I was unsure how to rewrite VM::InstructionSequence.disassemble to handle both methods and procs, so just wrote a separate method. Thoughts? -=R Index: iseq.c =================================================================== --- iseq.c (revision 19392) +++ iseq.c (working copy) @@ -928,6 +928,21 @@ return ret; } +static VALUE +iseq_s_disasm_proc(VALUE klass, VALUE proc) +{ + VALUE ret = Qnil; + rb_proc_t *proc_pointer; + GetProcPtr(proc, proc_pointer); + VALUE iseqval = (VALUE) proc_pointer->block.iseq->self; + if (RUBY_VM_NORMAL_ISEQ_P(iseqval)) + ret = ruby_iseq_disasm(iseqval); + + return ret; +} + + + const char * ruby_node_name(int node) { @@ -1339,5 +1354,6 @@ rb_define_singleton_method(rb_cISeq, "compile_option=", iseq_s_compile_option_set, 1); rb_define_singleton_method(rb_cISeq, "disasm", iseq_s_disasm, 1); rb_define_singleton_method(rb_cISeq, "disassemble", iseq_s_disasm, 1); + rb_define_singleton_method(rb_cISeq, "disassemble_proc", iseq_s_disasm_proc, 1); } ^ permalink raw reply [flat|nested] 27+ messages in thread
* [ruby-core:18498] Re: [ANN] Ruby 1.9.1 feature freeze 2008-09-08 9:36 ` [ruby-core:18491] " Yukihiro Matsumoto ` (2 preceding siblings ...) [not found] ` <966599840809080824t69da9db3saec127e89bb6069@mail.gmail.com> @ 2008-09-08 15:24 ` Robert Klemme 2008-09-08 19:27 ` [ruby-core:18502] " Joel VanderWerf 3 siblings, 1 reply; 27+ messages in thread From: Robert Klemme @ 2008-09-08 15:24 UTC (permalink / raw To: ruby-core 2008/9/8 Yukihiro Matsumoto <matz@ruby-lang.org>: > Hi, > > In message "Re: [ruby-core:18490] Re: [ANN] Ruby 1.9.1 feature freeze" > on Mon, 8 Sep 2008 18:05:05 +0900, Nobuyoshi Nakada <nobu@ruby-lang.org> writes: > > |At Sun, 7 Sep 2008 04:53:43 +0900, > |Yukihiro Matsumoto wrote in [ruby-core:18471]: > |> This is a proposal to add __file__ and __line__ methods to Method and > |> Proc objects. > > |I propose a new method, #location than those two new methods. > > I like the idea. How others think? Good! I'd like to suggest a name alternative though #defined_at IMHO #location is very general and #defined_at better expresses what's being returned (at least from what I gathered should be the semantics of this method). Usage would be def foo(&b) file, line = b.defined_at $stderr.printf "calling block which was defined at %s:%d\n", file, line b.call end Kind regards robert -- use.inject do |as, often| as.you_can - without end ^ permalink raw reply [flat|nested] 27+ messages in thread
* [ruby-core:18502] Re: [ANN] Ruby 1.9.1 feature freeze 2008-09-08 15:24 ` [ruby-core:18498] " Robert Klemme @ 2008-09-08 19:27 ` Joel VanderWerf 0 siblings, 0 replies; 27+ messages in thread From: Joel VanderWerf @ 2008-09-08 19:27 UTC (permalink / raw To: ruby-core Robert Klemme wrote: > #defined_at > > IMHO #location is very general and #defined_at better expresses what's > being returned (at least from what I gathered should be the semantics > of this method). Usage would be Agree. It is convenient to use subclasses of Procs. Having such a general method name taken away is sob-optimal. -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407 ^ permalink raw reply [flat|nested] 27+ messages in thread
* [ruby-core:18763] Re: [ANN] Ruby 1.9.1 feature freeze 2008-09-08 9:05 ` [ruby-core:18490] " Nobuyoshi Nakada 2008-09-08 9:36 ` [ruby-core:18491] " Yukihiro Matsumoto @ 2008-09-20 18:49 ` Roger Pack 2008-09-25 1:18 ` [ruby-core:18875] " Nobuyoshi Nakada [not found] ` <966599840809251625t77060aedq4248037a6c95cc84@mail.gmail.com> 2008-12-19 21:55 ` [ruby-core:20708] " Roger Pack 2 siblings, 2 replies; 27+ messages in thread From: Roger Pack @ 2008-09-20 18:49 UTC (permalink / raw To: ruby-core > I propose a new method, #location than those two new methods. ... > -- > Nobu Nakada Interestingly, there already was an rb_proc_location method in 1.9 [just no Proc#location nor Method#location nor Binding#location]. Anyway, here's Nobu Nakada's patch with the method name changed to source_location [should work against SVN head]. Note also the recent creation of Feature #578: add method to disassemble Proc objects Thanks for your help. I know I'm annoying with this :) -=R Index: proc.c =================================================================== --- proc.c (revision 19438) +++ proc.c (working copy) @@ -509,11 +509,11 @@ { rb_proc_t *proc; rb_block_t *blockptr = 0; + rb_iseq_t *iseq; GetProcPtr(procval, proc); + iseq = proc->block.iseq; + if (BUILTIN_TYPE(iseq) == T_NODE || iseq->arg_block != -1) { - if (BUILTIN_TYPE(proc->block.iseq) == T_NODE || - proc->block.iseq->arg_block != -1) { - if (rb_block_given_p()) { rb_proc_t *proc; VALUE procval; @@ -620,10 +620,9 @@ return iseq; } -VALUE -rb_proc_location(VALUE self) +static VALUE +iseq_location(rb_iseq_t *iseq) { - rb_iseq_t *iseq = get_proc_iseq(self); VALUE loc[2]; if (!iseq) return Qnil; @@ -639,6 +638,20 @@ /* * call-seq: + * prc.source_location => [String, Fixnum] + * + * returns the ruby source filename and line number containing this proc + * or nil if this proc was not defined in ruby (i.e. native) + */ + +VALUE +rb_proc_source_location(VALUE self) +{ + return iseq_location(get_proc_iseq(self)); +} + +/* + * call-seq: * prc == other_proc => true or false * * Return <code>true</code> if <i>prc</i> is the same object as @@ -1437,7 +1450,40 @@ return rb_mod_method_arity(CLASS_OF(obj), id); } +static rb_iseq_t * +get_method_iseq(VALUE method) +{ + struct METHOD *data; + NODE *body; + rb_iseq_t *iseq; + + Data_Get_Struct(method, struct METHOD, data); + body = data->body; + switch (nd_type(body)) { + case RUBY_VM_METHOD_NODE: + GetISeqPtr((VALUE)body->nd_body, iseq); + if (RUBY_VM_NORMAL_ISEQ_P(iseq)) break; + default: + return 0; + } + return iseq; +} + /* + * call-seq: + * meth.source_location => [String, Fixnum] + * + * returns the ruby source filename and line number containing this method + * or nil if this method was not defined in ruby (i.e. native) + */ + +VALUE +rb_method_source_location(VALUE method) +{ + return iseq_location(get_method_iseq(method)); +} + +/* * call-seq: * meth.to_s => string * meth.inspect => string @@ -1768,6 +1814,7 @@ rb_define_method(rb_cProc, "lambda?", proc_lambda_p, 0); rb_define_method(rb_cProc, "binding", proc_binding, 0); rb_define_method(rb_cProc, "curry", proc_curry, -1); + rb_define_method(rb_cProc, "source_location", rb_proc_source_location, 0); /* Exceptions */ rb_eLocalJumpError = rb_define_class("LocalJumpError", rb_eStandardError); @@ -1802,6 +1849,7 @@ rb_define_method(rb_cMethod, "name", method_name, 0); rb_define_method(rb_cMethod, "owner", method_owner, 0); rb_define_method(rb_cMethod, "unbind", method_unbind, 0); + rb_define_method(rb_cMethod, "source_location", rb_method_source_location, 0); rb_define_method(rb_mKernel, "method", rb_obj_method, 1); rb_define_method(rb_mKernel, "public_method", rb_obj_public_method, 1); @@ -1819,6 +1867,7 @@ rb_define_method(rb_cUnboundMethod, "name", method_name, 0); rb_define_method(rb_cUnboundMethod, "owner", method_owner, 0); rb_define_method(rb_cUnboundMethod, "bind", umethod_bind, 1); + rb_define_method(rb_cUnboundMethod, "source_location", rb_method_source_location, 0); /* Module#*_method */ rb_define_method(rb_cModule, "instance_method", rb_mod_instance_method, 1); Index: thread.c =================================================================== --- thread.c (revision 19438) +++ thread.c (working copy) @@ -528,10 +528,10 @@ } GetThreadPtr(thread, th); if (th->first_args) { - VALUE rb_proc_location(VALUE self); + VALUE rb_proc_source_location(VALUE self); VALUE proc = th->first_proc, line, loc; const char *file; - if (!proc || !RTEST(loc = rb_proc_location(proc))) { + if (!proc || !RTEST(loc = rb_proc_source_location(proc))) { rb_raise(rb_eThreadError, "already initialized thread"); } file = RSTRING_PTR(RARRAY_PTR(loc)[0]); ^ permalink raw reply [flat|nested] 27+ messages in thread
* [ruby-core:18875] Re: [ANN] Ruby 1.9.1 feature freeze 2008-09-20 18:49 ` [ruby-core:18763] " Roger Pack @ 2008-09-25 1:18 ` Nobuyoshi Nakada [not found] ` <966599840809251625t77060aedq4248037a6c95cc84@mail.gmail.com> 1 sibling, 0 replies; 27+ messages in thread From: Nobuyoshi Nakada @ 2008-09-25 1:18 UTC (permalink / raw To: ruby-core Hi, At Sun, 21 Sep 2008 03:49:50 +0900, Roger Pack wrote in [ruby-core:18763]: > Anyway, here's Nobu Nakada's patch with the method name changed to > source_location [should work against SVN head]. Are you proposing the change of C functions names together? -- Nobu Nakada ^ permalink raw reply [flat|nested] 27+ messages in thread
[parent not found: <966599840809251625t77060aedq4248037a6c95cc84@mail.gmail.com>]
* [ruby-core:18970] Re: [ANN] Ruby 1.9.1 feature freeze [not found] ` <966599840809251625t77060aedq4248037a6c95cc84@mail.gmail.com> @ 2008-09-26 4:17 ` Roger Pack 2008-09-26 14:09 ` [ruby-core:18978] " Nobuyoshi Nakada 0 siblings, 1 reply; 27+ messages in thread From: Roger Pack @ 2008-09-26 4:17 UTC (permalink / raw To: ruby-core >> Interestingly, there already was an rb_proc_location method in 1.9 >> [just no Proc#location nor Method#location nor Binding#location]. >> >> Anyway, here's Nobu Nakada's patch with the method name changed to >> source_location [should work against SVN head]. > Nobuyoshi Nakada wrote: > Are you proposing the change of C functions names together? Yeah I was thinking it would be less confusing to only have one function name [source_location] and have it apply to both proc and method. Also note that it was mentioned that Binding#source_location would be nice--I just have no idea how to add it so the current patch doesn't include it. Note also Feature #578: add method to disassemble Proc objects Any feedback on having that added as well? -=R ^ permalink raw reply [flat|nested] 27+ messages in thread
* [ruby-core:18978] Re: [ANN] Ruby 1.9.1 feature freeze 2008-09-26 4:17 ` [ruby-core:18970] " Roger Pack @ 2008-09-26 14:09 ` Nobuyoshi Nakada 2008-09-26 16:19 ` [ruby-core:18981] " Roger Pack 0 siblings, 1 reply; 27+ messages in thread From: Nobuyoshi Nakada @ 2008-09-26 14:09 UTC (permalink / raw To: ruby-core Hi, At Fri, 26 Sep 2008 13:17:25 +0900, Roger Pack wrote in [ruby-core:18970]: > > Nobuyoshi Nakada wrote: > > Are you proposing the change of C functions names together? > > Yeah I was thinking it would be less confusing to only have one > function name [source_location] and have it apply to both proc and > method. Meanwhile, I added the methods without changing the function names. The names are another story. # I've thought I'd committed it already, but forgotten. > Also note that it was mentioned that Binding#source_location would be > nice--I just have no idea how to add it so the current patch doesn't > include it. What is the line number of Binding? -- Nobu Nakada ^ permalink raw reply [flat|nested] 27+ messages in thread
* [ruby-core:18981] Re: [ANN] Ruby 1.9.1 feature freeze 2008-09-26 14:09 ` [ruby-core:18978] " Nobuyoshi Nakada @ 2008-09-26 16:19 ` Roger Pack 2008-09-26 18:25 ` [ruby-core:18984] " Nobuyoshi Nakada 0 siblings, 1 reply; 27+ messages in thread From: Roger Pack @ 2008-09-26 16:19 UTC (permalink / raw To: ruby-core >> Yeah I was thinking it would be less confusing to only have one >> function name [source_location] and have it apply to both proc and >> method. > > Meanwhile, I added the methods without changing the function > names. The names are another story. > > # I've thought I'd committed it already, but forgotten. Thanks for adding those. I see them in SVN now. I prefer #source_location but #location is livable :) >> Also note that it was mentioned that Binding#source_location would be >> nice--I just have no idea how to add it so the current patch doesn't >> include it. > > What is the line number of Binding? There isn't one in that patch. I'm unsure how to create Binding#source_location Thanks! -=R ^ permalink raw reply [flat|nested] 27+ messages in thread
* [ruby-core:18984] Re: [ANN] Ruby 1.9.1 feature freeze 2008-09-26 16:19 ` [ruby-core:18981] " Roger Pack @ 2008-09-26 18:25 ` Nobuyoshi Nakada 0 siblings, 0 replies; 27+ messages in thread From: Nobuyoshi Nakada @ 2008-09-26 18:25 UTC (permalink / raw To: ruby-core Hi, At Sat, 27 Sep 2008 01:19:37 +0900, Roger Pack wrote in [ruby-core:18981]: > >> Yeah I was thinking it would be less confusing to only have one > >> function name [source_location] and have it apply to both proc and > >> method. > > > > Meanwhile, I added the methods without changing the function > > names. The names are another story. > > > > # I've thought I'd committed it already, but forgotten. > > Thanks for adding those. I see them in SVN now. > I prefer #source_location but #location is livable :) Methods are #source_location, C functions are still *_location(). > >> Also note that it was mentioned that Binding#source_location would be > >> nice--I just have no idea how to add it so the current patch doesn't > >> include it. > > > > What is the line number of Binding? > > There isn't one in that patch. I'm unsure how to create Binding#source_location It's easy to implement, if it is OK to return the beginning of the code block where the binding was made. \f Index: proc.c =================================================================== --- proc.c (revision 19593) +++ proc.c (working copy) @@ -29,4 +29,5 @@ static VALUE bmcall(VALUE, VALUE); static int method_arity(VALUE); static VALUE rb_obj_is_method(VALUE m); +static VALUE iseq_location(rb_iseq_t *iseq); /* Proc */ @@ -339,4 +340,22 @@ bind_eval(int argc, VALUE *argv, VALUE b } +/* + * call-seq: + * binding.source_location => [String, Fixnum] + * + * returns the ruby source filename and line number containing this binding + * or nil if this binding was not defined in ruby (i.e. native) + */ +VALUE +rb_bind_location(VALUE self) +{ + rb_binding_t *bind; + rb_env_t *env; + + GetBindingPtr(self, bind); + GetEnvPtr(bind->env, env); + return iseq_location(env->block.iseq); +} + static VALUE proc_new(VALUE klass, int is_lambda) @@ -1923,4 +1942,5 @@ Init_Binding(void) rb_define_method(rb_cBinding, "dup", binding_dup, 0); rb_define_method(rb_cBinding, "eval", bind_eval, -1); + rb_define_method(rb_cBinding, "source_location", rb_bind_location, 0); rb_define_global_function("binding", rb_f_binding, 0); } Index: ruby.c =================================================================== --- ruby.c (revision 19593) +++ ruby.c (working copy) @@ -1478,4 +1478,5 @@ ruby_prog_init(void) rb_define_global_const("ARGV", rb_argv); + rb_define_global_const("TOPLEVEL_BINDING", rb_binding_new()); #ifdef MSDOS Index: vm.c =================================================================== --- vm.c (revision 19594) +++ vm.c (working copy) @@ -1241,7 +1241,4 @@ rb_iseq_eval(VALUE iseqval) vm_set_top_stack(th, iseqval); - if (!rb_const_defined(rb_cObject, rb_intern("TOPLEVEL_BINDING"))) { - rb_define_global_const("TOPLEVEL_BINDING", rb_binding_new()); - } val = vm_exec(th); tmp = iseqval; /* prohibit tail call optimization */ \f -- Nobu Nakada ^ permalink raw reply [flat|nested] 27+ messages in thread
* [ruby-core:20708] Re: [ANN] Ruby 1.9.1 feature freeze 2008-09-08 9:05 ` [ruby-core:18490] " Nobuyoshi Nakada 2008-09-08 9:36 ` [ruby-core:18491] " Yukihiro Matsumoto 2008-09-20 18:49 ` [ruby-core:18763] " Roger Pack @ 2008-12-19 21:55 ` Roger Pack 2 siblings, 0 replies; 27+ messages in thread From: Roger Pack @ 2008-12-19 21:55 UTC (permalink / raw To: ruby-core > +/* > + * call-seq: > + * meth.location => [String, Fixnum] > + * > + * returns the ruby source filename and line number containing this method > + * or nil if this method was not defined in ruby (i.e. native) > + */ Quite useful would also be meth.source_location => [String, Fixnum, Fixnum] # to designate which character it started at, to make it easier to go in and extract the original code. Or even meth.source_location => [[begin char], [end char]] that would be nice [though not necessary, I suppose, since once you have the beginning, you can scan for the end]. This would enable meth.to_s more readily. Thanks! -=R ^ permalink raw reply [flat|nested] 27+ messages in thread
end of thread, other threads:[~2008-12-19 22:07 UTC | newest] Thread overview: 27+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- [not found] <966599840809022308q2d6f4370l1de6340c9f1fee8a@mail.gmail.com> 2008-09-04 16:13 ` [ruby-core:18452] [ANN] Ruby 1.9.1 feature freeze Roger Pack 2008-09-04 17:34 ` [ruby-core:18455] " hemant 2008-09-06 18:38 ` [ruby-core:18469] " Charles Oliver Nutter 2008-09-06 19:53 ` [ruby-core:18471] " Yukihiro Matsumoto 2008-09-06 21:23 ` [ruby-core:18474] " Wilson Bilkovich 2008-09-08 4:02 ` [ruby-core:18487] " Roger Pack 2008-09-08 9:05 ` [ruby-core:18490] " Nobuyoshi Nakada 2008-09-08 9:36 ` [ruby-core:18491] " Yukihiro Matsumoto 2008-09-08 11:22 ` [ruby-core:18493] " Trans 2008-09-09 8:30 ` [ruby-core:18519] " Yukihiro Matsumoto 2008-09-09 23:39 ` [ruby-core:18528] " Trans 2008-09-08 14:41 ` [ruby-core:18496] " Wilson Bilkovich [not found] ` <966599840809080824t69da9db3saec127e89bb6069@mail.gmail.com> 2008-09-08 15:20 ` [ruby-core:18497] " Roger Pack 2008-09-08 18:26 ` [ruby-core:18501] " Wilson Bilkovich 2008-09-09 1:40 ` [ruby-core:18509] " Charles Oliver Nutter [not found] ` <966599840809091113k4c738de3kcfdb74bd747ac1d4@mail.gmail.com> 2008-09-09 18:21 ` [ruby-core:18521] " Roger Pack 2008-09-10 17:01 ` [ruby-core:18546] " Paul Brannan 2008-09-16 20:10 ` [ruby-core:18636] " Roger Pack 2008-09-08 15:24 ` [ruby-core:18498] " Robert Klemme 2008-09-08 19:27 ` [ruby-core:18502] " Joel VanderWerf 2008-09-20 18:49 ` [ruby-core:18763] " Roger Pack 2008-09-25 1:18 ` [ruby-core:18875] " Nobuyoshi Nakada [not found] ` <966599840809251625t77060aedq4248037a6c95cc84@mail.gmail.com> 2008-09-26 4:17 ` [ruby-core:18970] " Roger Pack 2008-09-26 14:09 ` [ruby-core:18978] " Nobuyoshi Nakada 2008-09-26 16:19 ` [ruby-core:18981] " Roger Pack 2008-09-26 18:25 ` [ruby-core:18984] " Nobuyoshi Nakada 2008-12-19 21:55 ` [ruby-core:20708] " Roger Pack
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).