我知道API非常不同,但是内置的JavaScript内容与Mozilla可以获得的Rhino构建之间是否有任何功能差异?
答案 0 :(得分:17)
我不确定API的含义是不同的。 Java 6有一个脚本引擎,其中一个可用的引擎是由“js”表示的Rhino。因此,捆绑的Mozilla Rhino 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();
}
}