What is the best way to return an host object to JavaScript in Rhino? I have two classes like this:
public class Hosted extends org.mozilla.javascript.ScriptableObject {
private static final long serialVersionUID = 1;
public Hosted() {}
public void jsConstructor() {}
public String getClassName() {
return "Hosted";
}
public Member jsGet_member() {
Member m = new Member();
m.defineFunctionProperties(new String[] { "toString" }, m.getClass(), DONTENUM);
return m;
}
}
public class Member extends org.mozilla.javascript.ScriptableObject {
private static final long serialVersionUID = 2;
public Member() {}
public void jsConstructor() {}
public String getClassName() {
return "Member";
}
public String toString() {
return "called toString()";
}
}
It works, in the sense that I can call the toString method, but the member object doesn't behave as I would expect:
js> defineClass("Hosted");
js> defineClass("Member");
js> var h = new Hosted();
js> h.toString();
[object Hosted]
js> h instanceof Hosted;
true
js> h.__proto__;
[object Hosted]
js>
js> var m = h.member;
js> m.toString();
called toString()
js> m instanceof Member; // Should be true
false
js> m.__proto__; // Should be [object Member]
null
If I call Object.prototype.toString
though, it does say it's a Member object:
js> Object.prototype.toString.call(m);
[object Member]
I've tried calling m.setPrototype
and Context.javaToJS
.