Skip to content

Commit

Permalink
Bug 1640839 - Generate enum helper for WebIDL enums. r=mccr8,media-pl…
Browse files Browse the repository at this point in the history
…ayback-reviewers,padenot

Differential Revision: https://phabricator.services.mozilla.com/D201335
  • Loading branch information
petervanderbeken committed Mar 2, 2024
1 parent 2b747df commit c9851aa
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 10 deletions.
54 changes: 46 additions & 8 deletions dom/bindings/Codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -12401,7 +12401,7 @@ def __init__(self, enum):
# -1 because nEnumStrings() includes a string for EndGuard_
real_entry_count=self.nEnumStrings() - 1,
name=self.enum.identifier.name,
type=self.underlyingType(),
type=CGEnum.underlyingType(enum),
)
strings = CGNamespace(
self.stringsNamespace(),
Expand Down Expand Up @@ -12431,15 +12431,14 @@ def stringsNamespace(self):
def nEnumStrings(self):
return len(self.enum.values()) + 1

def underlyingType(self):
count = self.nEnumStrings()
@staticmethod
def underlyingType(enum):
count = len(enum.values())
if count <= 256:
return "uint8_t"
if count <= 65536:
return "uint16_t"
raise ValueError(
"Enum " + self.enum.identifier.name + " has more than 65536 values"
)
raise ValueError("Enum " + enum.identifier.name + " has more than 65536 values")

def declare(self):
decl = fill(
Expand All @@ -12450,7 +12449,7 @@ def declare(self):
};
""",
name=self.enum.identifier.name,
ty=self.underlyingType(),
ty=CGEnum.underlyingType(self.enum),
enums=",\n".join(map(getEnumValueName, self.enum.values())) + ",\n",
)

Expand All @@ -12463,6 +12462,37 @@ def deps(self):
return self.enum.getDeps()


class CGMaxContiguousEnumValue(CGThing):
def __init__(self, enum):
CGThing.__init__(self)
self.enum = enum

def declare(self):
enumValues = self.enum.values()
return fill(
"""
template <>
struct MaxContiguousEnumValue<dom::${name}>
{
static constexpr dom::${name} value = dom::${name}::${maxValue};

static_assert(static_cast<${ty}>(dom::${name}::${minValue}) == 0,
"We rely on this in ContiguousEnumValues");
};
""",
name=self.enum.identifier.name,
ty=CGEnum.underlyingType(self.enum),
maxValue=getEnumValueName(enumValues[-1]),
minValue=getEnumValueName(enumValues[0]),
)

def define(self):
return ""

def deps(self):
return self.enum.getDeps()


def getUnionAccessorSignatureType(type, descriptorProvider):
"""
Returns the types that are used in the getter and setter signatures for
Expand Down Expand Up @@ -19073,9 +19103,11 @@ def descriptorClearsPropsInSlots(descriptor):
# Do codegen for all the enums
enums = config.getEnums(webIDLFile)
cgthings.extend(CGEnum(e) for e in enums)
maxEnumValues = CGList([CGMaxContiguousEnumValue(e) for e in enums], "\n")

bindingDeclareHeaders["mozilla/Span.h"] = enums
bindingDeclareHeaders["mozilla/ArrayUtils.h"] = enums
bindingDeclareHeaders["mozilla/EnumTypeTraits.h"] = enums

hasCode = descriptors or callbackDescriptors or dictionaries or callbacks
bindingHeaders["mozilla/dom/BindingUtils.h"] = hasCode
Expand Down Expand Up @@ -19175,7 +19207,13 @@ def getParentDescriptor(desc):
curr = CGWrapper(CGList(cgthings, "\n\n"), post="\n\n")

# Wrap all of that in our namespaces.
curr = CGNamespace.build(["mozilla", "dom"], CGWrapper(curr, pre="\n"))

if len(maxEnumValues) > 0:
curr = CGNamespace("dom", CGWrapper(curr, pre="\n"))
curr = CGWrapper(CGList([curr, maxEnumValues], "\n\n"), post="\n\n")
curr = CGNamespace("mozilla", CGWrapper(curr, pre="\n"))
else:
curr = CGNamespace.build(["mozilla", "dom"], CGWrapper(curr, pre="\n"))

curr = CGList(
[
Expand Down
3 changes: 1 addition & 2 deletions dom/media/mediasession/MediaSession.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,7 @@ class MediaSession final : public nsIDocumentActivity, public nsWrapperCache {

RefPtr<MediaMetadata> mMediaMetadata;

EnumeratedArray<MediaSessionAction, RefPtr<MediaSessionActionHandler>,
size_t(MediaSessionAction::EndGuard_)>
EnumeratedArray<MediaSessionAction, RefPtr<MediaSessionActionHandler>>
mActionHandlers;

// This is used as is a hint for the user agent to determine whether the
Expand Down

0 comments on commit c9851aa

Please sign in to comment.