Changeset 114185 in webkit


Ignore:
Timestamp:
Apr 13, 2012 5:26:25 PM (12 years ago)
Author:
commit-queue@webkit.org
Message:

Array.prototype.toString and Array.prototype.toLocaleString should be generic
https://bugs.webkit.org/show_bug.cgi?id=81588

Patch by Hojong Han <hojong.han@samsung.com> on 2012-04-13
Reviewed by Gavin Barraclough.

  • runtime/ArrayPrototype.cpp:

(JSC::arrayProtoFuncToString):
(JSC::arrayProtoFuncToLocaleString):

  • runtime/CommonIdentifiers.h:
  • tests/mozilla/ecma/Array/15.4.4.2.js:

(getTestCases.array.item.new.TestCase):
(getTestCases):

Location:
trunk/Source/JavaScriptCore
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/JavaScriptCore/ChangeLog

    r114183 r114185  
     12012-04-13  Hojong Han  <hojong.han@samsung.com>
     2
     3        Array.prototype.toString and Array.prototype.toLocaleString should be generic
     4        https://bugs.webkit.org/show_bug.cgi?id=81588
     5
     6        Reviewed by Gavin Barraclough.
     7
     8        * runtime/ArrayPrototype.cpp:
     9        (JSC::arrayProtoFuncToString):
     10        (JSC::arrayProtoFuncToLocaleString):
     11        * runtime/CommonIdentifiers.h:
     12        * tests/mozilla/ecma/Array/15.4.4.2.js:
     13        (getTestCases.array.item.new.TestCase):
     14        (getTestCases):
     15
    1162012-04-13  Gavin Barraclough  <barraclough@apple.com>
    217
  • trunk/Source/JavaScriptCore/runtime/ArrayPrototype.cpp

    r113396 r114185  
    255255    JSValue thisValue = exec->hostThisValue();
    256256
    257     bool isRealArray = isJSArray(thisValue);
    258     if (!isRealArray && !thisValue.inherits(&JSArray::s_info))
    259         return throwVMTypeError(exec);
     257    JSObject* thisObject = thisValue.toObject(exec);
     258    if (exec->hadException())
     259        return JSValue::encode(jsUndefined());
     260
     261    JSValue function = JSValue(thisObject).get(exec, exec->propertyNames().join);
     262    if (!function.isCell())
     263        return objectProtoFuncToString(exec);
     264
     265    CallData callData;
     266    CallType callType = getCallData(function, callData);
     267    if (callType == CallTypeNone)
     268        return objectProtoFuncToString(exec);
     269
     270    if (!isJSArray(thisObject) || callType != CallTypeHost || callData.native.function != arrayProtoFuncJoin)
     271        return JSValue::encode(call(exec, function, callType, callData, thisObject, exec->emptyList()));
     272
     273    ASSERT(isJSArray(thisValue));
    260274    JSArray* thisObj = asArray(thisValue);
    261    
     275
    262276    unsigned length = thisObj->get(exec, exec->propertyNames().length).toUInt32(exec);
    263277    if (exec->hadException())
     
    274288    for (unsigned k = 0; k < length; k++) {
    275289        JSValue element;
    276         if (isRealArray && thisObj->canGetIndex(k))
     290        if (thisObj->canGetIndex(k))
    277291            element = thisObj->getIndex(k);
    278292        else
     
    332346    JSValue thisValue = exec->hostThisValue();
    333347
    334     if (!thisValue.inherits(&JSArray::s_info))
    335         return throwVMTypeError(exec);
    336     JSObject* thisObj = asArray(thisValue);
     348    JSObject* thisObj = thisValue.toObject(exec);
     349    if (exec->hadException())
     350        return JSValue::encode(jsUndefined());
    337351
    338352    unsigned length = thisObj->get(exec, exec->propertyNames().length).toUInt32(exec);
     
    361375                str = call(exec, conversionFunction, callType, callData, element, exec->emptyList()).toUString(exec);
    362376            else
    363                 str = element.toUString(exec);
     377                return throwVMTypeError(exec);
    364378            if (exec->hadException())
    365379                return JSValue::encode(jsUndefined());
    366380            stringJoiner.append(str);
    367         }
     381        } else
     382            return JSValue::encode(jsEmptyString(exec));
    368383    }
    369384
  • trunk/Source/JavaScriptCore/runtime/CommonIdentifiers.h

    r113396 r114185  
    7373    macro(valueOf) \
    7474    macro(writable) \
    75     macro(displayName)
     75    macro(displayName)\
     76    macro(join)
    7677
    7778#define JSC_COMMON_IDENTIFIERS_EACH_KEYWORD(macro) \
  • trunk/Source/JavaScriptCore/tests/mozilla/ecma/Array/15.4.4.2.js

    r43410 r114185  
    5656    array[item++] = new TestCase( SECTION,  "(new Array(void 0,null)).toString()",    ",",    (new Array(void 0,null)).toString() );
    5757
     58    array[item++] = new TestCase( SECTION,
     59                                  "{__proto__: Array.prototype, 0: 'a', 1: 'b', 2: 'c', length: 3}.toString()",
     60                                  "a,b,c",
     61                                  {__proto__: Array.prototype, 0: 'a', 1: 'b', 2: 'c', length: 3}.toString() );
     62    array[item++] = new TestCase( SECTION,
     63                                  "{__proto__: Array.prototype, 0: 'a', 1: 'b', 2: 'c', join: function() { return 'join' }}.toString()",
     64                                  "join",
     65                                  {__proto__: Array.prototype, 0: 'a', 1: 'b', 2: 'c', join: function() { return 'join' }}.toString() );
     66    array[item++] = new TestCase( SECTION,
     67                                  "Array.prototype.toString.call({join: function() { return 'join' }})",
     68                                  "join",
     69                                  Array.prototype.toString.call({join: function() { return 'join' }}) );
     70    array[item++] = new TestCase( SECTION,
     71                                  "Array.prototype.toString.call({sort: function() { return 'sort' }})",
     72                                  "[object Object]",
     73                                  Array.prototype.toString.call({sort: function() { return 'sort' }}) );
     74    array[item++] = new TestCase( SECTION,
     75                                  "Array.prototype.toString.call(new Date)",
     76                                  "[object Date]",
     77                                  Array.prototype.toString.call(new Date) );
     78    array[item++] = new TestCase( SECTION,
     79                                  "Number.prototype.join = function() { return 'number join' }; Array.prototype.toString.call(42)",
     80                                  "number join",
     81                                  eval("Number.prototype.join = function() { return 'number join' }; Array.prototype.toString.call(42)") );
     82
    5883    var EXPECT_STRING = "";
    5984    var MYARR = new Array();
     
    6893    array[item++] = new TestCase( SECTION, "MYARR.toString()",  EXPECT_STRING,  MYARR.toString() );
    6994
     95    array[item++] = new TestCase( SECTION,
     96                                  "Array.prototype.join = function() { return 'join' }; [0, 1, 2].toString()",
     97                                  "join",
     98                                  eval("Array.prototype.join = function() { return 'join' }; [0, 1, 2].toString()") );
    7099
    71100    return ( array );
Note: See TracChangeset for help on using the changeset viewer.