Changeset 89895 in webkit


Ignore:
Timestamp:
Jun 27, 2011 10:55:45 PM (13 years ago)
Author:
barraclough@apple.com
Message:

Source/JavaScriptCore: Build fix attempt after r89885.

Patch by Ryosuke Niwa <rniwa@webkit.org> on 2011-06-27

LayoutTests: https://bugs.webkit.org/show_bug.cgi?id=50554
RegExp.prototype.toString does not escape slashes

Reviewed by Darin Adler & Oliver Hunt.

The problem here is that we don't escape forwards slashes when converting
a RegExp to a string. This means that RegExp("/").toString() is "/",
which is not a valid RegExp literal. Also, we return an invalid literal
for RegExp.prototype.toString() ("
", which is an empty single-line comment).

From ES5:
"NOTE: The returned String has the form of a RegularExpressionLiteral that
evaluates to another RegExp object with the same behaviour as this object."

Added test cases.

  • fast/regex/script-tests/toString.js: Added.

(testFwdSlash):

  • fast/regex/toString-expected.txt: Added.
  • fast/regex/toString.html: Added.
Location:
trunk
Files:
3 added
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/LayoutTests/ChangeLog

    r89891 r89895  
     12011-06-27  Gavin Barraclough  <barraclough@apple.com>
     2
     3        Reviewed by Darin Adler & Oliver Hunt.
     4
     5        https://bugs.webkit.org/show_bug.cgi?id=50554
     6        RegExp.prototype.toString does not escape slashes
     7
     8        The problem here is that we don't escape forwards slashes when converting
     9        a RegExp to a string. This means that RegExp("/").toString() is "///",
     10        which is not a valid RegExp literal. Also, we return an invalid literal
     11        for RegExp.prototype.toString() ("//", which is an empty single-line comment).
     12
     13        From ES5:
     14        "NOTE: The returned String has the form of a RegularExpressionLiteral that
     15        evaluates to another RegExp object with the same behaviour as this object."
     16
     17        Added test cases.
     18
     19        * fast/regex/script-tests/toString.js: Added.
     20        (testFwdSlash):
     21        * fast/regex/toString-expected.txt: Added.
     22        * fast/regex/toString.html: Added.
     23
    1242011-06-27  Pavel Feldman  <pfeldman@chromium.org>
    225
  • trunk/Source/JavaScriptCore/ChangeLog

    r89887 r89895  
    5252        (JSC::SafeRecompiler::operator()):
    5353        (JSC::JSGlobalData::releaseExecutableMemory):
     54
     552011-06-27  Gavin Barraclough  <barraclough@apple.com>
     56
     57        Reviewed by Darin Adler & Oliver Hunt.
     58
     59        https://bugs.webkit.org/show_bug.cgi?id=50554
     60        RegExp.prototype.toString does not escape slashes
     61
     62        The problem here is that we don't escape forwards slashes when converting
     63        a RegExp to a string. This means that RegExp("/").toString() is "///",
     64        which is not a valid RegExp literal. Also, we return an invalid literal
     65        for RegExp.prototype.toString() ("//", which is an empty single-line comment).
     66
     67        From ES5:
     68        "NOTE: The returned String has the form of a RegularExpressionLiteral that
     69        evaluates to another RegExp object with the same behaviour as this object."
     70
     71        * runtime/RegExpObject.cpp:
     72        (JSC::regExpObjectSource):
     73            - Escape forward slashes when getting the source of a RegExp.
     74        * runtime/RegExpPrototype.cpp:
     75        (JSC::regExpProtoFuncToString):
     76            - Remove unnecessary and erroneous hack to return "//" as the string
     77            representation of RegExp.prototype. This is not a valid RegExp literal
     78            (it is an empty single-line comment).
    5479
    55802011-06-27  Gavin Barraclough  <barraclough@apple.com>
  • trunk/Source/JavaScriptCore/runtime/RegExpObject.cpp

    r87346 r89895  
    3030#include "RegExpConstructor.h"
    3131#include "RegExpPrototype.h"
     32#include "UStringBuilder.h"
    3233#include "UStringConcatenate.h"
    3334#include <wtf/PassOwnPtr.h>
     
    112113JSValue regExpObjectSource(ExecState* exec, JSValue slotBase, const Identifier&)
    113114{
    114     return jsString(exec, asRegExpObject(slotBase)->regExp()->pattern());
     115    UString pattern = asRegExpObject(slotBase)->regExp()->pattern();
     116
     117    size_t forwardSlashPosition = pattern.find('/');
     118    if (forwardSlashPosition == notFound)
     119        return jsString(exec, pattern);
     120
     121    // 'completed' tracks the length of original pattern already copied
     122    // into the result buffer.
     123    size_t completed = 0;
     124    UStringBuilder result;
     125
     126    do {
     127        // 'slashesPosition' points to the first (of possibly zero) backslash
     128        // prior to the forwards slash.
     129        size_t slashesPosition = forwardSlashPosition;
     130        while (slashesPosition && pattern[slashesPosition - 1] == '\\')
     131            --slashesPosition;
     132
     133        // Check whether the number of backslashes is odd or even -
     134        // if odd, the forwards slash is already escaped, so we mustn't
     135        // double escape it.
     136        if ((forwardSlashPosition - slashesPosition) & 1)
     137            result.append(pattern.substringSharingImpl(completed, forwardSlashPosition + 1));
     138        else {
     139            result.append(pattern.substringSharingImpl(completed, forwardSlashPosition));
     140            result.append("\\/");
     141        }
     142        completed = forwardSlashPosition + 1;
     143
     144        forwardSlashPosition = pattern.find('/', completed);
     145    } while (forwardSlashPosition != notFound);
     146
     147    // Copy in the remainder of the pattern to the buffer.
     148    result.append(pattern.substringSharingImpl(completed));
     149    return jsString(exec, result.toUString());
    115150}
    116151
  • trunk/Source/JavaScriptCore/runtime/RegExpPrototype.cpp

    r87445 r89895  
    137137{
    138138    JSValue thisValue = exec->hostThisValue();
    139     if (!thisValue.inherits(&RegExpObject::s_info)) {
    140         if (thisValue.inherits(&RegExpPrototype::s_info))
    141             return JSValue::encode(jsNontrivialString(exec, "//"));
     139    if (!thisValue.inherits(&RegExpObject::s_info))
    142140        return throwVMTypeError(exec);
    143     }
    144141
    145142    RegExpObject* thisObject = asRegExpObject(thisValue);
Note: See TracChangeset for help on using the changeset viewer.