Bug 780970 - Add [infallible] attribute for XPIDL attributes. r=khuey

This commit is contained in:
Justin Lebar 2012-08-22 18:27:04 -07:00
parent 09617bdd09
commit 8387ccd3cb
2 changed files with 41 additions and 0 deletions

View File

@ -138,6 +138,11 @@ jspubtd_include = """
#include "jspubtd.h"
"""
infallible_includes = """
#include "mozilla/Assertions.h"
#include "mozilla/Util.h"
"""
header_end = """/* For IDL files that don't want to include root IDL files. */
#ifndef NS_NO_VTABLE
#define NS_NO_VTABLE
@ -170,6 +175,13 @@ def print_header(idl, fd, filename):
if idl.needsJSTypes():
fd.write(jspubtd_include)
# Include some extra files if any attributes are infallible.
for iface in [p for p in idl.productions if p.kind == 'interface']:
for attr in [m for m in iface.members if isinstance(m, xpidl.Attribute)]:
if attr.infallible:
fd.write(infallible_includes)
break
fd.write('\n')
fd.write(header_end)
@ -280,6 +292,16 @@ iface_template_epilog = """/* End of implementation class template. */
"""
attr_infallible_tmpl = """\
inline %(realtype)s%(nativename)s(%(args)s)
{
%(realtype)sresult;
mozilla::DebugOnly<nsresult> rv = %(nativename)s(%(argnames)s&result);
MOZ_ASSERT(NS_SUCCEEDED(rv));
return result;
}
"""
def write_interface(iface, fd):
if iface.namemap is None:
raise Exception("Interface was not resolved.")
@ -310,6 +332,13 @@ def write_interface(iface, fd):
fd.write(" /* %s */\n" % a.toIDL());
fd.write(" %s = 0;\n" % attributeAsNative(a, True))
if a.infallible:
fd.write(attr_infallible_tmpl %
{'realtype': a.realtype.nativeType('in'),
'nativename': attributeNativeName(a, getter=True),
'args': '' if not a.implicit_jscontext else 'JSContext* cx',
'argnames': '' if not a.implicit_jscontext else 'cx, '})
if not a.readonly:
fd.write(" %s = 0;\n" % attributeAsNative(a, False))
fd.write("\n")

View File

@ -715,6 +715,7 @@ class Attribute(object):
undefined = None
deprecated = False
nullable = False
infallible = False
defvalue = None
def __init__(self, type, name, attlist, readonly, nullable, defvalue, location, doccomments):
@ -770,6 +771,8 @@ class Attribute(object):
self.deprecated = True
elif name == 'nostdcall':
self.nostdcall = True
elif name == 'infallible':
self.infallible = True
else:
raise IDLError("Unexpected attribute '%s'" % name, aloc)
@ -788,6 +791,15 @@ class Attribute(object):
getBuiltinOrNativeTypeName(self.realtype) != '[domstring]'):
raise IDLError("Nullable types (T?) is supported only for DOMString",
self.location)
if self.infallible and not self.realtype.kind == 'builtin':
raise IDLError('[infallible] only works on builtin types '
'(numbers, bool, and raw char types)',
self.location)
if self.infallible and not iface.attributes.builtinclass:
raise IDLError('[infallible] attributes are only allowed on '
'[builtinclass] interfaces',
self.location)
def toIDL(self):
attribs = attlistToIDL(self.attlist)