Skip to content

Commit

Permalink
Handle default empty sequence values
Browse files Browse the repository at this point in the history
  • Loading branch information
KiChjang committed Nov 1, 2018
1 parent 95bfaa0 commit 99589d2
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
33 changes: 18 additions & 15 deletions components/script/dom/bindings/codegen/CodegenRust.py
Expand Up @@ -17,6 +17,7 @@
from WebIDL import (
BuiltinTypes,
IDLBuiltinType,
IDLEmptySequenceValue,
IDLInterfaceMember,
IDLNullableType,
IDLNullValue,
Expand Down Expand Up @@ -673,17 +674,19 @@ def onFailureNotCallable(failureCode):
('throw_type_error(cx, \"%s is not callable.\");\n'
'%s' % (firstCap(sourceDescription), exceptionCode)))

# A helper function for handling null default values. Checks that the
# default value, if it exists, is null.
def handleDefaultNull(nullValue):
# A helper function for handling default values.
def handleDefault(nullValue):
if defaultValue is None:
return None

if not isinstance(defaultValue, IDLNullValue):
raise TypeError("Can't handle non-null default value here")
if isinstance(defaultValue, IDLNullValue):
assert type.nullable() or type.isDictionary()
return nullValue
elif isinstance(defaultValue, IDLEmptySequenceValue):
assert type.isSequence()
return "Vec::new()"

assert type.nullable() or type.isDictionary()
return nullValue
raise TypeError("Can't handle non-null or non-empty sequence default value here")

# A helper function for wrapping up the template body for
# possibly-nullable objecty stuff
Expand Down Expand Up @@ -726,7 +729,7 @@ def wrapObjectTemplate(templateBody, nullValue, isDefinitelyObject, type,
" _ => { %s },\n"
"}" % (config, indent(failOrPropagate, 8), exceptionCode))

return handleOptional(templateBody, declType, handleDefaultNull("None"))
return handleOptional(templateBody, declType, handleDefault("None"))

if type.isUnion():
declType = CGGeneric(union_native_type(type))
Expand Down Expand Up @@ -758,7 +761,7 @@ def wrapObjectTemplate(templateBody, nullValue, isDefinitelyObject, type,
else:
default = None
else:
default = handleDefaultNull("None")
default = handleDefault("None")

return handleOptional(templateBody, declType, default)

Expand Down Expand Up @@ -810,7 +813,7 @@ def wrapObjectTemplate(templateBody, nullValue, isDefinitelyObject, type,
declType = CGGeneric("&Promise")
else:
declType = CGGeneric("Rc<Promise>")
return handleOptional(templateBody, declType, handleDefaultNull("None"))
return handleOptional(templateBody, declType, handleDefault("None"))

if type.isGeckoInterface():
assert not isEnforceRange and not isClamp
Expand All @@ -828,7 +831,7 @@ def wrapObjectTemplate(templateBody, nullValue, isDefinitelyObject, type,
isDefinitelyObject, type,
failureCode)

return handleOptional(template, declType, handleDefaultNull("None"))
return handleOptional(template, declType, handleDefault("None"))

conversionFunction = "root_from_handlevalue"
descriptorType = descriptor.returnType
Expand Down Expand Up @@ -875,7 +878,7 @@ def wrapObjectTemplate(templateBody, nullValue, isDefinitelyObject, type,
templateBody = wrapObjectTemplate(templateBody, "None",
isDefinitelyObject, type, failureCode)

return handleOptional(templateBody, declType, handleDefaultNull("None"))
return handleOptional(templateBody, declType, handleDefault("None"))

if is_typed_array(type):
if failureCode is None:
Expand Down Expand Up @@ -918,7 +921,7 @@ def wrapObjectTemplate(templateBody, nullValue, isDefinitelyObject, type,
templateBody = wrapObjectTemplate(templateBody, "None",
isDefinitelyObject, type, failureCode)

return handleOptional(templateBody, declType, handleDefaultNull("None"))
return handleOptional(templateBody, declType, handleDefault("None"))

elif type.isSpiderMonkeyInterface():
raise TypeError("Can't handle SpiderMonkey interface arguments other than typed arrays yet")
Expand Down Expand Up @@ -1145,7 +1148,7 @@ def wrapObjectTemplate(templateBody, nullValue, isDefinitelyObject, type,
isDefinitelyObject, type, failureCode)

return handleOptional(templateBody, declType,
handleDefaultNull(default))
handleDefault(default))

if type.isDictionary():
# There are no nullable dictionaries
Expand All @@ -1167,7 +1170,7 @@ def wrapObjectTemplate(templateBody, nullValue, isDefinitelyObject, type,
" _ => { %s },\n"
"}" % (indent(failOrPropagate, 8), exceptionCode))

return handleOptional(template, declType, handleDefaultNull(empty))
return handleOptional(template, declType, handleDefault(empty))

if type.isVoid():
# This one only happens for return values, and its easy: Just
Expand Down
2 changes: 2 additions & 0 deletions components/script/dom/testbinding.rs
Expand Up @@ -554,6 +554,7 @@ impl TestBindingMethods for TestBinding {
dict: RootedTraceableBox::new(TestDictionaryDefaults {
UnrestrictedDoubleValue: 0.0,
anyValue: RootedTraceableBox::new(Heap::default()),
arrayValue: Vec::new(),
booleanValue: false,
bytestringValue: ByteString::new(vec![]),
byteValue: 0,
Expand Down Expand Up @@ -790,6 +791,7 @@ impl TestBindingMethods for TestBinding {
fn PassOptionalUsvstringWithDefault(&self, _: USVString) {}
fn PassOptionalBytestringWithDefault(&self, _: ByteString) {}
fn PassOptionalEnumWithDefault(&self, _: TestEnum) {}
fn PassOptionalSequenceWithDefault(&self, _: Vec<i32>) {}

fn PassOptionalNullableBooleanWithDefault(&self, _: Option<bool>) {}
fn PassOptionalNullableByteWithDefault(&self, _: Option<i8>) {}
Expand Down
2 changes: 2 additions & 0 deletions components/script/dom/webidls/TestBinding.webidl
Expand Up @@ -64,6 +64,7 @@ dictionary TestDictionaryDefaults {
USVString usvstringValue = "foo";
TestEnum enumValue = "bar";
any anyValue = null;
sequence<object> arrayValue = [];

boolean? nullableBooleanValue = false;
byte? nullableByteValue = 7;
Expand Down Expand Up @@ -380,6 +381,7 @@ interface TestBinding {
void passOptionalStringWithDefault(optional DOMString arg = "x");
void passOptionalUsvstringWithDefault(optional USVString arg = "x");
void passOptionalEnumWithDefault(optional TestEnum arg = "foo");
void passOptionalSequenceWithDefault(optional sequence<long> seq = []);
// void passOptionalUnionWithDefault(optional (HTMLElement or long) arg = 9);
// void passOptionalUnion2WithDefault(optional(Event or DOMString)? data = "foo");

Expand Down

0 comments on commit 99589d2

Please sign in to comment.