forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Configuration.py
511 lines (451 loc) · 21.3 KB
/
Configuration.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
from WebIDL import IDLInterface, IDLExternalInterface
import os
autogenerated_comment = "/* THIS FILE IS AUTOGENERATED - DO NOT EDIT */\n"
class Configuration:
"""
Represents global configuration state based on IDL parse data and
the configuration file.
"""
def __init__(self, filename, parseData):
# Read the configuration file.
glbl = {}
execfile(filename, glbl)
config = glbl['DOMInterfaces']
# Build descriptors for all the interfaces we have in the parse data.
# This allows callers to specify a subset of interfaces by filtering
# |parseData|.
self.descriptors = []
self.interfaces = {}
self.maxProtoChainLength = 0;
for thing in parseData:
# Some toplevel things are sadly types, and those have an
# isInterface that doesn't mean the same thing as IDLObject's
# isInterface()...
if (not isinstance(thing, IDLInterface) and
not isinstance(thing, IDLExternalInterface)):
continue
iface = thing
self.interfaces[iface.identifier.name] = iface
if iface.identifier.name not in config:
# Completely skip consequential interfaces with no descriptor
# because chances are we don't need to do anything interesting
# with them.
if iface.isConsequential():
continue
entry = {}
else:
entry = config[iface.identifier.name]
if not isinstance(entry, list):
assert isinstance(entry, dict)
entry = [entry]
elif len(entry) == 1 and entry[0].get("workers", False):
# List with only a workers descriptor means we should
# infer a mainthread descriptor. If you want only
# workers bindings, don't use a list here.
entry.append({})
self.descriptors.extend([Descriptor(self, iface, x) for x in entry])
# Mark the descriptors for which the nativeType corresponds to exactly
# one interface.
for descriptor in self.descriptors:
descriptor.unsharedImplementation = all(
d.nativeType != descriptor.nativeType or d == descriptor
for d in self.descriptors)
self.enums = [e for e in parseData if e.isEnum()]
# Figure out what our main-thread and worker dictionaries and callbacks
# are.
mainTypes = set()
for descriptor in self.getDescriptors(workers=False, isExternal=False):
mainTypes |= set(getFlatTypes(getTypesFromDescriptor(descriptor)))
(mainCallbacks, mainDictionaries) = findCallbacksAndDictionaries(mainTypes)
workerTypes = set();
for descriptor in self.getDescriptors(workers=True, isExternal=False):
workerTypes |= set(getFlatTypes(getTypesFromDescriptor(descriptor)))
(workerCallbacks, workerDictionaries) = findCallbacksAndDictionaries(workerTypes)
self.dictionaries = [d for d in parseData if d.isDictionary()]
self.callbacks = [c for c in parseData if
c.isCallback() and not c.isInterface()]
def flagWorkerOrMainThread(items, main, worker):
for item in items:
if item in main:
item.setUserData("mainThread", True)
if item in worker:
item.setUserData("workers", True)
flagWorkerOrMainThread(self.dictionaries, mainDictionaries,
workerDictionaries);
flagWorkerOrMainThread(self.callbacks, mainCallbacks, workerCallbacks)
# Keep the descriptor list sorted for determinism.
self.descriptors.sort(lambda x,y: cmp(x.name, y.name))
def getInterface(self, ifname):
return self.interfaces[ifname]
def getDescriptors(self, **filters):
"""Gets the descriptors that match the given filters."""
curr = self.descriptors
for key, val in filters.iteritems():
if key == 'webIDLFile':
getter = lambda x: x.interface.filename()
elif key == 'hasInterfaceObject':
getter = lambda x: (not x.interface.isExternal() and
x.interface.hasInterfaceObject())
elif key == 'hasInterfacePrototypeObject':
getter = lambda x: (not x.interface.isExternal() and
x.interface.hasInterfacePrototypeObject())
elif key == 'hasInterfaceOrInterfacePrototypeObject':
getter = lambda x: x.hasInterfaceOrInterfacePrototypeObject()
elif key == 'isCallback':
getter = lambda x: x.interface.isCallback()
elif key == 'isExternal':
getter = lambda x: x.interface.isExternal()
else:
getter = lambda x: getattr(x, key)
curr = filter(lambda x: getter(x) == val, curr)
return curr
def getEnums(self, webIDLFile):
return filter(lambda e: e.filename() == webIDLFile, self.enums)
@staticmethod
def _filterForFileAndWorkers(items, filters):
"""Gets the items that match the given filters."""
for key, val in filters.iteritems():
if key == 'webIDLFile':
items = filter(lambda x: x.filename() == val, items)
elif key == 'workers':
if val:
items = filter(lambda x: x.getUserData("workers", False), items)
else:
items = filter(lambda x: x.getUserData("mainThread", False), items)
else:
assert(0) # Unknown key
return items
def getDictionaries(self, **filters):
return self._filterForFileAndWorkers(self.dictionaries, filters)
def getCallbacks(self, **filters):
return self._filterForFileAndWorkers(self.callbacks, filters)
def getDescriptor(self, interfaceName, workers):
"""
Gets the appropriate descriptor for the given interface name
and the given workers boolean.
"""
iface = self.getInterface(interfaceName)
descriptors = self.getDescriptors(interface=iface)
# The only filter we currently have is workers vs non-workers.
matches = filter(lambda x: x.workers is workers, descriptors)
# After filtering, we should have exactly one result.
if len(matches) is not 1:
raise NoSuchDescriptorError("For " + interfaceName + " found " +
str(len(matches)) + " matches");
return matches[0]
def getDescriptorProvider(self, workers):
"""
Gets a descriptor provider that can provide descriptors as needed,
for the given workers boolean
"""
return DescriptorProvider(self, workers)
class NoSuchDescriptorError(TypeError):
def __init__(self, str):
TypeError.__init__(self, str)
class DescriptorProvider:
"""
A way of getting descriptors for interface names
"""
def __init__(self, config, workers):
self.config = config
self.workers = workers
def getDescriptor(self, interfaceName):
"""
Gets the appropriate descriptor for the given interface name given the
context of the current descriptor. This selects the appropriate
implementation for cases like workers.
"""
return self.config.getDescriptor(interfaceName, self.workers)
class Descriptor(DescriptorProvider):
"""
Represents a single descriptor for an interface. See Bindings.conf.
"""
def __init__(self, config, interface, desc):
DescriptorProvider.__init__(self, config, desc.get('workers', False))
self.interface = interface
# Read the desc, and fill in the relevant defaults.
ifaceName = self.interface.identifier.name
if self.interface.isExternal():
if self.workers:
nativeTypeDefault = "JSObject"
else:
nativeTypeDefault = "nsIDOM" + ifaceName
elif self.interface.isCallback():
if self.workers:
nativeTypeDefault = "JSObject"
else:
nativeTypeDefault = "mozilla::dom::" + ifaceName
else:
if self.workers:
nativeTypeDefault = "mozilla::dom::workers::" + ifaceName
else:
nativeTypeDefault = "mozilla::dom::" + ifaceName
self.nativeType = desc.get('nativeType', nativeTypeDefault)
# Do something sane for JSObject
if self.nativeType == "JSObject":
headerDefault = "jsapi.h"
elif self.interface.isCallback():
# A copy of CGHeaders.getDeclarationFilename; we can't
# import it here, sadly.
# Use our local version of the header, not the exported one, so that
# test bindings, which don't export, will work correctly.
basename = os.path.basename(self.interface.filename())
headerDefault = basename.replace('.webidl', 'Binding.h')
else:
if self.workers:
headerDefault = "mozilla/dom/workers/bindings/%s.h" % ifaceName
else:
headerDefault = self.nativeType
headerDefault = headerDefault.replace("::", "/") + ".h"
self.headerFile = desc.get('headerFile', headerDefault)
self.skipGen = desc.get('skipGen', False)
self.notflattened = desc.get('notflattened', False)
self.register = desc.get('register', True)
self.hasXPConnectImpls = desc.get('hasXPConnectImpls', False)
# If we're concrete, we need to crawl our ancestor interfaces and mark
# them as having a concrete descendant.
self.concrete = (not self.interface.isExternal() and
not self.interface.isCallback() and
desc.get('concrete', True))
operations = {
'IndexedGetter': None,
'IndexedSetter': None,
'IndexedCreator': None,
'IndexedDeleter': None,
'NamedGetter': None,
'NamedSetter': None,
'NamedCreator': None,
'NamedDeleter': None,
'Stringifier': None
}
if self.concrete:
self.proxy = False
iface = self.interface
def addOperation(operation, m):
if not operations[operation]:
operations[operation] = m
# Since stringifiers go on the prototype, we only need to worry
# about our own stringifier, not those of our ancestor interfaces.
for m in iface.members:
if m.isMethod() and m.isStringifier():
addOperation('Stringifier', m)
while iface:
for m in iface.members:
if not m.isMethod():
continue
def addIndexedOrNamedOperation(operation, m):
self.proxy = True
if m.isIndexed():
operation = 'Indexed' + operation
else:
assert m.isNamed()
operation = 'Named' + operation
addOperation(operation, m)
if m.isGetter():
addIndexedOrNamedOperation('Getter', m)
if m.isSetter():
addIndexedOrNamedOperation('Setter', m)
if m.isCreator():
addIndexedOrNamedOperation('Creator', m)
if m.isDeleter():
addIndexedOrNamedOperation('Deleter', m)
iface.setUserData('hasConcreteDescendant', True)
iface = iface.parent
if self.proxy:
if (not operations['IndexedGetter'] and
(operations['IndexedSetter'] or
operations['IndexedDeleter'] or
operations['IndexedCreator'])):
raise SyntaxError("%s supports indexed properties but does "
"not have an indexed getter.\n%s" %
(self.interface, self.interface.location))
if (not operations['NamedGetter'] and
(operations['NamedSetter'] or
operations['NamedDeleter'] or
operations['NamedCreator'])):
raise SyntaxError("%s supports named properties but does "
"not have a named getter.\n%s" %
(self.interface, self.interface.location))
iface = self.interface
while iface:
iface.setUserData('hasProxyDescendant', True)
iface = iface.parent
self.operations = operations
if self.interface.isExternal() and 'prefable' in desc:
raise TypeError("%s is external but has a prefable setting" %
self.interface.identifier.name)
self.prefable = desc.get('prefable', False)
if self.workers:
if desc.get('nativeOwnership', 'worker') != 'worker':
raise TypeError("Worker descriptor for %s should have 'worker' "
"as value for nativeOwnership" %
self.interface.identifier.name)
self.nativeOwnership = "worker"
else:
self.nativeOwnership = desc.get('nativeOwnership', 'nsisupports')
if not self.nativeOwnership in ['owned', 'refcounted', 'nsisupports']:
raise TypeError("Descriptor for %s has unrecognized value (%s) "
"for nativeOwnership" %
(self.interface.identifier.name, self.nativeOwnership))
self.customTrace = desc.get('customTrace', self.workers)
self.customFinalize = desc.get('customFinalize', self.workers)
self.wrapperCache = (not self.interface.isCallback() and
(self.workers or
(self.nativeOwnership != 'owned' and
desc.get('wrapperCache', True))))
if not self.wrapperCache and self.prefable:
raise TypeError("Descriptor for %s is prefable but not wrappercached" %
self.interface.identifier.name)
def make_name(name):
return name + "_workers" if self.workers else name
self.name = make_name(interface.identifier.name)
# self.extendedAttributes is a dict of dicts, keyed on
# all/getterOnly/setterOnly and then on member name. Values are an
# array of extended attributes.
self.extendedAttributes = { 'all': {}, 'getterOnly': {}, 'setterOnly': {} }
def addExtendedAttribute(attribute, config):
def add(key, members, attribute):
for member in members:
self.extendedAttributes[key].setdefault(member, []).append(attribute)
if isinstance(config, dict):
for key in ['all', 'getterOnly', 'setterOnly']:
add(key, config.get(key, []), attribute)
elif isinstance(config, list):
add('all', config, attribute)
else:
assert isinstance(config, str)
if config == '*':
iface = self.interface
while iface:
add('all', map(lambda m: m.name, iface.members), attribute)
iface = iface.parent
else:
add('all', [config], attribute)
for attribute in ['implicitJSContext', 'resultNotAddRefed']:
addExtendedAttribute(attribute, desc.get(attribute, {}))
self.binaryNames = desc.get('binaryNames', {})
# Build the prototype chain.
self.prototypeChain = []
parent = interface
while parent:
self.prototypeChain.insert(0, make_name(parent.identifier.name))
parent = parent.parent
config.maxProtoChainLength = max(config.maxProtoChainLength,
len(self.prototypeChain))
def hasInterfaceOrInterfacePrototypeObject(self):
# Forward-declared interfaces don't need either interface object or
# interface prototype object as they're going to use QI (on main thread)
# or be passed as a JSObject (on worker threads).
if self.interface.isExternal():
return False
return self.interface.hasInterfaceObject() or self.interface.hasInterfacePrototypeObject()
def getExtendedAttributes(self, member, getter=False, setter=False):
def ensureValidThrowsExtendedAttribute(attr):
assert(attr is None or attr is True or len(attr) == 1)
if (attr is not None and attr is not True and
'Workers' not in attr and 'MainThread' not in attr):
raise TypeError("Unknown value for 'Throws': " + attr[0])
def maybeAppendInfallibleToAttrs(attrs, throws):
ensureValidThrowsExtendedAttribute(throws)
if (throws is None or
(throws is not True and
('Workers' not in throws or not self.workers) and
('MainThread' not in throws or self.workers))):
attrs.append("infallible")
name = member.identifier.name
if member.isMethod():
attrs = self.extendedAttributes['all'].get(name, [])
throws = member.getExtendedAttribute("Throws")
maybeAppendInfallibleToAttrs(attrs, throws)
return attrs
assert member.isAttr()
assert bool(getter) != bool(setter)
key = 'getterOnly' if getter else 'setterOnly'
attrs = self.extendedAttributes['all'].get(name, []) + self.extendedAttributes[key].get(name, [])
throws = member.getExtendedAttribute("Throws")
if throws is None:
throwsAttr = "GetterThrows" if getter else "SetterThrows"
throws = member.getExtendedAttribute(throwsAttr)
maybeAppendInfallibleToAttrs(attrs, throws)
return attrs
def supportsIndexedProperties(self):
return self.operations['IndexedGetter'] is not None
def supportsNamedProperties(self):
return self.operations['NamedGetter'] is not None
def needsConstructHookHolder(self):
assert self.interface.hasInterfaceObject()
return False
# Some utility methods
def getTypesFromDescriptor(descriptor):
"""
Get all argument and return types for all members of the descriptor
"""
members = [m for m in descriptor.interface.members]
if descriptor.interface.ctor():
members.append(descriptor.interface.ctor())
signatures = [s for m in members if m.isMethod() for s in m.signatures()]
types = []
for s in signatures:
assert len(s) == 2
(returnType, arguments) = s
types.append(returnType)
types.extend(a.type for a in arguments)
types.extend(a.type for a in members if a.isAttr())
return types
def getFlatTypes(types):
retval = set()
for type in types:
type = type.unroll()
if type.isUnion():
retval |= set(type.flatMemberTypes)
else:
retval.add(type)
return retval
def getTypesFromDictionary(dictionary):
"""
Get all member types for this dictionary
"""
types = []
curDict = dictionary
while curDict:
types.extend([m.type for m in curDict.members])
curDict = curDict.parent
return types
def getTypesFromCallback(callback):
"""
Get the types this callback depends on: its return type and the
types of its arguments.
"""
sig = callback.signatures()[0]
types = [sig[0]] # Return type
types.extend(arg.type for arg in sig[1]) # Arguments
return types
def findCallbacksAndDictionaries(inputTypes):
"""
Ensure that all callbacks and dictionaries reachable from types end up in
the returned callbacks and dictionaries sets.
Note that we assume that our initial invocation already includes all types
reachable via descriptors in "types", so we only have to deal with things
that are themeselves reachable via callbacks and dictionaries.
"""
def doFindCallbacksAndDictionaries(types, callbacks, dictionaries):
unhandledTypes = set()
for type in types:
if type.isCallback() and type not in callbacks:
unhandledTypes |= getFlatTypes(getTypesFromCallback(type))
callbacks.add(type)
elif type.isDictionary() and type.inner not in dictionaries:
d = type.inner
unhandledTypes |= getFlatTypes(getTypesFromDictionary(d))
while d:
dictionaries.add(d)
d = d.parent
if len(unhandledTypes) != 0:
doFindCallbacksAndDictionaries(unhandledTypes, callbacks, dictionaries)
retCallbacks = set()
retDictionaries = set()
doFindCallbacksAndDictionaries(inputTypes, retCallbacks, retDictionaries)
return (retCallbacks, retDictionaries)