Nosredna
Nosredna

Reputation: 86296

Can I use GWT (or Script#) to write individual functions in Java (or C#) and compile them to JavaScript?

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.

Upvotes: 4

Views: 649

Answers (3)

Robert Munteanu
Robert Munteanu

Reputation: 68308

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.

Upvotes: 2

Steve Armstrong
Steve Armstrong

Reputation: 5402

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?

Upvotes: 0

Vinay Sajip
Vinay Sajip

Reputation: 99445

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).

Upvotes: 0

Related Questions