Bug 792059 - Codegen generation of WebIDL constants in C++; r=bz draft
authorKyle Machulis <kyle@nonpolynomial.com>
Mon, 09 Oct 2017 19:59:36 -0700
changeset 685590 ef23ebf4501bfb5d898e4304e6fcf7e4bad68faa
parent 685447 967c95cee709756596860ed2a3e6ac06ea3a053f
child 685591 de187ad41cbbc3c38c5042cc3af06155a714022e
push id85990
push userbmo:kyle@nonpolynomial.com
push dateTue, 24 Oct 2017 22:11:11 +0000
reviewersbz
bugs792059
milestone58.0a1
Bug 792059 - Codegen generation of WebIDL constants in C++; r=bz Generate C++ const definitions for WebIDL interface members with primitive types. MozReview-Commit-ID: 4fLtteWZUWA
dom/bindings/Codegen.py
--- a/dom/bindings/Codegen.py
+++ b/dom/bindings/Codegen.py
@@ -45,16 +45,19 @@ def mayUseXrayExpandoSlots(descriptor, a
     assert not attr.getExtendedAttribute("NewObject")
     # For attributes whose type is a Gecko interface we always use
     # slots on the reflector for caching.  Also, for interfaces that
     # don't want Xrays we obviously never use the Xray expando slot.
     return descriptor.wantsXrays and not attr.type.isGeckoInterface()
 
 
 def toStringBool(arg):
+    """
+    Converts IDL/Python Boolean (True/False) to C++ Boolean (true/false)
+    """
     return str(not not arg).lower()
 
 
 def toBindingNamespace(arg):
     return arg + "Binding"
 
 
 def isTypeCopyConstructible(type):
@@ -2828,16 +2831,46 @@ class PropertyArrays():
 
     def __str__(self):
         define = ""
         for array in self.arrayNames():
             define += str(getattr(self, array))
         return define
 
 
+class CGConstDefinition(CGThing):
+    """
+    Given a const member of an interface, return the C++ static const definition
+    for the member. Should be part of the interface namespace in the header
+    file.
+    """
+    def __init__(self, member):
+        assert (member.isConst() and
+                member.value.type.isPrimitive() and
+                not member.value.type.nullable())
+
+        name = CppKeywords.checkMethodName(IDLToCIdentifier(member.identifier.name))
+        tag = member.value.type.tag()
+        value = member.value.value
+        if tag == IDLType.Tags.bool:
+            value = toStringBool(member.value.value)
+        self.const = "static const %s %s = %s;" % (builtinNames[tag],
+                                                   name,
+                                                   value)
+
+    def declare(self):
+        return self.const
+
+    def define(self):
+        return ""
+
+    def deps(self):
+        return []
+
+
 class CGNativeProperties(CGList):
     def __init__(self, descriptor, properties):
         def generateNativeProperties(name, chrome):
             def check(p):
                 return p.hasChromeOnly() if chrome else p.hasNonChromeOnly()
 
             nativePropsInts = []
             nativePropsPtrs = []
@@ -6370,17 +6403,17 @@ def convertConstIDLValueToJSVal(value):
     if tag in [IDLType.Tags.int8, IDLType.Tags.uint8, IDLType.Tags.int16,
                IDLType.Tags.uint16, IDLType.Tags.int32]:
         return "JS::Int32Value(%s)" % (value.value)
     if tag == IDLType.Tags.uint32:
         return "JS::NumberValue(%sU)" % (value.value)
     if tag in [IDLType.Tags.int64, IDLType.Tags.uint64]:
         return "JS::CanonicalizedDoubleValue(%s)" % numericValue(tag, value.value)
     if tag == IDLType.Tags.bool:
-        return "JS::BooleanValue(true)" if value.value else "JS::BooleanValue(false)"
+        return "JS::BooleanValue(%s)" % (toStringBool(value.value))
     if tag in [IDLType.Tags.float, IDLType.Tags.double]:
         return "JS::CanonicalizedDoubleValue(%s)" % (value.value)
     raise TypeError("Const value of unhandled type: %s" % value.type)
 
 
 class CGArgumentConverter(CGThing):
     """
     A class that takes an IDL argument object and its index in the
@@ -12765,16 +12798,18 @@ class CGDescriptor(CGThing):
                         crossOriginSetters.add(m.identifier.name)
                 elif m.getExtendedAttribute("Replaceable"):
                     cgThings.append(CGSpecializedReplaceableSetter(descriptor, m))
                 elif m.getExtendedAttribute("LenientSetter"):
                     cgThings.append(CGSpecializedLenientSetter(descriptor, m))
                 if (not m.isStatic() and
                     descriptor.interface.hasInterfacePrototypeObject()):
                     cgThings.append(CGMemberJITInfo(descriptor, m))
+            if m.isConst() and m.type.isPrimitive():
+                cgThings.append(CGConstDefinition(m))
 
             hasMethod = hasMethod or props.isGenericMethod
             hasPromiseReturningMethod = (hasPromiseReturningMethod or
                                          props.isPromiseReturningMethod)
             hasPromiseReturningGetter = (hasPromiseReturningGetter or
                                          props.isPromiseReturningGetter)
             hasGetter = hasGetter or props.isGenericGetter
             hasLenientGetter = hasLenientGetter or props.isLenientGetter