Ignore:
Timestamp:
Dec 30, 2007, 11:03:31 PM (17 years ago)
Author:
[email protected]
Message:

Reviewed by Sam.

More small cleanup to array_object.cpp

  • kjs/array_object.cpp: (KJS::ArrayProtoFuncToString::callAsFunction): (KJS::ArrayProtoFuncToLocaleString::callAsFunction): (KJS::ArrayProtoFuncJoin::callAsFunction): (KJS::ArrayProtoFuncConcat::callAsFunction): (KJS::ArrayProtoFuncReverse::callAsFunction): (KJS::ArrayProtoFuncShift::callAsFunction): (KJS::ArrayProtoFuncSlice::callAsFunction): (KJS::ArrayProtoFuncSort::callAsFunction): (KJS::ArrayProtoFuncSplice::callAsFunction): (KJS::ArrayProtoFuncUnShift::callAsFunction): (KJS::ArrayProtoFuncFilter::callAsFunction): (KJS::ArrayProtoFuncMap::callAsFunction): (KJS::ArrayProtoFuncEvery::callAsFunction):
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/kjs/array_object.cpp

    r29046 r29047  
    6767// ECMA 15.4.4
    6868ArrayPrototype::ArrayPrototype(ExecState*, ObjectPrototype* objProto)
    69         : ArrayInstance(objProto, 0)
     69    : ArrayInstance(objProto, 0)
    7070{
    7171}
     
    107107            str += separator;
    108108        if (str.isNull()) {
    109             JSObject *error = Error::create(exec, GeneralError, "Out of memory");
     109            JSObject* error = Error::create(exec, GeneralError, "Out of memory");
    110110            exec->setException(error);
    111111            break;
     
    119119
    120120        if (str.isNull()) {
    121             JSObject *error = Error::create(exec, GeneralError, "Out of memory");
     121            JSObject* error = Error::create(exec, GeneralError, "Out of memory");
    122122            exec->setException(error);
    123123        }
     
    149149            str += separator;
    150150        if (str.isNull()) {
    151             JSObject *error = Error::create(exec, GeneralError, "Out of memory");
     151            JSObject* error = Error::create(exec, GeneralError, "Out of memory");
    152152            exec->setException(error);
    153153            break;
     
    158158            continue;
    159159
    160         bool fallback = false;
    161160        JSObject* o = element->toObject(exec);
    162161        JSValue* conversionFunction = o->get(exec, exec->propertyNames().toLocaleString);
    163         if (conversionFunction->isObject() && static_cast<JSObject*>(conversionFunction)->implementsCall()) {
    164             List args;
    165             str += static_cast<JSObject*>(conversionFunction)->call(exec, o, args)->toString(exec);
    166         } else {
    167             // try toString() fallback
    168             fallback = true;
    169         }
    170 
    171         if (fallback)
     162        if (conversionFunction->isObject() && static_cast<JSObject*>(conversionFunction)->implementsCall())
     163            str += static_cast<JSObject*>(conversionFunction)->call(exec, o, List::empty())->toString(exec);
     164        else
    172165            str += element->toString(exec);
    173166
    174167        if (str.isNull()) {
    175             JSObject *error = Error::create(exec, GeneralError, "Out of memory");
     168            JSObject* error = Error::create(exec, GeneralError, "Out of memory");
    176169            exec->setException(error);
    177170        }
     
    203196            str += separator;
    204197        if (str.isNull()) {
    205             JSObject *error = Error::create(exec, GeneralError, "Out of memory");
     198            JSObject* error = Error::create(exec, GeneralError, "Out of memory");
    206199            exec->setException(error);
    207200            break;
     
    215208
    216209        if (str.isNull()) {
    217             JSObject *error = Error::create(exec, GeneralError, "Out of memory");
     210            JSObject* error = Error::create(exec, GeneralError, "Out of memory");
    218211            exec->setException(error);
    219212        }
     
    230223    JSObject* arr = static_cast<JSObject*>(exec->lexicalGlobalObject()->arrayConstructor()->construct(exec, List::empty()));
    231224    int n = 0;
    232     JSValue *curArg = thisObj;
    233     JSObject *curObj = static_cast<JSObject *>(thisObj);
     225    JSValue* curArg = thisObj;
     226    JSObject* curObj = static_cast<JSObject* >(thisObj);
    234227    List::const_iterator it = args.begin();
    235228    List::const_iterator end = args.end();
    236229    unsigned length = thisObj->get(exec, exec->propertyNames().length)->toUInt32(exec);
    237     for (;;) {
     230    while (1) {
    238231        if (curArg->isObject() && curObj->inherits(&ArrayInstance::info)) {
    239232            unsigned int k = 0;
     
    242235            length = curObj->get(exec, exec->propertyNames().length)->toUInt32(exec);
    243236            while (k < length) {
    244                 if (JSValue *v = getProperty(exec, curObj, k))
     237                if (JSValue* v = getProperty(exec, curObj, k))
    245238                    arr->put(exec, n, v);
    246239                n++;
     
    292285    for (unsigned int k = 0; k < middle; k++) {
    293286        unsigned lk1 = length - k - 1;
    294         JSValue *obj2 = getProperty(exec, thisObj, lk1);
    295         JSValue *obj = getProperty(exec, thisObj, k);
     287        JSValue* obj2 = getProperty(exec, thisObj, lk1);
     288        JSValue* obj = getProperty(exec, thisObj, k);
    296289
    297290        if (obj2)
     
    319312        result = thisObj->get(exec, 0);
    320313        for (unsigned int k = 1; k < length; k++) {
    321             if (JSValue *obj = getProperty(exec, thisObj, k))
    322                 thisObj->put(exec, k-1, obj);
     314            if (JSValue* obj = getProperty(exec, thisObj, k))
     315                thisObj->put(exec, k - 1, obj);
    323316            else
    324                 thisObj->deleteProperty(exec, k-1);
     317                thisObj->deleteProperty(exec, k - 1);
    325318        }
    326319        thisObj->deleteProperty(exec, length - 1);
     
    335328
    336329    // We return a new array
    337     JSObject *resObj = static_cast<JSObject *>(exec->lexicalGlobalObject()->arrayConstructor()->construct(exec,List::empty()));
     330    JSObject* resObj = static_cast<JSObject* >(exec->lexicalGlobalObject()->arrayConstructor()->construct(exec, List::empty()));
    338331    JSValue* result = resObj;
    339332    double begin = args[0]->toInteger(exec);
     
    366359    int e = static_cast<int>(end);
    367360    for (int k = b; k < e; k++, n++) {
    368         if (JSValue *v = getProperty(exec, thisObj, k))
     361        if (JSValue* v = getProperty(exec, thisObj, k))
    369362            resObj->put(exec, n, v);
    370363    }
     
    375368JSValue* ArrayProtoFuncSort::callAsFunction(ExecState* exec, JSObject* thisObj, const List& args)
    376369{
    377 #if 0
    378     printf("KJS Array::Sort length=%d\n", length);
    379     for (unsigned int i = 0 ; i<length ; ++i)
    380         printf("KJS Array::Sort: %d: %s\n", i, thisObj->get(exec, i)->toString(exec).ascii());
    381 #endif
    382     JSObject *sortFunction = NULL;
     370    JSObject* sortFunction = 0;
    383371    if (!args[0]->isUndefined()) {
    384372        sortFunction = args[0]->toObject(exec);
    385373        if (!sortFunction->implementsCall())
    386             sortFunction = NULL;
     374            sortFunction = 0;
    387375    }
    388376
    389377    if (thisObj->classInfo() == &ArrayInstance::info) {
    390378        if (sortFunction)
    391             ((ArrayInstance *)thisObj)->sort(exec, sortFunction);
     379            static_cast<ArrayInstance*>(thisObj)->sort(exec, sortFunction);
    392380        else
    393             ((ArrayInstance *)thisObj)->sort(exec);
     381            static_cast<ArrayInstance*>(thisObj)->sort(exec);
    394382        return thisObj;
    395383    }
     
    397385    unsigned length = thisObj->get(exec, exec->propertyNames().length)->toUInt32(exec);
    398386
    399     if (length == 0) {
     387    if (!length) {
    400388        thisObj->put(exec, exec->propertyNames().length, jsNumber(0), DontEnum | DontDelete);
    401389        return thisObj;
     
    404392    // "Min" sort. Not the fastest, but definitely less code than heapsort
    405393    // or quicksort, and much less swapping than bubblesort/insertionsort.
    406     for (unsigned int i = 0 ; i<length-1 ; ++i) {
    407         JSValue *iObj = thisObj->get(exec,i);
     394    for (unsigned int i = 0 ; i < length - 1 ; ++i) {
     395        JSValue* iObj = thisObj->get(exec, i);
    408396        unsigned int themin = i;
    409         JSValue *minObj = iObj;
    410         for (unsigned int j = i+1 ; j<length ; ++j) {
    411             JSValue *jObj = thisObj->get(exec,j);
    412             double cmp;
    413             if (jObj->isUndefined()) {
    414                 cmp = 1; // don't check minObj because there's no need to differentiate == (0) from > (1)
    415             } else if (minObj->isUndefined()) {
    416                 cmp = -1;
    417             } else if (sortFunction) {
     397        JSValue* minObj = iObj;
     398        for (unsigned int j = i + 1 ; j < length ; ++j) {
     399            JSValue* jObj = thisObj->get(exec, j);
     400            double compareResult;
     401            if (jObj->isUndefined())
     402                compareResult = 1; // don't check minObj because there's no need to differentiate == (0) from > (1)
     403            else if (minObj->isUndefined())
     404                compareResult = -1;
     405            else if (sortFunction) {
    418406                List l;
    419407                l.append(jObj);
    420408                l.append(minObj);
    421                 cmp = sortFunction->call(exec, exec->dynamicGlobalObject(), l)->toNumber(exec);
    422             } else {
    423                 cmp = (jObj->toString(exec) < minObj->toString(exec)) ? -1 : 1;
    424             }
    425             if (cmp < 0) {
     409                compareResult = sortFunction->call(exec, exec->dynamicGlobalObject(), l)->toNumber(exec);
     410            } else
     411                compareResult = (jObj->toString(exec) < minObj->toString(exec)) ? -1 : 1;
     412
     413            if (compareResult < 0) {
    426414                themin = j;
    427415                minObj = jObj;
     
    430418        // Swap themin and i
    431419        if (themin > i) {
    432             //printf("KJS Array::Sort: swapping %d and %d\n", i, themin);
    433420            thisObj->put(exec, i, minObj);
    434421            thisObj->put(exec, themin, iObj);
    435422        }
    436423    }
    437 #if 0
    438     printf("KJS Array::Sort -- Resulting array:\n");
    439     for (unsigned int i = 0 ; i<length ; ++i)
    440         printf("KJS Array::Sort: %d: %s\n", i, thisObj->get(exec, i)->toString(exec).ascii());
    441 #endif
    442424    return thisObj;
    443425}
     
    446428{
    447429    // 15.4.4.12
    448     JSObject *resObj = static_cast<JSObject *>(exec->lexicalGlobalObject()->arrayConstructor()->construct(exec, List::empty()));
     430    JSObject* resObj = static_cast<JSObject* >(exec->lexicalGlobalObject()->arrayConstructor()->construct(exec, List::empty()));
    449431    JSValue* result = resObj;
    450432    unsigned length = thisObj->get(exec, exec->propertyNames().length)->toUInt32(exec);
     
    457439
    458440    for (unsigned int k = 0; k < deleteCount; k++) {
    459         if (JSValue *v = getProperty(exec, thisObj, k+begin))
     441        if (JSValue* v = getProperty(exec, thisObj, k + begin))
    460442            resObj->put(exec, k, v);
    461443    }
     
    466448        if (additionalArgs < deleteCount) {
    467449            for (unsigned int k = begin; k < length - deleteCount; ++k) {
    468                 if (JSValue *v = getProperty(exec, thisObj, k+deleteCount))
    469                     thisObj->put(exec, k+additionalArgs, v);
     450                if (JSValue* v = getProperty(exec, thisObj, k + deleteCount))
     451                    thisObj->put(exec, k + additionalArgs, v);
    470452                else
    471                     thisObj->deleteProperty(exec, k+additionalArgs);
     453                    thisObj->deleteProperty(exec, k + additionalArgs);
    472454            }
    473455            for (unsigned int k = length ; k > length - deleteCount + additionalArgs; --k)
    474                 thisObj->deleteProperty(exec, k-1);
     456                thisObj->deleteProperty(exec, k - 1);
    475457        } else {
    476458            for (unsigned int k = length - deleteCount; (int)k > begin; --k) {
    477                 if (JSValue *obj = getProperty(exec, thisObj, k + deleteCount - 1))
     459                if (JSValue* obj = getProperty(exec, thisObj, k + deleteCount - 1))
    478460                    thisObj->put(exec, k + additionalArgs - 1, obj);
    479461                else
    480                     thisObj->deleteProperty(exec, k+additionalArgs-1);
     462                    thisObj->deleteProperty(exec, k + additionalArgs - 1);
    481463            }
    482464        }
    483465    }
    484     for (unsigned int k = 0; k < additionalArgs; ++k) {
    485         thisObj->put(exec, k+begin, args[k+2]);
    486     }
     466    for (unsigned int k = 0; k < additionalArgs; ++k)
     467        thisObj->put(exec, k + begin, args[k + 2]);
     468
    487469    thisObj->put(exec, exec->propertyNames().length, jsNumber(length - deleteCount + additionalArgs), DontEnum | DontDelete);
    488470    return result;
     
    495477    unsigned int nrArgs = args.size();
    496478    for (unsigned int k = length; k > 0; --k) {
    497         if (JSValue *v = getProperty(exec, thisObj, k - 1))
    498             thisObj->put(exec, k+nrArgs-1, v);
     479        if (JSValue* v = getProperty(exec, thisObj, k - 1))
     480            thisObj->put(exec, k + nrArgs-1, v);
    499481        else
    500             thisObj->deleteProperty(exec, k+nrArgs-1);
     482            thisObj->deleteProperty(exec, k + nrArgs-1);
    501483    }
    502484    for (unsigned int k = 0; k < nrArgs; ++k)
     
    514496        return throwError(exec, TypeError);
    515497
    516     JSObject *applyThis = args[1]->isUndefinedOrNull() ? exec->dynamicGlobalObject() :  args[1]->toObject(exec);
    517     JSObject *resultArray = static_cast<JSObject*>(exec->lexicalGlobalObject()->arrayConstructor()->construct(exec, List::empty()));
     498    JSObject* applyThis = args[1]->isUndefinedOrNull() ? exec->dynamicGlobalObject() :  args[1]->toObject(exec);
     499    JSObject* resultArray = static_cast<JSObject*>(exec->lexicalGlobalObject()->arrayConstructor()->construct(exec, List::empty()));
    518500
    519501    unsigned filterIndex = 0;
     
    525507            continue;
    526508
    527         JSValue *v = slot.getValue(exec, thisObj, k);
     509        JSValue* v = slot.getValue(exec, thisObj, k);
    528510
    529511        List eachArguments;
     
    533515        eachArguments.append(thisObj);
    534516
    535         JSValue *result = eachFunction->call(exec, applyThis, eachArguments);
     517        JSValue* result = eachFunction->call(exec, applyThis, eachArguments);
    536518
    537519        if (result->toBoolean(exec))
     
    547529        return throwError(exec, TypeError);
    548530
    549     JSObject *applyThis = args[1]->isUndefinedOrNull() ? exec->dynamicGlobalObject() :  args[1]->toObject(exec);
     531    JSObject* applyThis = args[1]->isUndefinedOrNull() ? exec->dynamicGlobalObject() :  args[1]->toObject(exec);
    550532
    551533    unsigned length = thisObj->get(exec, exec->propertyNames().length)->toUInt32(exec);
     
    560542            continue;
    561543
    562         JSValue *v = slot.getValue(exec, thisObj, k);
     544        JSValue* v = slot.getValue(exec, thisObj, k);
    563545
    564546        List eachArguments;
     
    568550        eachArguments.append(thisObj);
    569551
    570         JSValue *result = eachFunction->call(exec, applyThis, eachArguments);
     552        JSValue* result = eachFunction->call(exec, applyThis, eachArguments);
    571553        resultArray->put(exec, k, result);
    572554    }
     
    582564JSValue* ArrayProtoFuncEvery::callAsFunction(ExecState* exec, JSObject* thisObj, const List& args)
    583565{
    584     JSObject *eachFunction = args[0]->toObject(exec);
     566    JSObject* eachFunction = args[0]->toObject(exec);
    585567
    586568    if (!eachFunction->implementsCall())
    587569        return throwError(exec, TypeError);
    588570
    589     JSObject *applyThis = args[1]->isUndefinedOrNull() ? exec->dynamicGlobalObject() :  args[1]->toObject(exec);
     571    JSObject* applyThis = args[1]->isUndefinedOrNull() ? exec->dynamicGlobalObject() :  args[1]->toObject(exec);
    590572
    591573    JSValue* result = jsBoolean(true);
     
    732714// ------------------------------ ArrayObjectImp -------------------------------
    733715
    734 ArrayObjectImp::ArrayObjectImp(ExecState *exec,
    735                                FunctionPrototype *funcProto,
    736                                ArrayPrototype *arrayProto)
     716ArrayObjectImp::ArrayObjectImp(ExecState* exec,
     717                               FunctionPrototype* funcProto,
     718                               ArrayPrototype* arrayProto)
    737719        : InternalFunctionImp(funcProto)
    738720{
     
    750732
    751733// ECMA 15.4.2
    752 JSObject *ArrayObjectImp::construct(ExecState *exec, const List &args)
     734JSObject* ArrayObjectImp::construct(ExecState* exec, const List& args)
    753735{
    754736    // a single numeric argument denotes the array size (!)
     
    765747
    766748// ECMA 15.6.1
    767 JSValue *ArrayObjectImp::callAsFunction(ExecState *exec, JSObject *, const List &args)
     749JSValue* ArrayObjectImp::callAsFunction(ExecState* exec, JSObject*, const List& args)
    768750{
    769751    // equivalent to 'new Array(....)'
Note: See TracChangeset for help on using the changeset viewer.