mirror of
https://github.com/servo/servo.git
synced 2025-08-05 05:30:08 +01:00
Move optional/nullable handling out of CastableObjectUnwrapper.
This puts the code that wraps the type in Option<> and the code that wraps the expression in Some() into the same if block, which should clarify the code.
This commit is contained in:
parent
89d4fac36e
commit
7c3480de60
1 changed files with 11 additions and 15 deletions
|
@ -92,19 +92,18 @@ class CastableObjectUnwrapper():
|
||||||
|
|
||||||
codeOnFailure is the code to run if unwrapping fails.
|
codeOnFailure is the code to run if unwrapping fails.
|
||||||
"""
|
"""
|
||||||
def __init__(self, descriptor, source, codeOnFailure, isOptional=False):
|
def __init__(self, descriptor, source, codeOnFailure):
|
||||||
self.substitution = { "type" : descriptor.nativeType,
|
self.substitution = { "type" : descriptor.nativeType,
|
||||||
"depth": descriptor.interface.inheritanceDepth(),
|
"depth": descriptor.interface.inheritanceDepth(),
|
||||||
"prototype": "PrototypeList::id::" + descriptor.name,
|
"prototype": "PrototypeList::id::" + descriptor.name,
|
||||||
"protoID" : "PrototypeList::id::" + descriptor.name + " as uint",
|
"protoID" : "PrototypeList::id::" + descriptor.name + " as uint",
|
||||||
"source" : source,
|
"source" : source,
|
||||||
"codeOnFailure" : CGIndenter(CGGeneric(codeOnFailure), 4).define(),
|
"codeOnFailure" : CGIndenter(CGGeneric(codeOnFailure), 4).define()}
|
||||||
"unwrapped_val" : "Some(val)" if isOptional else "val"}
|
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return string.Template(
|
return string.Template(
|
||||||
"""match unwrap_jsmanaged(${source}, ${prototype}, ${depth}) {
|
"""match unwrap_jsmanaged(${source}, ${prototype}, ${depth}) {
|
||||||
Ok(val) => ${unwrapped_val},
|
Ok(val) => val,
|
||||||
Err(()) => {
|
Err(()) => {
|
||||||
${codeOnFailure}
|
${codeOnFailure}
|
||||||
}
|
}
|
||||||
|
@ -121,10 +120,9 @@ class FailureFatalCastableObjectUnwrapper(CastableObjectUnwrapper):
|
||||||
"""
|
"""
|
||||||
As CastableObjectUnwrapper, but defaulting to throwing if unwrapping fails
|
As CastableObjectUnwrapper, but defaulting to throwing if unwrapping fails
|
||||||
"""
|
"""
|
||||||
def __init__(self, descriptor, source, isOptional):
|
def __init__(self, descriptor, source):
|
||||||
CastableObjectUnwrapper.__init__(self, descriptor, source,
|
CastableObjectUnwrapper.__init__(self, descriptor, source,
|
||||||
"return 0; //XXXjdm return Throw(cx, rv);",
|
"return 0; //XXXjdm return Throw(cx, rv);")
|
||||||
isOptional)
|
|
||||||
|
|
||||||
class CGThing():
|
class CGThing():
|
||||||
"""
|
"""
|
||||||
|
@ -619,26 +617,24 @@ def getJSToNativeConversionTemplate(type, descriptorProvider, failureCode=None,
|
||||||
raise TypeError("Consequential interface %s being used as an "
|
raise TypeError("Consequential interface %s being used as an "
|
||||||
"argument" % descriptor.interface.identifier.name)
|
"argument" % descriptor.interface.identifier.name)
|
||||||
|
|
||||||
|
|
||||||
if failureCode is not None:
|
if failureCode is not None:
|
||||||
templateBody = str(CastableObjectUnwrapper(
|
templateBody = str(CastableObjectUnwrapper(
|
||||||
descriptor,
|
descriptor,
|
||||||
"(${val}).to_object()",
|
"(${val}).to_object()",
|
||||||
failureCode,
|
failureCode))
|
||||||
isOptional or type.nullable()))
|
|
||||||
else:
|
else:
|
||||||
templateBody = str(FailureFatalCastableObjectUnwrapper(
|
templateBody = str(FailureFatalCastableObjectUnwrapper(
|
||||||
descriptor,
|
descriptor,
|
||||||
"(${val}).to_object()",
|
"(${val}).to_object()"))
|
||||||
isOptional or type.nullable()))
|
|
||||||
|
|
||||||
templateBody = wrapObjectTemplate(templateBody, isDefinitelyObject,
|
|
||||||
type, failureCode)
|
|
||||||
|
|
||||||
declType = CGGeneric(descriptor.nativeType)
|
declType = CGGeneric(descriptor.nativeType)
|
||||||
if type.nullable() or isOptional:
|
if type.nullable() or isOptional:
|
||||||
|
templateBody = "Some(%s)" % templateBody
|
||||||
declType = CGWrapper(declType, pre="Option<", post=">")
|
declType = CGWrapper(declType, pre="Option<", post=">")
|
||||||
|
|
||||||
|
templateBody = wrapObjectTemplate(templateBody, isDefinitelyObject,
|
||||||
|
type, failureCode)
|
||||||
|
|
||||||
return (templateBody, declType, isOptional, "None" if isOptional else None)
|
return (templateBody, declType, isOptional, "None" if isOptional else None)
|
||||||
|
|
||||||
if type.isSpiderMonkeyInterface():
|
if type.isSpiderMonkeyInterface():
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue