mirror of
https://github.com/servo/servo.git
synced 2025-07-24 15:50:21 +01:00
bindings generator: support default ByteString values in dictionary
This commit is contained in:
parent
8cd4b772e9
commit
7fd65affab
4 changed files with 34 additions and 4 deletions
|
@ -866,11 +866,22 @@ def getJSToNativeConversionInfo(type, descriptorProvider, failureCode=None,
|
|||
" Err(_) => { %s },\n"
|
||||
"}" % exceptionCode)
|
||||
|
||||
declType = CGGeneric("ByteString")
|
||||
if defaultValue is None:
|
||||
default = None
|
||||
elif isinstance(defaultValue, IDLNullValue):
|
||||
assert type.nullable()
|
||||
default = "None"
|
||||
else:
|
||||
assert defaultValue.type.tag() in (IDLType.Tags.domstring, IDLType.Tags.bytestring)
|
||||
default = 'ByteString::new(b"%s".to_vec())' % defaultValue.value
|
||||
if type.nullable():
|
||||
declType = CGWrapper(declType, pre="Option<", post=">")
|
||||
default = "Some(%s)" % default
|
||||
|
||||
return handleOptional(conversionCode, declType, handleDefaultNull("None"))
|
||||
declType = "ByteString"
|
||||
if type.nullable():
|
||||
declType = "Option<%s>" % declType
|
||||
|
||||
return handleOptional(conversionCode, CGGeneric(declType), default)
|
||||
|
||||
if type.isEnum():
|
||||
assert not isEnforceRange and not isClamp
|
||||
|
|
|
@ -3391,6 +3391,11 @@ class IDLValue(IDLObject):
|
|||
# extra normalization step.
|
||||
assert self.type.isDOMString()
|
||||
return self
|
||||
elif self.type.isString() and type.isByteString():
|
||||
# Allow ByteStrings to use default value just like
|
||||
# DOMString. No coercion is required here.
|
||||
assert self.type.isDOMString()
|
||||
return self
|
||||
raise WebIDLError("Cannot coerce type %s to type %s." %
|
||||
(self.type, type), [location])
|
||||
|
||||
|
@ -5759,6 +5764,14 @@ class Parser(Tokenizer):
|
|||
booleanType = BuiltinTypes[IDLBuiltinType.Types.boolean]
|
||||
p[0] = IDLValue(location, booleanType, p[1])
|
||||
|
||||
def p_ConstValueByteString(self, p):
|
||||
"""
|
||||
ConstValue : BYTESTRING
|
||||
"""
|
||||
location = self.getLocation(p, 1)
|
||||
bytestringType = BuiltinTypes[IDLBuiltinType.Types.bytestring]
|
||||
p[0] = IDLValue(location, bytestringType, p[1])
|
||||
|
||||
def p_ConstValueInteger(self, p):
|
||||
"""
|
||||
ConstValue : INTEGER
|
||||
|
|
|
@ -312,6 +312,7 @@ impl TestBindingMethods for TestBinding {
|
|||
UnrestrictedDoubleValue: 0.0,
|
||||
anyValue: NullValue(),
|
||||
booleanValue: false,
|
||||
bytestringValue: ByteString::new(vec![]),
|
||||
byteValue: 0,
|
||||
doubleValue: Finite::new(1.0).unwrap(),
|
||||
enumValue: TestEnum::Foo,
|
||||
|
@ -319,6 +320,7 @@ impl TestBindingMethods for TestBinding {
|
|||
longLongValue: 54,
|
||||
longValue: 12,
|
||||
nullableBooleanValue: None,
|
||||
nullableBytestringValue: None,
|
||||
nullableByteValue: None,
|
||||
nullableDoubleValue: None,
|
||||
nullableFloatValue: None,
|
||||
|
@ -506,6 +508,7 @@ impl TestBindingMethods for TestBinding {
|
|||
fn PassOptionalUnsignedLongLongWithDefault(&self, _: u64) {}
|
||||
fn PassOptionalStringWithDefault(&self, _: DOMString) {}
|
||||
fn PassOptionalUsvstringWithDefault(&self, _: USVString) {}
|
||||
fn PassOptionalBytestringWithDefault(&self, _: ByteString) {}
|
||||
fn PassOptionalEnumWithDefault(&self, _: TestEnum) {}
|
||||
|
||||
fn PassOptionalNullableBooleanWithDefault(&self, _: Option<bool>) {}
|
||||
|
|
|
@ -53,6 +53,7 @@ dictionary TestDictionaryDefaults {
|
|||
float floatValue = 7.0;
|
||||
unrestricted double UnrestrictedDoubleValue = 7.0;
|
||||
double doubleValue = 7.0;
|
||||
ByteString bytestringValue = "foo";
|
||||
DOMString stringValue = "foo";
|
||||
USVString usvstringValue = "foo";
|
||||
TestEnum enumValue = "bar";
|
||||
|
@ -71,6 +72,7 @@ dictionary TestDictionaryDefaults {
|
|||
float? nullableFloatValue = 7.0;
|
||||
unrestricted double? nullableUnrestrictedDoubleValue = 7.0;
|
||||
double? nullableDoubleValue = 7.0;
|
||||
ByteString? nullableBytestringValue = "foo";
|
||||
DOMString? nullableStringValue = "foo";
|
||||
USVString? nullableUsvstringValue = "foo";
|
||||
// TestEnum? nullableEnumValue = "bar";
|
||||
|
@ -344,6 +346,7 @@ interface TestBinding {
|
|||
void passOptionalUnsignedLongWithDefault(optional unsigned long arg = 6);
|
||||
void passOptionalLongLongWithDefault(optional long long arg = -12);
|
||||
void passOptionalUnsignedLongLongWithDefault(optional unsigned long long arg = 17);
|
||||
void passOptionalBytestringWithDefault(optional ByteString arg = "x");
|
||||
void passOptionalStringWithDefault(optional DOMString arg = "x");
|
||||
void passOptionalUsvstringWithDefault(optional USVString arg = "x");
|
||||
void passOptionalEnumWithDefault(optional TestEnum arg = "foo");
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue