Method names changed in JRuby bytecode

I’m trying to understand the JVM bytecode generated from a JRuby file
using the jrubyc tool. One of the first things that springs to attention
is the fact that method names are mangled to include a set prefix of
“method__x$RUBY$”.
This means that something like :
def fact(n)
if n==0
return 1
else
puts “calling fact with arg #{n}”
return n*fact(n-1)
end
end

has a method signature in bytecode that matches:
public static org.jruby.runtime.builtin.IRubyObject
method__2$RUBY$fact(additional arguments)

can anyone provide any insights as to why this might be necessary?

Thanks

This blog post is excellent, and gets at why this is necessary.

2011/10/25 Rm Mz [email protected]

On Tue, Oct 25, 2011 at 5:19 AM, Rm Mz [email protected] wrote:

I’m trying to understand the JVM bytecode generated from a JRuby file
using the jrubyc tool. One of the first things that springs to attention
is the fact that method names are mangled to include a set prefix of
“method__x$RUBY$”.

has a method signature in bytecode that matches:
public static org.jruby.runtime.builtin.IRubyObject
method__2$RUBY$fact(additional arguments)

can anyone provide any insights as to why this might be necessary?

There’s a couple reasons.

The method_ prefix is to identify for (my) debugging purposes that
this JVM method corresponds to a Ruby method. There’s also block_ and
a couple other prefixes.

The number following method_ is the index of the method in this
particular compilation unit (e.g. Nth method in this file). They are
numbered to avoid name conflicts, since Ruby allows you to define the
same method many times in the same file (and even in the same class)
but they will need different JVM methods to represent the body of the
code.

RUBY indicates to the backtrace-generating logic that this is a
compiled Ruby method that should be included in a Ruby backtrace.

You will also see that the method names get mangled as in the following
example:

def +@(a); a + 1; end

method__0$RUBY$plus_40

The method name used in the compiled JVM method is “plus” and 40, the
character code for @.

  • Charlie

Thanks to both for replying with such useful posts!
I’m in the process of trying to determine how difficult or not it is to
use bytecode manipulation libraries (such as ASM or JavaAssist) to weave
some standard instrumentation code (compiled from java) into JRuby class
files at particular points (method entry or prior to method calls).
Understanding the method signatures is the first step for me to start
this analysis.

Ruth

Keep me posted and feel free to email [email protected] if you
want to talk about ways to make it easier to weave JRuby’s generated
bytecode. I’m interested in making the bytecode and class/method
structures we generate easier for tools to grok.

  • Charlie