mirror of
https://github.com/servo/servo.git
synced 2025-08-03 20:50:07 +01:00
Implement named constants in bindings
This commit is contained in:
parent
71f4fd0478
commit
a8362e103a
1 changed files with 32 additions and 0 deletions
|
@ -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:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue