Move responsability for the assignment out of CastableObjectUnwrapper.

This is a second step towards making getJSToNativeConversionTemplate return an
expression, which will improve dictionary codegen in particular.

This commit does not make any change to the generated code.
This commit is contained in:
Ms2ger 2014-04-16 21:21:19 +02:00
parent ab1b429aef
commit 82afae123e

View file

@ -87,30 +87,28 @@ numericTags = [
class CastableObjectUnwrapper(): class CastableObjectUnwrapper():
""" """
A class for unwrapping an object named by the "source" argument A class for unwrapping an object named by the "source" argument
based on the passed-in descriptor and storing it in a variable based on the passed-in descriptor. Stringifies to a Rust expression of
called by the name in the "target" argument. the appropriate type.
codeOnFailure is the code to run if unwrapping fails. codeOnFailure is the code to run if unwrapping fails.
""" """
def __init__(self, descriptor, source, target, codeOnFailure, isOptional=False): def __init__(self, descriptor, source, codeOnFailure, isOptional=False):
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,
"target" : target,
"codeOnFailure" : CGIndenter(CGGeneric(codeOnFailure), 4).define(), "codeOnFailure" : CGIndenter(CGGeneric(codeOnFailure), 4).define(),
"unwrapped_val" : "Some(val)" if isOptional else "val"} "unwrapped_val" : "Some(val)" if isOptional else "val"}
def __str__(self): def __str__(self):
return string.Template( return string.Template(
"""${target} = match unwrap_jsmanaged(${source}, ${prototype}, ${depth}) { """match unwrap_jsmanaged(${source}, ${prototype}, ${depth}) {
Ok(val) => ${unwrapped_val}, Ok(val) => ${unwrapped_val},
Err(()) => { Err(()) => {
${codeOnFailure} ${codeOnFailure}
} }
}; }""").substitute(self.substitution)
""").substitute(self.substitution)
#"""{ #"""{
# nsresult rv = UnwrapObject<${protoID}, ${type}>(cx, ${source}, ${target}); # nsresult rv = UnwrapObject<${protoID}, ${type}>(cx, ${source}, ${target});
@ -123,8 +121,8 @@ 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, target, isOptional): def __init__(self, descriptor, source, isOptional):
CastableObjectUnwrapper.__init__(self, descriptor, source, target, CastableObjectUnwrapper.__init__(self, descriptor, source,
"return 0; //XXXjdm return Throw(cx, rv);", "return 0; //XXXjdm return Throw(cx, rv);",
isOptional) isOptional)
@ -622,19 +620,20 @@ 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)
templateBody = "${declName} = "
if failureCode is not None: if failureCode is not None:
templateBody += str(CastableObjectUnwrapper( templateBody += str(CastableObjectUnwrapper(
descriptor, descriptor,
"(${val}).to_object()", "(${val}).to_object()",
"${declName}",
failureCode, failureCode,
isOptional or type.nullable())) isOptional or type.nullable()))
else: else:
templateBody += str(FailureFatalCastableObjectUnwrapper( templateBody += str(FailureFatalCastableObjectUnwrapper(
descriptor, descriptor,
"(${val}).to_object()", "(${val}).to_object()",
"${declName}",
isOptional or type.nullable())) isOptional or type.nullable()))
templateBody += ";\n"
templateBody = wrapObjectTemplate(templateBody, isDefinitelyObject, templateBody = wrapObjectTemplate(templateBody, isDefinitelyObject,
type, failureCode) type, failureCode)
@ -2495,9 +2494,9 @@ class CGAbstractBindingMethod(CGAbstractExternMethod):
# know that we're the real deal. So fake a descriptor here for # know that we're the real deal. So fake a descriptor here for
# consumption by FailureFatalCastableObjectUnwrapper. # consumption by FailureFatalCastableObjectUnwrapper.
unwrapThis = CGIndenter(CGGeneric( unwrapThis = CGIndenter(CGGeneric(
str(CastableObjectUnwrapper( "this = " + str(CastableObjectUnwrapper(
FakeCastableDescriptor(self.descriptor), FakeCastableDescriptor(self.descriptor),
"obj", "this", self.unwrapFailureCode)))) "obj", self.unwrapFailureCode)) + ";\n"))
return CGList([ self.getThis(), unwrapThis, return CGList([ self.getThis(), unwrapThis,
self.generate_code() ], "\n").define() self.generate_code() ], "\n").define()