[webkit-dev] JavaJSObject vs JSObject

Per Bothner per.bothner at oracle.com
Mon Mar 21 15:03:49 PDT 2011

At Oracle we're working on embedding WebKit inside a Java library.
One of our goals is JavaScript/Java briding, similar to LiveConnect,
but inside applications, not necessarily applets.

So I've implemented (a subclass of) netscape.javascript.JSObject
to wrap JavaScript objects as java Objects.  I've got the essentials
working, helped by looking at code in jni_jsobject.mm and

The problem is, I would rather re-use the existing code, instead
of copy-and paste!  However, the existing code I'd like to use
is in instance methods of the JavaJSObject class, and I'm unclear
on the intended usage.  I.e. the JavaJSObject meant to live as
long as the Java JSObject, or it is only a transient convenience
that only lives as long as a method call?

Assuming the latter, is the following approach reasonable/correct?

class JSObject extends netscape.javascript.JSObject {
     long peer; // Cast from a JSObject*

     public Object getMember(String name) {
         return getMemberImpl(peer, name);

     public static native Object getMemberImpl(long peer, String name);

     ... etc ...;

where the native method just does:

    return JavaJSObject(nativeHandle).getMember(name);

Is this the right way to do it?

Also, what is the recommended magic (if any) I need to do with the
original C++ (JSObject*) to make sure it doesn't get GC'd before the
Java JSObject wrapper?  I don't see that JavaJSObject::convertValueToJObject
does anything special, so perhaps it isn't an issue?
	--Per Bothner
per.bothner at oracle.com   per at bothner.com   http://per.bothner.com/

More information about the webkit-dev mailing list