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

@ -13,6 +13,7 @@ use crate::dom::bindings::codegen::Bindings::AudioContextBinding::{
};
use crate::dom::bindings::codegen::Bindings::BaseAudioContextBinding::AudioContextState;
use crate::dom::bindings::codegen::Bindings::BaseAudioContextBinding::BaseAudioContextBinding::BaseAudioContextMethods;
use crate::dom::bindings::codegen::UnionTypes::AudioContextLatencyCategoryOrDouble;
use crate::dom::bindings::error::{Error, Fallible};
use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::num::Finite;
@ -48,7 +49,12 @@ impl AudioContext {
);
// Step 4.1.
let latency_hint = options.latencyHint;
let latency_hint = match options.latencyHint {
AudioContextLatencyCategoryOrDouble::AudioContextLatencyCategory(category) => category,
AudioContextLatencyCategoryOrDouble::Double(_) => {
AudioContextLatencyCategory::Interactive
}, // TODO
};
// Step 4.2. The sample rate is set during the creation of the BaseAudioContext.
// servo-media takes care of setting the default sample rate of the output device
@ -250,7 +256,12 @@ impl<'a> From<&'a AudioContextOptions> for RealTimeAudioContextOptions {
fn from(options: &AudioContextOptions) -> Self {
Self {
sample_rate: *options.sampleRate.unwrap_or(Finite::wrap(44100.)),
latency_hint: options.latencyHint.into(),
latency_hint: match options.latencyHint {
AudioContextLatencyCategoryOrDouble::AudioContextLatencyCategory(category) => {
category.into()
},
AudioContextLatencyCategoryOrDouble::Double(_) => LatencyCategory::Interactive, // TODO
},
}
}
}

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()

View file

@ -13,7 +13,7 @@ enum AudioContextLatencyCategory {
};
dictionary AudioContextOptions {
AudioContextLatencyCategory latencyHint = "interactive";
(AudioContextLatencyCategory or double) latencyHint = "interactive";
float sampleRate;
};

View file

@ -1,23 +1,8 @@
[audiocontextoptions.html]
[X creating two high latency contexts incorrectly threw TypeError: "'0' is not a valid enum value for enumeration 'AudioContextLatencyCategory'.".]
expected: FAIL
[X context = new AudioContext({'latencyHint': interactiveLatency/2}) incorrectly threw TypeError: "'0' is not a valid enum value for enumeration 'AudioContextLatencyCategory'.".]
expected: FAIL
[X context = new AudioContext({'latencyHint': validLatency}) incorrectly threw TypeError: "'0' is not a valid enum value for enumeration 'AudioContextLatencyCategory'.".]
expected: FAIL
[X context = new AudioContext({sampleRate: 1}) did not throw an exception.]
expected: FAIL
[< [test-audiocontextoptions-latencyHint-double\] 3 out of 6 assertions were failed.]
expected: FAIL
[# AUDIT TASK RUNNER FINISHED: 2 out of 3 tasks were failed.]
expected: FAIL
[Executing "test-audiocontextoptions-latencyHint-double"]
[# AUDIT TASK RUNNER FINISHED: 1 out of 3 tasks were failed.]
expected: FAIL
[X context = new AudioContext({sampleRate: 1000000}) did not throw an exception.]