[ruby-core:18354] Retrieving bytecode for method

1 view
Skip to first unread message

Michael Neumann

unread,
Aug 20, 2008, 6:56:57 AM8/20/08
to ruby...@ruby-lang.org
Hi,

I'd like to get the bytecode of a method in Ruby 1.9. I know that I
can compile a string or file into bytecode with:

iseq = RubyVM::InstructionSequence.compile_file("name_of_file")

But is there a method which allows me to do access the bytecode
of a method from a running script as shown below:

class A
def b
1 + 2
end
end

iseq = A.bytecode_for_method(:b) # => RubyVM::InstructionSequence

If not, is it possible/hard to implement? I think there should be a
method cache somewhere which contains an instruction pointer to the
start of the method.

I'd love to experiment with translating the bytecode into a different
language (maybe Javascript), that's why I am ask :)

Regards,

Michael


David Majda

unread,
Aug 21, 2008, 2:33:48 AM8/21/08
to ruby...@ruby-lang.org
Michael Neumann napsal(a):

> I'd love to experiment with translating the bytecode into a different
> language (maybe Javascript), that's why I am ask :)

FYI, a project that translates YARV bytecode into JavaScript already exists:

http://hotruby.yukoba.jp/

David Majda

--
Everyone gets everything he wants. --Captain Willard in Apocalypse Now

Personal :: da...@majda.cz :: www.majda.cz
Work :: david...@impaladesign.cz :: www.impaladesign.cz

Michael Neumann

unread,
Aug 21, 2008, 2:51:17 AM8/21/08
to ruby...@ruby-lang.org
David Majda wrote:
> Michael Neumann napsal(a):
>> I'd love to experiment with translating the bytecode into a different
>> language (maybe Javascript), that's why I am ask :)
>
> FYI, a project that translates YARV bytecode into JavaScript already
> exists:
>
> http://hotruby.yukoba.jp/

Thanks. I already know of this project (which is great btw) and I am
working on something similar (http://www.ntecs.de/projects/rubyjs), but
I want to generate Javascript instead of interpreting Ruby in
Javascript. A combination of both would of course be perfect :)

Regards,

Michael

Michael Neumann

unread,
Aug 21, 2008, 3:56:57 AM8/21/08
to ruby...@ruby-lang.org
Michael Neumann wrote:
> Hi,
>
> I'd like to get the bytecode of a method in Ruby 1.9. I know that I
> can compile a string or file into bytecode with:
>
> iseq = RubyVM::InstructionSequence.compile_file("name_of_file")
>
> But is there a method which allows me to do access the bytecode
> of a method from a running script as shown below:
>
> class A
> def b
> 1 + 2
> end
> end
>
> iseq = A.bytecode_for_method(:b) # => RubyVM::InstructionSequence

I solved this myself with extending class Method and UnboundMethod for
method #iseq. See the attached patch.

Regards,

Michael

ruby.iseq.diff
Reply all
Reply to author
Forward
0 new messages