[英]What's the difference between Java 6's built-in version of Rhino and the Rhino package direct from Mozilla?
我知道API非常不同,但是內置的JavaScript東西和Mozilla可以獲得的Rhino構建之間是否有任何功能差異?
我不確定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 |
============= =============
更新
只是回答一下你的問題, 是的 ,如果直接使用Rhino ,Java 6腳本引擎會處理你必須手動完成的上下文和其他設置操作 。 這是使用這兩者的一個例子。 請記住,當您使用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();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.