Changeset 91284 in webkit


Ignore:
Timestamp:
Jul 19, 2011 12:16:28 PM (13 years ago)
Author:
barraclough@apple.com
Message:

https://bugs.webkit.org/show_bug.cgi?id=64677
Fix bugs in String.prototype this handling.

Reviewed by Oliver Hunt.

Source/JavaScriptCore:

undefined/null this values should throw TypeErrors, not convert to
the global object, and primitive values should not be converted via
object types.

  • runtime/StringPrototype.cpp:

(JSC::stringProtoFuncReplace):
(JSC::stringProtoFuncCharAt):
(JSC::stringProtoFuncCharCodeAt):
(JSC::stringProtoFuncIndexOf):
(JSC::stringProtoFuncLastIndexOf):
(JSC::stringProtoFuncMatch):
(JSC::stringProtoFuncSearch):
(JSC::stringProtoFuncSlice):
(JSC::stringProtoFuncSplit):
(JSC::stringProtoFuncSubstr):
(JSC::stringProtoFuncSubstring):
(JSC::stringProtoFuncToLowerCase):
(JSC::stringProtoFuncToUpperCase):
(JSC::stringProtoFuncLocaleCompare):
(JSC::stringProtoFuncBig):
(JSC::stringProtoFuncSmall):
(JSC::stringProtoFuncBlink):
(JSC::stringProtoFuncBold):
(JSC::stringProtoFuncFixed):
(JSC::stringProtoFuncItalics):
(JSC::stringProtoFuncStrike):
(JSC::stringProtoFuncSub):
(JSC::stringProtoFuncSup):
(JSC::stringProtoFuncFontcolor):
(JSC::stringProtoFuncFontsize):
(JSC::stringProtoFuncAnchor):
(JSC::stringProtoFuncLink):
(JSC::trimString):

  • These methods should throw if this value is undefined, convert ToString directly, not via ToObject.

LayoutTests:

  • fast/js/script-tests/string-prototype-properties.js: Added.
  • fast/js/string-prototype-properties-expected.txt: Added.
  • fast/js/string-prototype-properties.html: Added.
    • Added layout test for string prototype functions with undefined/number as this value.
Location:
trunk
Files:
3 added
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/LayoutTests/ChangeLog

    r91277 r91284  
     12011-07-19  Gavin Barraclough  <barraclough@apple.com>
     2
     3        https://bugs.webkit.org/show_bug.cgi?id=64677
     4        Fix bugs in String.prototype this handling.
     5
     6        Reviewed by Oliver Hunt.
     7
     8        * fast/js/script-tests/string-prototype-properties.js: Added.
     9        * fast/js/string-prototype-properties-expected.txt: Added.
     10        * fast/js/string-prototype-properties.html: Added.
     11            - Added layout test for string prototype functions with undefined/number as this value.
     12
    1132011-07-19  Robert Hogan  <robert@webkit.org>
    214
  • trunk/Source/JavaScriptCore/ChangeLog

    r91280 r91284  
     12011-07-19  Gavin Barraclough  <barraclough@apple.com>
     2
     3        https://bugs.webkit.org/show_bug.cgi?id=64677
     4        Fix bugs in String.prototype this handling.
     5
     6        Reviewed by Oliver Hunt.
     7
     8        undefined/null this values should throw TypeErrors, not convert to
     9        the global object, and primitive values should not be converted via
     10        object types.
     11
     12        * runtime/StringPrototype.cpp:
     13        (JSC::stringProtoFuncReplace):
     14        (JSC::stringProtoFuncCharAt):
     15        (JSC::stringProtoFuncCharCodeAt):
     16        (JSC::stringProtoFuncIndexOf):
     17        (JSC::stringProtoFuncLastIndexOf):
     18        (JSC::stringProtoFuncMatch):
     19        (JSC::stringProtoFuncSearch):
     20        (JSC::stringProtoFuncSlice):
     21        (JSC::stringProtoFuncSplit):
     22        (JSC::stringProtoFuncSubstr):
     23        (JSC::stringProtoFuncSubstring):
     24        (JSC::stringProtoFuncToLowerCase):
     25        (JSC::stringProtoFuncToUpperCase):
     26        (JSC::stringProtoFuncLocaleCompare):
     27        (JSC::stringProtoFuncBig):
     28        (JSC::stringProtoFuncSmall):
     29        (JSC::stringProtoFuncBlink):
     30        (JSC::stringProtoFuncBold):
     31        (JSC::stringProtoFuncFixed):
     32        (JSC::stringProtoFuncItalics):
     33        (JSC::stringProtoFuncStrike):
     34        (JSC::stringProtoFuncSub):
     35        (JSC::stringProtoFuncSup):
     36        (JSC::stringProtoFuncFontcolor):
     37        (JSC::stringProtoFuncFontsize):
     38        (JSC::stringProtoFuncAnchor):
     39        (JSC::stringProtoFuncLink):
     40        (JSC::trimString):
     41            - These methods should throw if this value is undefined,
     42              convert ToString directly, not via ToObject.
     43
    1442011-07-19  Filip Pizlo  <fpizlo@apple.com>
    245
  • trunk/Source/JavaScriptCore/runtime/StringPrototype.cpp

    r91194 r91284  
    298298{
    299299    JSValue thisValue = exec->hostThisValue();
    300     JSString* sourceVal = thisValue.toThisJSString(exec);
     300    if (thisValue.isUndefinedOrNull()) // CheckObjectCoercible
     301        return throwVMTypeError(exec);
     302    JSString* sourceVal = thisValue.isString() ? asString(thisValue) : jsString(exec, thisValue.toString(exec));
    301303    JSValue pattern = exec->argument(0);
    302304    JSValue replacement = exec->argument(1);
     
    488490    if (thisValue.isUndefinedOrNull()) // CheckObjectCoercible
    489491        return throwVMTypeError(exec);
    490     UString s = thisValue.toThisString(exec);
     492    UString s = thisValue.toString(exec);
    491493    unsigned len = s.length();
    492494    JSValue a0 = exec->argument(0);
     
    508510    if (thisValue.isUndefinedOrNull()) // CheckObjectCoercible
    509511        return throwVMTypeError(exec);
    510     UString s = thisValue.toThisString(exec);
     512    UString s = thisValue.toString(exec);
    511513    unsigned len = s.length();
    512514    JSValue a0 = exec->argument(0);
     
    542544    if (thisValue.isUndefinedOrNull()) // CheckObjectCoercible
    543545        return throwVMTypeError(exec);
    544     UString s = thisValue.toThisString(exec);
     546    UString s = thisValue.toString(exec);
    545547    int len = s.length();
    546548
     
    573575    if (thisValue.isUndefinedOrNull()) // CheckObjectCoercible
    574576        return throwVMTypeError(exec);
    575     UString s = thisValue.toThisString(exec);
     577    UString s = thisValue.toString(exec);
    576578    int len = s.length();
    577579
     
    602604    if (thisValue.isUndefinedOrNull()) // CheckObjectCoercible
    603605        return throwVMTypeError(exec);
    604     UString s = thisValue.toThisString(exec);
     606    UString s = thisValue.toString(exec);
    605607    JSGlobalData* globalData = &exec->globalData();
    606608
     
    651653    if (thisValue.isUndefinedOrNull()) // CheckObjectCoercible
    652654        return throwVMTypeError(exec);
    653     UString s = thisValue.toThisString(exec);
     655    UString s = thisValue.toString(exec);
    654656    JSGlobalData* globalData = &exec->globalData();
    655657
     
    679681    if (thisValue.isUndefinedOrNull()) // CheckObjectCoercible
    680682        return throwVMTypeError(exec);
    681     UString s = thisValue.toThisString(exec);
     683    UString s = thisValue.toString(exec);
    682684    int len = s.length();
    683685
     
    706708    if (thisValue.isUndefinedOrNull()) // CheckObjectCoercible
    707709        return throwVMTypeError(exec);
    708     UString s = thisValue.toThisString(exec);
     710    UString s = thisValue.toString(exec);
    709711    JSGlobalData* globalData = &exec->globalData();
    710712
     
    770772{
    771773    JSValue thisValue = exec->hostThisValue();
    772     if (thisValue.isUndefinedOrNull()) // CheckObjectCoercible
    773         return throwVMTypeError(exec);
    774774    unsigned len;
    775775    JSString* jsString = 0;
     
    778778        jsString = static_cast<JSString*>(thisValue.asCell());
    779779        len = jsString->length();
     780    } else if (thisValue.isUndefinedOrNull()) {
     781        // CheckObjectCoercible
     782        return throwVMTypeError(exec);
    780783    } else {
    781         uString = thisValue.toThisObject(exec)->toString(exec);
     784        uString = thisValue.toString(exec);
     785        if (exec->hadException())
     786            return JSValue::encode(jsUndefined());
    782787        len = uString.length();
    783788    }
     
    807812{
    808813    JSValue thisValue = exec->hostThisValue();
    809     if (thisValue.isUndefinedOrNull()) // CheckObjectCoercible
    810         return throwVMTypeError(exec);
    811814    int len;
    812815    JSString* jsString = 0;
     
    815818        jsString = static_cast<JSString*>(thisValue.asCell());
    816819        len = jsString->length();
     820    } else if (thisValue.isUndefinedOrNull()) {
     821        // CheckObjectCoercible
     822        return throwVMTypeError(exec);
    817823    } else {
    818         uString = thisValue.toThisObject(exec)->toString(exec);
     824        uString = thisValue.toString(exec);
     825        if (exec->hadException())
     826            return JSValue::encode(jsUndefined());
    819827        len = uString.length();
    820828    }
     
    855863    if (thisValue.isUndefinedOrNull()) // CheckObjectCoercible
    856864        return throwVMTypeError(exec);
    857     JSString* sVal = thisValue.toThisJSString(exec);
     865    JSString* sVal = thisValue.isString() ? asString(thisValue) : jsString(exec, thisValue.toString(exec));
    858866    const UString& s = sVal->value(exec);
    859867
     
    895903    if (thisValue.isUndefinedOrNull()) // CheckObjectCoercible
    896904        return throwVMTypeError(exec);
    897     JSString* sVal = thisValue.toThisJSString(exec);
     905    JSString* sVal = thisValue.isString() ? asString(thisValue) : jsString(exec, thisValue.toString(exec));
    898906    const UString& s = sVal->value(exec);
    899907
     
    938946    if (thisValue.isUndefinedOrNull()) // CheckObjectCoercible
    939947        return throwVMTypeError(exec);
    940 
    941     UString s = thisValue.toThisString(exec);
     948    UString s = thisValue.toString(exec);
     949
    942950    JSValue a0 = exec->argument(0);
    943951    return JSValue::encode(jsNumber(localeCompare(s, a0.toString(exec))));
     
    947955{
    948956    JSValue thisValue = exec->hostThisValue();
    949     UString s = thisValue.toThisString(exec);
     957    if (thisValue.isUndefinedOrNull()) // CheckObjectCoercible
     958        return throwVMTypeError(exec);
     959    UString s = thisValue.toString(exec);
    950960    return JSValue::encode(jsMakeNontrivialString(exec, "<big>", s, "</big>"));
    951961}
     
    954964{
    955965    JSValue thisValue = exec->hostThisValue();
    956     UString s = thisValue.toThisString(exec);
     966    if (thisValue.isUndefinedOrNull()) // CheckObjectCoercible
     967        return throwVMTypeError(exec);
     968    UString s = thisValue.toString(exec);
    957969    return JSValue::encode(jsMakeNontrivialString(exec, "<small>", s, "</small>"));
    958970}
     
    961973{
    962974    JSValue thisValue = exec->hostThisValue();
    963     UString s = thisValue.toThisString(exec);
     975    if (thisValue.isUndefinedOrNull()) // CheckObjectCoercible
     976        return throwVMTypeError(exec);
     977    UString s = thisValue.toString(exec);
    964978    return JSValue::encode(jsMakeNontrivialString(exec, "<blink>", s, "</blink>"));
    965979}
     
    968982{
    969983    JSValue thisValue = exec->hostThisValue();
    970     UString s = thisValue.toThisString(exec);
     984    if (thisValue.isUndefinedOrNull()) // CheckObjectCoercible
     985        return throwVMTypeError(exec);
     986    UString s = thisValue.toString(exec);
    971987    return JSValue::encode(jsMakeNontrivialString(exec, "<b>", s, "</b>"));
    972988}
     
    975991{
    976992    JSValue thisValue = exec->hostThisValue();
    977     UString s = thisValue.toThisString(exec);
     993    if (thisValue.isUndefinedOrNull()) // CheckObjectCoercible
     994        return throwVMTypeError(exec);
     995    UString s = thisValue.toString(exec);
    978996    return JSValue::encode(jsMakeNontrivialString(exec, "<tt>", s, "</tt>"));
    979997}
     
    9821000{
    9831001    JSValue thisValue = exec->hostThisValue();
    984     UString s = thisValue.toThisString(exec);
     1002    if (thisValue.isUndefinedOrNull()) // CheckObjectCoercible
     1003        return throwVMTypeError(exec);
     1004    UString s = thisValue.toString(exec);
    9851005    return JSValue::encode(jsMakeNontrivialString(exec, "<i>", s, "</i>"));
    9861006}
     
    9891009{
    9901010    JSValue thisValue = exec->hostThisValue();
    991     UString s = thisValue.toThisString(exec);
     1011    if (thisValue.isUndefinedOrNull()) // CheckObjectCoercible
     1012        return throwVMTypeError(exec);
     1013    UString s = thisValue.toString(exec);
    9921014    return JSValue::encode(jsMakeNontrivialString(exec, "<strike>", s, "</strike>"));
    9931015}
     
    9961018{
    9971019    JSValue thisValue = exec->hostThisValue();
    998     UString s = thisValue.toThisString(exec);
     1020    if (thisValue.isUndefinedOrNull()) // CheckObjectCoercible
     1021        return throwVMTypeError(exec);
     1022    UString s = thisValue.toString(exec);
    9991023    return JSValue::encode(jsMakeNontrivialString(exec, "<sub>", s, "</sub>"));
    10001024}
     
    10031027{
    10041028    JSValue thisValue = exec->hostThisValue();
    1005     UString s = thisValue.toThisString(exec);
     1029    if (thisValue.isUndefinedOrNull()) // CheckObjectCoercible
     1030        return throwVMTypeError(exec);
     1031    UString s = thisValue.toString(exec);
    10061032    return JSValue::encode(jsMakeNontrivialString(exec, "<sup>", s, "</sup>"));
    10071033}
     
    10101036{
    10111037    JSValue thisValue = exec->hostThisValue();
    1012     UString s = thisValue.toThisString(exec);
     1038    if (thisValue.isUndefinedOrNull()) // CheckObjectCoercible
     1039        return throwVMTypeError(exec);
     1040    UString s = thisValue.toString(exec);
    10131041    JSValue a0 = exec->argument(0);
    10141042    return JSValue::encode(jsMakeNontrivialString(exec, "<font color=\"", a0.toString(exec), "\">", s, "</font>"));
     
    10181046{
    10191047    JSValue thisValue = exec->hostThisValue();
    1020     UString s = thisValue.toThisString(exec);
     1048    if (thisValue.isUndefinedOrNull()) // CheckObjectCoercible
     1049        return throwVMTypeError(exec);
     1050    UString s = thisValue.toString(exec);
    10211051    JSValue a0 = exec->argument(0);
    10221052
     
    10611091{
    10621092    JSValue thisValue = exec->hostThisValue();
    1063     UString s = thisValue.toThisString(exec);
     1093    if (thisValue.isUndefinedOrNull()) // CheckObjectCoercible
     1094        return throwVMTypeError(exec);
     1095    UString s = thisValue.toString(exec);
    10641096    JSValue a0 = exec->argument(0);
    10651097    return JSValue::encode(jsMakeNontrivialString(exec, "<a name=\"", a0.toString(exec), "\">", s, "</a>"));
     
    10691101{
    10701102    JSValue thisValue = exec->hostThisValue();
    1071     UString s = thisValue.toThisString(exec);
     1103    if (thisValue.isUndefinedOrNull()) // CheckObjectCoercible
     1104        return throwVMTypeError(exec);
     1105    UString s = thisValue.toString(exec);
    10721106    JSValue a0 = exec->argument(0);
    10731107    UString linkText = a0.toString(exec);
     
    11141148    if (thisValue.isUndefinedOrNull()) // CheckObjectCoercible
    11151149        return throwTypeError(exec);
    1116     UString str = thisValue.toThisString(exec);
     1150    UString str = thisValue.toString(exec);
    11171151    unsigned left = 0;
    11181152    if (trimKind & TrimLeft) {
Note: See TracChangeset for help on using the changeset viewer.