I need to provide some better examples, but the missing bit of code is 
generating the class files after you've done the building process:

builder.generate do |filename, class_builder|
   File.open(filename, 'w') {|f| f.write(class_builder.generate)}
end

The generate method on the main builder passes the .class filename and 
the ClassBuilder for each class. ClassBuilder#generate produces the 
bytes for the class.

Note that I've never released JVMScript officially, but I want to; if 
you start playing around with it, keep in mind the following:

1. I'm interested in input and willing to make changes before doing a 
release
2. The Compiler namespace will probably become JVMScript

So a shorter, more complete example:

~/projects/duby ➔ cat simple.rb
require 'jvmscript'

builder = Compiler::FileBuilder.build('simple.source') do
   public_class 'simple', object do
     public_static_method 'main', void, string[] do
       aload 0
       ldc_int 0
       aaload
       aprintln
     end
   end
end

builder.generate do |file, cls|
   File.open(file, 'w') {|f| f.write(cls.generate)}
end

~/projects/duby ➔ jruby -I ../jvmscript/lib simple.rb

~/projects/duby ➔ javap -c simple
Compiled from "simple.source"
public class simple extends java.lang.Object{
public static void main(java.lang.String[]);
   Code:
    0:  aload_0
    1:  ldc     #8; //int 0
    3:  aaload
    4:  dup
    5:  getstatic       #14; //Field java/lang/System.out:Ljava/io/PrintStream;
    8:  swap
    9:  invokevirtual   #20; //Method 
java/io/PrintStream.println:(Ljava/lang/Object;)V

}

- Charlie

René Jansen wrote:
> Hi Charlie,
> 
> thanks for sharing this, it looks most useful. I downloaded and ran it; 
> the testcases run ok. I cannot figure out how to make it write 
> MyClass.class; please forgive me my Ruby-ignorance here. I removed the 
> things I though to be for the testcase only, and have it execute outside 
> of a method. It executes ok, but Erik Meijer would be very happy: there 
> are no side-effects. That I know of.
> 
> best regards and thanks in advance.
> 
> René.
> 
> On Fri, Oct 10, 2008 at 7:58 PM, Charles Oliver Nutter 
> <[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>> wrote:
> 
> 
>     René Jansen wrote:
>      > Just a quick question: which assembler are you guys using for
>      > experimentation? I find myself wanting to try out some things,
>     but I am
>      > in a bind between ooLong and Jasmin or maybe ASM. Is there an
>     unofficial
>      > official assembler, preferably something with macros (old s/370 bal
>      > programmer that I am) and its own disassembler.
>      >
>      > I noticed there is a class called Assembler in the tools sources, is
>      > this only usable from javac or is it addressable in any other way?
>      >
>      > If no disassembler goes with the package, I would prefer
>     something that
>      > is close enough to javap that I can write a small thingy to rearrange
>      > its output into something that I can assemble - something that
>     ASM can
>      > but in a very verbose way.
>      >
>      > Please let me know what you use.
> 
>     I'm using ASM exclusively, but usually wrapped behind one of my own
>     shims.
> 
>     Either this super-trivial shim that just exposes a method-per-opcode:
> 
>     http://is.gd/3RsP
> 
>     Or my unreleased Ruby DSL for ASM, "JVMScript":
> 
>     http://kenai.com/projects/jvmscript
> 
>     The latter is probably more interesting to you, but obviously has a
>     dependency on JRuby. Perhaps that's not a problem for your toolchain.
> 
>     Here's a short sample:
> 
>         builder = Compiler::FileBuilder.build("somefile.source") do
>           package "org.awesome", "stuff" 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
>         end
> 
>     - Charlie
> 
> 
> 
> 
> 
> > 



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "JVM 
Languages" group.
To post to this group, send email to jvm-languages@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/jvm-languages?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to