[webkit-dev] javascriptcore: explicitly call default getProperties function?

Geoffrey Garen ggaren at apple.com
Wed Feb 27 16:50:01 PST 2008


No, there's no way to call a parent class's hasProperty function (or  
any other function), excluding a child class.

I suppose you could do something like this:

JSValueRef GetProperty(JSContextRef ctx, JSObjectRef obj, JSStringRef  
prop, JSValueRef* exception)
{
  static bool skipLookup;

  if (skipLookup)
    return NULL;

  skipLookup = true;
  JSValueRef* result = JSObjectGetProperty(ctx, obj, prop, exception);
  skipLookup = false;

  if (!result) {
    result = lazy_load(...);
    JSObjectSetProperty(ctx, obj, prop, result, ...)
  }

  return result;
}

That's not the most efficient way to do things, though.

The JavaScriptCore API is designed to support lazy initialization of  
property values, using JSStaticValue and JSStaticFunction descriptors.  
The assumption, though, is that the lazily computed value will be  
store in an underlying model object, and not in the JavaScript wrapper  
object. The idiom of a static value callback would look something like  
this:

JSValueRef GetProperty(JSContextRef ctx, JSObjectRef obj, JSStringRef  
prop, JSValueRef* exception)
{
  MyObject* myObject = JSObjectGetPrivate(obj);
  MyValue* myValue = myObject->getOrCreateMyValue();
  return jsWrapperFor(myValue);
}

Maybe this area needs some improvement, though.

Geoff

On Feb 27, 2008, at 1:29 PM, Michael Bieniosek wrote:

> Thanks, that seems to work.
>
> Is there any way to explicitly call hasProperty on the parent  
> object?  That would let me omit the bitmap.
>
> -Michael
>
> On 2/26/08 11:46 AM, "Geoffrey Garen" <ggaren at apple.com> wrote:
>
> Hi Michael.
>
> One solution would be to maintain a hash or bitmap indicating which
> lazy properties you had allocated so far:
>
> JSValueRef GetProperty(JSContextRef ctx, JSObjectRef obj, JSStringRef
> prop,
> JSValueRef* exception)
> {
>   if (!bitmapContains(prop)) {
>     JSValueRef lazy_loaded = lazy_load(...);
>     JSObjectSetProperty(ctx, obj, prop, lazy_loaded, ...)
>   }
>   return NULL; // Forward the property request to our parent class,
> which holds the property we set through JSObjectSetProperty.
> }
>
> The hash / bitmap can be stored in the JSObjectRef, using the
> JSObjectSetPrivate API.
>
> Geoff
>
> On Feb 25, 2008, at 4:31 PM, Michael Bieniosek wrote:
>
> > Hi,
> >
> > I have a situation where I want to do lazy-loading of a javascript
> > object's
> > properties.  I'm using the C API, in particular the getProperty
> > callback.
> >
> > I want to write code that looks like:
> >
> > JSValueRef GetProperty(JSContextRef ctx, JSObjectRef obj,
> > JSStringRef prop,
> > JSValueRef* exception)
> > {
> >  if (!JSValueHasProperty(ctx, obj, prop)) {
> >    JSValueRef lazy_loaded = lazy_load(...);
> >    JSObjectSetProperty(ctx, obj, prop, lazy_loaded, ...)
> >    return lazy_loaded;
> >  } else {
> >    return JSObjectGetProperty(ctx, obj, prop, exception);
> >  }
> > }
> >
> > That is, I want use obj as a cache for my lazy_load function.  This
> > code
> > doesn't work though, because the calls to JSObjectHasProperty and
> > JSObjectGetProperty recursively call GetProperty.
> >
> > Is there a way to make this work?
> >
> > Thanks,
> > Michael
> >
> > _______________________________________________
> > webkit-dev mailing list
> > webkit-dev at lists.webkit.org
> > http://lists.webkit.org/mailman/listinfo.cgi/webkit-dev
>
>
>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.webkit.org/pipermail/webkit-dev/attachments/20080227/7fb8f674/attachment.html 


More information about the webkit-dev mailing list