Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[JSC] String#toWellFormed should return stringified value
https://bugs.webkit.org/show_bug.cgi?id=251757
rdar://problem/105104668

Reviewed by Justin Michaud and Mark Lam.

We should return stringified value instead of original thisValue for the fast path.

* JSTests/stress/string-to-well-formed-number.js: Added.
(shouldBe):
* Source/JavaScriptCore/runtime/StringPrototype.cpp:
(JSC::JSC_DEFINE_HOST_FUNCTION):

Canonical link: https://commits.webkit.org/260043@main
  • Loading branch information
Constellation committed Feb 9, 2023
1 parent 5cf63af commit aa30faf
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
7 changes: 7 additions & 0 deletions JSTests/stress/string-to-well-formed-number.js
@@ -0,0 +1,7 @@
function shouldBe(actual, expected) {
if (actual !== expected)
throw new Error('bad value: ' + actual);
}

shouldBe(String.prototype.toWellFormed.call(1), '1');
shouldBe(String.prototype.toWellFormed.call(42.5), '42.5');
11 changes: 7 additions & 4 deletions Source/JavaScriptCore/runtime/StringPrototype.cpp
Expand Up @@ -1825,17 +1825,20 @@ JSC_DEFINE_HOST_FUNCTION(stringProtoFuncToWellFormed, (JSGlobalObject* globalObj
if (thisValue.isString() && asString(thisValue)->is8Bit())
return JSValue::encode(thisValue);

String string = thisValue.toWTFString(globalObject);
JSString* stringValue = thisValue.toString(globalObject);
RETURN_IF_EXCEPTION(scope, { });

if (string.is8Bit())
return JSValue::encode(thisValue);
if (stringValue->is8Bit())
return JSValue::encode(stringValue);

String string = stringValue->value(globalObject);
RETURN_IF_EXCEPTION(scope, { });

const UChar* characters = string.characters16();
unsigned length = string.length();
auto firstIllFormedIndex = illFormedIndex(characters, length);
if (!firstIllFormedIndex)
return JSValue::encode(thisValue);
return JSValue::encode(stringValue);

Vector<UChar> buffer;
buffer.reserveInitialCapacity(length);
Expand Down

0 comments on commit aa30faf

Please sign in to comment.