Skip to content

Commit

Permalink
Implement named constants in bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
evilpie committed Mar 14, 2014
1 parent 71f4fd0 commit a8362e1
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/components/script/dom/bindings/codegen/CodegenRust.py
Expand Up @@ -2970,6 +2970,34 @@ def define(self):
""" % (",\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
Expand Down Expand Up @@ -4309,6 +4337,10 @@ def __init__(self, descriptor):
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:
Expand Down

5 comments on commit a8362e1

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from Ms2ger
at evilpie@a8362e1

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging evilpie/servo/constants = a8362e1 into auto

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

evilpie/servo/constants = a8362e1 merged ok, testing candidate = 082de0e

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 082de0e

Please sign in to comment.