Bug 742170. Use CGThings in CGCreateInterfaceObjectsMethod more. r=peterv

This commit is contained in:
Boris Zbarsky 2012-04-18 12:10:48 -04:00
parent 1c8d9735f3
commit 396f480085

View File

@ -188,9 +188,11 @@ class CGList(CGThing):
def prepend(self, child):
self.children.insert(0, child)
def declare(self):
return self.joiner.join([child.declare() for child in self.children])
return self.joiner.join([child.declare() for child in self.children
if child is not None])
def define(self):
return self.joiner.join([child.define() for child in self.children])
return self.joiner.join([child.define() for child in self.children
if child is not None])
class CGGeneric(CGThing):
"""
@ -237,7 +239,7 @@ class CGWrapper(CGThing):
"""
def __init__(self, child, pre="", post="", declarePre=None,
declarePost=None, definePre=None, definePost=None,
declareOnly=False):
declareOnly=False, reindent=False):
CGThing.__init__(self)
self.child = child
self.declarePre = declarePre or pre
@ -245,12 +247,25 @@ class CGWrapper(CGThing):
self.definePre = definePre or pre
self.definePost = definePost or post
self.declareOnly = declareOnly
self.reindent = reindent
def declare(self):
return self.declarePre + self.child.declare() + self.declarePost
decl = self.child.declare()
if self.reindent:
# We don't use lineStartDetector because we don't want to
# insert whitespace at the beginning of our _first_ line.
decl = stripTrailingWhitespace(
decl.replace("\n", "\n" + (" " * len(self.declarePre))))
return self.declarePre + decl + self.declarePost
def define(self):
if self.declareOnly:
return ''
return self.definePre + self.child.define() + self.definePost
defn = self.child.define()
if self.reindent:
# We don't use lineStartDetector because we don't want to
# insert whitespace at the beginning of our _first_ line.
defn = stripTrailingWhitespace(
defn.replace("\n", "\n" + (" " * len(self.definePre))))
return self.definePre + defn + self.definePost
class CGNamespace(CGWrapper):
def __init__(self, namespace, child, declareOnly=False):
@ -810,45 +825,54 @@ class CGCreateInterfaceObjectsMethod(CGAbstractMethod):
idsToInit.append(props.variableName(False))
if props.hasChromeOnly() and not self.descriptor.workers:
idsToInit.append(props.variableName(True))
initIds = ""
if len(idsToInit) > 0:
init = ' ||\n '.join(["!InitIds(aCx, %s, %s_ids)" % (varname, varname)
for varname in idsToInit])
initIds = CGList(
[CGGeneric("!InitIds(aCx, %s, %s_ids)" % (varname, varname)) for
varname in idsToInit], ' ||\n')
if len(idsToInit) > 1:
init = '(' + init + ')'
initIds = (" if (%s_ids[0] == JSID_VOID &&\n" +
" %s) {\n" +
" %s_ids[0] = JSID_VOID;\n"
" return NULL;\n"
" }\n\n") % (idsToInit[0], init, idsToInit[0])
initIds = CGWrapper(initIds, pre="(", post=")", reindent=True)
initIds = CGList(
[CGGeneric("%s_ids[0] == JSID_VOID &&" % idsToInit[0]), initIds],
"\n")
initIds = CGWrapper(initIds, pre="if (", post=") {", reindent=True)
initIds = CGList(
[initIds,
CGGeneric((" %s_ids[0] = JSID_VOID;\n"
" return NULL;") % idsToInit[0]),
CGGeneric("}")],
"\n")
else:
initIds = None
getParentProto = (" JSObject* parentProto = %s;\n" +
" if (!parentProto) {\n" +
" return NULL;\n" +
" }") % getParentProto
getParentProto = ("JSObject* parentProto = %s;\n"
"if (!parentProto) {\n"
" return NULL;\n"
"}") % getParentProto
call = """return bindings::CreateInterfaceObjects(aCx, aGlobal, parentProto,
%s, %s,
%%(methods)s, %%(attrs)s, %%(consts)s, %%(staticMethods)s,
%s);""" % (
call = CGGeneric(("return bindings::CreateInterfaceObjects(aCx, aGlobal, parentProto,\n"
" %s, %s,\n"
" %%(methods)s, %%(attrs)s, %%(consts)s, %%(staticMethods)s,\n"
" %s);") % (
"&PrototypeClass" if needInterfacePrototypeObject else "NULL",
"&InterfaceObjectClass" if needInterfaceObject else "NULL",
'"' + self.descriptor.interface.identifier.name + '"' if needInterfaceObject else "NULL")
'"' + self.descriptor.interface.identifier.name + '"' if needInterfaceObject else "NULL"))
if self.properties.hasChromeOnly():
if self.descriptor.workers:
accessCheck = "mozilla::dom::workers::GetWorkerPrivateFromContext(aCx)->IsChromeWorker()"
else:
accessCheck = "xpc::AccessCheck::isChrome(js::GetObjectCompartment(aGlobal))"
chrome = """
if (%s) {
%s
}
""" % (accessCheck, call.replace("\n ", "\n ") % self.properties.variableNames(True))
accessCheck = "if (" + accessCheck + ") {\n"
chrome = CGWrapper(CGGeneric((CGIndenter(call).define() % self.properties.variableNames(True))),
pre=accessCheck, post="\n}")
else:
chrome = ""
return initIds + getParentProto + chrome + "\n " + call % self.properties.variableNames(False)
chrome = None
functionBody = CGList(
[CGGeneric(getParentProto), initIds, chrome,
CGGeneric(call.define() % self.properties.variableNames(False))],
"\n\n")
return CGIndenter(functionBody).define()
class CGGetPerInterfaceObject(CGAbstractMethod):
"""
@ -1736,12 +1760,12 @@ class CGCase(CGList):
def __init__(self, expression, body, fallThrough=False):
CGList.__init__(self, [], "\n")
self.append(CGWrapper(CGGeneric(expression), pre="case ", post=": {"))
if body is not None:
self.append(CGIndenter(body))
bodyList = CGList([body], "\n")
if fallThrough:
self.append(CGIndenter(CGGeneric("/* Fall through */")))
bodyList.append(CGGeneric("/* Fall through */"))
else:
self.append(CGIndenter(CGGeneric("break;")))
bodyList.append(CGGeneric("break;"))
self.append(CGIndenter(bodyList));
self.append(CGGeneric("}"))
class CGMethodCall(CGThing):