From 7627aed5097abdc8f5b79e387d78bd5a18a4688e Mon Sep 17 00:00:00 2001 From: Boris Zbarsky Date: Thu, 20 Mar 2014 23:19:43 -0400 Subject: [PATCH] Bug 983261. Improve example codegen for non-leaf and non-root interfaces. r=khuey --- dom/bindings/Codegen.py | 108 ++++++++++++++++++++++++++++++++-------- 1 file changed, 88 insertions(+), 20 deletions(-) diff --git a/dom/bindings/Codegen.py b/dom/bindings/Codegen.py index b945c1e82d30..a09d00d4d6f4 100644 --- a/dom/bindings/Codegen.py +++ b/dom/bindings/Codegen.py @@ -10568,25 +10568,92 @@ class CGExampleClass(CGBindingImplClass): def __init__(self, descriptor): CGBindingImplClass.__init__(self, descriptor, CGExampleMethod, CGExampleGetter, CGExampleSetter) - extradeclarations=( - "public:\n" - " NS_DECL_CYCLE_COLLECTING_ISUPPORTS\n" - " NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(%s)\n" - "\n" % descriptor.nativeType.split('::')[-1]) + self.refcounted = descriptor.nativeOwnership == "refcounted" - CGClass.__init__(self, descriptor.nativeType.split('::')[-1], - bases=[ClassBase("nsISupports /* Change nativeOwnership in the binding configuration if you don't want this */"), - ClassBase("nsWrapperCache /* Change wrapperCache in the binding configuration if you don't want this */")], + self.parentIface = descriptor.interface.parent + if self.parentIface: + self.parentDesc = descriptor.getDescriptor( + self.parentIface.identifier.name) + bases = [ClassBase(self.nativeLeafName(self.parentDesc))] + else: + bases = [] + if self.refcounted: + bases.append(ClassBase("nsISupports /* Change nativeOwnership in the binding configuration if you don't want this */")) + if descriptor.wrapperCache: + bases.append(ClassBase("nsWrapperCache /* Change wrapperCache in the binding configuration if you don't want this */")) + else: + bases.append(ClassBase("NonRefcountedDOMObject")) + + if self.refcounted: + if self.parentIface: + extradeclarations=( + "public:\n" + " NS_DECL_ISUPPORTS_INHERITED\n" + " NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(%s, %s)\n" + "\n" % (self.nativeLeafName(descriptor), + self.nativeLeafName(self.parentDesc))) + else: + extradeclarations=( + "public:\n" + " NS_DECL_CYCLE_COLLECTING_ISUPPORTS\n" + " NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(%s)\n" + "\n" % self.nativeLeafName(descriptor)) + else: + extradeclarations="" + + if descriptor.interface.hasChildInterfaces(): + decorators = "" + else: + decorators = "MOZ_FINAL" + + CGClass.__init__(self, self.nativeLeafName(descriptor), + bases=bases, constructors=[ClassConstructor([], visibility="public")], destructor=ClassDestructor(visibility="public"), methods=self.methodDecls, - decorators="MOZ_FINAL", + decorators=decorators, extradeclarations=extradeclarations) def define(self): # Just override CGClass and do our own thing - classImpl = """ + if self.descriptor.wrapperCache: + setDOMBinding = " SetIsDOMBinding();\n" + else: + setDOMBinding = "" + if self.refcounted: + ctordtor = """${nativeType}::${nativeType}() +{ +%s} + +${nativeType}::~${nativeType}() +{ +} +""" % setDOMBinding + else: + ctordtor = """${nativeType}::${nativeType}() +{ + MOZ_COUNT_CTOR(${nativeType}); +} + +${nativeType}::~${nativeType}() +{ + MOZ_COUNT_DTOR(${nativeType}); +} +""" + + if self.refcounted: + if self.parentIface: + classImpl = """ +NS_IMPL_CYCLE_COLLECTION_INHERITED_0(${nativeType}, ${parentType}) +NS_IMPL_ADDREF_INHERITED(${nativeType}, ${parentType}) +NS_IMPL_RELEASE_INHERITED(${nativeType}, ${parentType}) +NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(${nativeType}) +NS_INTERFACE_MAP_END_INHERITING(${parentType}) + +""" + else: + classImpl = """ NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_0(${nativeType}) NS_IMPL_CYCLE_COLLECTING_ADDREF(${nativeType}) NS_IMPL_CYCLE_COLLECTING_RELEASE(${nativeType}) @@ -10595,27 +10662,28 @@ NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(${nativeType}) NS_INTERFACE_MAP_ENTRY(nsISupports) NS_INTERFACE_MAP_END -${nativeType}::${nativeType}() -{ - SetIsDOMBinding(); -} - -${nativeType}::~${nativeType}() -{ -} +""" + else: + classImpl = "" + classImpl += """%s JSObject* ${nativeType}::WrapObject(JSContext* aCx, JS::Handle aScope) { return ${ifaceName}Binding::Wrap(aCx, aScope, this); } -""" +""" % ctordtor return string.Template(classImpl).substitute( { "ifaceName": self.descriptor.name, - "nativeType": self.descriptor.nativeType.split('::')[-1] } + "nativeType": self.nativeLeafName(self.descriptor), + "parentType": self.nativeLeafName(self.parentDesc) if self.parentIface else "", + } ) + @staticmethod + def nativeLeafName(descriptor): + return descriptor.nativeType.split('::')[-1] class CGExampleRoot(CGThing): """