Skip to content

Commit e09da7b

Browse files
committed
Bug 1582892. Expose the number of strings in a Web IDL enum in a nice way. r=edgar
Differential Revision: https://phabricator.services.mozilla.com/D49536 --HG-- extra : moz-landing-system : lando
1 parent 047d40e commit e09da7b

File tree

11 files changed

+35
-30
lines changed

11 files changed

+35
-30
lines changed

dom/base/ChromeUtils.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -672,12 +672,11 @@ void ChromeUtils::ClearRecentJSDevError(GlobalObject&) {
672672
return WebIDLProcType::_webidl
673673

674674
static WebIDLProcType ProcTypeToWebIDL(mozilla::ProcType aType) {
675-
// |strings| contains an extra non-enum value, so subtract one.
676675
// Max is the value of the last enum, not the length, so add one.
677-
static_assert(ArrayLength(WebIDLProcTypeValues::strings) - 1 ==
678-
static_cast<size_t>(ProcType::Max) + 1,
679-
"In order for this static cast to be okay, "
680-
"WebIDLProcType must match ProcType exactly");
676+
static_assert(
677+
WebIDLProcTypeValues::Count == static_cast<size_t>(ProcType::Max) + 1,
678+
"In order for this static cast to be okay, "
679+
"WebIDLProcType must match ProcType exactly");
681680

682681
switch (aType) {
683682
PROCTYPE_TO_WEBIDL_CASE(Web, Web);

dom/bindings/Codegen.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10098,17 +10098,25 @@ def __init__(self, enum):
1009810098
"""
1009910099
extern const EnumEntry ${entry_array}[${entry_count}];
1010010100

10101-
static_assert(static_cast<size_t>(${name}::EndGuard_) == ${entry_count} - 1,
10102-
"Mismatch between enum strings and enum values");
10101+
static constexpr size_t Count = ${real_entry_count};
10102+
10103+
// Our "${entry_array}" contains an extra entry with a null string.
10104+
static_assert(mozilla::ArrayLength(${entry_array}) - 1 == Count,
10105+
"Mismatch between enum strings and enum count");
10106+
10107+
static_assert(static_cast<size_t>(${name}::EndGuard_) == Count,
10108+
"Mismatch between enum value and enum count");
1010310109

1010410110
inline Span<const char> GetString(${name} stringId) {
10105-
MOZ_ASSERT(static_cast<${type}>(stringId) < static_cast<${type}>(${name}::EndGuard_));
10111+
MOZ_ASSERT(static_cast<${type}>(stringId) < Count);
1010610112
const EnumEntry& entry = ${entry_array}[static_cast<${type}>(stringId)];
1010710113
return MakeSpan(entry.value, entry.length);
1010810114
}
1010910115
""",
1011010116
entry_array=ENUM_ENTRY_VARIABLE_NAME,
1011110117
entry_count=self.nEnumStrings(),
10118+
# -1 because nEnumStrings() includes a string for EndGuard_
10119+
real_entry_count=self.nEnumStrings() - 1,
1011210120
name=self.enum.identifier.name,
1011310121
type=self.underlyingType())
1011410122
strings = CGNamespace(
@@ -14931,6 +14939,7 @@ def descriptorClearsPropsInSlots(descriptor):
1493114939
cgthings.extend(CGEnum(e) for e in enums)
1493214940

1493314941
bindingDeclareHeaders["mozilla/Span.h"] = enums
14942+
bindingDeclareHeaders["mozilla/ArrayUtils.h"] = enums
1493414943

1493514944
hasCode = (descriptors or callbackDescriptors or dictionaries or
1493614945
callbacks)

dom/cache/CacheStorage.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -470,9 +470,8 @@ already_AddRefed<CacheStorage> CacheStorage::Constructor(
470470
static_assert(
471471
CHROME_ONLY_NAMESPACE == (uint32_t)CacheStorageNamespace::Chrome,
472472
"Chrome namespace should match webidl Chrome enum");
473-
static_assert(
474-
NUMBER_OF_NAMESPACES == (uint32_t)CacheStorageNamespace::EndGuard_,
475-
"Number of namespace should match webidl endguard enum");
473+
static_assert(NUMBER_OF_NAMESPACES == CacheStorageNamespaceValues::Count,
474+
"Number of namespace should match webidl count");
476475

477476
Namespace ns = static_cast<Namespace>(aNamespace);
478477
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());

dom/cache/DBSchema.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ static_assert(int(HeadersGuardEnum::None) == 0 &&
229229
int(HeadersGuardEnum::Request_no_cors) == 2 &&
230230
int(HeadersGuardEnum::Response) == 3 &&
231231
int(HeadersGuardEnum::Immutable) == 4 &&
232-
int(HeadersGuardEnum::EndGuard_) == 5,
232+
HeadersGuardEnumValues::Count == 5,
233233
"HeadersGuardEnum values are as expected");
234234
static_assert(int(ReferrerPolicy::_empty) == 0 &&
235235
int(ReferrerPolicy::No_referrer) == 1 &&
@@ -240,38 +240,38 @@ static_assert(int(ReferrerPolicy::_empty) == 0 &&
240240
int(ReferrerPolicy::Same_origin) == 6 &&
241241
int(ReferrerPolicy::Strict_origin) == 7 &&
242242
int(ReferrerPolicy::Strict_origin_when_cross_origin) == 8 &&
243-
int(ReferrerPolicy::EndGuard_) == 9,
243+
ReferrerPolicyValues::Count == 9,
244244
"ReferrerPolicy values are as expected");
245245
static_assert(int(RequestMode::Same_origin) == 0 &&
246246
int(RequestMode::No_cors) == 1 &&
247247
int(RequestMode::Cors) == 2 &&
248248
int(RequestMode::Navigate) == 3 &&
249-
int(RequestMode::EndGuard_) == 4,
249+
RequestModeValues::Count == 4,
250250
"RequestMode values are as expected");
251251
static_assert(int(RequestCredentials::Omit) == 0 &&
252252
int(RequestCredentials::Same_origin) == 1 &&
253253
int(RequestCredentials::Include) == 2 &&
254-
int(RequestCredentials::EndGuard_) == 3,
254+
RequestCredentialsValues::Count == 3,
255255
"RequestCredentials values are as expected");
256256
static_assert(int(RequestCache::Default) == 0 &&
257257
int(RequestCache::No_store) == 1 &&
258258
int(RequestCache::Reload) == 2 &&
259259
int(RequestCache::No_cache) == 3 &&
260260
int(RequestCache::Force_cache) == 4 &&
261261
int(RequestCache::Only_if_cached) == 5 &&
262-
int(RequestCache::EndGuard_) == 6,
262+
RequestCacheValues::Count == 6,
263263
"RequestCache values are as expected");
264264
static_assert(int(RequestRedirect::Follow) == 0 &&
265265
int(RequestRedirect::Error) == 1 &&
266266
int(RequestRedirect::Manual) == 2 &&
267-
int(RequestRedirect::EndGuard_) == 3,
267+
RequestRedirectValues::Count == 3,
268268
"RequestRedirect values are as expected");
269269
static_assert(int(ResponseType::Basic) == 0 && int(ResponseType::Cors) == 1 &&
270270
int(ResponseType::Default) == 2 &&
271271
int(ResponseType::Error) == 3 &&
272272
int(ResponseType::Opaque) == 4 &&
273273
int(ResponseType::Opaqueredirect) == 5 &&
274-
int(ResponseType::EndGuard_) == 6,
274+
ResponseTypeValues::Count == 6,
275275
"ResponseType values are as expected");
276276

277277
// If the static_asserts below fails, it means that you have changed the

dom/console/ConsoleInstance.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ ConsoleLogLevel PrefToValue(const nsAString& aPref) {
5656
return ConsoleLogLevel::All;
5757
}
5858

59-
MOZ_ASSERT(index < (int)ConsoleLogLevel::EndGuard_);
59+
MOZ_ASSERT(index < ConsoleLogLevelValues::Count);
6060
return static_cast<ConsoleLogLevel>(index);
6161
}
6262

dom/media/mediasession/MediaSession.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class MediaSession final : public nsISupports, public nsWrapperCache {
5151
nsCOMPtr<nsPIDOMWindowInner> mParent;
5252

5353
RefPtr<MediaMetadata> mMediaMetadata;
54-
static const size_t ACTIONS = ArrayLength(MediaSessionActionValues::strings);
54+
static const size_t ACTIONS = MediaSessionActionValues::Count;
5555
RefPtr<MediaSessionActionHandler> mActionHandlers[ACTIONS] = {nullptr};
5656
};
5757

dom/permission/PermissionUtils.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ static const nsLiteralCString kPermissionTypes[] = {
1919
// clang-format on
2020
};
2121

22-
// `-1` for the last null entry.
23-
const size_t kPermissionNameCount =
24-
MOZ_ARRAY_LENGTH(PermissionNameValues::strings) - 1;
22+
const size_t kPermissionNameCount = PermissionNameValues::Count;
2523

2624
static_assert(MOZ_ARRAY_LENGTH(kPermissionTypes) == kPermissionNameCount,
2725
"kPermissionTypes and PermissionName count should match");

dom/serviceworkers/ServiceWorkerInfo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ static_assert(nsIServiceWorkerInfo::STATE_REDUNDANT ==
4343
"ServiceWorkerState enumeration value should match state values "
4444
"from nsIServiceWorkerInfo.");
4545
static_assert(nsIServiceWorkerInfo::STATE_UNKNOWN ==
46-
static_cast<uint16_t>(ServiceWorkerState::EndGuard_),
46+
ServiceWorkerStateValues::Count,
4747
"ServiceWorkerState enumeration value should match state values "
4848
"from nsIServiceWorkerInfo.");
4949

dom/serviceworkers/ServiceWorkerManager.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ static_assert(
127127
static_cast<uint32_t>(RequestRedirect::Manual),
128128
"RequestRedirect enumeration value should make Necko Redirect mode value.");
129129
static_assert(
130-
3 == static_cast<uint32_t>(RequestRedirect::EndGuard_),
130+
3 == RequestRedirectValues::Count,
131131
"RequestRedirect enumeration value should make Necko Redirect mode value.");
132132

133133
static_assert(
@@ -155,7 +155,7 @@ static_assert(
155155
static_cast<uint32_t>(RequestCache::Only_if_cached),
156156
"RequestCache enumeration value should match Necko Cache mode value.");
157157
static_assert(
158-
6 == static_cast<uint32_t>(RequestCache::EndGuard_),
158+
6 == RequestCacheValues::Count,
159159
"RequestCache enumeration value should match Necko Cache mode value.");
160160

161161
static_assert(static_cast<uint16_t>(ServiceWorkerUpdateViaCache::Imports) ==

dom/webauthn/WebAuthnManager.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -539,8 +539,7 @@ already_AddRefed<Promise> WebAuthnManager::GetAssertion(
539539
NS_ConvertUTF16toUTF8 cStr(str);
540540
int i = FindEnumStringIndexImpl(
541541
cStr.get(), cStr.Length(), AuthenticatorTransportValues::strings);
542-
if (i < 0 ||
543-
i >= static_cast<int>(AuthenticatorTransport::EndGuard_)) {
542+
if (i < 0) {
544543
continue; // Unknown enum
545544
}
546545
AuthenticatorTransport t = static_cast<AuthenticatorTransport>(i);

0 commit comments

Comments
 (0)