auto merge of #2227 : Ms2ger/servo/assign-outside-match, r=jdm

This commit is contained in:
bors-servo 2014-04-24 18:13:13 -04:00
commit 0745d656a3

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(
"""match unwrap_jsmanaged(${source}, ${prototype}, ${depth}) { """match unwrap_jsmanaged(${source}, ${prototype}, ${depth}) {
Ok(val) => ${target} = ${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)
@ -586,10 +584,10 @@ def getJSToNativeConversionTemplate(type, descriptorProvider, failureCode=None,
declType = CGWrapper(declType, pre="Option<", post=" >") declType = CGWrapper(declType, pre="Option<", post=" >")
value = CGWrapper(value, pre="Some(", post=")") value = CGWrapper(value, pre="Some(", post=")")
templateBody = CGGeneric("match %s::from_value(cx, ${val}) {\n" templateBody = CGGeneric("${declName} = match %s::from_value(cx, ${val}) {\n"
" Err(()) => { %s },\n" " Err(()) => { %s },\n"
" Ok(value) => ${declName} = %s,\n" " Ok(value) => %s,\n"
"}" % (type.name, exceptionCode, value.define())) "};" % (type.name, exceptionCode, value.define()))
if type.nullable(): if type.nullable():
templateBody = CGIfElseWrapper( templateBody = CGIfElseWrapper(
@ -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)
@ -668,10 +667,10 @@ def getJSToNativeConversionTemplate(type, descriptorProvider, failureCode=None,
strval = "Some(%s)" % strval strval = "Some(%s)" % strval
conversionCode = ( conversionCode = (
"match FromJSValConvertible::from_jsval(cx, ${val}, %s) {\n" "${declName} = match FromJSValConvertible::from_jsval(cx, ${val}, %s) {\n"
" Ok(strval) => ${declName} = %s,\n" " Ok(strval) => %s,\n"
" Err(_) => { %s },\n" " Err(_) => { %s },\n"
"}" % (nullBehavior, strval, exceptionCode)) "};" % (nullBehavior, strval, exceptionCode))
if defaultValue is None: if defaultValue is None:
return conversionCode return conversionCode
@ -833,10 +832,10 @@ def getJSToNativeConversionTemplate(type, descriptorProvider, failureCode=None,
#XXXjdm support conversionBehavior here #XXXjdm support conversionBehavior here
template = ( template = (
"match FromJSValConvertible::from_jsval(cx, ${val}, ()) {\n" "${declName} = match FromJSValConvertible::from_jsval(cx, ${val}, ()) {\n"
" Ok(v) => ${declName} = %s,\n" " Ok(v) => %s,\n"
" Err(_) => { %s }\n" " Err(_) => { %s }\n"
"}" % (value, exceptionCode)) "};" % (value, exceptionCode))
if defaultValue is not None: if defaultValue is not None:
if isinstance(defaultValue, IDLNullValue): if isinstance(defaultValue, IDLNullValue):
@ -2490,21 +2489,17 @@ class CGAbstractBindingMethod(CGAbstractExternMethod):
# we're someone's consequential interface. But for this-unwrapping, we # we're someone's consequential interface. But for this-unwrapping, we
# 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 = str(CastableObjectUnwrapper(
str(CastableObjectUnwrapper(
FakeCastableDescriptor(self.descriptor), FakeCastableDescriptor(self.descriptor),
"obj", "this", self.unwrapFailureCode)))) "obj", self.unwrapFailureCode))
return CGList([ self.getThis(), unwrapThis, unwrapThis = CGIndenter(
self.generate_code() ], "\n").define()
def getThis(self):
return CGIndenter(
CGGeneric("let obj: *JSObject = JS_THIS_OBJECT(cx, vp as *mut JSVal);\n" CGGeneric("let obj: *JSObject = JS_THIS_OBJECT(cx, vp as *mut JSVal);\n"
"if obj.is_null() {\n" "if obj.is_null() {\n"
" return false as JSBool;\n" " return false as JSBool;\n"
"}\n" "}\n"
"\n" "\n"
"let this: JS<%s>;" % self.descriptor.concreteType)) "let this: JS<%s> = %s;\n" % (self.descriptor.concreteType, unwrapThis)))
return CGList([ unwrapThis, self.generate_code() ], "\n").define()
def generate_code(self): def generate_code(self):
assert(False) # Override me assert(False) # Override me