From 40a355a42d4a9444dc753c04c6608dade2f06a23 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 19 Apr 2024 03:13:27 +0200 Subject: Adding upstream version 125.0.1. Signed-off-by: Daniel Baumann --- dom/bindings/BindingUtils.h | 48 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 39 insertions(+), 9 deletions(-) (limited to 'dom/bindings/BindingUtils.h') diff --git a/dom/bindings/BindingUtils.h b/dom/bindings/BindingUtils.h index df3c509ab7..dd7a42b670 100644 --- a/dom/bindings/BindingUtils.h +++ b/dom/bindings/BindingUtils.h @@ -24,6 +24,8 @@ #include "mozilla/Array.h" #include "mozilla/Assertions.h" #include "mozilla/DeferredFinalize.h" +#include "mozilla/EnumTypeTraits.h" +#include "mozilla/EnumeratedRange.h" #include "mozilla/UniquePtr.h" #include "mozilla/dom/BindingCallContext.h" #include "mozilla/dom/BindingDeclarations.h" @@ -1343,26 +1345,27 @@ inline bool EnumValueNotFound(BindingCallContext& cx, deflated.get(), type); } +namespace binding_detail { + template inline int FindEnumStringIndexImpl(const CharT* chars, size_t length, - const EnumEntry* values) { - int i = 0; - for (const EnumEntry* value = values; value->value; ++value, ++i) { - if (length != value->length) { + const Span& values) { + for (size_t i = 0; i < values.Length(); ++i) { + const nsLiteralCString& value = values[i]; + if (length != value.Length()) { continue; } bool equal = true; - const char* val = value->value; for (size_t j = 0; j != length; ++j) { - if (unsigned(val[j]) != unsigned(chars[j])) { + if (unsigned(value.CharAt(j)) != unsigned(chars[j])) { equal = false; break; } } if (equal) { - return i; + return (int)i; } } @@ -1371,8 +1374,9 @@ inline int FindEnumStringIndexImpl(const CharT* chars, size_t length, template inline bool FindEnumStringIndex(BindingCallContext& cx, JS::Handle v, - const EnumEntry* values, const char* type, - const char* sourceDescription, int* index) { + const Span& values, + const char* type, const char* sourceDescription, + int* index) { // JS_StringEqualsAscii is slow as molasses, so don't use it here. JS::Rooted str(cx, JS::ToString(cx, v)); if (!str) { @@ -1405,6 +1409,31 @@ inline bool FindEnumStringIndex(BindingCallContext& cx, JS::Handle v, return EnumValueNotFound(cx, str, type, sourceDescription); } +} // namespace binding_detail + +template +inline Maybe StringToEnum(const StringT& aString) { + int index = binding_detail::FindEnumStringIndexImpl( + aString.BeginReading(), aString.Length(), + binding_detail::EnumStrings::Values); + return index >= 0 ? Some(static_cast(index)) : Nothing(); +} + +template +inline const nsCString& GetEnumString(Enum stringId) { + MOZ_RELEASE_ASSERT( + static_cast(stringId) < + mozilla::ArrayLength(binding_detail::EnumStrings::Values)); + return binding_detail::EnumStrings::Values[static_cast( + stringId)]; +} + +template +constexpr mozilla::detail::EnumeratedRange MakeWebIDLEnumeratedRange() { + return MakeInclusiveEnumeratedRange(ContiguousEnumValues::min, + ContiguousEnumValues::max); +} + inline nsWrapperCache* GetWrapperCache(const ParentObject& aParentObject) { return aParentObject.mWrapperCache; } @@ -3255,6 +3284,7 @@ already_AddRefed CreateRejectedPromiseFromThrownException( } // namespace binding_detail } // namespace dom + } // namespace mozilla #endif /* mozilla_dom_BindingUtils_h__ */ -- cgit v1.2.3