[webkit-dev] JSC: Array Patch, Seeking Advice

Brian Barnes ggadwa at charter.net
Wed Aug 19 08:37:38 PDT 2009


The last thing I want to do is make you guys job with patches harder, so 
I thought I'd seek some advice.  I created two convenience functions for 
building arrays with consecutive indexes (i.e., new Array(1,2,3,4,5,6,7,8)).

I haven't gone over all the style guidelines yet (but did a quick 
overview.)  I'll do that before I go further, but my question is this:  
This patch doesn't use anything internal inside JSC itself (it was 
created outside in my code.)  Therefore, it's probably a slow/less 
efficient path.  Is this a bad idea?  If so, I'll just scrap the idea 
before going any further.  The functions are:

(in JSObjectRef)

JSValueRef JSObjectSetArray(JSContextRef ctx,size_t valueCount,const 
JSValueRef values[],JSValueRef *exception)
{
    JSObjectRef        arrayProtoObj;
    JSStringRef        jsStr;
    JSValueRef        arrayVal;

        // get the array prototype

    jsStr=JSStringCreateWithUTF8CString("Array");
    
arrayVal=JSObjectGetProperty(ctx,JSContextGetGlobalObject(ctx),jsStr,exception);
    JSStringRelease(jsStr);

    if (arrayVal==NULL) return(NULL);

    arrayProtoObj=JSValueToObject(ctx,arrayVal,exception);
    if (arrayProtoObj==NULL) return(NULL);

        // create the new array object

    
return((JSValueRef)JSObjectCallAsConstructor(ctx,arrayProtoObj,valueCount,values,exception));
}


(in JSValueRef)

bool JSValueIsArray(JSContextRef ctx,JSValueRef value)
{
    char                *ch,str[64];
    JSStringRef            jsStr;
    JSObjectRef            arrayObj;
    JSValueRef            val;
   
        // only check objects

    if (!JSValueIsObject(ctx,value)) return(FALSE);
   
        // get constructor

    arrayObj=JSValueToObject(ctx,value,NULL);
    if (arrayObj==NULL) return(FALSE);

    jsStr=JSStringCreateWithUTF8CString("constructor");
    val=JSObjectGetProperty(ctx,arrayObj,jsStr,NULL);
    JSStringRelease(jsStr);

    if (val==NULL) return(FALSE);

        // get constructor as a function

    jsStr=JSValueToStringCopy(ctx,val,NULL);
    if (jsStr==NULL) return(FALSE);

    JSStringGetUTF8CString(jsStr,str,64);
    str[63]=0x0;

    JSStringRelease(jsStr);

        // special check to make sure we don't have Array
        // anywhere else in a function body or there just
        // instead a function body

    ch=strchr(str,'{');
    if (ch==NULL) return(FALSE);

    *ch=0x0;

        // look for array in string

    return(strstr(str,"Array")!=NULL);
}

Thanks for any help ...

[>] Brian



More information about the webkit-dev mailing list