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?
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();
}
}
© 2022 - 2024 — McMap. All rights reserved.