diff options
author | Tom Schuster <evilpies@gmail.com> | 2014-03-14 12:58:37 +0100 |
---|---|---|
committer | Tom Schuster <evilpies@gmail.com> | 2014-03-14 18:15:49 +0100 |
commit | a8362e103a838c8b14e45363d6f7a692371af58f (patch) | |
tree | 8e273c24e29d5eced43e17b8fcce68a721ae5f10 | |
parent | 71f4fd0478183692ba114351841b44c58691e665 (diff) | |
download | servo-a8362e103a838c8b14e45363d6f7a692371af58f.tar.gz servo-a8362e103a838c8b14e45363d6f7a692371af58f.zip |
Implement named constants in bindings
-rw-r--r-- | src/components/script/dom/bindings/codegen/CodegenRust.py | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/src/components/script/dom/bindings/codegen/CodegenRust.py b/src/components/script/dom/bindings/codegen/CodegenRust.py index 2b3b4ca8bab..443f68aa318 100644 --- a/src/components/script/dom/bindings/codegen/CodegenRust.py +++ b/src/components/script/dom/bindings/codegen/CodegenRust.py @@ -2970,6 +2970,34 @@ class CGEnum(CGThing): """ % (",\n ".join(map(getEnumValueName, self.enum.values())), ",\n ".join(['EnumEntry {value: &"' + val + '", length: ' + str(len(val)) + '}' for val in self.enum.values()])) + +def convertConstIDLValueToRust(value): + tag = value.type.tag() + if tag in [IDLType.Tags.int8, IDLType.Tags.uint8, + IDLType.Tags.int16, IDLType.Tags.uint16, + IDLType.Tags.int32, IDLType.Tags.uint32, + IDLType.Tags.int64, IDLType.Tags.uint64, + IDLType.Tags.float, IDLType.Tags.double]: + return str(value.value) + + if tag == IDLType.Tags.bool: + return toStringBool(value.value) + + raise TypeError("Const value of unhandled type: " + value.type) + +class CGConstant(CGThing): + def __init__(self, constants): + CGThing.__init__(self) + self.constants = constants + + def define(self): + def stringDecl(const): + name = const.identifier.name + value = convertConstIDLValueToRust(const.value) + return CGGeneric("static %s: %s = %s;\n" % (name, builtinNames[const.value.type.tag()], value)) + + return CGIndenter(CGList(stringDecl(m) for m in self.constants)).define() + def getUnionAccessorSignatureType(type, descriptorProvider): """ Returns the types that are used in the getter and setter signatures for @@ -4309,6 +4337,10 @@ class CGDescriptor(CGThing): cgThings.append(CGGeneric(str(properties))) cgThings.append(CGCreateInterfaceObjectsMethod(descriptor, properties)) + cgThings.append(CGNamespace.build([descriptor.name + "Constants"], + CGConstant(m for m in descriptor.interface.members if m.isConst()), + public=True)) + # Set up our Xray callbacks as needed. if descriptor.interface.hasInterfacePrototypeObject(): if descriptor.concrete and descriptor.proxy: |