ScriptObjectMirror and setMember

Remi Forax forax at
Wed Dec 10 12:09:30 UTC 2014

Hi guys,
I was playing a little with my proxy library to try to auto-convert 
Nashorn object to Java objects that implement an interface.
That way, you have access to typed javascript object from the Java side.
(BTW, the code use the declared type in the interfaces to wrap object 
and not the hidden class so the mapping is static and not dynamic)

so I have this javascript file [1]:

function cons(value, next) {
   return {
     value: value,
     next: next,
     size: function() { return 1 + next.size(); },
     forEach: function(consumer) {

var NIL = {
   size: function() { return 0; },
   forEach: function(consumer) { }
function nil() {
   return NIL

and this Java file [2]:

   public interface FunList extends Bridge {
     int size();
     public void forEach(IntConsumer consumer);
   public interface FunCons extends FunList {
     int getValue();
     void setValue(int value);
     FunList getNext();
   public interface FunListFactory extends Bridge {
     public FunCons cons(int value, FunList next);
     public FunList nil();
   public static void main(String[] args) throws ScriptException, IOException {
     ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
     try(Reader reader = Files.newBufferedReader(Paths.get("demo8/funlist.js"))) {
     ScriptObjectMirror global = (ScriptObjectMirror)engine.eval("this");
     FunListFactory f = bridge(FunListFactory.class, global);
     FunCons list = f.cons(666, f.cons(2, f.cons(3, f.nil())));

The whole code compiles and works ... almost:
I get the following output:
666   <-----

It seems that setMember on an ScriptObjectMirror change the value of the 
mirror itself but not the value of the mirrored object.
It doesn't seem to be the right semantics to me.



More information about the nashorn-dev mailing list