Wednesday, June 29, 2005

PHASE 1: Discovery (1 - 2 weeks)

Study the implementation and design of existing code, this should involve contacting the Inline::Java author. In addition, continue to become familiar with all tools involved including, gijh, SWIG, and GCJ which will include example programs and modules illustrating functionality. Lastly, establish a project space on sourceforge.net for tracking of the project. At the end of this phase the author will have a clear understanding of how the Inline::Java module works and where he should concentrate his development efforts. A deliverale for this phase will be a document/set of documents detailing design considerations, limitations and future steps.

===========================

I am officially starting phase one of this project. Over the last three days I've been knee deep in the details surrounding SWIG and GCJ and I've already gotten a good handle on using these tools. In fact, I've been able to make some progress using these tools to create example programs. I'm still waiting to hear from a contact from at the Perl Foundation so I'm going to hold off on publishing code until I find out how they want me to handle it; In the mean time, I guess I can explain what I've been able to produce so far:

I started this whole thing reading the GCJ documentaion, since this will be the major point of integration for this project, I figured it was the best place to start. After a through reading of the documenation I got to the good stuff and started coding. The first thing I did was to just use the CNI interface made available from libgcj and become familiar with it. This pretty much just involved writing a simple Java class as well as a C++ class to call it. The GCJ tools really made this easy; all I had to do was class compile my Java class (with gcj -C) then run gcjh on it to create a C++ class header file. I then wrote my C++ class to use the generated definition, compiled it, linked the two and viola! It worked quite nice. I haven't really studied parameter passing and exception handling in any greate detail yet so this may turn out to be more of a pain that I am led to believe, we'll see.

The next test I did involved getting SWIG into the fray and get to calling the natively compiled Java from Perl. I basically followed the above steps but went one step further by running SWIG on the generated C++ header file to create a C++ XS wrapper and a Perl module. This didn't exactly turn out how I planned; when I ran the perl test driver I was greeted by the ever wonderful "Segmentation Fault". At this point it was obvious I had dome something very very wrong. Well, it turns out that I didn't RTFM well enough and forgot that I needed to init the JVM through libgcj's CNI interface (JvCreateJavaVM, JvAttachCurrentThread) before I can call a natively compiled Java Class, oops. This is obviously a very cirtical step that I've overlooked and led me to try the next thing.

After I remembered that I needed to call the CNI JVM init code first I decided to write a C++ class to act as a proxy between the natively compiled Java and the Perl code. So the layers at this point consisted of Perl -> SWIG generated C++ -> C++ proxy class -> natively compiled Java. If this seems like a lot of layers to you, well I feel the same way, I'm working on it. Anyway, this let me call the JVM init code in the C++ proxy class directly instead of having to write a Perl compatibility layer. In practive this was convenient and well, a cop-out, inexcusable. In the end it all worked; still no exception handling or argument passing though.

Lastly, as I was looking though the GCJ documentation I saw a passage that explained that I could mix natively and class comiled Java code all in the same program (through the -findirect-dispatch compiler flag and a properly set CLASSPATH). So, I followed the above procedure but added one more layer so it was as follows: Perl -> SWIG generated C++ -> C++ proxy class -> natively compiled Java -> Class compiled Java. I hope you can appreciate how cool this is. What's even more amazing is how easy it was to do. This will help big time when I get to designing and writing my final module.

In addition to working on the above examples and tests I started refreshing myself with the Perl Package model, specifically the AUTOLOAD functionality. It's really not that hard, I just haven't used it in awhile so I wrote myself a rather indepth example program. It turns out that this example program is probably going to be the foundation for my final module.

Speaking of final product, I've decided to call my new Perl module 'Java::Import'. However, for lack of a cool code name for this project it'll continue to be called "Open Sourcing Java/Perl Integration", I'm working on another name. The reason for 'Java::Import' should be pretty evident by the following code example:

use Java::Import qw {
java.lang.StringBuffer
java.lang.Exception
};

eval {
my $sb = new java::lang::StringBuffer('buffer');
$sb.append('string');
};

if ( $@ ) {
...
}

Finally, the design for how I'm going to make the above example work is starting to come together in my head but that's for another post.

0 Comments:

Post a Comment

<< Home