måndag, juli 16, 2007

The results of JRuby compilation

If you are interested in what actually happens when JRuby compiles Ruby to Java bytecode, I have added some small utilities to help out with this. To compile a string:
require 'jruby'
JRuby::compile("puts 1,2,3")
If you are running with -J-Djruby.jit.enabled=false, you can also inspect the result of compiling a block:
require 'jruby'

JRuby::compile do
puts "Hello World"
end
The results of both of these invocations will be an object of type JRuby::CompiledScript. It has four attributes: name, class_name, original_script and code. The original_script attribute is only available when compiling from a string. The code attribute contains a Java byte array, and as such is not so useful in itself. But you can use the inspect_bytecode method to get a string which describes the compiled class. So, to see how JRuby compiles a puts "Hello, World":
require 'jruby'

puts JRuby::compile(<<CODE).inspect_bytecode
puts "Hello, World"
CODE
Once you know what happens, you can start contributing to the compiler! =)

Inga kommentarer: