BiteScript 0.0.1 – A Ruby DSL for JVM Bytecode

how-to
Mar 28, 20092 mins

I have finally released the first version of BiteScript, my little DSL for generating JVM bytecode. Install it as a gem with “gem install bitescript”.

require 'bitescript'

include BiteScript

fb = FileBuilder.build(__FILE__) do
public_class "SimpleLoop" do
 public_static_method "main", void, string[] do
   aload 0
   push_int 0
   aaload
   label :top
   dup
   aprintln
   goto :top
   returnvoid
 end
end
end

fb.generate do |filename, class_builder|
File.open(filename, 'w') do |file|
 file.write(class_builder.generate)
end
end

BiteScript grew out of my work on Duby. I did not want to call directly into a Java bytecode API like ASM, so I wrapped it with a nice Ruby-like layer. I also wanted the option of having blocks of bytecode look like raw assembly, but also callable as an API.

Currently only two projects I know of make use of BiteScript: Duby and the upcoming Ruby-to-Java “compiler2” in JRuby, which will also be released as a gem.

For a longer example, you can look at tool/compiler2.rb in JRuby, lib/duby/jvm/jvm_compiler.rb in Duby, or an example implementation of Fibonacci in BiteScript.

I’m open to suggestions for how to improve the API, and I’d also like to add the missing Java 5 features. The better BiteScript works, the better Duby and “compiler2” will work.

For folks interested in using BiteScript, the JVM Specification is an easy-to-read complete reference for targeting the JVM, and here is my favorite JVM opcode quickref.