Changeset 42563 in webkit for trunk/JavaScriptCore/runtime


Ignore:
Timestamp:
Apr 15, 2009, 4:35:31 PM (16 years ago)
Author:
[email protected]
Message:

Bug 25159: Support Array.prototype.reduce
<https://bugs.webkit.org/show_bug.cgi?id=25159>

Reviewed by Gavin Barraclough

Implement Array.prototype.reduce

File:
1 edited

Legend:

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

    r42537 r42563  
    11/*
    22 *  Copyright (C) 1999-2000 Harri Porten ([email protected])
    3  *  Copyright (C) 2003, 2007, 2008 Apple Inc. All rights reserved.
     3 *  Copyright (C) 2003, 2007, 2008, 2009 Apple Inc. All rights reserved.
    44 *  Copyright (C) 2003 Peter Kelly ([email protected])
    55 *  Copyright (C) 2006 Alexey Proskuryakov ([email protected])
     
    5858static JSValuePtr arrayProtoFuncFilter(ExecState*, JSObject*, JSValuePtr, const ArgList&);
    5959static JSValuePtr arrayProtoFuncMap(ExecState*, JSObject*, JSValuePtr, const ArgList&);
     60static JSValuePtr arrayProtoFuncReduce(ExecState*, JSObject*, JSValuePtr, const ArgList&);
    6061static JSValuePtr arrayProtoFuncLastIndexOf(ExecState*, JSObject*, JSValuePtr, const ArgList&);
    6162
     
    106107  lastIndexOf    arrayProtoFuncLastIndexOf    DontEnum|Function 1
    107108  filter         arrayProtoFuncFilter         DontEnum|Function 1
     109  reduce         arrayProtoFuncReduce         DontEnum|Function 1
    108110  map            arrayProtoFuncMap            DontEnum|Function 1
    109111@end
     
    768770}
    769771
     772JSValuePtr arrayProtoFuncReduce(ExecState* exec, JSObject*, JSValuePtr thisValue, const ArgList& args)
     773{
     774    JSObject* thisObj = thisValue.toThisObject(exec);
     775   
     776    JSValuePtr function = args.at(exec, 0);
     777    CallData callData;
     778    CallType callType = function.getCallData(callData);
     779    if (callType == CallTypeNone)
     780        return throwError(exec, TypeError);
     781
     782    unsigned i = 0;
     783    JSValuePtr rv;
     784    unsigned length = thisObj->get(exec, exec->propertyNames().length).toUInt32(exec);
     785    if (!length && args.size() == 1)
     786        return throwError(exec, TypeError);
     787    JSArray* array = 0;
     788    if (isJSArray(&exec->globalData(), thisObj))
     789        array = asArray(thisObj);
     790
     791    if (args.size() >= 2)
     792        rv = args.at(exec, 1);
     793    else if (array && array->canGetIndex(0)){
     794        rv = array->getIndex(0);
     795        i = 1;
     796    } else {
     797        for (i = 0; i < length; i++) {
     798            rv = getProperty(exec, thisObj, i);
     799            if (rv)
     800                break;
     801        }
     802        if (!rv)
     803            return throwError(exec, TypeError);
     804        i++;
     805    }
     806
     807    if (callType == CallTypeJS && array) {
     808        CachedCall cachedCall(exec, asFunction(function), 4, exec->exceptionSlot());
     809        for (; i < length && !exec->hadException(); ++i) {
     810            cachedCall.setThis(jsNull());
     811            cachedCall.setArgument(0, rv);
     812            JSValuePtr v;
     813            if (LIKELY(array->canGetIndex(i)))
     814                v = array->getIndex(i);
     815            else
     816                break; // length has been made unsafe while we enumerate fallback to slow path
     817            cachedCall.setArgument(1, v);
     818            cachedCall.setArgument(2, jsNumber(exec, i));
     819            cachedCall.setArgument(3, array);
     820            rv = cachedCall.call();
     821        }
     822        if (i == length) // only return if we reached the end of the array
     823            return rv;
     824    }
     825
     826    for (; i < length && !exec->hadException(); ++i) {
     827        JSValuePtr prop = getProperty(exec, thisObj, i);
     828        if (!prop)
     829            continue;
     830       
     831        ArgList eachArguments;
     832        eachArguments.append(rv);
     833        eachArguments.append(prop);
     834        eachArguments.append(jsNumber(exec, i));
     835        eachArguments.append(thisObj);
     836       
     837        rv = call(exec, function, callType, callData, jsNull(), eachArguments);
     838    }
     839    return rv;
     840}
     841
    770842JSValuePtr arrayProtoFuncIndexOf(ExecState* exec, JSObject*, JSValuePtr thisValue, const ArgList& args)
    771843{
Note: See TracChangeset for help on using the changeset viewer.