Skip to content

Commit

Permalink
bindings generator: support default ByteString values in dictionary
Browse files Browse the repository at this point in the history
  • Loading branch information
malisas committed Aug 10, 2016
1 parent 8cd4b77 commit 7fd65af
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
17 changes: 14 additions & 3 deletions components/script/dom/bindings/codegen/CodegenRust.py
Expand Up @@ -866,11 +866,22 @@ def wrapObjectTemplate(templateBody, nullValue, isDefinitelyObject, type,
" 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():
default = "Some(%s)" % default

declType = "ByteString"
if type.nullable():
declType = CGWrapper(declType, pre="Option<", post=">")
declType = "Option<%s>" % declType

return handleOptional(conversionCode, declType, handleDefaultNull("None"))
return handleOptional(conversionCode, CGGeneric(declType), default)

if type.isEnum():
assert not isEnforceRange and not isClamp
Expand Down
13 changes: 13 additions & 0 deletions components/script/dom/bindings/codegen/parser/WebIDL.py
Expand Up @@ -3391,6 +3391,11 @@ def coerceToType(self, type, location):
# 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])

Expand Down Expand Up @@ -5759,6 +5764,14 @@ def p_ConstValueBoolean(self, p):
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
Expand Down
3 changes: 3 additions & 0 deletions components/script/dom/testbinding.rs
Expand Up @@ -312,13 +312,15 @@ 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,
floatValue: Finite::new(1.0).unwrap(),
longLongValue: 54,
longValue: 12,
nullableBooleanValue: None,
nullableBytestringValue: None,
nullableByteValue: None,
nullableDoubleValue: None,
nullableFloatValue: None,
Expand Down Expand Up @@ -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>) {}
Expand Down
3 changes: 3 additions & 0 deletions components/script/dom/webidls/TestBinding.webidl
Expand Up @@ -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";
Expand All @@ -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";
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit 7fd65af

Please sign in to comment.