Yang Jiang
Yang Jiang

Reputation: 3

expose toString method to js objects in nashorn

Potentially dumb question but I could not find any direct documentation about it so here it is.

I'd like to expose a Java "constructor" to JS side, when instantiated it should create an instance with a "toString" method on it.

// java class
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import jdk.nashorn.api.scripting.AbstractJSObject;

public class Foo extends AbstractJSObject {
    String bar;

    public Foo() {
        super();
    }

    public Foo(String b) {
        super();
        this.bar = b;
    }

    @Override
    public Object newObject(Object... args) {
         String bar = null;
        if (args[0] != null) bar = args[0].toString();

        Foo f = new Foo(bar);
        return f;
    }

    @Override
    public String toString() {
        return "Foo: " + this.bar;
    }


    public static void main(String[] args) {
        ScriptEngine engine = new  ScriptEngineManager().getEngineByName("nashorn");
        engine.put("foo", new Foo());

        // f.toString however is null
        engine.eval("var f = new foo('hi'); f.toString();")
    }
}

Upvotes: 0

Views: 637

Answers (2)

A. Sundararajan
A. Sundararajan

Reputation: 4405

If you implement JSObject, nashorn will route all property/index access, calls via JSObject methods like getMember, getSlot, setMember, setSlot, call methods. I modified your code slightly to expose "toString" as a property of your Foo object and made it compilable & runnable code:

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import jdk.nashorn.api.scripting.AbstractJSObject;
import java.util.function.Supplier;

public class Foo extends AbstractJSObject {
    String bar;

    public Foo() {
        super();
    }

    public Foo(String b) {
        super();
        this.bar = b;
    }

    @Override
    public Object newObject(Object... args) {
        String bar = null;
        if (args[0] != null) bar = args[0].toString();

        Foo f = new Foo(bar);
        return f;
    }

    @Override
    public Object getMember(String name) {
        if (name.equals("toString")) {
            // Returning a lambda function as value of this property.
            // nashorn code can call lambdas just like script functions!
            // You may also choose to return another JSObject that
            // returns true in isFunction() method and implements "call"
            // as well.

            return (Supplier<String>)this::toString;
        } else {
            // implement other exposed properties here.
            return null;
        }
    }

    @Override
    public String toString() {
        return "Foo: " + this.bar;
    }


    public static void main(String[] args) throws Exception {
        ScriptEngine engine = new  ScriptEngineManager().getEngineByName("nashorn");
        engine.put("foo", new Foo());

        // f.toString() would call getMember("toString") and the
        // returning lambda is called!

        engine.eval("var f = new foo('hi'); print(f.toString());");
    }
}

Upvotes: 2

user6158055
user6158055

Reputation: 382

You can try in this way:

import javax.script.*;

    import jdk.nashorn.api.scripting.AbstractJSObject;

    public class Foo extends AbstractJSObject {
        String bar;

        public Foo() {
            super();
        }

        public Foo(String b) {
            super();
            this.bar = b;
        }

        @Override
        public Object newObject(Object... args) {
            String bar = null;
            if (args[0] != null) bar = args[0].toString();

            Foo f = new Foo(bar);
            return f;
        }

        @Override
        public String toString() {
            return "Foo: " + this.bar;
        }


        public static void main(String[] args) throws ScriptException {
            ScriptEngine engine = new ScriptEngineManager().getEngineByName("javascript");
            engine.put("foo", new Foo());

            Compilable compilable = (Compilable) engine;
            Bindings bindings = engine.createBindings();
            String script = "function add(op1,op2){return op1+op2} add(a, b)";
            CompiledScript jsFunction = compilable.compile(script);
            bindings.put("a", 1);bindings.put("b", 2); //put java object into js runtime environment
            Object result = jsFunction.eval(bindings);
            System.out.println(result);
        }
    }

Upvotes: 0

Related Questions