Title: [239537] trunk
Revision
239537
Author
[email protected]
Date
2018-12-21 22:41:03 -0800 (Fri, 21 Dec 2018)

Log Message

[JSC] Implement "well-formed JSON.stringify" proposal
https://bugs.webkit.org/show_bug.cgi?id=191677

Reviewed by Darin Adler.

JSTests:

* stress/json-surrogate-pair.js: Added.
(shouldBe):
* test262/expectations.yaml:

Source/WTF:

This patch implements "well-formed JSON.stringify" proposal[1], which is now stage 3.
JSON.stringify appended surrogate pair codes even if it is not paired appropriately.
The proposal requires that broken surrogate pairs are unicode-escaped.

[1]: https://github.com/tc39/proposal-well-formed-stringify

* wtf/text/StringBuilderJSON.cpp:
(WTF::appendQuotedJSONStringInternal):

LayoutTests:

* css3/escape-dom-api-expected.txt:
* js/dom/webidl-type-mapping-expected.txt:
* js/resources/json2-es5-compat.js:
(isHighSurrogate):
(isLowSurrogate):
(isSurrogate):
(quote):

Modified Paths

Added Paths

Diff

Modified: trunk/JSTests/ChangeLog (239536 => 239537)


--- trunk/JSTests/ChangeLog	2018-12-22 06:39:23 UTC (rev 239536)
+++ trunk/JSTests/ChangeLog	2018-12-22 06:41:03 UTC (rev 239537)
@@ -1,3 +1,14 @@
+2018-12-20  Yusuke Suzuki  <[email protected]>
+
+        [JSC] Implement "well-formed JSON.stringify" proposal
+        https://bugs.webkit.org/show_bug.cgi?id=191677
+
+        Reviewed by Darin Adler.
+
+        * stress/json-surrogate-pair.js: Added.
+        (shouldBe):
+        * test262/expectations.yaml:
+
 2018-12-20  Keith Miller  <[email protected]>
 
         Add support for globalThis

Added: trunk/JSTests/stress/json-surrogate-pair.js (0 => 239537)


--- trunk/JSTests/stress/json-surrogate-pair.js	                        (rev 0)
+++ trunk/JSTests/stress/json-surrogate-pair.js	2018-12-22 06:41:03 UTC (rev 239537)
@@ -0,0 +1,14 @@
+function shouldBe(actual, expected) {
+    if (actual !== expected)
+        throw new Error('bad value: ' + actual);
+}
+
+shouldBe(JSON.stringify('𝌆'), `"𝌆"`);
+shouldBe(JSON.stringify('\uD834\uDF06'), `"𝌆"`);
+shouldBe(JSON.stringify('\uD834'), `"\\ud834"`);
+shouldBe(JSON.stringify('\uDF06'), `"\\udf06"`);
+shouldBe(JSON.stringify('\uDF06\uD834'), `"\\udf06\\ud834"`);
+shouldBe(JSON.stringify('\uDEAD'), `"\\udead"`);
+shouldBe(JSON.stringify('\uD834\uD834\uDF06'), `"\\ud834𝌆"`);
+shouldBe(JSON.stringify('\uD834a'), `"\\ud834a"`);
+shouldBe(JSON.stringify('\uD834\u0400'), `"\\ud834Ѐ"`);

Modified: trunk/JSTests/test262/expectations.yaml (239536 => 239537)


--- trunk/JSTests/test262/expectations.yaml	2018-12-22 06:39:23 UTC (rev 239536)
+++ trunk/JSTests/test262/expectations.yaml	2018-12-22 06:41:03 UTC (rev 239537)
@@ -1022,9 +1022,6 @@
 test/built-ins/JSON/parse/reviver-array-length-get-err.js:
   default: 'Test262Error: Expected a Test262Error to be thrown but no exception was thrown at all'
   strict mode: 'Test262Error: Expected a Test262Error to be thrown but no exception was thrown at all'
-test/built-ins/JSON/stringify/string-escape-unicode.js:
-  default: 'Test262Error: JSON.stringify("\uD834") Expected SameValue(«"í ´"», «"\ud834"») to be true'
-  strict mode: 'Test262Error: JSON.stringify("\uD834") Expected SameValue(«"í ´"», «"\ud834"») to be true'
 test/built-ins/Map/proto-from-ctor-realm.js:
   default: 'Test262Error: Expected SameValue(«[object Map]», «[object Map]») to be true'
   strict mode: 'Test262Error: Expected SameValue(«[object Map]», «[object Map]») to be true'

Modified: trunk/LayoutTests/ChangeLog (239536 => 239537)


--- trunk/LayoutTests/ChangeLog	2018-12-22 06:39:23 UTC (rev 239536)
+++ trunk/LayoutTests/ChangeLog	2018-12-22 06:41:03 UTC (rev 239537)
@@ -1,3 +1,18 @@
+2018-12-20  Yusuke Suzuki  <[email protected]>
+
+        [JSC] Implement "well-formed JSON.stringify" proposal
+        https://bugs.webkit.org/show_bug.cgi?id=191677
+
+        Reviewed by Darin Adler.
+
+        * css3/escape-dom-api-expected.txt:
+        * js/dom/webidl-type-mapping-expected.txt:
+        * js/resources/json2-es5-compat.js:
+        (isHighSurrogate):
+        (isLowSurrogate):
+        (isSurrogate):
+        (quote):
+
 2018-12-21  Youenn Fablet  <[email protected]>
 
         RTCRtpSender.setParameters() does set active parameter

Modified: trunk/LayoutTests/css3/escape-dom-api-expected.txt (239536 => 239537)


--- trunk/LayoutTests/css3/escape-dom-api-expected.txt	2018-12-22 06:39:23 UTC (rev 239536)
+++ trunk/LayoutTests/css3/escape-dom-api-expected.txt	2018-12-22 06:41:03 UTC (rev 239537)
@@ -61,8 +61,8 @@
 PASS CSS.escape('ABCDEFGHIJKLMNOPQRSTUVWXYZ') is "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
 PASS CSS.escape(' !xy') is "\\ \\!xy"
 PASS CSS.escape('𝌆') is "𝌆"
-PASS CSS.escape('í¼†') is "í¼†"
-PASS CSS.escape('í ´') is "í ´"
+PASS CSS.escape('í¼†') is "\udf06"
+PASS CSS.escape('í ´') is "\ud834"
 PASS successfullyParsed is true
 
 TEST COMPLETE

Modified: trunk/LayoutTests/js/dom/webidl-type-mapping-expected.txt (239536 => 239537)


--- trunk/LayoutTests/js/dom/webidl-type-mapping-expected.txt	2018-12-22 06:39:23 UTC (rev 239536)
+++ trunk/LayoutTests/js/dom/webidl-type-mapping-expected.txt	2018-12-22 06:41:03 UTC (rev 239537)
@@ -1019,26 +1019,26 @@
 PASS converter.testString = {toString: function() { throw Error(); }} threw exception Error.
 PASS converter.testUSVString is "Ā"
 PASS converter.testString is "Ā"
-converter.testUSVString = "í €"
-converter.testString = "í €"
+converter.testUSVString = "\ud800"
+converter.testString = "\ud800"
 PASS converter.testUSVString is "�"
-PASS converter.testString is "í €"
-converter.testUSVString = "í°€"
-converter.testString = "í°€"
+PASS converter.testString is "\ud800"
+converter.testUSVString = "\udc00"
+converter.testString = "\udc00"
 PASS converter.testUSVString is "�"
-PASS converter.testString is "í°€"
-converter.testUSVString = "í €\u0000"
-converter.testString = "í €\u0000"
+PASS converter.testString is "\udc00"
+converter.testUSVString = "\ud800\u0000"
+converter.testString = "\ud800\u0000"
 PASS converter.testUSVString is "�\u0000"
-PASS converter.testString is "í €\u0000"
-converter.testUSVString = "í°€\u0000"
-converter.testString = "í°€\u0000"
+PASS converter.testString is "\ud800\u0000"
+converter.testUSVString = "\udc00\u0000"
+converter.testString = "\udc00\u0000"
 PASS converter.testUSVString is "�\u0000"
-PASS converter.testString is "í°€\u0000"
-converter.testUSVString = "í°€í €"
-converter.testString = "í°€í €"
+PASS converter.testString is "\udc00\u0000"
+converter.testUSVString = "\udc00\ud800"
+converter.testString = "\udc00\ud800"
 PASS converter.testUSVString is "��"
-PASS converter.testString is "í°€í €"
+PASS converter.testString is "\udc00\ud800"
 converter.testUSVString = "𝄞"
 converter.testString = "𝄞"
 PASS converter.testUSVString is "𝄞"

Modified: trunk/LayoutTests/js/resources/json2-es5-compat.js (239536 => 239537)


--- trunk/LayoutTests/js/resources/json2-es5-compat.js	2018-12-22 06:39:23 UTC (rev 239536)
+++ trunk/LayoutTests/js/resources/json2-es5-compat.js	2018-12-22 06:41:03 UTC (rev 239537)
@@ -182,7 +182,7 @@
     }
 
     var cx = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
-        escapable = /[\\\"\x00-\x1f]/g,
+        escapable = /[\\\"\x00-\x1f\ud800-\udbff\udc00-\udfff]/g,
         gap,
         indent,
         meta = {    // table of character substitutions
@@ -196,7 +196,19 @@
         },
         rep;
 
+    function isHighSurrogate(code) {
+        return code >= 0xd800 && code <= 0xdbff;
+    }
 
+    function isLowSurrogate(code) {
+        return code >= 0xdc00 && code <= 0xdfff;
+    }
+
+    function isSurrogate(code) {
+        return isHighSurrogate(code) || isLowSurrogate(code);
+    }
+
+
     function quote(string) {
 
 // If the string contains no control characters, no quote characters, and no
@@ -206,10 +218,21 @@
 
         escapable.lastIndex = 0;
         return escapable.test(string) ?
-            '"' + string.replace(escapable, function (a) {
+            '"' + string.replace(escapable, function (a, offset) {
                 var c = meta[a];
-                return typeof c === 'string' ? c :
-                    '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
+                if (typeof c === 'string')
+                    return c;
+                var code = a.charCodeAt(0);
+                if (!isSurrogate(code))
+                    return '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
+
+                if (isLowSurrogate(code) && (offset - 1) >= 0 && isHighSurrogate(string.charCodeAt(offset - 1)))
+                    return a;
+
+                if (isHighSurrogate(code) && (offset + 1) < string.length && isLowSurrogate(string.charCodeAt(offset + 1)))
+                    return a;
+
+                return '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
             }) + '"' :
             '"' + string + '"';
     }

Modified: trunk/Source/WTF/ChangeLog (239536 => 239537)


--- trunk/Source/WTF/ChangeLog	2018-12-22 06:39:23 UTC (rev 239536)
+++ trunk/Source/WTF/ChangeLog	2018-12-22 06:41:03 UTC (rev 239537)
@@ -1,3 +1,19 @@
+2018-12-20  Yusuke Suzuki  <[email protected]>
+
+        [JSC] Implement "well-formed JSON.stringify" proposal
+        https://bugs.webkit.org/show_bug.cgi?id=191677
+
+        Reviewed by Darin Adler.
+
+        This patch implements "well-formed JSON.stringify" proposal[1], which is now stage 3.
+        JSON.stringify appended surrogate pair codes even if it is not paired appropriately.
+        The proposal requires that broken surrogate pairs are unicode-escaped.
+
+        [1]: https://github.com/tc39/proposal-well-formed-stringify
+
+        * wtf/text/StringBuilderJSON.cpp:
+        (WTF::appendQuotedJSONStringInternal):
+
 2018-12-21  Alex Christensen  <[email protected]>
 
         Expand use of sourceApplicationAuditData

Modified: trunk/Source/WTF/wtf/text/StringBuilderJSON.cpp (239536 => 239537)


--- trunk/Source/WTF/wtf/text/StringBuilderJSON.cpp	2018-12-22 06:39:23 UTC (rev 239536)
+++ trunk/Source/WTF/wtf/text/StringBuilderJSON.cpp	2018-12-22 06:41:03 UTC (rev 239537)
@@ -57,20 +57,47 @@
 {
     for (auto* end = input + length; input != end; ++input) {
         auto character = *input;
-        auto escaped = escapedFormsForJSON[character & 0xFF];
-        if (LIKELY(!escaped || character > 0xFF)) {
+        if (LIKELY(character <= 0xFF)) {
+            auto escaped = escapedFormsForJSON[character];
+            if (LIKELY(!escaped)) {
+                *output++ = character;
+                continue;
+            }
+
+            *output++ = '\\';
+            *output++ = escaped;
+            if (UNLIKELY(escaped == 'u')) {
+                *output++ = '0';
+                *output++ = '0';
+                *output++ = upperNibbleToLowercaseASCIIHexDigit(character);
+                *output++ = lowerNibbleToLowercaseASCIIHexDigit(character);
+            }
+            continue;
+        }
+
+        if (LIKELY(!U16_IS_SURROGATE(character))) {
             *output++ = character;
             continue;
         }
 
+        auto next = input + 1;
+        bool isValidSurrogatePair = U16_IS_SURROGATE_LEAD(character) && next != end && U16_IS_TRAIL(*next);
+        if (isValidSurrogatePair) {
+            *output++ = character;
+            *output++ = *next;
+            ++input;
+            continue;
+        }
+
+        uint8_t upper = static_cast<uint32_t>(character) >> 8;
+        uint8_t lower = static_cast<uint8_t>(character);
         *output++ = '\\';
-        *output++ = escaped;
-        if (UNLIKELY(escaped == 'u')) {
-            *output++ = '0';
-            *output++ = '0';
-            *output++ = upperNibbleToLowercaseASCIIHexDigit(character);
-            *output++ = lowerNibbleToLowercaseASCIIHexDigit(character);
-        }
+        *output++ = 'u';
+        *output++ = upperNibbleToLowercaseASCIIHexDigit(upper);
+        *output++ = lowerNibbleToLowercaseASCIIHexDigit(upper);
+        *output++ = upperNibbleToLowercaseASCIIHexDigit(lower);
+        *output++ = lowerNibbleToLowercaseASCIIHexDigit(lower);
+        continue;
     }
 }
 
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to