Skip to content

Commit

Permalink
Cherry-pick 00ae065. rdar://problem/113605152
Browse files Browse the repository at this point in the history
    Cherry-pick 7b6f950. rdar://problem/113143380

        [JSC] Early return from isCanonicalNumericIndexString when it is definitely not a number
        https://bugs.webkit.org/show_bug.cgi?id=259230
        rdar://112292849

        Reviewed by Alexey Shvayka.

        This patch makes isCanonicalNumericIndexString early return for obvious non number cases
        so that we avoid super costly number-to-string & string-to-number path.

        * Source/JavaScriptCore/runtime/PropertyName.h:
        (JSC::isCanonicalNumericIndexString):

        Canonical link: https://commits.webkit.org/266080@main

    Identifier: 265870.281@safari-7616.1.27.10-branch

Identifier: 265870.298@safari-7616-branch
  • Loading branch information
Constellation authored and MyahCobbs committed Aug 9, 2023
1 parent 7f30af5 commit 061c7ff
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions Source/JavaScriptCore/runtime/PropertyName.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,27 @@ ALWAYS_INLINE bool isCanonicalNumericIndexString(UniquedStringImpl* propertyName
return false;
if (propertyName->isSymbol())
return false;
if (equal(propertyName, "-0"_s))
return true;

StringView view(propertyName);
unsigned length = view.length();
if (!length)
return false;
UChar first = view[0];
if (length == 1)
return isASCIIDigit(first);
UChar second = view[1];
if (first == '-') {
// -Infinity case should go to the slow path. -NaN cannot exist since it becomes NaN.
if (!isASCIIDigit(second) && (length != strlen("-Infinity") || second != 'I'))
return false;
if (length == 2) // Including -0, and it should be accepted.
return true;
} else if (!isASCIIDigit(first)) {
// Infinity and NaN should go to the slow path.
if (!(length == strlen("Infinity") || first == 'I') && !(length == strlen("NaN") || first == 'N'))
return false;
}

double index = jsToNumber(propertyName);
NumberToStringBuffer buffer;
const char* indexString = WTF::numberToString(index, buffer);
Expand Down

0 comments on commit 061c7ff

Please sign in to comment.