Skip to content
headius edited this page Dec 28, 2012 · 4 revisions

The RubyFlux Compiler

The RubyFlux compiler generate Java sources from Ruby sources without any need for a separate runtime library. It also avoids the need for dynamic dispatching frameworks and features (like invokedynamic). This page describes details of how it does that.

Reading in sources

Sources are parsed by JRuby's compiler into AST form. This form provides the general structure of all files to be compiled, along with classes and methods they contain.

Walking the ASTs

Each of the ASTs are walked, and information on the classes and methods being defined is gathered in memory. The scripts, classes, and methods are then used to generate Java sources.

Generating Java source

We use the Java Development Tools API (JDT) from the Eclipse project to emit valid Java sources. JDT provides a Java AST form suitable for building, transforming, and emitting Java source. It is much easier to use this API instead of emitting raw strings, since JDT will guarantee that a valid AST will produce valid, clean, formatted Java code.

Avoiding Dynamic Dispatch

Dynamic dispatch is avoided by the following process:

  • The compiler gathers the names of all methods defined or called in the system.
  • As part of the generation phase, the "Object" class (RObject.java) is generated with stub implementations of all these methods.
  • The stub implementations normally raise an error, indicating the target object does not define that method.
  • Classes that define those methods simply override them and leave the others unimplemented, allowing what seems like "dynamic" dispatch to simply be Java virtual method dispatch.

As a result, all calls in the system are direct Java method invocations, and the JVM handles dynamically wiring up the appropriate overridden method implementation.