Changeset 42570 in webkit for trunk/JavaScriptCore/runtime


Ignore:
Timestamp:
Apr 15, 2009, 9:07:23 PM (16 years ago)
Author:
[email protected]
Message:

Bug 25229: Need support for Array.prototype.reduceRight
<https://bugs.webkit.org/show_bug.cgi?id=25229>

Reviewed by Gavin Barraclough.

Implement Array.reduceRight

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/runtime/ArrayPrototype.cpp

    r42567 r42570  
    5959static JSValuePtr arrayProtoFuncMap(ExecState*, JSObject*, JSValuePtr, const ArgList&);
    6060static JSValuePtr arrayProtoFuncReduce(ExecState*, JSObject*, JSValuePtr, const ArgList&);
     61static JSValuePtr arrayProtoFuncReduceRight(ExecState*, JSObject*, JSValuePtr, const ArgList&);
    6162static JSValuePtr arrayProtoFuncLastIndexOf(ExecState*, JSObject*, JSValuePtr, const ArgList&);
    6263
     
    108109  filter         arrayProtoFuncFilter         DontEnum|Function 1
    109110  reduce         arrayProtoFuncReduce         DontEnum|Function 1
     111  reduceRight    arrayProtoFuncReduceRight    DontEnum|Function 1
    110112  map            arrayProtoFuncMap            DontEnum|Function 1
    111113@end
     
    844846}
    845847
     848JSValuePtr arrayProtoFuncReduceRight(ExecState* exec, JSObject*, JSValuePtr thisValue, const ArgList& args)
     849{
     850    JSObject* thisObj = thisValue.toThisObject(exec);
     851   
     852    JSValuePtr function = args.at(exec, 0);
     853    CallData callData;
     854    CallType callType = function.getCallData(callData);
     855    if (callType == CallTypeNone)
     856        return throwError(exec, TypeError);
     857   
     858    unsigned i = 0;
     859    JSValuePtr rv;
     860    unsigned length = thisObj->get(exec, exec->propertyNames().length).toUInt32(exec);
     861    if (!length && args.size() == 1)
     862        return throwError(exec, TypeError);
     863    JSArray* array = 0;
     864    if (isJSArray(&exec->globalData(), thisObj))
     865        array = asArray(thisObj);
     866   
     867    if (args.size() >= 2)
     868        rv = args.at(exec, 1);
     869    else if (array && array->canGetIndex(length - 1)){
     870        rv = array->getIndex(length - 1);
     871        i = 1;
     872    } else {
     873        for (i = 0; i < length; i++) {
     874            rv = getProperty(exec, thisObj, length - i - 1);
     875            if (rv)
     876                break;
     877        }
     878        if (!rv)
     879            return throwError(exec, TypeError);
     880        i++;
     881    }
     882   
     883    if (callType == CallTypeJS && array) {
     884        CachedCall cachedCall(exec, asFunction(function), 4, exec->exceptionSlot());
     885        for (; i < length && !exec->hadException(); ++i) {
     886            unsigned idx = length - i - 1;
     887            cachedCall.setThis(jsNull());
     888            cachedCall.setArgument(0, rv);
     889            if (UNLIKELY(!array->canGetIndex(idx)))
     890                break; // length has been made unsafe while we enumerate fallback to slow path
     891            cachedCall.setArgument(1, array->getIndex(idx));
     892            cachedCall.setArgument(2, jsNumber(exec, idx));
     893            cachedCall.setArgument(3, array);
     894            rv = cachedCall.call();
     895        }
     896        if (i == length) // only return if we reached the end of the array
     897            return rv;
     898    }
     899   
     900    for (; i < length && !exec->hadException(); ++i) {
     901        unsigned idx = length - i - 1;
     902        JSValuePtr prop = getProperty(exec, thisObj, idx);
     903        if (!prop)
     904            continue;
     905       
     906        ArgList eachArguments;
     907        eachArguments.append(rv);
     908        eachArguments.append(prop);
     909        eachArguments.append(jsNumber(exec, idx));
     910        eachArguments.append(thisObj);
     911       
     912        rv = call(exec, function, callType, callData, jsNull(), eachArguments);
     913    }
     914    return rv;       
     915}
     916
    846917JSValuePtr arrayProtoFuncIndexOf(ExecState* exec, JSObject*, JSValuePtr thisValue, const ArgList& args)
    847918{
Note: See TracChangeset for help on using the changeset viewer.