DOM protos need a custom stringification, so need to have a custom class. Also, improve infrastruture for adding [NoInterfaceObject] support once the parser tells us about it
authorBoris Zbarsky <bzbarsky@mit.edu>
Wed, 01 Feb 2012 10:25:58 -0500
changeset 86005 98112293e26376b44af89c19f570b8b3140fdca1
parent 86004 6b8a7a4d4655cf3212637624efd24fc4ad2d194f
child 86006 8a06ecf9b6fdba19b1004e63c567403e9fae5e07
push id86
push userbzbarsky@mozilla.com
push dateWed, 01 Feb 2012 15:26:05 +0000
milestone13.0a1
DOM protos need a custom stringification, so need to have a custom class. Also, improve infrastruture for adding [NoInterfaceObject] support once the parser tells us about it
dom/bindings/Codegen.py
dom/bindings/Utils.cpp
dom/bindings/Utils.h
--- a/dom/bindings/Codegen.py
+++ b/dom/bindings/Codegen.py
@@ -50,16 +50,46 @@ DOMJSClass Class = {
     NULL,                  /* trace */
     NULL                   /* reserved1 */
   },
   { %s }, -1, %s
 };
 """ % (self.descriptor.interface.identifier.name, prototypeChainString,
        str(self.descriptor.nativeIsISupports).lower())
 
+class CGPrototypeJSClass(CGThing):
+    def __init__(self, descriptor):
+        CGThing.__init__(self)
+        self.descriptor = descriptor
+    def declare(self):
+        # We're purely for internal consumption
+        return ""
+    def define(self):
+        return """
+static JSClass PrototypeClass = {
+  "%s Prototype", 0,
+  JS_PropertyStub,       /* addProperty */
+  JS_PropertyStub,       /* delProperty */
+  JS_PropertyStub,       /* getProperty */
+  JS_StrictPropertyStub, /* setProperty */
+  JS_EnumerateStub,
+  JS_ResolveStub,
+  JS_ConvertStub,
+  NULL,                  /* finalize */
+  NULL,                  /* reserved0 */
+  NULL,                  /* checkAccess */
+  NULL,                  /* call */
+  NULL,                  /* construct */
+  NULL,                  /* xdrObject */
+  NULL,                  /* hasInstance */
+  NULL,                  /* trace */
+  NULL                   /* reserved1 */
+};
+""" % (self.descriptor.interface.identifier.name)
+
 class CGConstructorJSClass(CGThing):
     def __init__(self, descriptor):
         CGThing.__init__(self)
         self.descriptor = descriptor
     def declare(self):
         # We're purely for internal consumption
         return ""
     def define(self):
@@ -71,16 +101,17 @@ static JSClass ConstructorClass = {
   JS_PropertyStub,       /* getProperty */
   JS_StrictPropertyStub, /* setProperty */
   JS_EnumerateStub,
   JS_ResolveStub,
   JS_ConvertStub,
   NULL,                  /* finalize */
   NULL,                  /* reserved0 */
   NULL,                  /* checkAccess */
+  // XXXbz This may need a useful call hook
   NULL,                  /* call */
   NULL,                  /* construct */
   NULL,                  /* xdrObject */
   // XXXbz This needs a useful hasInstance hook
   NULL,                  /* hasInstance */
   NULL,                  /* trace */
   NULL                   /* reserved1 */
 };
@@ -306,32 +337,36 @@ class CGCreateProtoObjectMethod(CGAbstra
             getParentProto = "GetCanonicalProto(aCx, aGlobal, JSProto_Object)"
         else:
             parentProtoName = self.descriptor.prototypeChain[-2]
             getParentProto = "%s::GetProtoObject(aCx, aGlobal)" % (parentProtoName)
 
         defineMethods = MethodDefiner(self.descriptor)
         defineAttributes = AttrDefiner(self.descriptor);
 
+        # XXXbz this should be False if we're [NoInterfaceObject]
+        needInterfaceObject = True
+
         return """
   JSObject* parentProto = %s;
   if (!parentProto) {
     return NULL;
   }
 
 %s
 %s
 
-  return FinishProtoCreation(aCx, parentProto, &ConstructorClass, %s,
-                             %s, aGlobal, "%s");""" % (
+  return bindings::CreateProtoObject(aCx, parentProto, &PrototypeClass,
+                                     %s, %s, %s,
+                                     aGlobal, "%s");""" % (
             getParentProto, defineMethods, defineAttributes,
+            "&ConstructorClass" if needInterfaceObject else "NULL",
             "methods" if defineMethods.hasMethods() else "NULL",
             "props" if defineAttributes.hasAttrs() else "NULL",
-            # XXXbz this should be "NULL" if we're [NoInterfaceObject]
-            self.descriptor.interface.identifier.name)
+            self.descriptor.interface.identifier.name if needInterfaceObject else "NULL")
 
 class CGGetProtoObjectMethod(CGAbstractMethod):
     def __init__(self, descriptor):
         args = [Argument('JSContext*', 'aCx'), Argument('JSObject*', 'aGlobal')]
         CGAbstractMethod.__init__(self, descriptor, 'GetProtoObject',
                                   'JSObject*', args, inline=True)
     def definition_body(self):
         return """
@@ -424,18 +459,21 @@ class CGDescriptor(CGThing):
         # XXXbholley - Not everything should actually have a jsclass.
         cgThings = [CGNativeMethod(descriptor, m) for m in
                     descriptor.interface.members if m.isMethod()]
         cgThings.extend([CGNativeGetter(descriptor, a) for a in
                          descriptor.interface.members if a.isAttr()])
         cgThings.extend([CGNativeSetter(descriptor, a) for a in
                          descriptor.interface.members if
                          a.isAttr() and not a.readonly])
+        # XXXbz this should check for [NoInterfaceObject]
+        if True:
+            cgThings.append(CGConstructorJSClass(descriptor))
         cgThings.extend([CGDOMJSClass(descriptor),
-                         CGConstructorJSClass(descriptor),
+                         CGPrototypeJSClass(descriptor),
                          CGCreateProtoObjectMethod(descriptor),
                          CGGetProtoObjectMethod(descriptor)])
 
         allCGThings = CGList(cgThings)
         allCGThings = CGWrapper(allCGThings, post="\n")
         self.cgRoot = CGWrapper(CGNamespace(descriptor.name, allCGThings), post="\n")
     def declare(self):
         return self.cgRoot.declare()
--- a/dom/bindings/Utils.cpp
+++ b/dom/bindings/Utils.cpp
@@ -6,53 +6,60 @@
 
 #include "Utils.h"
 
 namespace mozilla {
 namespace dom {
 namespace bindings {
 
 JSObject*
-FinishProtoCreation(JSContext* cx, JSObject* parentProto,
-                    JSClass* constructorClass,
-                    JSFunctionSpec* methods,
-                    JSPropertySpec* properties,
-                    JSObject* global, const char* name)
+CreateProtoObject(JSContext *cx, JSObject *parentProto,
+                  JSClass *protoClass,
+                  JSClass *constructorClass,
+                  JSFunctionSpec *methods,
+                  JSPropertySpec *properties,
+                  JSObject *global,
+                  const char* name)
 {
-  JSObject* ourProto = JS_NewObject(cx, NULL, parentProto, global);
+  MOZ_ASSERT(bool(name) == bool(constructorClass),
+             "Must have name precisely when we have an interface object");
+
+  JSObject* ourProto = JS_NewObject(cx, protoClass, parentProto, global);
   if (!ourProto) {
     return NULL;
   }
 
   JSObject* functionProto = GetCanonicalProto(cx, global, JSProto_Function);
   if (!functionProto) {
     return NULL;
   }
 
-  JSObject* constructor =
-    JS_NewObject(cx, constructorClass, functionProto, global);
-  if (!constructor) {
-    return NULL;
-  }
-
-  if (!JS_LinkConstructorAndPrototype(cx, constructor, ourProto)) {
-    return NULL;
-  }
-
   if (methods && !JS_DefineFunctions(cx, ourProto, methods)) {
     return NULL;
   }
 
   if (properties && !JS_DefineProperties(cx, ourProto, properties)) {
     return NULL;
   }
 
-  if (name && !JS_DefineProperty(cx, global, name, OBJECT_TO_JSVAL(constructor),
-                                 NULL, NULL, 0)) {
-    return NULL;
+  if (constructorClass) {
+    JSObject* constructor =
+      JS_NewObject(cx, constructorClass, functionProto, global);
+    if (!constructor) {
+      return NULL;
+    }
+
+    if (!JS_LinkConstructorAndPrototype(cx, constructor, ourProto)) {
+      return NULL;
+    }
+
+    if (!JS_DefineProperty(cx, global, name, OBJECT_TO_JSVAL(constructor),
+                           NULL, NULL, 0)) {
+      return NULL;
+    }
   }
 
   return ourProto;
 }
 
 } // namespace bindings
 } // namespace dom
 } // namespace mozilla
--- a/dom/bindings/Utils.h
+++ b/dom/bindings/Utils.h
@@ -159,31 +159,39 @@ GetCanonicalProto(JSContext *cx, JSObjec
   JSObject* proto;
   if (!js_GetClassPrototype(cx, global, protoKey, &proto)) {
     return NULL;
   }
   return proto;
 }
 
 /*
- * Complete creation of a proto object.  parentProto is the prototype
- * our new object should use.  constructorClass is the class to use
- * for the corresponding constructor.  methods and properties are to
- * be defined on the prototype; those arguments are allowed to be
- * null.  The resulting constructor object will be defined on the
- * given global with property name |name| unless |name| is null.
+ * Create a DOM prototype object.
+ *
+ * parentProto is the prototype our new object should use.
+ * protoClass is the JSClass our new object should use.
+ * constructorClass is the class to use for the corresponding interface object.
+ *                  This is null if we should not create an interface object.
+ * methods and properties are to be defined on the prototype; these arguments
+ *                        are allowed to be null if there are no methods or
+ *                        properties respectively.
+ * If constructorClass is non-null, The resulting constructor object will be
+ * defined on the given global with property name |name|, which must also be
+ * non-null.
  *
  * The return value is the newly-created prototype object.
  */
 JSObject*
-FinishProtoCreation(JSContext *cx, JSObject *parentProto,
-                    JSClass *constructorClass,
-                    JSFunctionSpec *methods,
-                    JSPropertySpec *properties,
-                    JSObject *global, const char* name);
+CreateProtoObject(JSContext *cx, JSObject *parentProto,
+                  JSClass *protoClass,
+                  JSClass *constructorClass,
+                  JSFunctionSpec *methods,
+                  JSPropertySpec *properties,
+                  JSObject *global,
+                  const char* name);
 
 template<class T>
 inline bool
 WrapNewBindingObject(JSContext *cx, JSObject *scope, T *value, jsval *vp)
 {
   JSObject *obj = value->GetWrapper();
   if (obj)
     return obj;