Also blogged here:

http://blog.headius.com/2009/03/more-compiling-ruby-to-java-types.html

Charles Oliver Nutter wrote:
I have committed another round of work on compiler2, and it now supports signatures. Here's an example:

require 'rbconfig'
require 'java'
require 'tool/signature'

class MyRubyClass
  def helloWorld
    puts "Hello from Ruby"
  end
  def goodbyeWorld(a)
    puts a
  end
  %w[boolean byte short char int long float double].each do |type|
    java_type = Java.send type
    eval "def #{type}Method(a); a; end"
    signature "#{type}Method", [java_type] => java_type
  end
  def nevermore(*a)
    a[0]
  end

  signature :helloWorld, [] => Java::void
  signature :goodbyeWorld, [java.lang.String] => Java::void
end

The signature logic here is totally up for discussion; ultimately all the compiler looks for is that the target class object (MyRubyClass in this case) responds to a "signature" method, which it uses to look up signatures. The signatures are simply a hash mapping a parameter list (as an array) to a return type.

The above example defines several methods. First there's the obvious helloWorld, goodbyeWorld and nevermore. helloWorld takes no parameters and returns void. goodbyeWorld takes a java String and returns void. nevermore does not have a signature definition, so it's all IRubyObject on the way in and out.

More interesting are the *Method methods. The class definition iterates over all Java primitive types, creating a method for each that accepts and returns that type. And because compiler2 inspects *runtime* types, it sees all these methods. Here's the command line again to compile this:

jruby -I ../bitescript/lib/ tool/compiler2.rb MyObject MyRubyClass myruby

And the resulting Java class file:

Compiled from "MyObject.java.rb"
public class MyObject extends org.jruby.RubyObject{
    static {};
    public MyObject();
    public double doubleMethod(double);
    public int intMethod(int);
    public void helloWorld();
    public char charMethod(char);
    public long longMethod(long);
    public byte byteMethod(byte);
    public short shortMethod(short);
    public boolean booleanMethod(boolean);
public org.jruby.runtime.builtin.IRubyObject nevermore(org.jruby.runtime.builtin.IRubyObject[]);
    public float floatMethod(float);
    public void goodbyeWorld(java.lang.String);
}

In the non-signature case, nevermore, you see that it just accepts IRubyObject[] and returns IRubyObject, so IRubyObject arguments will just pass in and out without any coercion. In all the other cases, we perform Java integration-style coercion on the way in and out.

There's more to come, but what we have right now is already checked into trunk. Please, play with it!

- Charlie

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email




---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email


Reply via email to