martedì 15 gennaio 2013

Rubyflux: convert Ruby source to Java JAR file

RubyFlux: a Ruby to Java compiler, by headius, currently in development. Yes, a Ruby source compiled to a Java jar "executable" file: from test.rb to test.jar --Worth a look!

Please note I'm not into the github thing --neither into Java, even if it may sound weird in 2013 A.D.

- maven 3.x (I couldn't get maven 2.x working)
- jruby
- a Java jdk

I have an Ubuntu 12.04 machine with openjdk-7 installed, so I just apt-got jruby and maven from stock repositories.

After cloning the RubyFlux git repo the mvn package command failed, so I had to add the maven-compiler-plugin goal to the pom file and call mvn package again:

Ready to go!

Create the fib.rb example and verify execution time with stock Ruby interpreter (arrrrgh: 4'06" on an AMDv140 processor!).

Convert to Java sources: I had to specify Ruby 1.9 version on the jruby command-line because Ruby 1.8.x does not have the Dir.exists? method (yeah, right):
  jruby --1.9 -I target:src/main/ruby src/main/ruby/ruby_flux.rb fib.rb

Compile to .class files and execute:
  cd build
  java fib

If you get some eerie "unsupported version 51.0" error, you have the stock Java 1.6 installed (look what is linked to /etc/alternatives/java); I just to explicitly called the 1.7:
  /usr/lib/jvm/java-7-openjdk-amd64/bin/java fib

Build the .jar archive as usual, and then run it:
  cd build
  jar -cfe fib.jar fib *.class
  java -jar fib.jar

Arrrrgh: 6.7 seconds. Nice. Remember that fib.rb is pure number-crunching, don't expect a 37× speed improvement on "anything".

Oh, yeah: it would be nice to have a rubyflux inputfile.rb script. Here is my ugly quick-hack:
  #!/bin/bash -e

  # usage: $0 inputfile.rb
  VAI=`basename $1 .rb`
  #renice 20 $$
  rm -f $DIR/$VAI.rb
  cp $1 $DIR/$VAI.rb
  cd $DIR
  rm -rf build
  jruby --1.9 -I target:src/main/ruby src/main/ruby/ruby\_flux.rb $VAI.rb
  cd build
  javac $
  jar -cfe $VAI.jar $VAI *.class
  cd "$CWD"
  mv $DIR/build/$VAI.jar .
  rm -f $DIR/$VAI.rb
  ls -al $VAI*

Before you go totally apeshit, remember that RubyFlux is still a work-in-progress. Don't expect anything more than "print" and "puts" functions and very basic operations. For example, "sleep" is not supported, so I had to write mine, adding to source:
  public RObject sleep(RFloat j) {
    try { Thread.sleep(new Double(1000.0*j.flo).longValue()); }
    catch(InterruptedException ie) { }
    return RNil;

and changing the builtins definitions in the main ruby_flux.rb (yes, there were only "puts" and "print"):
  BUILTINS = %w[puts print sleep]

Thus I was able to build this Ruby source into a Java .jar "executable" archive:
  i = 0
  p = 0.234
  puts "FORMAT C:"
  sleep 0.15
  while i < 80
    print "\rFormatting track "
    puts i
    i += 1
    p *= 0.937
    sleep p
  puts "Formatting complete. Your hard disk has more free space now"

Hey, boss. Check out this Hard Disk Maintenance. Yeah, it's a safe JAR. #LOL

Nessun commento:

Posta un commento