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) {
      consumer.accept(value);
      next.forEach(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"))) 
{
      engine.eval(reader);
    }
    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())));
    list.setValue(1);
System.out.println(list.size());
    list.forEach(System.out::println);
  }

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

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.

Rémi

[1] https://github.com/forax/proxy2/blob/master/demo8/funlist.js
[2] https://github.com/forax/proxy2/blob/master/demo8/src/NashornAutoBridge.java



Reply via email to