Merge pull request #3003 from Ms2ger/interface-unwrap-failure

Throw a TypeError when unwrapping an interface fails; r=Manishearth+jdm
This commit is contained in:
Ms2ger 2014-08-05 16:23:45 +02:00
commit 6b6857ae2e
2 changed files with 37 additions and 33 deletions

View file

@ -101,36 +101,24 @@ 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): def __init__(self, descriptor, source, codeOnFailure):
self.substitution = { "type" : descriptor.nativeType, self.substitution = {
"depth": descriptor.interface.inheritanceDepth(), "type": descriptor.nativeType,
"prototype": "PrototypeList::id::" + descriptor.name, "depth": descriptor.interface.inheritanceDepth(),
"protoID" : "PrototypeList::id::" + descriptor.name + " as uint", "prototype": "PrototypeList::id::" + descriptor.name,
"source" : source, "protoID": "PrototypeList::id::" + descriptor.name + " as uint",
"codeOnFailure" : CGIndenter(CGGeneric(codeOnFailure), 4).define()} "source": source,
"codeOnFailure": CGIndenter(CGGeneric(codeOnFailure), 4).define(),
}
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) => val, Ok(val) => val,
Err(()) => { Err(()) => {
${codeOnFailure} ${codeOnFailure}
} }
}""").substitute(self.substitution) }""").substitute(self.substitution)
#"""{
# nsresult rv = UnwrapObject<${protoID}, ${type}>(cx, ${source}, ${target});
# if (NS_FAILED(rv)) {
#${codeOnFailure}
# }
#}""").substitute(self.substitution)
class FailureFatalCastableObjectUnwrapper(CastableObjectUnwrapper):
"""
As CastableObjectUnwrapper, but defaulting to throwing if unwrapping fails
"""
def __init__(self, descriptor, source):
CastableObjectUnwrapper.__init__(self, descriptor, source,
"return 0; //XXXjdm return Throw(cx, rv);")
class CGThing(): class CGThing():
""" """
@ -597,15 +585,23 @@ 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 None:
templateBody = str(CastableObjectUnwrapper( substitutions = {
descriptor, "sourceDescription": sourceDescription,
"(${val}).to_object()", "interface": descriptor.interface.identifier.name,
failureCode)) "exceptionCode": exceptionCode,
}
unwrapFailureCode = string.Template(
'throw_type_error(cx, "${sourceDescription} does not '
'implement interface ${interface}.");\n'
'${exceptionCode}').substitute(substitutions)
else: else:
templateBody = str(FailureFatalCastableObjectUnwrapper( unwrapFailureCode = failureCode
descriptor,
"(${val}).to_object()")) templateBody = str(CastableObjectUnwrapper(
descriptor,
"(${val}).to_object()",
unwrapFailureCode))
declType = CGGeneric(descriptorType) declType = CGGeneric(descriptorType)
if type.nullable(): if type.nullable():
@ -2381,7 +2377,10 @@ class CGAbstractBindingMethod(CGAbstractExternMethod):
CGAbstractExternMethod.__init__(self, descriptor, name, "JSBool", args) CGAbstractExternMethod.__init__(self, descriptor, name, "JSBool", args)
if unwrapFailureCode is None: if unwrapFailureCode is None:
self.unwrapFailureCode = "return 0; //XXXjdm return Throw(cx, rv);" self.unwrapFailureCode = (
'throw_type_error(cx, "\\"this\\" object does not '
'implement interface %s.");\n'
'return 0;' % descriptor.interface.identifier.name)
else: else:
self.unwrapFailureCode = unwrapFailureCode self.unwrapFailureCode = unwrapFailureCode

View file

@ -1,6 +1,11 @@
[Node-insertBefore.html] [Node-insertBefore.html]
type: testharness type: testharness
expected: TIMEOUT [If the context node is a document, inserting a document or text node should throw a HierarchyRequestError.]
[Calling insertBefore with a non-Node first argument must throw TypeError.] expected: FAIL
expected: TIMEOUT
[If the context node is a DocumentFragment, inserting a document or a doctype should throw a HierarchyRequestError.]
expected: FAIL
[If the context node is an element, inserting a document or a doctype should throw a HierarchyRequestError.]
expected: FAIL