[webkit-dev] JS: Getter/Setter callbacks at Property level

Oliver Hunt oliver at apple.com
Mon Jul 20 12:16:38 PDT 2009


On Jul 20, 2009, at 11:48 AM, Brian Barnes wrote:

> I'm not sure you get exactly what I'm saying as you put class in  
> places where I'd expect property.  For instance:
>
> "
> I looked briefly, and it seems like it would be relatively easy to  
> add an API for adding C getters and setters to a class individually.  
> So, that seems like a reasonable feature request. "
>
> Replacing class with property is what I'd expect here, i.e., each  
> property set on an object has it's own getter and setter.  Something  
> like:
>
> void JSObjectSetPropertyWithCallback 
> (ctx 
> ,object 
> ,propertyName 
> ,value,getterFunction,setterFunction,attributres,exception);
>
> Also I'm a little worried about the word "dynamic", these are  
> definitely static.  I have all the functions created, you just have  
> a pointer to them.  If the pointer exist, call it for the value and  
> break out.  If it doesn't, go down the chain.  More pseudo code:
 From the point of view of the runtime any property with a getter/ 
setter is dynamic -- they can't be optimised.

>
> void main(...)
> {
> obj=JSObjectMake(ctx,NULL,NULL);
> JSObjectSetPropertyWithCallback(ctx,obj,"red",JSValueMakeNull 
> (),myRedGetter,myRedSetter,attributes,exception);
> }
>
> JSValueRef myRedGetter(...ctx...object...exception)
> {
> return(JSValueMakeNumber(red_value));
> }
>
> bool myRedSetter(..ctx..object..value..exception)
> {
> red_value=JSValueToNumber(ctx,value,exception);
> }
>
> Here are the benefits (as I see them):
>
> 1) I don't need to define classes for objects; right now, the only  
> reason I would need to create classes (instead of just passing NULL)  
> is to setup the getters & setters.  This reduces workload and  
> generalizes a lot of my code

However it takes more memory, the class based properties are  
effectively what are used for the DOM, eg. the getters and setters are  
defined on a prototype object that is shared among all instances of  
the class.

>
> 2) The JS engine has already looked up the property by name; with  
> class based getters/setters, I also have to lookup the property by  
> name.  With property based getters/setters, it's only looked up once  
> and directly called to me.  This should be a big savings win and  
> should be more simple at the back end (if there's no getter/setter  
> associated with a property, just skip forward down the chain.)
>
> I'm not sure how this could be anything but faster then class-based.
If you're using getters and setters you've already destroyed  
performance as getters and setters aren't optimisable so the  
additional cost is likely to be irrelevant.  Additionally the per- 
object instances of the getter/setter function is likely to increase  
memory usage and gc pressure, both of which could actually hurt  
performance.

Note: I'm not saying this isn't a reasonable feature request, i'm just  
trying to emphasise that getters/setters are expensive, and  
performance characteristics are hard to guess without actually  
measuring.

In the mean time you could work around this API deficiency by creating  
a function object and then manually calling __defineGetter__, etc to  
set up your getter/setter code.

--Oliver



More information about the webkit-dev mailing list