ScriptEngine - 从 java 调用 javascript 闭包
ScriptEngine - Calling javascript closure from java
我正在尝试使用 ScriptEngine 从 Java 调用 javascript 闭包。请参阅下面的代码片段。我删除了 brevity.I 的脚本引擎评估代码能够调用具有闭包但不是闭包的函数,感谢任何帮助
//Java 代码片段
ScriptObjectMirror execute = (ScriptObjectMirror) engine.get("transform");
ScriptObjectMirror closure = (ScriptObjectMirror) execute.callMember("execute", new TestObj());
for (String s: closure.getOwnKeys(true)) {
System.out.println(s);
}
//Java脚本代码
var transform = {
execute : function(execution) {
print("hello");execution.setVariable("test","testing");
function transform(execution) {
execution.setVariable("result", {result:"myjson object"});
print("Testing");
}
return transform;
}
}
hello
name:test,testing
prototype
length
name
问题已解决,这是完整的工作代码
import jdk.nashorn.api.scripting.ScriptObjectMirror;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class Main {
public static class TestObj {
public TestObj() {
}
public void setVariable(String name, Object value) {
System.out.println(name + ":" + value);
if (value instanceof ScriptObjectMirror) {
ScriptObjectMirror scriptObjectMirror = (ScriptObjectMirror) value;
String[] ownKeys = scriptObjectMirror.getOwnKeys(true);
for (String k: ownKeys) {
System.out.println(scriptObjectMirror.get(k));
}
}
}
}
public static void main(String[] args) throws ScriptException {
System.out.println("Hello World!");
ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
ScriptEngine engine = scriptEngineManager.getEngineByName("javascript");
String js = "var transform = {\n" +
"execute : function(execution) {\n" +
" print(\"hello\");" +
"execution.setVariable(\"test\",\"testing\");\n" +
" function transform(execution) {\n" +
" execution.setVariable(\"result\", {result:\"myjson object\"});\n" +
" print(\"Testing\");\n" +
" }\n" +
" return transform;\n" +
"}}";
System.out.println(engine);
engine.eval(js);
//engine.put("execution", new TestObj());
ScriptObjectMirror transform = (ScriptObjectMirror) engine.get("transform");
ScriptObjectMirror execute = (ScriptObjectMirror) transform.callMember("execute", new TestObj());
execute.call(execute,new TestObj());
System.out.println("fully working code");
}
}
输出
Hello World!
jdk.nashorn.api.scripting.NashornScriptEngine@6d7b4f4c
hello
test:testing
result:[object Object]
myjson object
Testing
fully working code
我正在尝试使用 ScriptEngine 从 Java 调用 javascript 闭包。请参阅下面的代码片段。我删除了 brevity.I 的脚本引擎评估代码能够调用具有闭包但不是闭包的函数,感谢任何帮助
//Java 代码片段
ScriptObjectMirror execute = (ScriptObjectMirror) engine.get("transform");
ScriptObjectMirror closure = (ScriptObjectMirror) execute.callMember("execute", new TestObj());
for (String s: closure.getOwnKeys(true)) {
System.out.println(s);
}
//Java脚本代码
var transform = {
execute : function(execution) {
print("hello");execution.setVariable("test","testing");
function transform(execution) {
execution.setVariable("result", {result:"myjson object"});
print("Testing");
}
return transform;
}
}
hello
name:test,testing
prototype
length
name
问题已解决,这是完整的工作代码
import jdk.nashorn.api.scripting.ScriptObjectMirror;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class Main {
public static class TestObj {
public TestObj() {
}
public void setVariable(String name, Object value) {
System.out.println(name + ":" + value);
if (value instanceof ScriptObjectMirror) {
ScriptObjectMirror scriptObjectMirror = (ScriptObjectMirror) value;
String[] ownKeys = scriptObjectMirror.getOwnKeys(true);
for (String k: ownKeys) {
System.out.println(scriptObjectMirror.get(k));
}
}
}
}
public static void main(String[] args) throws ScriptException {
System.out.println("Hello World!");
ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
ScriptEngine engine = scriptEngineManager.getEngineByName("javascript");
String js = "var transform = {\n" +
"execute : function(execution) {\n" +
" print(\"hello\");" +
"execution.setVariable(\"test\",\"testing\");\n" +
" function transform(execution) {\n" +
" execution.setVariable(\"result\", {result:\"myjson object\"});\n" +
" print(\"Testing\");\n" +
" }\n" +
" return transform;\n" +
"}}";
System.out.println(engine);
engine.eval(js);
//engine.put("execution", new TestObj());
ScriptObjectMirror transform = (ScriptObjectMirror) engine.get("transform");
ScriptObjectMirror execute = (ScriptObjectMirror) transform.callMember("execute", new TestObj());
execute.call(execute,new TestObj());
System.out.println("fully working code");
}
}
输出
Hello World!
jdk.nashorn.api.scripting.NashornScriptEngine@6d7b4f4c
hello
test:testing
result:[object Object]
myjson object
Testing
fully working code