Retrieving bytecode for method
The group you are posting to is a
Usenet group . Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
From:
Michael Neumann <mneum... @ntecs.de>
Date: Wed, 20 Aug 2008 19:56:57 +0900
Local: Wed, Aug 20 2008 6:56 am
Subject: [ruby-core:18354] Retrieving bytecode for method
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
You must
Sign in before you can post messages.
You do not have the permission required to post.
From:
David Majda <da... @majda.cz>
Date: Thu, 21 Aug 2008 15:33:48 +0900
Local: Thurs, Aug 21 2008 2:33 am
Subject: [ruby-core:18359] Re: Retrieving bytecode for method
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.ma... @impaladesign.cz :: www.impaladesign.cz
You must
Sign in before you can post messages.
You do not have the permission required to post.
Newsgroups: gmane.comp.lang.ruby.core
From:
Michael Neumann <mneum... @ntecs.de>
Date: Thu, 21 Aug 2008 15:51:17 +0900
Local: Thurs, Aug 21 2008 2:51 am
Subject: [ruby-core:18360] Re: Retrieving bytecode for method
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
You must
Sign in before you can post messages.
You do not have the permission required to post.
From:
Michael Neumann <mneum... @ntecs.de>
Date: Thu, 21 Aug 2008 16:56:57 +0900
Local: Thurs, Aug 21 2008 3:56 am
Subject: [ruby-core:18362] Re: Retrieving bytecode for method
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 1K ]
Index: proc.c =================================================================== --- proc.c (revision 18724) +++ proc.c (working copy) @@ -1332,6 +1332,18 @@ return method; } +static VALUE +method_iseq(VALUE method) +{ + struct METHOD *data; + + Data_Get_Struct(method, struct METHOD, data); + + VALUE iseqval = (VALUE)data->body->nd_body; + + return iseqval; +} + int rb_node_arity(NODE* body) { @@ -1790,6 +1802,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, "iseq", method_iseq, 0); rb_define_method(rb_mKernel, "method", rb_obj_method, 1); rb_define_method(rb_mKernel, "public_method", rb_obj_public_method, 1);
@@ -1807,6 +1820,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, "iseq", method_iseq, 0);
/* Module#*_method */ rb_define_method(rb_cModule, "instance_method", rb_mod_instance_method, 1);
You must
Sign in before you can post messages.
You do not have the permission required to post.