简体   繁体   中英

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

我知道API非常不同,但是内置的JavaScript东西和Mozilla可以获得的Rhino构建之间是否有任何功能差异?

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();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM