Java to JavaScript Compiler Discussion (1)

Last year, I joined in a discussion about Java to JavaScript compiler in Google Web Toolkit (GWT) group. I think it is worthy to re-post my opinions here for a better understanding of Java2Script. Or later I may use the discussion to clarify my opinions.

---------------------------

"To be as small and as efficient as possible"!? I admire GWT team for their excellent 100k ~ 200k final *.js files with very good performance.

Java2Script (http://j2s.sourceforge.net/), as mentioned,? is a similar project for Java to JavaScript code generation. But the motivation in Java2Script is something different: "To provide same familiar Java APIs in JavaScript".

I am just a lazy developer, lazy to learn new APIs for some same functions. For example, Swing and SWT are similar GUI toolkit. And using SWT can do all the things that Swing can do and vice versa. I would be lazy to learn one of them only and not to learn the other. And I prefer to SWT. And as now I am familiar with SWT, and if all things done by using GWT can be done by using SWT, I would be lazy not to learn the new GWT APIs but use my familiar SWT. So this lazy programmer will use Java2Script and its Java2Script SWT library.

In order to provide the same familiar Java APIs in JavaScript, Java2Script has to keep all fields, all methods, all inheritances and all polymorphic information of a class in the JavaScript codes so developer will always be happy and feel comfortable with those familiar APIs. Java2Script is actually providing compiler that compiles Java sources into static JavaScript libraries.

And following, I will just try to figure out my understanding of GWT's Java to JavaScript compiling procedure. If I am wrong, please be kind to correct me.

GWT's compiling procedure is somewhat similar to the procedure of compiling C sources into an executable file. In C compiling, C source is first compiled into an *.obj.? And then comes a linker, which will link *.obj with other *.lib files into an *.exe. In the linking procedure, not all method calls in those *.lib are linked into *.exe file. Only those which are used in the *.obj will be kept. Others may be discarded so the final *.exe is small and efficient. In order to find out which methods must be kept in linking, some recursive searching algorithm may be required.

In GWT compiling, there are no middle *.obj actually. GWT compiler tracks down those method calls which are related to the application. And if the other methods have no relationship with the application, they will be discarded even they are marked "public". So the final *.js is very small and efficient. Those class meta information discarded may include class inheritances, overriding, polymorphisms and other OO information. So after compiling, there are actually not OO JavaScript. So OO concepts like "this" keyword or reflection calls can not be used. In the whole compiling, the key technology of GWT may be the algorithm to flat those super calls and polymorphic method calls into static calls correctly.

And I admire GWT team for their excellent 100k ~ 200k final *.js files with very good performance.

Even basing on the same JDT toolkit, compilers with different motivations result in different JavaScripts. In a comparison, Java2Script generates 500k+ *.js files, and? has its bottleneck on long waiting on loading those bundles of huge *.js files and poor performance on spending most of its CPU time in searching for polymorphic method calls. In fact, Java2Script has many optimizations on reducing the generated *.js file size and improving JavaScript performance, like minimizing variable name identities, generating smart scripts to avoid polymorphic method calls and others. But it seems that Java2Script is still far behind GWT in file size and performance.

But GWT, as the above compiling process described, may have its difficulties in supporting Java refection and dynamical class loading. But Java2Script already has its ClassLoader and is loading classes lazily. And it also has its early Java reflection implementation on JUnit tests (Reusing JUnit tests directly). But the way, GWT APIs and Java2Script SWT APIs is far different for comparison.

In some simple words, GWT compiler is an excellent Java to JavaScript *runtime compiler*, while Java2Script is a Java to JavaScript *library compiler*.

Maybe GWT would be kind to support Java reflection and dynamic class loading, if more developers vote for such features.

This entry was posted in Architecture. Bookmark the permalink.

17 Responses to Java to JavaScript Compiler Discussion (1)

  1. Pingback: Inside Java2Script » Blog Archive » Performance Matters or Not

  2. Hello, I found this article while searching for help with JavaScript. I've recently changed browsers from Chrome to Microsoft IE 6. After the change I seem to have a problem with loading JavaScript. Every time I go on a site that needs Javascript, my computer doesn't load and I get a "runtime error javascript.JSException: Unknown name". I can't seem to find out how to fix the problem. Any help is greatly appreciated! Thanks

  3. Zhou Renjian says:

    @Wilfredo Ming
    You should ask the question on some groups talking JavaScript problems.

  4. I additionally added your own twitter page to my personal myspace web Thanks Rob Rasner Magic Castle

  5. I needed to write down a brief be aware in buy to thanks for all the fantastic suggestions you might be placing at this website. My prolonged internet lookup has at the finish of the day been recognized with trustworthy details and techniques to trade with my co-workers. I would assert that plenty of of us readers in reality are definitely lucky to live in a important location with extremely a handful of great individuals with insightful pointers.

  6. katalog says:

    After examine just a few of the weblog posts on your website now, and I truly like your means of blogging. I bookmarked it to my bookmark web site list and can be checking again soon. Pls try my site as effectively and let me know what you think.

  7. I needed to write you a very small word in order to give thanks over again for your personal pleasant things you've shown above. It was so unbelievably generous of you to give unreservedly what exactly a few people could have sold as an electronic book in order to make some bucks on their own, even more so considering that you might well have tried it in the event you wanted. The advice as well acted like a fantastic way to recognize that other individuals have similar zeal really like my very own to realize whole lot more when it comes to this matter. I believe there are several more fun situations up front for folks who look over your site.

  8. Pingback: Gold in 2012

  9. Pingback: Gutter Installation Austin

  10. Interesting article. Were do you got everything from.

  11. [Trackback] Java to JavaScript Compiler Discussion (1) | Inside Java2Script http://www.blogabond.com/CommentView.aspx?CommentID=387457 [Trackback]

  12. ... http://www.blogabond.com/CommentView.aspx?CommentID=387457 Java to JavaScript Compiler Discussion (1) | Inside Java2Script ...

  13. Chu Roehler says:

    There are certainly a number of particulars like that to take into consideration. That is a nice level to deliver up. I provide the thoughts above as common inspiration but clearly there are questions like the one you convey up where crucial thing will likely be working in sincere good faith. I don?t know if best practices have emerged round issues like that, however I am sure that your job is clearly recognized as a fair game. Each boys and girls really feel the influence of only a second’s pleasure, for the remainder of their lives.how to burn belly fat for men

  14. incudgina says:

    outpersonals.com Outpersonals.com The largest Gay singles Network!

  15. vimax says:

    Apenas onde é que estes bloggers trolling obter este material?
    Brastodwhy.2011
    vimax

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>