0

I am trying to invoke a javascript closure from Java using ScriptEngine. See below the code snippet. I removed the script engine eval code for brevity.I was able to invoke the function which has a closure but not the closure, any help is appreciated

//Java code snippet

        ScriptObjectMirror execute = (ScriptObjectMirror) engine.get("transform");
    ScriptObjectMirror closure = (ScriptObjectMirror) execute.callMember("execute", new TestObj());
    for (String s: closure.getOwnKeys(true)) {
        System.out.println(s);
    }

//Javascript code

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
webjockey
  • 1,159
  • 2
  • 15
  • 24

1 Answers1

1

Solved the issue, here is the fully working code

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");
    }
}

Output

Hello World!
jdk.nashorn.api.scripting.NashornScriptEngine@6d7b4f4c
hello
test:testing
result:[object Object]
myjson object
Testing
fully working code
webjockey
  • 1,159
  • 2
  • 15
  • 24