Java 6 内置版本的 Rhino 和直接来自 Mozilla 的 Rhino 包有什么区别?

发布于 2024-10-11 13:21:21 字数 71 浏览 8 评论 0原文

我知道 API 非常不同,但是内置 JavaScript 内容和可从 Mozilla 获得的 Rhino 版本之间有功能差异吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

給妳壹絲溫柔 2024-10-18 13:21:21

我不确定你所说的 API 有何不同。 Java 6 有一个脚本引擎,其中可用的引擎之一是 Rhino,用“js”表示。因此,捆绑的 Mozilla Rhino ECMAScript 与您从其网站上获得的 ECMAScript 之间的唯一区别是版本之间的差异。我相信 Mozilla Rhino ECMAScript 的捆绑版本是 1.6 rev2。

这与 XML 库的工作方式类似。有一个具有默认实现的“引擎”。

客户端使用示例

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

更新

只是为了进一步回答您的问题,是的,Java 6 脚本引擎会处理您需要的上下文和其他设置操作如果直接使用Rhino则必须手动完成。这是使用两者的示例。请记住,当您使用 Java6 脚本引擎时,类似的事情也会在幕后发生。这里使用的 ScriptingEngine 不必由 Rhino 支持。它可以有一个自定义脚本实现。

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

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();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文