简体   繁体   English

Java 6的Rhino内置版本和Mozilla直接使用的Rhino软件包有什么区别?

[英]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. 我不确定API的含义是不同的。 Java 6 has a scripting engine in which one of the available engines is Rhino denoted by "js". Java 6有一个脚本引擎,其中一个可用的引擎是由“js”表示的Rhino。 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. 因此,捆绑的Mozilla Rhino ECMAScript与您可以从他们的网站获得的唯一区别将是版本之间的差异。 I believe the bundled version of Mozilla Rhino ECMAScript is 1.6 rev2. 我相信Mozilla Rhino ECMAScript的捆绑版本是1.6 rev2。

This is similar to the way the XML libraries work. 这类似于XML库的工作方式。 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. 只是回答一下你的问题, 是的 ,如果直接使用Rhino ,Java 6脚本引擎会处理你必须手动完成的上下文和其他设置操作 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. 请记住,当您使用Java6脚本引擎时,类似的事情发生在幕后。 The ScriptingEngine used here DOES NOT have to be backed by Rhino. 这里使用的ScriptingEngine不必由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();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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