auto merge of #2452 : Ms2ger/servo/getDefaultRetval, r=jdm

This commit is contained in:
bors-servo 2014-05-16 10:25:51 -04:00
commit 58283527bf

View file

@ -4311,50 +4311,38 @@ class CGNativeMember(ClassMethod):
The first element is the type declaration for the retval The first element is the type declaration for the retval
The second element is a default value that can be used on error returns. The second element is a template for actually returning a value stored in
For cases whose behavior depends on isMember, the second element will be
None if isMember is true.
The third element is a template for actually returning a value stored in
"${declName}". This means actually returning it if "${declName}". This means actually returning it if
we're not outparam, else assigning to the "retval" outparam. If we're not outparam, else assigning to the "retval" outparam. If
isMember is true, this can be None, since in that case the caller will isMember is true, this can be None, since in that case the caller will
never examine this value. never examine this value.
""" """
if type.isVoid(): if type.isVoid():
typeDecl, errorDefault, template = "", "", "" typeDecl, template = "", ""
elif type.isPrimitive() and type.tag() in builtinNames: elif type.isPrimitive() and type.tag() in builtinNames:
result = CGGeneric(builtinNames[type.tag()]) result = CGGeneric(builtinNames[type.tag()])
defaultReturnArg = "0"
if type.nullable(): if type.nullable():
result = CGTemplatedType("Nullable", result) result = CGTemplatedType("Nullable", result)
defaultReturnArg = "" typeDecl, template = result.define(), "return ${declName};"
typeDecl, errorDefault, template = \
(result.define(),
"%s(%s)" % (result.define(), defaultReturnArg),
"return ${declName};")
elif type.isDOMString(): elif type.isDOMString():
if isMember: if isMember:
# No need for a third element in the isMember case # No need for a third element in the isMember case
typeDecl, errorDefault, template = "nsString", None, None typeDecl, template = "nsString", None
# Outparam # Outparam
else: else:
typeDecl, errorDefault, template = "void", "", "retval = ${declName};" typeDecl, template = "void", "retval = ${declName};"
elif type.isByteString(): elif type.isByteString():
if isMember: if isMember:
# No need for a third element in the isMember case # No need for a third element in the isMember case
typeDecl, errorDefault, template = "nsCString", None, None typeDecl, template = "nsCString", None
# Outparam # Outparam
typeDecl, errorDefault, template = "void", "", "retval = ${declName};" typeDecl, template = "void", "retval = ${declName};"
elif type.isEnum(): elif type.isEnum():
enumName = type.unroll().inner.identifier.name enumName = type.unroll().inner.identifier.name
if type.nullable(): if type.nullable():
enumName = CGTemplatedType("Nullable", enumName = CGTemplatedType("Nullable",
CGGeneric(enumName)).define() CGGeneric(enumName)).define()
defaultValue = "%s()" % enumName typeDecl, template = enumName, "return ${declName};"
else:
defaultValue = "%s(0)" % enumName
typeDecl, errorDefault, template = enumName, defaultValue, "return ${declName};"
elif type.isGeckoInterface(): elif type.isGeckoInterface():
iface = type.unroll().inner; iface = type.unroll().inner;
nativeType = self.descriptorProvider.getDescriptor( nativeType = self.descriptorProvider.getDescriptor(
@ -4369,24 +4357,21 @@ class CGNativeMember(ClassMethod):
# Since we always force an owning type for callback return values, # Since we always force an owning type for callback return values,
# our ${declName} is an OwningNonNull or nsRefPtr. So we can just # our ${declName} is an OwningNonNull or nsRefPtr. So we can just
# .forget() to get our already_AddRefed. # .forget() to get our already_AddRefed.
typeDecl, errorDefault, template = \ typeDecl, template = result.define(), "return ${declName}.forget();"
result.define(), "nullptr", "return ${declName}.forget();"
elif type.isCallback(): elif type.isCallback():
typeDecl, errorDefault, template = \ typeDecl, template = \
("already_AddRefed<%s>" % type.unroll().identifier.name, ("already_AddRefed<%s>" % type.unroll().identifier.name,
"nullptr", "return ${declName}.forget();") "return ${declName}.forget();")
elif type.isAny(): elif type.isAny():
typeDecl, errorDefault, template = \ typeDecl, template = "JS::Value", "return ${declName};"
"JS::Value", "JS::UndefinedValue()", "return ${declName};"
elif type.isObject(): elif type.isObject():
typeDecl, errorDefault, template = \ typeDecl, template = "JSObject*", "return ${declName};"
"JSObject*", "nullptr", "return ${declName};"
elif type.isSpiderMonkeyInterface(): elif type.isSpiderMonkeyInterface():
if type.nullable(): if type.nullable():
returnCode = "return ${declName}.IsNull() ? nullptr : ${declName}.Value().Obj();" returnCode = "return ${declName}.IsNull() ? nullptr : ${declName}.Value().Obj();"
else: else:
returnCode = "return ${declName}.Obj();" returnCode = "return ${declName}.Obj();"
typeDecl, errorDefault, template = "JSObject*", "nullptr", returnCode typeDecl, template = "JSObject*", returnCode
elif type.isSequence(): elif type.isSequence():
# If we want to handle sequence-of-sequences return values, we're # If we want to handle sequence-of-sequences return values, we're
# going to need to fix example codegen to not produce nsTArray<void> # going to need to fix example codegen to not produce nsTArray<void>
@ -4401,13 +4386,12 @@ class CGNativeMember(ClassMethod):
"}") "}")
else: else:
returnCode = "retval.SwapElements(${declName});" returnCode = "retval.SwapElements(${declName});"
typeDecl, errorDefault, template = "void", "", returnCode typeDecl, template = "void", returnCode
elif type.isDate(): elif type.isDate():
result = CGGeneric("Date") result = CGGeneric("Date")
if type.nullable(): if type.nullable():
result = CGTemplatedType("Nullable", result) result = CGTemplatedType("Nullable", result)
typeDecl, errorDefault, template = \ typeDecl, template = result.define(), "return ${declName};"
(result.define(), "%s()" % result.define(), "return ${declName};")
else: else:
raise TypeError("Don't know how to declare return value for %s" % type) raise TypeError("Don't know how to declare return value for %s" % type)
@ -4416,11 +4400,9 @@ class CGNativeMember(ClassMethod):
typeDecl = "Fallible<%s>" % typeDecl typeDecl = "Fallible<%s>" % typeDecl
else: else:
typeDecl = "ErrorResult" typeDecl = "ErrorResult"
if not errorDefault:
errorDefault = "Err(FailureUnknown)"
if not template: if not template:
template = "return Ok(());" template = "return Ok(());"
return typeDecl, errorDefault, template return typeDecl, template
def getArgs(self, returnType, argList): def getArgs(self, returnType, argList):
args = [self.getArg(arg) for arg in argList] args = [self.getArg(arg) for arg in argList]
@ -4676,17 +4658,16 @@ class CGCallback(CGClass):
setupCall = ("let s = CallSetup::new(cx_for_dom_object(${cxProvider}), aExceptionHandling);\n" setupCall = ("let s = CallSetup::new(cx_for_dom_object(${cxProvider}), aExceptionHandling);\n"
"if s.GetContext().is_null() {\n" "if s.GetContext().is_null() {\n"
" return${errorReturn};\n" " return Err(FailureUnknown);\n"
"}\n") "}\n")
bodyWithThis = string.Template( bodyWithThis = string.Template(
setupCall+ setupCall+
"let thisObjJS = WrapCallThisObject(s.GetContext(), ptr::null() /*XXXjdm proper scope*/, thisObj);\n" "let thisObjJS = WrapCallThisObject(s.GetContext(), ptr::null() /*XXXjdm proper scope*/, thisObj);\n"
"if thisObjJS.is_null() {\n" "if thisObjJS.is_null() {\n"
" return${errorReturn};\n" " return Err(FailureUnknown);\n"
"}\n" "}\n"
"return ${methodName}(${callArgs});").substitute({ "return ${methodName}(${callArgs});").substitute({
"errorReturn" : method.getDefaultRetval(),
"callArgs" : ", ".join(argnamesWithThis), "callArgs" : ", ".join(argnamesWithThis),
"methodName": 'self.' + method.name, "methodName": 'self.' + method.name,
"cxProvider": 'thisObj' "cxProvider": 'thisObj'
@ -4694,7 +4675,6 @@ class CGCallback(CGClass):
bodyWithoutThis = string.Template( bodyWithoutThis = string.Template(
setupCall + setupCall +
"return ${methodName}(${callArgs});").substitute({ "return ${methodName}(${callArgs});").substitute({
"errorReturn" : method.getDefaultRetval(),
"callArgs" : ", ".join(argnamesWithoutThis), "callArgs" : ", ".join(argnamesWithoutThis),
"methodName": 'self.' + method.name, "methodName": 'self.' + method.name,
"cxProvider": args[2].name #XXXjdm There's no guarantee that this is a DOM object "cxProvider": args[2].name #XXXjdm There's no guarantee that this is a DOM object
@ -4811,7 +4791,6 @@ class CallbackMember(CGNativeMember):
def getImpl(self): def getImpl(self):
replacements = { replacements = {
"declRval": self.getRvalDecl(), "declRval": self.getRvalDecl(),
"errorReturn" : self.getDefaultRetval(),
"returnResult": self.getResultConversion(), "returnResult": self.getResultConversion(),
"convertArgs": self.getArgConversions(), "convertArgs": self.getArgConversions(),
"doCall": self.getCall(), "doCall": self.getCall(),
@ -4864,7 +4843,7 @@ class CallbackMember(CGNativeMember):
assignRetval = string.Template( assignRetval = string.Template(
self.getRetvalInfo(self.retvalType, self.getRetvalInfo(self.retvalType,
False)[2]).substitute(replacements) False)[1]).substitute(replacements)
return convertType.define() + "\n" + assignRetval + "\n" return convertType.define() + "\n" + assignRetval + "\n"
def getArgConversions(self): def getArgConversions(self):
@ -4928,12 +4907,6 @@ class CallbackMember(CGNativeMember):
"}" % (i+1, i)) "}" % (i+1, i))
return conversion return conversion
def getDefaultRetval(self):
default = self.getRetvalInfo(self.retvalType, False)[1]
if len(default) != 0:
default = " " + default
return default
def getArgs(self, returnType, argList): def getArgs(self, returnType, argList):
args = CGNativeMember.getArgs(self, returnType, argList) args = CGNativeMember.getArgs(self, returnType, argList)
if not self.needThisHandling: if not self.needThisHandling:
@ -4966,11 +4939,10 @@ class CallbackMember(CGNativeMember):
"${callSetup}\n" "${callSetup}\n"
"JSContext* cx = s.GetContext();\n" "JSContext* cx = s.GetContext();\n"
"if (!cx) {\n" "if (!cx) {\n"
" return${errorReturn};\n" " return Err(FailureUnknown);\n"
"}\n").substitute({ "}\n").substitute({
"callSetup": callSetup, "callSetup": callSetup,
"errorReturn" : self.getDefaultRetval(), })
})
def getArgcDecl(self): def getArgcDecl(self):
return CGGeneric("let argc = %su32;" % self.argCountStr); return CGGeneric("let argc = %su32;" % self.argCountStr);
@ -4999,7 +4971,6 @@ class CallbackMethod(CallbackMember):
def getCall(self): def getCall(self):
replacements = { replacements = {
"errorReturn" : self.getDefaultRetval(),
"thisObj": self.getThisObj(), "thisObj": self.getThisObj(),
"getCallable": self.getCallableDecl() "getCallable": self.getCallableDecl()
} }
@ -5015,7 +4986,7 @@ class CallbackMethod(CallbackMember):
" ${argc}, ${argv}, &rval)\n" " ${argc}, ${argv}, &rval)\n"
"};\n" "};\n"
"if ok == 0 {\n" "if ok == 0 {\n"
" return${errorReturn};\n" " return Err(FailureUnknown);\n"
"}\n").substitute(replacements) "}\n").substitute(replacements)
class CallCallback(CallbackMethod): class CallCallback(CallbackMethod):
@ -5048,12 +5019,11 @@ class CallbackOperationBase(CallbackMethod):
def getCallableDecl(self): def getCallableDecl(self):
replacements = { replacements = {
"errorReturn" : self.getDefaultRetval(),
"methodName": self.methodName "methodName": self.methodName
} }
getCallableFromProp = string.Template( getCallableFromProp = string.Template(
'match self.parent.GetCallableProperty(cx, "${methodName}") {\n' 'match self.parent.GetCallableProperty(cx, "${methodName}") {\n'
' Err(_) => return${errorReturn},\n' ' Err(_) => return Err(FailureUnknown),\n'
' Ok(callable) => callable,\n' ' Ok(callable) => callable,\n'
'}').substitute(replacements) '}').substitute(replacements)
if not self.singleOperation: if not self.singleOperation:
@ -5094,13 +5064,11 @@ class CallbackGetter(CallbackMember):
def getCall(self): def getCall(self):
replacements = { replacements = {
"errorReturn" : self.getDefaultRetval(),
"attrName": self.attrName "attrName": self.attrName
} }
return string.Template( return string.Template(
'if (!JS_GetProperty(cx, mCallback, "${attrName}", &rval)) {\n' 'if (!JS_GetProperty(cx, mCallback, "${attrName}", &rval)) {\n'
' aRv.Throw(NS_ERROR_UNEXPECTED);\n' ' return Err(FailureUnknown);\n'
' return${errorReturn};\n'
'}\n').substitute(replacements); '}\n').substitute(replacements);
class CallbackSetter(CallbackMember): class CallbackSetter(CallbackMember):
@ -5121,15 +5089,13 @@ class CallbackSetter(CallbackMember):
def getCall(self): def getCall(self):
replacements = { replacements = {
"errorReturn" : self.getDefaultRetval(),
"attrName": self.attrName, "attrName": self.attrName,
"argv": "argv.handleAt(0)", "argv": "argv.handleAt(0)",
} }
return string.Template( return string.Template(
'MOZ_ASSERT(argv.length() == 1);\n' 'MOZ_ASSERT(argv.length() == 1);\n'
'if (!JS_SetProperty(cx, mCallback, "${attrName}", ${argv})) {\n' 'if (!JS_SetProperty(cx, mCallback, "${attrName}", ${argv})) {\n'
' aRv.Throw(NS_ERROR_UNEXPECTED);\n' ' return Err(FailureUnknown);\n'
' return${errorReturn};\n'
'}\n').substitute(replacements) '}\n').substitute(replacements)
def getArgcDecl(self): def getArgcDecl(self):