Hi, I've been using Rhino to port an application from a custom 
C++/JavaScriptCore runtime environment to a Java/Rhino environment.  For the 
most part, it's been great, as Rhino has made things quite easy.  However, 
there's one thing JavaScriptCore does in an incredibly straightforward manner 
which has no obvious equivalent in Rhino, namely overriding the default 
property getter/setter for a native object.

In JavaScriptCore, it's a pretty much standard part of the language bindings; 
when you construct a native object, you tell it what the default property 
getter/setter is.  However, in Rhino it doesn't seem quite so clear. Do I have 
to subclass ScriptableObject and override the put/get methods? Is there 
anything else I need to override?  What order are object properties resolved, 
and if I override put/get, what does it do to method lookups?

In this particular instance, I have a class named Dictionary (for key/value 
storage), which has a few methods for traversing the dictionary structure, but 
then the default getter/setter modifies values in a database.  If I just 
override get/put, will that also override the method lookup?  What I want to be 
able to do is something like:

class Dictionary extends ScriptableObject {
        public Dictionary jsFunction_getChild(String child) {...}
        public Dictionary jsFunction_getParent() {...}
        public Object jsFunction_getProperty(String propertyName) {...}
        public void jsFunction_setProperty(String propertyName, Object 
propertyValue) {...}
}

where getProperty and setProperty just override the default getter/setter, so 
for example the JS code:

        var dict = new Dictionary();
        dict.foo = "bar";
        dict["baz"] = "quux";

would be roughly equivalent to the Java code:

        Dictionary dict = new Dictionary();
        Dict.setProperty("foo", "bar");
        Dict.setProperty("baz", "quux");

In an ideal world I could just change the JavaScript API so that we don't have 
this mess of methods-and-properties, but unfortunately this isn't an ideal 
world and I have to support existing code. (This is one of those cases where 
the API was built around what was possible, rather than what was best.)  And 
unfortunately, our local JavaScript guru wants future versions of the API to 
have even MORE automatic getters/setters, "to make things easier."

Even without a simple method to do a default override, is there is a document 
somewhere that explains how properties and methods get looked up on an object 
and which Java-side methods get called as part of the resolution process?

Also, on a slightly different topic, where do we discuss and submit patches to 
Rhino itself?  We've made a few small modifications to make byte-compilation 
function on our target platform (which isn't using standard Java) and also to 
make Java objects behave a bit more like JavaScript objects (for example, 
making it so that auto-imported Java classes will return true for instanceof 
Object, which I did by making a tiny modification to 
NativeJavaObject.getPrototype()).  Our modifications have been based on rapha's 
fork at https://github.com/rapha/rhino

Thanks!


_______________________________________________
dev-tech-js-engine-rhino mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-js-engine-rhino

Reply via email to