Auto merge of #24471 - saschanaz:enum-default, r=ferjm

Support enum value as a union default value

<!-- Please describe your changes on the following line: -->

Didn't implement the actual latency thing because the relevant things are already marked as TODO.

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `___` with appropriate data: -->
- [x] `./mach build -d` does not report any errors
- [x] `./mach test-tidy` does not report any errors
- [x] These changes fix #21342

<!-- Either: -->
- [x] There are tests for these changes

<!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.-->

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->
This commit is contained in:
bors-servo 2019-10-21 03:27:18 -04:00 committed by GitHub
commit 5011e9c21c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 29 deletions

View file

@ -736,6 +736,13 @@ def getJSToNativeConversionInfo(type, descriptorProvider, failureCode=None,
default = '%s::USVString(USVString("%s".to_owned()))' % (
union_native_type(type),
defaultValue.value)
elif defaultValue.type.isEnum():
enum = defaultValue.type.inner.identifier.name
default = "%s::%s(%s::%s)" % (
union_native_type(type),
enum,
enum,
getEnumValueName(defaultValue.value))
else:
raise("We don't currently support default values that aren't null, boolean or default dictionary")
elif dictionaries:
@ -2373,20 +2380,19 @@ def getAllTypes(descriptors, dictionaries, callbacks, typedefs):
"""
Generate all the types we're dealing with. For each type, a tuple
containing type, descriptor, dictionary is yielded. The
descriptor and dictionary can be None if the type does not come
from a descriptor or dictionary; they will never both be non-None.
descriptor can be None if the type does not come from a descriptor.
"""
for d in descriptors:
for t in getTypesFromDescriptor(d):
yield (t, d, None)
yield (t, d)
for dictionary in dictionaries:
for t in getTypesFromDictionary(dictionary):
yield (t, None, dictionary)
yield (t, None)
for callback in callbacks:
for t in getTypesFromCallback(callback):
yield (t, None, None)
yield (t, None)
for typedef in typedefs:
yield (typedef.innerType, None, None)
yield (typedef.innerType, None)
def UnionTypes(descriptors, dictionaries, callbacks, typedefs, config):
@ -2411,6 +2417,7 @@ def UnionTypes(descriptors, dictionaries, callbacks, typedefs, config):
'crate::dom::bindings::str::DOMString',
'crate::dom::bindings::str::USVString',
'crate::dom::bindings::trace::RootedTraceableBox',
'crate::dom::bindings::utils::find_enum_value',
'crate::dom::types::*',
'crate::script_runtime::JSContext as SafeJSContext',
'js::error::throw_type_error',
@ -2426,13 +2433,17 @@ def UnionTypes(descriptors, dictionaries, callbacks, typedefs, config):
# Now find all the things we'll need as arguments and return values because
# we need to wrap or unwrap them.
unionStructs = dict()
for (t, descriptor, dictionary) in getAllTypes(descriptors, dictionaries, callbacks, typedefs):
if dictionary:
imports.append("%s::%s" % (CGDictionary.makeModuleName(dictionary),
CGDictionary.makeDictionaryName(dictionary)))
for (t, descriptor) in getAllTypes(descriptors, dictionaries, callbacks, typedefs):
t = t.unroll()
if not t.isUnion():
continue
for memberType in t.flatMemberTypes:
if memberType.isDictionary() or memberType.isEnum():
memberModule = getModuleFromObject(memberType)
memberName = memberType.inner.identifier.name
imports.append("%s::%s" % (memberModule, memberName))
if memberType.isEnum():
imports.append("%s::%sValues" % (memberModule, memberName))
name = str(t)
if name not in unionStructs:
provider = descriptor or config.getDescriptorProvider()