[webkit-dev] JS: Getter/Setter callbacks at Property level
Brian Barnes
ggadwa at charter.net
Mon Jul 20 11:48:57 PDT 2009
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:
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
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.
[>] Brian
Geoffrey Garen wrote:
> Hi Brian.
>
> I see what you mean now.
>
> In JavaScriptCore, there's no API for defining C getters and setters
> individually. There's an API for associating a set of C getters and
> setters with a class, and there's an API for defining a generic
> fallback getter and setter for a class.
>
> 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.
>
> I'm not sure how important it is to you to be able to add C getters
> and setters to objects, as opposed to classes, individually. I think
> that would be a trickier API to get right. The main challenge is that
> getters and setters of that dynamic nature tend to slow down the
> object system. So, we would either want to engineer a way to avoid the
> performance cost in most cases, or we would want to design a way to
> steer API users away from that particular API in the general case. If
> you have a good example of how this feature would be generally useful
> to API clients, it's still a reasonable request, though.
>
> Geoff
>
> On Jul 20, 2009, at 10:21 AM, Brian Barnes wrote:
>
>> In SpiderMonkey, you can create an object, and that object has a
>> callback to a getter or a setter in C. You get the name, look it up,
>> return or set a value. Nitro has that same functionality.
>>
>> On SpiderMonkey, though, when creating a property, you can also do this:
>>
>> JS_DefineProperty(context,object,name,value,getter,setter,flags);
>>
>> Where "getter" and "setter" are direct calls for the property, for
>> only that property. There's no lookup logic in my code.
>>
>> So, in pseudo code, at object level, we'd have a single getter:
>>
>> void getMyObjectValues(.... name ...)
>> {
>> if (name == 'red') return(red)
>> if (name == 'green') return(green)
>> if (name == 'blue') return(blue)
>> }
>>
>> And property level, we'd have 3 getters:
>>
>> void getMyObjectRed(...)
>> {
>> return(red);
>> }
>>
>> void getMyObjectGreen(...)
>> {
>> return(green);
>> }
>>
>> void getMyObjectBlue(...)
>> {
>> return(blue);
>> }
>>
>> [>] Brian
>>
>> Geoffrey Garen wrote:
>>> Hi Brian.
>>>
>>> I don't understand the distinction you're drawing between "the
>>> property level" and "the object level". Can you explain what those
>>> mean and give an example of each?
>>>
>>> Thanks,
>>> Geoff
>>>
>>> On Jul 20, 2009, at 9:14 AM, Brian Barnes wrote:
>>>
>>>> I was getting ready to try the first move from SpiderMonkey to
>>>> Nitro, and ran into a large problem. Right now, all my getters and
>>>> setters are at the property level. In the documentation I have,
>>>> Nitro only seems to put them at the object level. This would force
>>>> a huge refactoring of my code (which I'm willing to do if I have
>>>> to, I would just like to avoid it as I have hundred or so objects.)
>>>>
>>>> Is my documentation old, did I miss something, or am I stuck? If
>>>> I'm stuck, is there any call to have this put into Nitro at some
>>>> time in the future?
>>>>
>>>> [>] Brian
>>>>
>>>>
>>>> _______________________________________________
>>>> webkit-dev mailing list
>>>> webkit-dev at lists.webkit.org
>>>> http://lists.webkit.org/mailman/listinfo.cgi/webkit-dev
>>>
>>>
>>
>
>
More information about the webkit-dev
mailing list