为 Nashorn 脚本引擎定义一个 default/global Java 对象?
Defining a default/global Java object to Nashorn script engine?
借助 Java 的 Nashorn 脚本引擎,我可以使用如下绑定在 eval() 的上下文中提供对象:
Bindings bindings = scriptContext.getBindings(ScriptContext.ENGINE_SCOPE);
bindings.put("rules", myObj);
scriptEngine.eval("rules.someMethod('Hello')", scriptContext);
我希望能够通过提供默认对象来简化 javascript,这样 javascript 就不会变成:
rules.someMethod('Hello')
我可以写:
someMethod('Hello')
有什么办法可以做到吗? (someMethod 是对象上的方法,不是静态方法)
你可以使用nashorn的Object.bindProperties扩展来绑定任意对象的属性到JS全局对象。这样用户就可以从脚本中调用 "default" 对象上的方法(和访问属性)而无需限定。
请在此处查看 Object.bindProperties 文档 https://wiki.openjdk.java.net/display/Nashorn/Nashorn+extensions#Nashornextensions-Object.bindProperties
示例代码:
import javax.script.*;
public class Main {
public static void main(String[] args) throws Exception {
ScriptEngineManager m = new ScriptEngineManager();
ScriptEngine e = m.getEngineByName("nashorn");
// get JavaScript "global" object
Object global = e.eval("this");
// get JS "Object" constructor object
Object jsObject = e.eval("Object");
Invocable invocable = (Invocable)e;
// calling Object.bindProperties(global, "hello");
// which will "bind" properties of "hello" String object
// to JS global object
invocable.invokeMethod(jsObject, "bindProperties", global, "hello");
// you're calling "hello".toUpperCase()"
e.eval("print(toUpperCase())");
// accessing bean property "empty" on 'hello' object
e.eval("print(empty)");
// just print all (enumerable) properties of global
// you'll see methods, properties of String class
// (which'd be called on "hello" instance when accessed)
e.eval("for (var i in this) print(i)");
}
}
借助 Java 的 Nashorn 脚本引擎,我可以使用如下绑定在 eval() 的上下文中提供对象:
Bindings bindings = scriptContext.getBindings(ScriptContext.ENGINE_SCOPE);
bindings.put("rules", myObj);
scriptEngine.eval("rules.someMethod('Hello')", scriptContext);
我希望能够通过提供默认对象来简化 javascript,这样 javascript 就不会变成:
rules.someMethod('Hello')
我可以写:
someMethod('Hello')
有什么办法可以做到吗? (someMethod 是对象上的方法,不是静态方法)
你可以使用nashorn的Object.bindProperties扩展来绑定任意对象的属性到JS全局对象。这样用户就可以从脚本中调用 "default" 对象上的方法(和访问属性)而无需限定。 请在此处查看 Object.bindProperties 文档 https://wiki.openjdk.java.net/display/Nashorn/Nashorn+extensions#Nashornextensions-Object.bindProperties
示例代码:
import javax.script.*;
public class Main {
public static void main(String[] args) throws Exception {
ScriptEngineManager m = new ScriptEngineManager();
ScriptEngine e = m.getEngineByName("nashorn");
// get JavaScript "global" object
Object global = e.eval("this");
// get JS "Object" constructor object
Object jsObject = e.eval("Object");
Invocable invocable = (Invocable)e;
// calling Object.bindProperties(global, "hello");
// which will "bind" properties of "hello" String object
// to JS global object
invocable.invokeMethod(jsObject, "bindProperties", global, "hello");
// you're calling "hello".toUpperCase()"
e.eval("print(toUpperCase())");
// accessing bean property "empty" on 'hello' object
e.eval("print(empty)");
// just print all (enumerable) properties of global
// you'll see methods, properties of String class
// (which'd be called on "hello" instance when accessed)
e.eval("for (var i in this) print(i)");
}
}