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.
Pingback: Inside Java2Script » Blog Archive » Performance Matters or Not
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
@Wilfredo Ming
You should ask the question on some groups talking JavaScript problems.