summaryrefslogtreecommitdiffstats
path: root/dom/bindings/BindingUtils.h
diff options
context:
space:
mode:
Diffstat (limited to 'dom/bindings/BindingUtils.h')
-rw-r--r--dom/bindings/BindingUtils.h48
1 files changed, 39 insertions, 9 deletions
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<true>(BindingCallContext& cx,
deflated.get(), type);
}
+namespace binding_detail {
+
template <typename CharT>
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<const nsLiteralCString>& 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 <bool InvalidValueFatal>
inline bool FindEnumStringIndex(BindingCallContext& cx, JS::Handle<JS::Value> v,
- const EnumEntry* values, const char* type,
- const char* sourceDescription, int* index) {
+ const Span<const nsLiteralCString>& values,
+ const char* type, const char* sourceDescription,
+ int* index) {
// JS_StringEqualsAscii is slow as molasses, so don't use it here.
JS::Rooted<JSString*> str(cx, JS::ToString(cx, v));
if (!str) {
@@ -1405,6 +1409,31 @@ inline bool FindEnumStringIndex(BindingCallContext& cx, JS::Handle<JS::Value> v,
return EnumValueNotFound<InvalidValueFatal>(cx, str, type, sourceDescription);
}
+} // namespace binding_detail
+
+template <typename Enum, class StringT>
+inline Maybe<Enum> StringToEnum(const StringT& aString) {
+ int index = binding_detail::FindEnumStringIndexImpl(
+ aString.BeginReading(), aString.Length(),
+ binding_detail::EnumStrings<Enum>::Values);
+ return index >= 0 ? Some(static_cast<Enum>(index)) : Nothing();
+}
+
+template <typename Enum>
+inline const nsCString& GetEnumString(Enum stringId) {
+ MOZ_RELEASE_ASSERT(
+ static_cast<size_t>(stringId) <
+ mozilla::ArrayLength(binding_detail::EnumStrings<Enum>::Values));
+ return binding_detail::EnumStrings<Enum>::Values[static_cast<size_t>(
+ stringId)];
+}
+
+template <typename Enum>
+constexpr mozilla::detail::EnumeratedRange<Enum> MakeWebIDLEnumeratedRange() {
+ return MakeInclusiveEnumeratedRange(ContiguousEnumValues<Enum>::min,
+ ContiguousEnumValues<Enum>::max);
+}
+
inline nsWrapperCache* GetWrapperCache(const ParentObject& aParentObject) {
return aParentObject.mWrapperCache;
}
@@ -3255,6 +3284,7 @@ already_AddRefed<Promise> CreateRejectedPromiseFromThrownException(
} // namespace binding_detail
} // namespace dom
+
} // namespace mozilla
#endif /* mozilla_dom_BindingUtils_h__ */