Implement a cleaner way of dealing with optional arguments in codegen and use it for primitive types.

This puts the Some constructor outside the match, making the actual
generating code independent of the optionality of the argument.
This commit is contained in:
Ms2ger 2014-04-26 17:10:59 +02:00
parent 35f6a24de7
commit b264c65f2e

View file

@ -494,6 +494,16 @@ def getJSToNativeConversionTemplate(type, descriptorProvider, failureCode=None,
if exceptionCode is None: if exceptionCode is None:
exceptionCode = "return 0;" exceptionCode = "return 0;"
def handleOptional(template, declType, isOptional):
if isOptional:
template = "Some(%s)" % template
declType = CGWrapper(declType, pre="Option<", post=">")
initialValue = "None"
else:
initialValue = None
return (template, declType, isOptional, initialValue)
# Unfortunately, .capitalize() on a string will lowercase things inside the # Unfortunately, .capitalize() on a string will lowercase things inside the
# string, which we do not want. # string, which we do not want.
def firstCap(string): def firstCap(string):
@ -800,21 +810,16 @@ def getJSToNativeConversionTemplate(type, descriptorProvider, failureCode=None,
if failureCode is None: if failureCode is None:
failureCode = 'return 0' failureCode = 'return 0'
value = "v"
declType = CGGeneric(builtinNames[type.tag()]) declType = CGGeneric(builtinNames[type.tag()])
if type.nullable(): if type.nullable():
declType = CGWrapper(declType, pre="Option<", post=">") declType = CGWrapper(declType, pre="Option<", post=">")
if isOptional:
value = "Some(%s)" % value
declType = CGWrapper(declType, pre="Option<", post=">")
#XXXjdm support conversionBehavior here #XXXjdm support conversionBehavior here
template = ( template = (
"match FromJSValConvertible::from_jsval(cx, ${val}, ()) {\n" "match FromJSValConvertible::from_jsval(cx, ${val}, ()) {\n"
" Ok(v) => %s,\n" " Ok(v) => v,\n"
" Err(_) => { %s }\n" " Err(_) => { %s }\n"
"}" % (value, exceptionCode)) "}" % exceptionCode)
if defaultValue is not None: if defaultValue is not None:
if isinstance(defaultValue, IDLNullValue): if isinstance(defaultValue, IDLNullValue):
@ -835,7 +840,7 @@ def getJSToNativeConversionTemplate(type, descriptorProvider, failureCode=None,
CGGeneric(template), CGGeneric(template),
CGGeneric(defaultStr)).define() CGGeneric(defaultStr)).define()
return (template, declType, isOptional, "None" if isOptional else None) return handleOptional(template, declType, isOptional)
def instantiateJSToNativeConversionTemplate(templateTuple, replacements, def instantiateJSToNativeConversionTemplate(templateTuple, replacements,
argcAndIndex=None): argcAndIndex=None):