All: there's a new feature in Java SE 6 Update 10 which might be of 
interest to subscribers of this list.

If you host a Java applet on a web page, the JavaScript engine can 
operate upon it: call methods, fetch fields, operate upon other objects 
returned from previous method invocations, etc.

We wanted to enable language developers and users on the Java platform 
such as yourselves to write applets in non-Java languages like JRuby, 
Jython, Groovy, Scala, Fan, Clojure, JavaFX Script, etc., and still be 
able to script these applets from the JavaScript engine in the web browser.

Java SE 6 Update 10 exposes some new interfaces from the Java Plug-In's 
LiveConnect implementation that allows a non-Java language runtime to 
hook in to the dispatch sequence for JavaScript operations coming in 
from the web browser.

This lets you, the language developer, use your language to write 
applets, and call into those applets from the web page. Essentially, you 
define a mapping from your language to JavaScript syntax.

To give the basic idea, here's an example written in JavaFX Script. The 
.fx file looks like this:

     var color = Color.YELLOW;

     public function setColor(red: Number,
                              green: Number,
                              blue: Number) : Void {
         color = Color { red: red, green: green, blue: blue };
     }

     public function run(args: String[]) {
         Stage {
             scene: Scene {
                 fill: Color.DARKGRAY
                 content: [
                     Circle {
                         centerX : 125
                         centerY : 125
                         radius: 100
                         fill: bind color
                     }
                 ]
             }
         }
     }

and we can operate upon it from the web page using JavaScript functions 
like these (assume it is hosted in an applet whose ID is "app"):

     function makeRed() {
         try {
             app.script.setColor(1.0, 0.0, 0.0);
         } catch (e) {
             reportException(e);
         }
     }

     function makeBlue() {
         try {
             app.script.setColor(0.0, 0.0, 1.0);
         } catch (e) {
             reportException(e);
         }
     }

Note the use of the synthetic "script" field attached to the applet, 
which provides access to the script-level scope. This field is 
synthesized by the JavaFX runtime using the new APIs in the Java Plug-In.

More complex examples are possible. Here's JavaScript which dives into a 
scene graph (similar structure to the one above) and modifies the color 
directly:

     function makeRed() {
         try {
             app.stage.scene.content[0].fill =
                 app.Packages.javafx.scene.paint.Color.RED;
         } catch (e) {
             reportException(e);
         }
     }

     function makeBlue() {
         try {
             app.stage.scene.content[0].fill =
                 app.Packages.javafx.scene.paint.Color.BLUE;
         } catch (e) {
             reportException(e);
         }
     }

This example uses the built-in per-applet Packages keyword to access the 
javafx.scene.paint namespace.

Complex data type conversions are possible. The JavaFX runtime uses the 
new APIs to define conversion operations between JavaScript arrays and 
JavaFX sequences. Here's a JavaFX function from another example which 
takes in two sequences of numbers and makes a Timeline animation out of 
them:

     public function animate(times: Number[],
                             xyCoords: Number[]) {
         var t = Timeline {
             keyFrames: for (i in [0..(sizeof times - 1)]) {
                 KeyFrame {
                     time: Duration.valueOf(times[i]);
                     values: [
                         x => xyCoords[2*i],
                         y => xyCoords[2*i+1],
                     ]
                 }
             }
         }

         if (timeline != null) {
             timeline.stop();
         }

         timeline = t;
         timeline.play();
     }

The function above can be called from JavaScript as follows:

     function clockwise() {
         try {
             app.script.animate([250, 500, 750, 1000],
                                [225, 225,
                                 25, 225,
                                 25, 25,
                                 225, 25]);
         } catch (e) {
             reportException(e);
         }
     }

     function counterclockwise() {
         try {
             app.script.animate([250, 500, 750, 1000],
                                [25, 25,
                                 25, 225,
                                 225, 225,
                                 225, 25]);
         } catch (e) {
             reportException(e);
         }
     }

We think these new APIs offer a lot of interesting interoperability 
possibilities for dynamic language implementors on the JVM. They tie 
into the meta-object protocol work being done by Attila Szegedi and 
others, and while they aren't currently related, it would be interesting 
to see if unification is possible.

The documentation (including javadoc) for the new APIs is available here:

   https://jdk6.dev.java.net/plugin2/liveconnect/#NON_JAVA_LIVECONNECT

but of course the most useful thing is example code. The first and 
proof-of-concept implementation of these interfaces has been done for 
JavaFX Script. Roughly 90% of the code is in the open-source repository 
for the JavaFX compiler at https://openjfx-compiler.dev.java.net/, under 
the directory src/share/classes/com/sun/javafx/runtime/liveconnect/ . 
The only pieces missing are those which instantiate and register the 
delegates; these necessarily went into a different (currently non-open) 
repository but are pretty straightforward.

If you have any feedback on this new functionality please post it to the 
list. We're hoping this opens up new interesting possibilities for web 
application development on the JVM.

Thanks,

-Ken


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "JVM 
Languages" group.
To post to this group, send email to jvm-languages@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/jvm-languages?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to