diff options
author | Ms2ger <ms2ger@gmail.com> | 2014-04-27 19:44:09 +0200 |
---|---|---|
committer | Ms2ger <ms2ger@gmail.com> | 2014-04-27 19:44:09 +0200 |
commit | 7c3480de60760d6a298fedcca8edbf74ad89bb9a (patch) | |
tree | e5eeefc207a87642c9dbff60b11eb7149b796cfc /src | |
parent | 89d4fac36edb094f09db2c89afb4ca48b9c81522 (diff) | |
download | servo-7c3480de60760d6a298fedcca8edbf74ad89bb9a.tar.gz servo-7c3480de60760d6a298fedcca8edbf74ad89bb9a.zip |
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.
Diffstat (limited to 'src')
-rw-r--r-- | src/components/script/dom/bindings/codegen/CodegenRust.py | 26 |
1 files changed, 11 insertions, 15 deletions
diff --git a/src/components/script/dom/bindings/codegen/CodegenRust.py b/src/components/script/dom/bindings/codegen/CodegenRust.py index 40b5d9c56f7..1fa4b47e8e4 100644 --- a/src/components/script/dom/bindings/codegen/CodegenRust.py +++ b/src/components/script/dom/bindings/codegen/CodegenRust.py @@ -92,19 +92,18 @@ class CastableObjectUnwrapper(): 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, "depth": descriptor.interface.inheritanceDepth(), "prototype": "PrototypeList::id::" + descriptor.name, "protoID" : "PrototypeList::id::" + descriptor.name + " as uint", "source" : source, - "codeOnFailure" : CGIndenter(CGGeneric(codeOnFailure), 4).define(), - "unwrapped_val" : "Some(val)" if isOptional else "val"} + "codeOnFailure" : CGIndenter(CGGeneric(codeOnFailure), 4).define()} def __str__(self): return string.Template( """match unwrap_jsmanaged(${source}, ${prototype}, ${depth}) { - Ok(val) => ${unwrapped_val}, + Ok(val) => val, Err(()) => { ${codeOnFailure} } @@ -121,10 +120,9 @@ class FailureFatalCastableObjectUnwrapper(CastableObjectUnwrapper): """ 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, - "return 0; //XXXjdm return Throw(cx, rv);", - isOptional) + "return 0; //XXXjdm return Throw(cx, rv);") class CGThing(): """ @@ -619,26 +617,24 @@ def getJSToNativeConversionTemplate(type, descriptorProvider, failureCode=None, raise TypeError("Consequential interface %s being used as an " "argument" % descriptor.interface.identifier.name) - if failureCode is not None: templateBody = str(CastableObjectUnwrapper( descriptor, "(${val}).to_object()", - failureCode, - isOptional or type.nullable())) + failureCode)) else: templateBody = str(FailureFatalCastableObjectUnwrapper( descriptor, - "(${val}).to_object()", - isOptional or type.nullable())) - - templateBody = wrapObjectTemplate(templateBody, isDefinitelyObject, - type, failureCode) + "(${val}).to_object()")) declType = CGGeneric(descriptor.nativeType) if type.nullable() or isOptional: + templateBody = "Some(%s)" % templateBody declType = CGWrapper(declType, pre="Option<", post=">") + templateBody = wrapObjectTemplate(templateBody, isDefinitelyObject, + type, failureCode) + return (templateBody, declType, isOptional, "None" if isOptional else None) if type.isSpiderMonkeyInterface(): |