Search code examples
javascriptjavajava-8nashorn

Java Nashorn store Function


I have a custom functional interface

public interface ThingFunction {
    Object apply(Thing thing, String... flags);
}

I currently have a method of storing them for later use

public class Thing {
    private static Map<String, ThingFunction> functions = new HashMap<>();

    ...

    public static void addFunction(String key, ThingFunction function) {
        functions.put(key, function);
    }

    ...

    public Object executeFunction(String key, String... flags) {
        return functions.get(key).accept(this, flags);
    }

    ...
}

I'm trying to expose these functions through a JS API (using the Nashorn engine). Basically, I want the user to be able to write a javascript function like function(thing, flags) {...} and have it be stored as a ThingFunction in the functions map.

I know I can case the engine to an Invocable and use the Invocable::getInteface(Class) to create a ThingFunction from javascript code

...
engine.eval("function apply(actor, flags) {return 'There are ' + flags.length + ' arguments';}");
Invocable invocable = (Invocable) enging;
ThingFunction function = invocable.getInterface(ThingFunction.class);
function.apply(thing, "this", "is", "a", "test");
...

However, this approach means I can only have one apply method in the engine. Is there a way I can make many functions and store them in a map as shown above?


Solution

  • Nashorn allows passing a script function as an argument for any Java method that requires a single-abstract-method (SAM) interface type object. Since your ThingFunction is a SAM interface, you can do something like this:

    File: Main.java

    import javax.script.*;
    import java.io.*;
    
    public class Main {
        public static void main(String[] args) throws Exception {
            ScriptEngineManager m = new ScriptEngineManager();
            ScriptEngine e = m.getEngineByName("nashorn");
            e.eval(new FileReader(args[0])); 
            Thing th = new Thing();
            // script evaluated is expected to 'register' func, foo
            // call those functions from java
            th.executeFunction("func", "arg1", "arg2");
            th.executeFunction("foo", "bar", "j");
        }
    }
    

    File: main.js

    var Thing = Java.type("Thing");
    
    // register two functions with Thing.
    // Nashorn auto-converts a script function to an object
    // implementing any SAM interface (ThingFunction in this case)
    
    Thing.addFunction("func", function(thing, args) {
        print("in func");
        for each (var i in args) print(i);
    });
    
    Thing.addFunction("foo", function(thing, args) {
        print("in foo");
        for each (var i in args) print(i);
    });
    

    To compile and run, you can use the following commands:

    javac *.java
    java Main main.js
    

    Another approach (which is Nashorn independent and would work with older Rhino jsr-223 engine as well) is to use Invocable.getInterface(Object, Class) [ http://docs.oracle.com/javase/7/docs/api/javax/script/Invocable.html#getInterface%28java.lang.Object,%20java.lang.Class%29 ]

    In your script you'd define multiple objects - each having a script function property called "apply". And you can create one ThingFunction instance on top of each such script object. Evaluated script would look like

    var obj1 = { apply: function(thing, args)  { ... } };
    var obj2 = { apply: function(thing, args)  { ....} };
    

    From the Java code, you'd do something like:

     Object obj1 = e.get("obj1");
     Object obj2 = e.get("obj2");
     ThingFunction tf1 = invocable.getInterface(obj1, ThingFunction.class);
     ThingFunction tf2 = invocable.getInterface(obj2, ThingFunction.class);