views:

520

answers:

1

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.

+1  A: 
    public Scriptable jsGet_member() {
        Scriptable scope = ScriptableObject.getTopLevelScope(this);
        Member m = new Member();
        m.setParentScope(scope);
        // defineClass("Member") must have previously been called.
        m.setPrototype(ScriptableObject.getClassPrototype(scope, "Member"));
        m.defineFunctionProperties(new String[] { "toString" },
                m.getClass(), DONTENUM);
        return m;
    }


js> defineClass("Member")
js> defineClass("Hosted")
js> var h = new Hosted();
js> var m = h.member;
js> m.toString();
called toString()
js> m instanceof Member;
true
js> m.__proto__;
[object Member]


Edit: The method can also be written:

    public Scriptable jsGet_member() {
        Scriptable scope = ScriptableObject.getTopLevelScope(this);
        Context cx = Context.getCurrentContext();
        Member m = (Member)cx.newObject(scope, "Member");
        m.defineFunctionProperties(new String[] { "toString" },
                m.getClass(), DONTENUM);
        return m;
    }

which will call Member.jsConstructor; there may be other differences as well.

Miles
Thanks, that's perfect.
Matthew Crumley