What's the difference between Java 6's built-in version of Rhino and the Rhino package direct from Mozilla?
Asked Answered
M

1

12

I know the APIs are very different, but is there any functional difference between the built-in JavaScript stuff and the Rhino builds obtainable from Mozilla?

Mohr answered 9/1, 2011 at 15:9 Comment(0)
I
17

I'm not sure what you meant by the API's are different. Java 6 has a scripting engine in which one of the available engines is Rhino denoted by "js". So the only difference between the bundled Mozilla Rhino ECMAScript and the one you can get from their website will be the differences between the versions. I believe the bundled version of Mozilla Rhino ECMAScript is 1.6 rev2.

This is similar to the way the XML libraries work. There is a "engine" that has a default implementation.

Example Client Usage

                       ==========
                       | Client |
                       ==========   
                           |
             ===============================
             |                             |
 =========================           =============
 | Java Scripting Engine |           | Rhino API |
 =========================           =============
             |
      ==================
      |                |
 =============   =============    
 | Rhino API |   | Other API |
 =============   =============

Update

Just to answer your question a little more, yes the Java 6 Scripting Engine takes care of contexts and other setup operations that you have to do manually if using Rhino directly. Here is an example of using the two. Keep in mind that when you use the Java6 Scripting Engine, similar things are happening underneath the hood. The ScriptingEngine used here DOES NOT have to be backed by Rhino. It could have a custom scriping implementation.

public class Main {

    static class Shell extends ScriptableObject {

        @Override
        public String getClassName() {
            return "global";
        }

        public static void print(Context cx, Scriptable thisObj, Object[] args, Function funObj) {
            for (int i = 0; i < args.length; i++) {
                String s = Context.toString(args[i]);
                System.out.print(s);
            }
        }
    }

    public static void useJava6ScriptingEngine() throws Exception {
        ScriptEngineManager mgr = new ScriptEngineManager();
        ScriptEngine jsEngine = mgr.getEngineByName("JavaScript");
        jsEngine.eval("print('Hello, world!')");
    }

    public static void useRhinoDirectly() throws Exception {
        Context context = Context.enter();
        try {
            Shell shell = new Shell();
            String[] names = {"print"};
            shell.defineFunctionProperties(names, Shell.class, ScriptableObject.DONTENUM);
            Scriptable scope = context.initStandardObjects(shell);
            context.evaluateString(scope, "print('Hello, world!')", null, 0, null);
        } finally {
            Context.exit();
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws Exception {
        useJava6ScriptingEngine();
        useRhinoDirectly();
    }
}
Infrasonic answered 9/1, 2011 at 15:37 Comment(2)
The difference in the APIs that I'm referring to is that the Rhino package from Mozilla uses a lot of explicit handling of Contexts and Scopes, but from all of the examples that I've seen of the built-in implementation in Java 6 it appears as though those details are maybe swept into the background so that you don't have to worry about them?Mohr
@Jeremy From what I can tell I wouldn't consider them swept under the rug. You will need to learn how the ScriptEngine in Java 6 works. Like I was saying Java 6 introduces a Scripting Engine framework. The Mozilla Rhino ECMAScript has an implementation of that Scripting Engine. This means Mozilla Rhino ECMAScript plays within the context of the Java 6 scripting framework. You have two choices, use Rhino API directly, or learn the new Java 6 Scripting engine API.Infrasonic

© 2022 - 2024 — McMap. All rights reserved.