Feature #578

add method to disassemble Proc objects

Added by Roger Pack over 6 years ago. Updated over 2 years ago.

[ruby-core:18762]
Status:Closed
Priority:Low
Assignee:Koichi Sasada

Description

=begin
Currently
RubyVM::InstructionSequence.disassemble
works only for methods.

I wasn't sure how to patch
RubyVM::InstructionSequence.disassemble
to work with proc objects AND methods, so added a new method
RubyVM::InstructionSequence.disassemble_proc
which works for proc objects, in the accompanying patch.
Either way would work.

Hope this can be included before the feature freeze, if possible :)
-=R
=end

add_disassemble_proc.diff Magnifier (889 Bytes) Roger Pack, 09/21/2008 03:10 AM

iseq.diff Magnifier (786 Bytes) Roger Pack, 01/20/2010 05:14 AM

History

#1 Updated by Koichi Sasada over 6 years ago

  • Assignee set to Koichi Sasada

=begin

=end

#2 Updated by Yuki Sonoda over 6 years ago

  • Target version set to 2.0.0

=begin

=end

#3 Updated by Roger Pack over 6 years ago

=begin
Question: any chance of getting this [or something like it] included before feature freezing all the way?
Thanks all. Ruby rox :)
=end

#4 Updated by Koichi Sasada over 6 years ago

  • Priority changed from Normal to Low

=begin
I want to fix it with modifying ISeq#disasm to receive Proc object on 1.9.2. Sorry for late response.
=end

#5 Updated by Roger Pack over 6 years ago

=begin
thanks for looking into that :)
-=R
=end

#6 Updated by Roger Pack almost 6 years ago

=begin
Any progress on this? any way I can help?
Thanks!
-=r
=end

#7 Updated by Nobuyoshi Nakada over 5 years ago

=begin
Hi,

At Sun, 21 Sep 2008 03:02:44 +0900,
Roger Pack wrote in :

I wasn't sure how to patch
RubyVM::InstructionSequence.disassemble
to work with proc objects AND methods, so added a new method
RubyVM::InstructionSequence.disassemble_proc
which works for proc objects, in the accompanying patch.
Either way would work.

It's pretty easy.

Index: iseq.c
===================================================================
--- iseq.c (revision 23974)
+++ iseq.c (working copy)
@@ -1007,4 +1007,13 @@ iseq_s_disasm(VALUE klass, VALUE body)
}
}
+ else {
+ rb_proc_t *proc;
+ VALUE iseqval;
+ GetProcPtr(body, proc);
+ iseqval = proc->block.iseq->self;
+ if (RUBY_VM_NORMAL_ISEQ_P(iseqval)) {
+ ret = rb_iseq_disasm(iseqval);
+ }
+ }

  return ret;

--
Nobu Nakada

=end

#8 Updated by Rocky Bernstein over 5 years ago

=begin
Another possiblility would be to add an instruction-sequence method (iseq) into Proc. Off of the instruction sequence, one could use the methods like disasm or to_a.

The actual additional code (put in the right place) for this seems pretty small:

VALUE
proc_iseq(VALUE self)
{
rb_proc_t *proc;
rb_iseq_t *iseq;
VALUE rb_iseq;
GetProcPtr(self, proc);
iseq = proc->block.iseq;
if (!iseq) return Qnil;
rb_iseq = iseq_alloc_shared(rb_cISeq);
RDATA(rb_iseq)->data = iseq;
return rb_iseq;
}

rb_define_method(rb_cProc, "iseq", proc_iseq, 0);

Warning: the above may have flaws in it.
=end

#9 Updated by Roger Pack over 5 years ago

=begin
Would it be possible for somebody to commit this? Do we need more approval?
Thanks.
-r
=end

#10 Updated by Yukihiro Matsumoto over 5 years ago

=begin
Hi,

In message "Re: [Feature #578] add method to disassemble Proc objects"
on Tue, 6 Oct 2009 20:55:57 +0900, Roger Pack redmine@ruby-lang.org writes:

|Would it be possible for somebody to commit this? Do we need more approval?

Yea, a patch from Nobu in needs permission from
Koichi.

                        matz.

=end

#11 Updated by Koichi Sasada over 5 years ago

=begin
Roger Pack wrote::

Yea, a patch from Nobu in needs permission from
Koichi.

Ok. Koichi do you have any feedback on the patch?

no. go ahead.

--
// SASADA Koichi at atdot dot net

=end

#12 Updated by Nobuyoshi Nakada over 5 years ago

  • Status changed from Open to Closed
  • % Done changed from 0 to 100

=begin
Applied in changeset r25258.
=end

#13 Updated by Roger Pack over 5 years ago

=begin
The fact that we can get backtraces from the VM easily almost makes me wonder if the following API is possible:

Thread#backtrace_with_bindings
=>
[[file, line, method, [[:arg1_name, current_value], [:arg2_name, current_value]]

or
file, line, method, binding # so you can lookup argument values if so desired

mostly inspired by http://barelyenough.org/blog/2005/04/ruby-backtraces/ -- not sure if it's actually necessary/high priority since you can get somewhat higher quality backtraces using ruby-debug already (http://github.com/rdp/backtracer)

Thoughts?
-r
=end

#14 Updated by Marc-Andre Lafortune about 5 years ago

  • Category set to core
  • Status changed from Closed to Open
  • Target version changed from 2.0.0 to 1.9.2

=begin
I didn't check the code, but isn't it supposed to make the following work?

$ rubydev -e 'RubyVM::InstructionSequence.disassemble(Proc.new{40+2})'
-e:1:in disassemble': wrong argument type proc (expected method) (TypeError)
from -e:1:in
'

=end

#15 Updated by Roger Pack about 5 years ago

=begin
This diff appears to fix it, if somebody could take a look at it.
Thanks.
-r

=end

#16 Updated by Roger Pack about 5 years ago

  • Status changed from Open to Closed

=begin
Appears fixed now. Thanks!

r26363
=end

Also available in: Atom PDF