From 18308210a891ef32a0da77a23ab928b158f7aea5 Mon Sep 17 00:00:00 2001 From: serge-sans-paille Date: Thu, 5 Sep 2024 06:12:03 +0000 Subject: [PATCH] Bug 1916234 - Remove global constructor from dom/html/nsGenericHTMLElement.cpp r=emilio Making static array constexpr and adjusting methods / codegen accordingly. Differential Revision: https://phabricator.services.mozilla.com/D220813 --- dom/bindings/BindingUtils.h | 2 +- dom/bindings/Codegen.py | 20 ++++++++-------- dom/html/FetchPriority.cpp | 3 --- dom/html/FetchPriority.h | 6 ++--- dom/html/nsGenericHTMLElement.cpp | 38 +++++++++++++++---------------- xpcom/string/nsTSubstring.cpp | 10 ++++++++ xpcom/string/nsTSubstring.h | 5 ++++ 7 files changed, 47 insertions(+), 37 deletions(-) diff --git a/dom/bindings/BindingUtils.h b/dom/bindings/BindingUtils.h index bf62a67c50ba..67ae76349d04 100644 --- a/dom/bindings/BindingUtils.h +++ b/dom/bindings/BindingUtils.h @@ -1493,7 +1493,7 @@ inline Maybe StringToEnum(const StringT& aString) { } template -inline const nsCString& GetEnumString(Enum stringId) { +inline constexpr const nsLiteralCString& GetEnumString(Enum stringId) { MOZ_RELEASE_ASSERT( static_cast(stringId) < mozilla::ArrayLength(binding_detail::EnumStrings::Values)); diff --git a/dom/bindings/Codegen.py b/dom/bindings/Codegen.py index 76dd49010980..c0f5b4a6b0e7 100644 --- a/dom/bindings/Codegen.py +++ b/dom/bindings/Codegen.py @@ -12458,22 +12458,22 @@ class CGEnum(CGThing): declare=fill( """ template <> struct EnumStrings<${name}> { - static const nsLiteralCString Values[${count}]; - }; - """, - name=self.enum.identifier.name, - count=self.nEnumStrings(), - ), - define=fill( - """ - const nsLiteralCString EnumStrings<${name}>::Values[${count}] = { - $*{entries} + static constexpr nsLiteralCString Values[${count}] { + $*{entries} + }; }; """, name=self.enum.identifier.name, count=self.nEnumStrings(), entries="".join('"%s"_ns,\n' % val for val in self.enum.values()), ), + define=fill( + """ + constexpr nsLiteralCString EnumStrings<${name}>::Values[${count}]; + """, + name=self.enum.identifier.name, + count=self.nEnumStrings(), + ), ), ) toJSValue = CGEnumToJSValue(enum) diff --git a/dom/html/FetchPriority.cpp b/dom/html/FetchPriority.cpp index 259c05c6c313..40286162f34d 100644 --- a/dom/html/FetchPriority.cpp +++ b/dom/html/FetchPriority.cpp @@ -13,9 +13,6 @@ #include "nsStringFwd.h" namespace mozilla::dom { -const char* kFetchPriorityAttributeValueHigh = "high"; -const char* kFetchPriorityAttributeValueLow = "low"; -const char* kFetchPriorityAttributeValueAuto = "auto"; FetchPriority ToFetchPriority(RequestPriority aRequestPriority) { switch (aRequestPriority) { diff --git a/dom/html/FetchPriority.h b/dom/html/FetchPriority.h index 525f0ffb17a7..36944ad8cd3e 100644 --- a/dom/html/FetchPriority.h +++ b/dom/html/FetchPriority.h @@ -37,9 +37,9 @@ void LogPriorityMapping(LazyLogModule& aLazyLogModule, FetchPriority aFetchPriority, int32_t aSupportsPriority); -extern const char* kFetchPriorityAttributeValueHigh; -extern const char* kFetchPriorityAttributeValueLow; -extern const char* kFetchPriorityAttributeValueAuto; +constexpr const char kFetchPriorityAttributeValueHigh[] = "high"; +constexpr const char kFetchPriorityAttributeValueLow[] = "low"; +constexpr const char kFetchPriorityAttributeValueAuto[] = "auto"; } // namespace dom } // namespace mozilla diff --git a/dom/html/nsGenericHTMLElement.cpp b/dom/html/nsGenericHTMLElement.cpp index bd73716e892a..ede394873802 100644 --- a/dom/html/nsGenericHTMLElement.cpp +++ b/dom/html/nsGenericHTMLElement.cpp @@ -104,7 +104,7 @@ static const uint8_t NS_INPUTMODE_NUMERIC = 6; static const uint8_t NS_INPUTMODE_DECIMAL = 7; static const uint8_t NS_INPUTMODE_SEARCH = 8; -static const nsAttrValue::EnumTable kInputmodeTable[] = { +static constexpr nsAttrValue::EnumTable kInputmodeTable[] = { {"none", NS_INPUTMODE_NONE}, {"text", NS_INPUTMODE_TEXT}, {"tel", NS_INPUTMODE_TEL}, @@ -123,7 +123,7 @@ static const uint8_t NS_ENTERKEYHINT_PREVIOUS = 5; static const uint8_t NS_ENTERKEYHINT_SEARCH = 6; static const uint8_t NS_ENTERKEYHINT_SEND = 7; -static const nsAttrValue::EnumTable kEnterKeyHintTable[] = { +static constexpr nsAttrValue::EnumTable kEnterKeyHintTable[] = { {"enter", NS_ENTERKEYHINT_ENTER}, {"done", NS_ENTERKEYHINT_DONE}, {"go", NS_ENTERKEYHINT_GO}, @@ -138,7 +138,7 @@ static const uint8_t NS_AUTOCAPITALIZE_SENTENCES = 2; static const uint8_t NS_AUTOCAPITALIZE_WORDS = 3; static const uint8_t NS_AUTOCAPITALIZE_CHARACTERS = 4; -static const nsAttrValue::EnumTable kAutocapitalizeTable[] = { +static constexpr nsAttrValue::EnumTable kAutocapitalizeTable[] = { {"none", NS_AUTOCAPITALIZE_NONE}, {"sentences", NS_AUTOCAPITALIZE_SENTENCES}, {"words", NS_AUTOCAPITALIZE_WORDS}, @@ -168,7 +168,7 @@ nsresult nsGenericHTMLElement::CopyInnerTo(Element* aDst) { return NS_OK; } -static const nsAttrValue::EnumTable kDirTable[] = { +static constexpr nsAttrValue::EnumTable kDirTable[] = { {"ltr", Directionality::Ltr}, {"rtl", Directionality::Rtl}, {"auto", Directionality::Auto}, @@ -179,11 +179,11 @@ namespace { // See . enum class PopoverAttributeKeyword : uint8_t { Auto, EmptyString, Manual }; -static const char* kPopoverAttributeValueAuto = "auto"; -static const char* kPopoverAttributeValueEmptyString = ""; -static const char* kPopoverAttributeValueManual = "manual"; +static constexpr const char kPopoverAttributeValueAuto[] = "auto"; +static constexpr const char kPopoverAttributeValueEmptyString[] = ""; +static constexpr const char kPopoverAttributeValueManual[] = "manual"; -static const nsAttrValue::EnumTable kPopoverTable[] = { +static constexpr nsAttrValue::EnumTable kPopoverTable[] = { {kPopoverAttributeValueAuto, PopoverAttributeKeyword::Auto}, {kPopoverAttributeValueEmptyString, PopoverAttributeKeyword::EmptyString}, {kPopoverAttributeValueManual, PopoverAttributeKeyword::Manual}, @@ -210,7 +210,7 @@ FetchPriority nsGenericHTMLElement::ToFetchPriority(const nsAString& aValue) { namespace { // . -static const nsAttrValue::EnumTable kFetchPriorityEnumTable[] = { +static constexpr nsAttrValue::EnumTable kFetchPriorityEnumTable[] = { {kFetchPriorityAttributeValueHigh, FetchPriority::High}, {kFetchPriorityAttributeValueLow, FetchPriority::Low}, {kFetchPriorityAttributeValueAuto, FetchPriority::Auto}, @@ -1109,7 +1109,7 @@ nsMapRuleToAttributesFunc nsGenericHTMLElement::GetAttributeMappingFunction() return &MapCommonAttributesInto; } -static const nsAttrValue::EnumTable kDivAlignTable[] = { +static constexpr nsAttrValue::EnumTable kDivAlignTable[] = { {"left", StyleTextAlign::MozLeft}, {"right", StyleTextAlign::MozRight}, {"center", StyleTextAlign::MozCenter}, @@ -1117,7 +1117,7 @@ static const nsAttrValue::EnumTable kDivAlignTable[] = { {"justify", StyleTextAlign::Justify}, {nullptr, 0}}; -static const nsAttrValue::EnumTable kFrameborderTable[] = { +static constexpr nsAttrValue::EnumTable kFrameborderTable[] = { {"yes", FrameBorderProperty::Yes}, {"no", FrameBorderProperty::No}, {"1", FrameBorderProperty::One}, @@ -1125,7 +1125,7 @@ static const nsAttrValue::EnumTable kFrameborderTable[] = { {nullptr, 0}}; // TODO(emilio): Nobody uses the parsed attribute here. -static const nsAttrValue::EnumTable kScrollingTable[] = { +static constexpr nsAttrValue::EnumTable kScrollingTable[] = { {"yes", ScrollingAttribute::Yes}, {"no", ScrollingAttribute::No}, {"on", ScrollingAttribute::On}, @@ -1135,7 +1135,7 @@ static const nsAttrValue::EnumTable kScrollingTable[] = { {"auto", ScrollingAttribute::Auto}, {nullptr, 0}}; -static const nsAttrValue::EnumTable kTableVAlignTable[] = { +static constexpr nsAttrValue::EnumTable kTableVAlignTable[] = { {"top", StyleVerticalAlignKeyword::Top}, {"middle", StyleVerticalAlignKeyword::Middle}, {"bottom", StyleVerticalAlignKeyword::Bottom}, @@ -1144,7 +1144,7 @@ static const nsAttrValue::EnumTable kTableVAlignTable[] = { bool nsGenericHTMLElement::ParseAlignValue(const nsAString& aString, nsAttrValue& aResult) { - static const nsAttrValue::EnumTable kAlignTable[] = { + static constexpr nsAttrValue::EnumTable kAlignTable[] = { {"left", StyleTextAlign::Left}, {"right", StyleTextAlign::Right}, @@ -1194,7 +1194,7 @@ bool nsGenericHTMLElement::ParseAlignValue(const nsAString& aString, //---------------------------------------- -static const nsAttrValue::EnumTable kTableHAlignTable[] = { +static constexpr nsAttrValue::EnumTable kTableHAlignTable[] = { {"left", StyleTextAlign::Left}, {"right", StyleTextAlign::Right}, {"center", StyleTextAlign::Center}, @@ -1209,7 +1209,7 @@ bool nsGenericHTMLElement::ParseTableHAlignValue(const nsAString& aString, //---------------------------------------- // This table is used for td, th, tr, col, thead, tbody and tfoot. -static const nsAttrValue::EnumTable kTableCellHAlignTable[] = { +static constexpr nsAttrValue::EnumTable kTableCellHAlignTable[] = { {"left", StyleTextAlign::MozLeft}, {"right", StyleTextAlign::MozRight}, {"center", StyleTextAlign::MozCenter}, @@ -1251,9 +1251,7 @@ bool nsGenericHTMLElement::ParseImageAttribute(nsAtom* aAttribute, bool nsGenericHTMLElement::ParseReferrerAttribute(const nsAString& aString, nsAttrValue& aResult) { using mozilla::dom::ReferrerInfo; - // This is a bit sketchy, we assume GetEnumString(…).get() points to a static - // buffer, relying on the fact that GetEnumString(…) returns a literal string. - static const nsAttrValue::EnumTable kReferrerPolicyTable[] = { + static constexpr nsAttrValue::EnumTable kReferrerPolicyTable[] = { {GetEnumString(ReferrerPolicy::No_referrer).get(), static_cast(ReferrerPolicy::No_referrer)}, {GetEnumString(ReferrerPolicy::Origin).get(), @@ -2770,7 +2768,7 @@ void nsGenericHTMLFormControlElement::SetFormAutofillState( //---------------------------------------------------------------------- -static const nsAttrValue::EnumTable kPopoverTargetActionTable[] = { +static constexpr nsAttrValue::EnumTable kPopoverTargetActionTable[] = { {"toggle", PopoverTargetAction::Toggle}, {"show", PopoverTargetAction::Show}, {"hide", PopoverTargetAction::Hide}, diff --git a/xpcom/string/nsTSubstring.cpp b/xpcom/string/nsTSubstring.cpp index 0cdd83d353e7..c83bd19d9358 100644 --- a/xpcom/string/nsTSubstring.cpp +++ b/xpcom/string/nsTSubstring.cpp @@ -439,6 +439,11 @@ void nsTSubstring::AssignASCII(const char* aData, size_type aLength) { } } +template +void nsTSubstring::AssignASCII(const nsLiteralCString& aData) { + AssignASCII(aData.get(), aData.Length()); +} + template bool nsTSubstring::AssignASCII(const char* aData, size_type aLength, const fallible_t& aFallible) { @@ -800,6 +805,11 @@ void nsTSubstring::AppendASCII(const char* aData, size_type aLength) { } } +template +void nsTSubstring::AppendASCII(const nsLiteralCString& aData) { + AppendASCII(aData.get(), aData.Length()); +} + template bool nsTSubstring::AppendASCII(const char* aData, const fallible_t& aFallible) { diff --git a/xpcom/string/nsTSubstring.h b/xpcom/string/nsTSubstring.h index 5f6851a1cebb..8a69fe7db635 100644 --- a/xpcom/string/nsTSubstring.h +++ b/xpcom/string/nsTSubstring.h @@ -457,6 +457,9 @@ class nsTSubstring : public mozilla::detail::nsTStringRepr { void NS_FASTCALL AssignASCII(const char* aData) { AssignASCII(aData, strlen(aData)); } + + void NS_FASTCALL AssignASCII(const nsLiteralCString& aData); + [[nodiscard]] bool NS_FASTCALL AssignASCII(const char* aData, const fallible_t& aFallible) { return AssignASCII(aData, strlen(aData), aFallible); @@ -720,6 +723,8 @@ class nsTSubstring : public mozilla::detail::nsTStringRepr { void AppendASCII(const char* aData, size_type aLength = size_type(-1)); + void AppendASCII(const nsLiteralCString& aData); + [[nodiscard]] bool AppendASCII(const char* aData, const fallible_t& aFallible);