JVMScript

In honor of the new project-hosting site kenai.com, I’ve spun off a
library I’ve been working on as part of JRuby. I’m hoping to find
interested parties to help improve it and test it (I have tests, but
testing a library like this is a little tricky).

http://kenai.com/projects/jvmscript

JVMScript is, basically, a Ruby DSL for emitting JVM bytecode. It’s
based on the ASM bytecode-gen library, but the bottom line is that it
looks and feels like a limited structured assembly language for the JVM.
You have the entire power of Ruby available to you, and calling an
opcode is essentially the same as emitting that bytecode. Here’s a
snippit:

builder = Compiler::FileBuilder.build(“somefile.source”) do
public_class “MyClass”, object do
public_field “list”, ArrayList

 public_constructor string, ArrayList do
   aload 0
   invokespecial object, "<init>", [void]
   aload 0
   aload 1
   aload 2
   invokevirtual this, "bar", [ArrayList, string, ArrayList]
   aload 0
   swap
   putfield this, "list", ArrayList
   returnvoid
 end

 public_static_method "foo", this, string do
   new this
   dup
   aload 0
   new ArrayList
   dup
   invokespecial ArrayList, "<init>", [void]
   invokespecial this, "<init>", [void, string, ArrayList]
   areturn
 end

 public_method "bar", ArrayList, string, ArrayList do
   aload 1
   invokevirtual(string, "toLowerCase", string)
   aload 2
   swap
   invokevirtual(ArrayList, "add", [boolean, object])
   aload 2
   areturn
 end

 public_method("getList", ArrayList) do
   aload 0
   getfield this, "list", ArrayList
   areturn
 end

 public_static_method("main", void, string[]) do
   aload 0
   ldc_int 0
   aaload
   invokestatic this, "foo", [this, string]
   invokevirtual this, "getList", ArrayList
   aprintln
   returnvoid
 end

end
end

then call builder.generate to dump the bytecode

The general syntax is still up for debate (it would be nice to eliminate
those underscores) but the library itself is already being used in my
pet language project Duby.

Interested parties, contributions, patches, and bug reports are welcome.
Check test/test_builder.rb for examples. I’m hoping to release this as a
Ruby gem soon.

Have fun…feel free to join the lists and let me know what you think.

  • Charlie

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs