Title: [293301] trunk/Source
Revision
293301
Author
cdu...@apple.com
Date
2022-04-23 21:48:31 -0700 (Sat, 23 Apr 2022)

Log Message

[IDL] Add support for [AtomString] with USVString & ByteString types
https://bugs.webkit.org/show_bug.cgi?id=239695

Reviewed by Cameron McCormack.

Add support for [AtomString] with USVString and ByteString types in our WebIDL
bindings. This is needed to mark the AtomString(const String&) constructor
explicit.

* Source/WTF/wtf/text/AtomString.cpp:
(WTF::replaceUnpairedSurrogatesWithReplacementCharacterInternal):
(WTF::replaceUnpairedSurrogatesWithReplacementCharacter):
* Source/WTF/wtf/text/AtomString.h:
* Source/WTF/wtf/text/WTFString.cpp:
(WTF::replaceUnpairedSurrogatesWithReplacementCharacter): Deleted.
* Source/WTF/wtf/text/WTFString.h:
* Source/WebCore/Modules/fetch/FetchResponse.h:
* Source/WebCore/Modules/fetch/FetchResponse.idl:
* Source/WebCore/bindings/js/JSDOMConvertStrings.cpp:
(WebCore::throwIfInvalidByteString):
(WebCore::identifierToByteString):
(WebCore::valueToByteString):
(WebCore::valueToByteAtomString):
(WebCore::valueToUSVAtomString):
(WebCore::stringToByteString): Deleted.
* Source/WebCore/bindings/js/JSDOMConvertStrings.h:
(WebCore::Converter<IDLAtomStringAdaptor<T>>::convert):
(WebCore::Converter<IDLAtomStringAdaptor<IDLUSVString>>::convert):
(WebCore::Converter<IDLAtomStringAdaptor<IDLByteString>>::convert):
(WebCore::JSConverter<IDLAtomStringAdaptor<T>>::convert):
(WebCore::JSConverter<IDLAtomStringAdaptor<IDLUSVString>>::convert):
(WebCore::JSConverter<IDLAtomStringAdaptor<IDLByteString>>::convert):
* Source/WebCore/html/HTMLAnchorElement.idl:
* Source/WebCore/html/HTMLAreaElement.idl:
* Source/WebCore/platform/network/ResourceResponseBase.cpp:
(WebCore::ResourceResponseBase::crossThreadData const):
(WebCore::ResourceResponseBase::fromCrossThreadData):
(WebCore::ResourceResponseBase::httpStatusText const):
(WebCore::ResourceResponseBase::setHTTPStatusText):
* Source/WebCore/platform/network/ResourceResponseBase.h:

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

Modified Paths

Diff

Modified: trunk/Source/WTF/wtf/text/AtomString.cpp (293300 => 293301)


--- trunk/Source/WTF/wtf/text/AtomString.cpp	2022-04-24 03:54:39 UTC (rev 293300)
+++ trunk/Source/WTF/wtf/text/AtomString.cpp	2022-04-24 04:48:31 UTC (rev 293301)
@@ -27,6 +27,8 @@
 #include <wtf/text/IntegerToStringConversion.h>
 
 #include <wtf/dtoa.h>
+#include <wtf/text/StringBuilder.h>
+#include <wtf/unicode/CharacterNames.h>
 
 namespace WTF {
 
@@ -160,4 +162,35 @@
     });
 }
 
+static inline StringBuilder replaceUnpairedSurrogatesWithReplacementCharacterInternal(StringView view)
+{
+    // Slow path: https://infra.spec.whatwg.org/#_javascript_-string-convert
+    // Replaces unpaired surrogates with the replacement character.
+    StringBuilder result;
+    result.reserveCapacity(view.length());
+    for (auto codePoint : view.codePoints()) {
+        if (U_IS_SURROGATE(codePoint))
+            result.append(replacementCharacter);
+        else
+            result.appendCharacter(codePoint);
+    }
+    return result;
+}
+
+AtomString replaceUnpairedSurrogatesWithReplacementCharacter(AtomString&& string)
+{
+    // Fast path for the case where there are no unpaired surrogates.
+    if (LIKELY(!hasUnpairedSurrogate(string)))
+        return WTFMove(string);
+    return replaceUnpairedSurrogatesWithReplacementCharacterInternal(string).toAtomString();
+}
+
+String replaceUnpairedSurrogatesWithReplacementCharacter(String&& string)
+{
+    // Fast path for the case where there are no unpaired surrogates.
+    if (LIKELY(!hasUnpairedSurrogate(string)))
+        return WTFMove(string);
+    return replaceUnpairedSurrogatesWithReplacementCharacterInternal(string).toString();
+}
+
 } // namespace WTF

Modified: trunk/Source/WTF/wtf/text/AtomString.h (293300 => 293301)


--- trunk/Source/WTF/wtf/text/AtomString.h	2022-04-24 03:54:39 UTC (rev 293300)
+++ trunk/Source/WTF/wtf/text/AtomString.h	2022-04-24 04:48:31 UTC (rev 293301)
@@ -208,6 +208,9 @@
 
 template<unsigned length> bool equalLettersIgnoringASCIICase(const AtomString&, const char (&lowercaseLetters)[length]);
 
+WTF_EXPORT_PRIVATE AtomString replaceUnpairedSurrogatesWithReplacementCharacter(AtomString&&);
+WTF_EXPORT_PRIVATE String replaceUnpairedSurrogatesWithReplacementCharacter(String&&);
+
 inline AtomString::AtomString()
 {
 }

Modified: trunk/Source/WTF/wtf/text/WTFString.cpp (293300 => 293301)


--- trunk/Source/WTF/wtf/text/WTFString.cpp	2022-04-24 03:54:39 UTC (rev 293300)
+++ trunk/Source/WTF/wtf/text/WTFString.cpp	2022-04-24 04:48:31 UTC (rev 293301)
@@ -665,26 +665,6 @@
     return nullString;
 }
 
-String replaceUnpairedSurrogatesWithReplacementCharacter(String&& string)
-{
-    // Fast path for the case where there are no unpaired surrogates.
-    if (!hasUnpairedSurrogate(string))
-        return WTFMove(string);
-
-    // Slow path: https://infra.spec.whatwg.org/#_javascript_-string-convert
-    // Replaces unpaired surrogates with the replacement character.
-    StringBuilder result;
-    result.reserveCapacity(string.length());
-    StringView view { string };
-    for (auto codePoint : view.codePoints()) {
-        if (U_IS_SURROGATE(codePoint))
-            result.append(replacementCharacter);
-        else
-            result.appendCharacter(codePoint);
-    }
-    return result.toString();
-}
-
 } // namespace WTF
 
 #ifndef NDEBUG

Modified: trunk/Source/WTF/wtf/text/WTFString.h (293300 => 293301)


--- trunk/Source/WTF/wtf/text/WTFString.h	2022-04-24 03:54:39 UTC (rev 293300)
+++ trunk/Source/WTF/wtf/text/WTFString.h	2022-04-24 04:48:31 UTC (rev 293301)
@@ -413,8 +413,6 @@
 
 #endif
 
-WTF_EXPORT_PRIVATE String replaceUnpairedSurrogatesWithReplacementCharacter(String&&);
-
 // Definitions of string operations
 
 inline String::String(StringImpl& string)

Modified: trunk/Source/WebCore/Modules/fetch/FetchResponse.h (293300 => 293301)


--- trunk/Source/WebCore/Modules/fetch/FetchResponse.h	2022-04-24 03:54:39 UTC (rev 293300)
+++ trunk/Source/WebCore/Modules/fetch/FetchResponse.h	2022-04-24 04:48:31 UTC (rev 293301)
@@ -53,7 +53,7 @@
 
     struct Init {
         unsigned short status { 200 };
-        String statusText;
+        AtomString statusText;
         std::optional<FetchHeaders::Init> headers;
     };
 

Modified: trunk/Source/WebCore/Modules/fetch/FetchResponse.idl (293300 => 293301)


--- trunk/Source/WebCore/Modules/fetch/FetchResponse.idl	2022-04-24 03:54:39 UTC (rev 293300)
+++ trunk/Source/WebCore/Modules/fetch/FetchResponse.idl	2022-04-24 04:48:31 UTC (rev 293301)
@@ -33,7 +33,7 @@
 
 dictionary FetchResponseInit {
     unsigned short status = 200;
-    ByteString statusText = "";
+    [AtomString] ByteString statusText = "";
     HeadersInit headers;
 };
 

Modified: trunk/Source/WebCore/bindings/js/JSDOMConvertStrings.cpp (293300 => 293301)


--- trunk/Source/WebCore/bindings/js/JSDOMConvertStrings.cpp	2022-04-24 03:54:39 UTC (rev 293300)
+++ trunk/Source/WebCore/bindings/js/JSDOMConvertStrings.cpp	2022-04-24 04:48:31 UTC (rev 293301)
@@ -43,14 +43,13 @@
     return identifier.string();
 }
 
-static inline String stringToByteString(JSGlobalObject& lexicalGlobalObject, JSC::ThrowScope& scope, String&& string)
+static inline bool throwIfInvalidByteString(JSGlobalObject& lexicalGlobalObject, JSC::ThrowScope& scope, const String& string)
 {
-    if (!string.isAllLatin1()) {
+    if (UNLIKELY(!string.isAllLatin1())) {
         throwTypeError(&lexicalGlobalObject, scope);
-        return { };
+        return true;
     }
-
-    return WTFMove(string);
+    return false;
 }
 
 String identifierToByteString(JSGlobalObject& lexicalGlobalObject, const Identifier& identifier)
@@ -60,7 +59,9 @@
 
     auto string = identifierToString(lexicalGlobalObject, identifier);
     RETURN_IF_EXCEPTION(scope, { });
-    return stringToByteString(lexicalGlobalObject, scope, WTFMove(string));
+    if (UNLIKELY(throwIfInvalidByteString(lexicalGlobalObject, scope, string)))
+        return { };
+    return string;
 }
 
 String valueToByteString(JSGlobalObject& lexicalGlobalObject, JSValue value)
@@ -71,9 +72,25 @@
     auto string = value.toWTFString(&lexicalGlobalObject);
     RETURN_IF_EXCEPTION(scope, { });
 
-    return stringToByteString(lexicalGlobalObject, scope, WTFMove(string));
+    if (UNLIKELY(throwIfInvalidByteString(lexicalGlobalObject, scope, string)))
+        return { };
+    return string;
 }
 
+AtomString valueToByteAtomString(JSC::JSGlobalObject& lexicalGlobalObject, JSC::JSValue value)
+{
+    VM& vm = lexicalGlobalObject.vm();
+    auto scope = DECLARE_THROW_SCOPE(vm);
+
+    auto string = value.toString(&lexicalGlobalObject)->toAtomString(&lexicalGlobalObject);
+    RETURN_IF_EXCEPTION(scope, { });
+
+    if (UNLIKELY(throwIfInvalidByteString(lexicalGlobalObject, scope, string.string())))
+        return nullAtom();
+
+    return string;
+}
+
 String identifierToUSVString(JSGlobalObject& lexicalGlobalObject, const Identifier& identifier)
 {
     return replaceUnpairedSurrogatesWithReplacementCharacter(identifierToString(lexicalGlobalObject, identifier));
@@ -90,4 +107,15 @@
     return replaceUnpairedSurrogatesWithReplacementCharacter(WTFMove(string));
 }
 
+AtomString valueToUSVAtomString(JSGlobalObject& lexicalGlobalObject, JSValue value)
+{
+    VM& vm = lexicalGlobalObject.vm();
+    auto scope = DECLARE_THROW_SCOPE(vm);
+
+    auto string = value.toString(&lexicalGlobalObject)->toAtomString(&lexicalGlobalObject);
+    RETURN_IF_EXCEPTION(scope, { });
+
+    return replaceUnpairedSurrogatesWithReplacementCharacter(WTFMove(string));
+}
+
 } // namespace WebCore

Modified: trunk/Source/WebCore/bindings/js/JSDOMConvertStrings.h (293300 => 293301)


--- trunk/Source/WebCore/bindings/js/JSDOMConvertStrings.h	2022-04-24 03:54:39 UTC (rev 293300)
+++ trunk/Source/WebCore/bindings/js/JSDOMConvertStrings.h	2022-04-24 04:48:31 UTC (rev 293301)
@@ -34,8 +34,10 @@
 WEBCORE_EXPORT String identifierToString(JSC::JSGlobalObject&, const JSC::Identifier&);
 WEBCORE_EXPORT String identifierToByteString(JSC::JSGlobalObject&, const JSC::Identifier&);
 WEBCORE_EXPORT String valueToByteString(JSC::JSGlobalObject&, JSC::JSValue);
+WEBCORE_EXPORT AtomString valueToByteAtomString(JSC::JSGlobalObject&, JSC::JSValue);
 WEBCORE_EXPORT String identifierToUSVString(JSC::JSGlobalObject&, const JSC::Identifier&);
 WEBCORE_EXPORT String valueToUSVString(JSC::JSGlobalObject&, JSC::JSValue);
+WEBCORE_EXPORT AtomString valueToUSVAtomString(JSC::JSGlobalObject&, JSC::JSValue);
 
 inline String propertyNameToString(JSC::PropertyName propertyName)
 {
@@ -177,6 +179,20 @@
     }
 };
 
+template<> struct Converter<IDLAtomStringAdaptor<IDLUSVString>> : DefaultConverter<IDLAtomStringAdaptor<IDLUSVString>> {
+    static AtomString convert(JSC::JSGlobalObject& lexicalGlobalObject, JSC::JSValue value)
+    {
+        return valueToUSVAtomString(lexicalGlobalObject, value);
+    }
+};
+
+template<> struct Converter<IDLAtomStringAdaptor<IDLByteString>> : DefaultConverter<IDLAtomStringAdaptor<IDLByteString>> {
+    static String convert(JSC::JSGlobalObject& lexicalGlobalObject, JSC::JSValue value)
+    {
+        return valueToByteAtomString(lexicalGlobalObject, value);
+    }
+};
+
 template<typename T>  struct JSConverter<IDLAtomStringAdaptor<T>> {
     static constexpr bool needsState = true;
     static constexpr bool needsGlobalObject = false;
@@ -189,6 +205,26 @@
     }
 };
 
+template<>  struct JSConverter<IDLAtomStringAdaptor<IDLUSVString>> {
+    static constexpr bool needsState = true;
+    static constexpr bool needsGlobalObject = false;
+
+    static JSC::JSValue convert(JSC::JSGlobalObject& lexicalGlobalObject, const AtomString& value)
+    {
+        return JSConverter<IDLUSVString>::convert(lexicalGlobalObject, value.string());
+    }
+};
+
+template<>  struct JSConverter<IDLAtomStringAdaptor<IDLByteString>> {
+    static constexpr bool needsState = true;
+    static constexpr bool needsGlobalObject = false;
+
+    static JSC::JSValue convert(JSC::JSGlobalObject& lexicalGlobalObject, const AtomString& value)
+    {
+        return JSConverter<IDLByteString>::convert(lexicalGlobalObject, value.string());
+    }
+};
+
 template<typename T> struct Converter<IDLRequiresExistingAtomStringAdaptor<T>> : DefaultConverter<IDLRequiresExistingAtomStringAdaptor<T>> {
     static AtomString convert(JSC::JSGlobalObject& lexicalGlobalObject, JSC::JSValue value)
     {

Modified: trunk/Source/WebCore/html/HTMLAnchorElement.idl (293300 => 293301)


--- trunk/Source/WebCore/html/HTMLAnchorElement.idl	2022-04-24 03:54:39 UTC (rev 293300)
+++ trunk/Source/WebCore/html/HTMLAnchorElement.idl	2022-04-24 04:48:31 UTC (rev 293301)
@@ -29,7 +29,7 @@
     [CEReactions=NotNeeded, Conditional=DOWNLOAD_ATTRIBUTE, EnabledBySetting=DownloadAttributeEnabled, Reflect] attribute [AtomString] DOMString download;
     [CEReactions=NotNeeded, Reflect] attribute [AtomString] DOMString hreflang;
     [CEReactions=NotNeeded, Reflect] attribute [AtomString] DOMString name;
-    [CEReactions=NotNeeded, Reflect] attribute USVString ping;
+    [CEReactions=NotNeeded, Reflect] attribute [AtomString] USVString ping;
     [CEReactions=NotNeeded, Reflect] attribute [AtomString] DOMString rel;
     [CEReactions=NotNeeded, Reflect] attribute [AtomString] DOMString rev;
     [CEReactions=NotNeeded, Reflect] attribute [AtomString] DOMString shape;

Modified: trunk/Source/WebCore/html/HTMLAreaElement.idl (293300 => 293301)


--- trunk/Source/WebCore/html/HTMLAreaElement.idl	2022-04-24 03:54:39 UTC (rev 293300)
+++ trunk/Source/WebCore/html/HTMLAreaElement.idl	2022-04-24 04:48:31 UTC (rev 293301)
@@ -24,7 +24,7 @@
     [CEReactions=NotNeeded, Reflect] attribute [AtomString] DOMString alt;
     [CEReactions=NotNeeded, Reflect] attribute [AtomString] DOMString coords;
     [CEReactions=NotNeeded, Reflect] attribute boolean noHref;
-    [CEReactions=NotNeeded, Reflect] attribute USVString ping;
+    [CEReactions=NotNeeded, Reflect] attribute [AtomString] USVString ping;
     [CEReactions=NotNeeded, Reflect] attribute DOMString rel;
     [CEReactions=NotNeeded, Reflect] attribute DOMString shape;
     [CEReactions=NotNeeded, Reflect] attribute DOMString target;

Modified: trunk/Source/WebCore/platform/network/ResourceResponseBase.cpp (293300 => 293301)


--- trunk/Source/WebCore/platform/network/ResourceResponseBase.cpp	2022-04-24 03:54:39 UTC (rev 293300)
+++ trunk/Source/WebCore/platform/network/ResourceResponseBase.cpp	2022-04-24 04:48:31 UTC (rev 293301)
@@ -97,7 +97,7 @@
     data.textEncodingName = textEncodingName().isolatedCopy();
 
     data.httpStatusCode = httpStatusCode();
-    data.httpStatusText = httpStatusText().isolatedCopy();
+    data.httpStatusText = httpStatusText().string().isolatedCopy();
     data.httpVersion = httpVersion().isolatedCopy();
 
     data.httpHeaderFields = httpHeaderFields().isolatedCopy();
@@ -121,7 +121,7 @@
     response.setTextEncodingName(WTFMove(data.textEncodingName));
 
     response.setHTTPStatusCode(data.httpStatusCode);
-    response.setHTTPStatusText(data.httpStatusText);
+    response.setHTTPStatusText(AtomString { data.httpStatusText });
     response.setHTTPVersion(data.httpVersion);
 
     response.m_httpHeaderFields = WTFMove(data.httpHeaderFields);
@@ -342,7 +342,7 @@
     return isRedirectionStatusCode(m_httpStatusCode);
 }
 
-const String& ResourceResponseBase::httpStatusText() const 
+const AtomString& ResourceResponseBase::httpStatusText() const
 {
     lazyInit(AllFields);
 
@@ -349,7 +349,7 @@
     return m_httpStatusText; 
 }
 
-void ResourceResponseBase::setHTTPStatusText(const String& statusText) 
+void ResourceResponseBase::setHTTPStatusText(const AtomString& statusText)
 {
     lazyInit(AllFields);
 

Modified: trunk/Source/WebCore/platform/network/ResourceResponseBase.h (293300 => 293301)


--- trunk/Source/WebCore/platform/network/ResourceResponseBase.h	2022-04-24 03:54:39 UTC (rev 293300)
+++ trunk/Source/WebCore/platform/network/ResourceResponseBase.h	2022-04-24 04:48:31 UTC (rev 293301)
@@ -106,8 +106,8 @@
     WEBCORE_EXPORT void setHTTPStatusCode(int);
     WEBCORE_EXPORT bool isRedirection() const;
 
-    WEBCORE_EXPORT const String& httpStatusText() const;
-    WEBCORE_EXPORT void setHTTPStatusText(const String&);
+    WEBCORE_EXPORT const AtomString& httpStatusText() const;
+    WEBCORE_EXPORT void setHTTPStatusText(const AtomString&);
 
     WEBCORE_EXPORT const String& httpVersion() const;
     WEBCORE_EXPORT void setHTTPVersion(const String&);
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to