JSR-223 script engine for browser's JS interpreter?

You may have used Java-to-JavaScript communication in the context of the Java plugin. This API allows a Java applet to call/use JavaScript interpreter of the web browser.

When you hammer, everything looks like a nail :-) I've implemented JSR-223 script engine by wrapping netscape.javascript API. As usual, it is available at https://scripting.dev.java.net

This script engine uses the JavaScript interpreter embedded in web browsers. Unlike the other script engines, this script engine is not expected to work on all settings. This engine works only inside the browsers - which effectively means only Java applets can make use of this script engine. I call this script engine as "browserjs" engine.

Because this script engine uses LiveConnect, the applet's APPLET tag has to include mayscript="true" attribute so that browser's window object can be obtained. Also, user has to set Applet object as a variable named "applet" in the current ScriptContext.

Following is a simple Applet that uses browser's JS interpreter through javax.script API.

import java.applet.\*;
import javax.script.\*;

public class ScriptClient extends Applet {
    private ScriptEngine engine;

    public void init() {
        ClassLoader myloader = getClass().getClassLoader();
        ScriptEngineManager manager = new ScriptEngineManager(myloader);
        engine = manager.getEngineByName("BrowserJS");
        try {
            engine.put("applet", this);
            // eval alert..
            engine.eval("alert('hello world');");
            // call alert...
            ((Invocable)engine).invokeFunction("alert", "hello, world!");
        } catch (ScriptException exp) {
            throw new RuntimeException(exp);
        } catch (NoSuchMethodException nsme) {
            throw new RuntimeException(nsme);
        }
    }  
}

How does this "browserjs" JavaScript script engine compares to the bundled Rhino-based JavaScript engine in JDK 6?
  • Rhino is pure-Java and works in all contexts. Browser's built-in JS script engine is native.
  • Rhino-based JavaScript engine works in all contexts - including the server side. This "new" browserjs jsr223 engine works only inside the web browsers.
  • The above point also implies that you have to handle the differences b/w browser versions/implementations (of JavaScript). With Rhino-based implementation, you have the same implementation on all platforms/contexts.
  • With "browserjs" engine, you can easily access browser's HTML DOM objects using the browserjs engine. With Rhino based engine, you have expose those objects explicitly by ScriptContext. Even then it may not be easy to access browser's objects such as window, window.document etc.- you have to call JSObject methods.

In addition to adding this "new" script engine, I've updated scripting.dev.java.net for the following:

  • Updated TCL script engine to use Jacl version 1.1.3
  • Updated Scheme script engine to use SISC version 1.15.2
Comments:

Post a Comment:
Comments are closed for this entry.
About

sundararajan

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today
Bookmarks
Links

No bookmarks in folder

Blogroll

No bookmarks in folder

News

No bookmarks in folder