views:

319

answers:

3

I understand the purpose of GWT, but I'm wondering if I can use it to compile a few functions from Java to JavaScript just to make sure that I don't have to maintain the same code in two different languages.

Or would GWT bring along too much library/support overhead along to make this a reasonable choice?

For future projects, I have the same question about Script# (the C# compiler).


Followup:

Script# seems to produce very readable JavaScript from C# (pages 35-51 have some examples of C# code and the generated JS code).

I found out that there is a -STYLE flag to make the GWT output "pretty" or even "detailed." I still don't know if the emitted JS relies on large libraries or if there are other "gotchas" involved.

A: 

GWT is not a general-purpose Java-to-JavaScript converter, though it converts a useful part of the JRE for web applications (and of course the GWT widgets). It's not really worth using for a "few" functions just to avoid coding in JavaScript. Of course if you are building a complex UI which consequently involves complex JavaScript coding, then GWT really shines. If you write Java methods to help implement the logic of your GWT event handling, then these will get converted to JavaScript functions along with everything else. However the main benefit of GWT is how easy and natural it is to debug your UI in hosted mode - that's the main productivity gain (as well as the familiarity of the widget API for desktop application developers).

Vinay Sajip
That doesn't really answer my question. I know what GWT is for. What I want to know is what happens if I compile two or three moderately large functions that are heavy on logic and floating point math (in my case). I already have these in JavaScript, but I need them on the server as well, and I don't want to maintain the separately. So when GWT compiles a function to JavaScript, what's the drawback? Does it bring along a large library? If I have a Java function that is, say, 40 lines of logic (not interface stuff), what do I get on the JavaScript side after the compile?
Nosredna
A: 

If you've already got the functions implemented in javascript, would something like Rhino make sense to execute the Javascript code inside the JVM on the server-side?

Steve Armstrong
A good idea. I'd still like to know what Java functions look like when compiled with GWT. I guess I'll just try it.
Nosredna
+2  A: 

Yes, you can do just that. Here's the way to invoke it from Javascript (Source):

How can I call one of my GWT Java methods from my application host page?

In order to accomplish this, you'll first need to create a JSNI method that creates a JavaScript method that in turn makes the call to your Java method. In your GWT application's onModuleLoad(), you would call that JSNI method so that the JavaScript method is defined. From your application host page you would then call the created JavaScript method.

Confused yet? It's actually quite simple.

The code snippet below shows an example of this (courtesy of Robert Hanson):

private native void initPlaylistJS  (PlaylistTable pl) /*-{   
   $wnd.addClipToPlaylist = function (clipId, clipTitle) {
        [email protected]::addClip(Ljava/lang/String;Ljava/lang/String;)(clipId, clipTitle);
    };
}-*/;

In this example, you would need to make a call to initPlaylistJS(pl) in your GWT module's onModuleLoad(). Once your GWT application loads, the JavaScript method is defined and is callable from outside of the GWT application.

As for the 'baggage', GWT compiles a single monolithic file, so you don't need to include anything else.


One extra thing to note is that in my experience GWT is not perfectly suited for sharing code between the server and the client, since the server part needs to become GWT-compilable, that is only include classes which are part of the emulated JRE or for which you have the source available for compilation.

Robert Munteanu
But if the code is logic/floating point heavy, chances are its using only the subset of the JRE, and thus more likely to be compatible.
Chii
Yes, that's true. Many times it does work, but not always.
Robert Munteanu