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
|
import WebIDL
def firstArgType(method):
return method.signatures()[0][1][0].type
def WebIDLTest(parser, harness):
parser.parse(
"""
// Give our dictionary a required member so we don't need to
// mess with optional and default values.
dictionary Dict {
required long member;
};
callback interface Foo {
};
interface Bar {
// Bit of a pain to get things that have dictionary types
undefined passDict(Dict arg);
undefined passFoo(Foo arg);
undefined passNullableUnion((object? or DOMString) arg);
undefined passNullable(Foo? arg);
};
"""
)
results = parser.finish()
iface = results[2]
harness.ok(iface.isInterface(), "Should have interface")
dictMethod = iface.members[0]
ifaceMethod = iface.members[1]
nullableUnionMethod = iface.members[2]
nullableIfaceMethod = iface.members[3]
dictType = firstArgType(dictMethod)
ifaceType = firstArgType(ifaceMethod)
harness.ok(dictType.isDictionary(), "Should have dictionary type")
harness.ok(ifaceType.isInterface(), "Should have interface type")
harness.ok(ifaceType.isCallbackInterface(), "Should have callback interface type")
harness.ok(
not dictType.isDistinguishableFrom(ifaceType),
"Dictionary not distinguishable from callback interface",
)
harness.ok(
not ifaceType.isDistinguishableFrom(dictType),
"Callback interface not distinguishable from dictionary",
)
nullableUnionType = firstArgType(nullableUnionMethod)
nullableIfaceType = firstArgType(nullableIfaceMethod)
harness.ok(nullableUnionType.isUnion(), "Should have union type")
harness.ok(nullableIfaceType.isInterface(), "Should have interface type")
harness.ok(nullableIfaceType.nullable(), "Should have nullable type")
harness.ok(
not nullableUnionType.isDistinguishableFrom(nullableIfaceType),
"Nullable type not distinguishable from union with nullable " "member type",
)
harness.ok(
not nullableIfaceType.isDistinguishableFrom(nullableUnionType),
"Union with nullable member type not distinguishable from " "nullable type",
)
parser = parser.reset()
parser.parse(
"""
interface TestIface {
undefined passKid(Kid arg);
undefined passParent(Parent arg);
undefined passGrandparent(Grandparent arg);
undefined passUnrelated1(Unrelated1 arg);
undefined passUnrelated2(Unrelated2 arg);
undefined passArrayBuffer(ArrayBuffer arg);
undefined passArrayBuffer(ArrayBufferView arg);
};
interface Kid : Parent {};
interface Parent : Grandparent {};
interface Grandparent {};
interface Unrelated1 {};
interface Unrelated2 {};
"""
)
results = parser.finish()
iface = results[0]
harness.ok(iface.isInterface(), "Should have interface")
argTypes = [firstArgType(method) for method in iface.members]
unrelatedTypes = [firstArgType(method) for method in iface.members[-3:]]
for type1 in argTypes:
for type2 in argTypes:
distinguishable = type1 is not type2 and (
type1 in unrelatedTypes or type2 in unrelatedTypes
)
harness.check(
type1.isDistinguishableFrom(type2),
distinguishable,
"Type %s should %sbe distinguishable from type %s"
% (type1, "" if distinguishable else "not ", type2),
)
harness.check(
type2.isDistinguishableFrom(type1),
distinguishable,
"Type %s should %sbe distinguishable from type %s"
% (type2, "" if distinguishable else "not ", type1),
)
parser = parser.reset()
parser.parse(
"""
interface Dummy {};
interface TestIface {
undefined method(long arg1, TestIface arg2);
undefined method(long arg1, long arg2);
undefined method(long arg1, Dummy arg2);
undefined method(DOMString arg1, DOMString arg2, DOMString arg3);
};
"""
)
results = parser.finish()
harness.check(len(results[1].members), 1, "Should look like we have one method")
harness.check(
len(results[1].members[0].signatures()), 4, "Should have four signatures"
)
parser = parser.reset()
threw = False
try:
parser.parse(
"""
interface Dummy {};
interface TestIface {
undefined method(long arg1, TestIface arg2);
undefined method(long arg1, long arg2);
undefined method(any arg1, Dummy arg2);
undefined method(DOMString arg1, DOMString arg2, DOMString arg3);
};
"""
)
parser.finish()
except WebIDL.WebIDLError:
threw = True
harness.ok(
threw,
"Should throw when args before the distinguishing arg are not "
"all the same type",
)
parser = parser.reset()
threw = False
try:
parser.parse(
"""
interface Dummy {};
interface TestIface {
undefined method(long arg1, TestIface arg2);
undefined method(long arg1, long arg2);
undefined method(any arg1, DOMString arg2);
undefined method(DOMString arg1, DOMString arg2, DOMString arg3);
};
"""
)
parser.finish()
except WebIDL.WebIDLError:
threw = True
harness.ok(threw, "Should throw when there is no distinguishing index")
# Now let's test our whole distinguishability table
argTypes = [
"long",
"short",
"long?",
"short?",
"boolean",
"boolean?",
"undefined",
"undefined?",
"DOMString",
"ByteString",
"UTF8String",
"Enum",
"Enum2",
"Interface",
"Interface?",
"AncestorInterface",
"UnrelatedInterface",
"CallbackInterface",
"CallbackInterface?",
"CallbackInterface2",
"object",
"Callback",
"Callback2",
"Dict",
"Dict2",
"sequence<long>",
"sequence<short>",
"record<DOMString, object>",
"record<USVString, Dict>",
"record<ByteString, long>",
"record<UTF8String, long>",
"any",
"Promise<any>",
"Promise<any>?",
"USVString",
"JSString",
"ArrayBuffer",
"ArrayBufferView",
"Uint8Array",
"Uint16Array",
"(long or Callback)",
"(long or Dict)",
]
# Try to categorize things a bit to keep list lengths down
def allBut(list1, list2):
return [
a
for a in list1
if a not in list2
and (a != "any" and a != "Promise<any>" and a != "Promise<any>?")
]
unionsWithCallback = ["(long or Callback)"]
unionsNoCallback = ["(long or Dict)"]
unions = unionsWithCallback + unionsNoCallback
numerics = ["long", "short", "long?", "short?"]
booleans = ["boolean", "boolean?"]
undefineds = ["undefined", "undefined?"]
primitives = numerics + booleans
nonNumerics = allBut(argTypes, numerics + unions)
nonBooleans = allBut(argTypes, booleans)
strings = [
"DOMString",
"ByteString",
"Enum",
"Enum2",
"USVString",
"JSString",
"UTF8String",
]
nonStrings = allBut(argTypes, strings)
nonObjects = undefineds + primitives + strings
bufferSourceTypes = ["ArrayBuffer", "ArrayBufferView", "Uint8Array", "Uint16Array"]
interfaces = [
"Interface",
"Interface?",
"AncestorInterface",
"UnrelatedInterface",
] + bufferSourceTypes
nullables = [
"long?",
"short?",
"boolean?",
"undefined?",
"Interface?",
"CallbackInterface?",
"Dict",
"Dict2",
"Date?",
"any",
"Promise<any>?",
] + unionsNoCallback
sequences = ["sequence<long>", "sequence<short>"]
nonUserObjects = nonObjects + interfaces + sequences
otherObjects = allBut(argTypes, nonUserObjects + ["object"])
notRelatedInterfaces = (
nonObjects
+ ["UnrelatedInterface"]
+ otherObjects
+ sequences
+ bufferSourceTypes
)
records = [
"record<DOMString, object>",
"record<USVString, Dict>",
"record<ByteString, long>",
"record<UTF8String, long>",
] # JSString not supported in records
dicts = ["Dict", "Dict2"]
callbacks = ["Callback", "Callback2"]
callbackInterfaces = [
"CallbackInterface",
"CallbackInterface?",
"CallbackInterface2",
]
dictionaryLike = dicts + callbackInterfaces + records + unionsNoCallback
# Build a representation of the distinguishability table as a dict
# of dicts, holding True values where needed, holes elsewhere.
data = dict()
for type in argTypes:
data[type] = dict()
def setDistinguishable(type, types):
for other in types:
data[type][other] = True
setDistinguishable("long", nonNumerics)
setDistinguishable("short", nonNumerics)
setDistinguishable("long?", allBut(nonNumerics, nullables))
setDistinguishable("short?", allBut(nonNumerics, nullables))
setDistinguishable("boolean", nonBooleans)
setDistinguishable("boolean?", allBut(nonBooleans, nullables))
setDistinguishable("undefined", allBut(argTypes, undefineds + dictionaryLike))
setDistinguishable(
"undefined?", allBut(argTypes, undefineds + dictionaryLike + nullables)
)
setDistinguishable("DOMString", nonStrings)
setDistinguishable("ByteString", nonStrings)
setDistinguishable("UTF8String", nonStrings)
setDistinguishable("USVString", nonStrings)
setDistinguishable("JSString", nonStrings)
setDistinguishable("Enum", nonStrings)
setDistinguishable("Enum2", nonStrings)
setDistinguishable("Interface", notRelatedInterfaces)
setDistinguishable("Interface?", allBut(notRelatedInterfaces, nullables))
setDistinguishable("AncestorInterface", notRelatedInterfaces)
setDistinguishable(
"UnrelatedInterface", allBut(argTypes, ["object", "UnrelatedInterface"])
)
setDistinguishable(
"CallbackInterface",
allBut(nonUserObjects + callbacks + unionsWithCallback, undefineds),
)
setDistinguishable(
"CallbackInterface?",
allBut(nonUserObjects + callbacks + unionsWithCallback, nullables + undefineds),
)
setDistinguishable(
"CallbackInterface2",
allBut(nonUserObjects + callbacks + unionsWithCallback, undefineds),
)
setDistinguishable("object", nonObjects)
setDistinguishable(
"Callback",
nonUserObjects + unionsNoCallback + dicts + records + callbackInterfaces,
)
setDistinguishable(
"Callback2",
nonUserObjects + unionsNoCallback + dicts + records + callbackInterfaces,
)
setDistinguishable(
"Dict",
allBut(nonUserObjects + unionsWithCallback + callbacks, nullables + undefineds),
)
setDistinguishable(
"Dict2",
allBut(nonUserObjects + unionsWithCallback + callbacks, nullables + undefineds),
)
setDistinguishable(
"sequence<long>",
allBut(argTypes, sequences + ["object"]),
)
setDistinguishable(
"sequence<short>",
allBut(argTypes, sequences + ["object"]),
)
setDistinguishable(
"record<DOMString, object>",
allBut(nonUserObjects + unionsWithCallback + callbacks, undefineds),
)
setDistinguishable(
"record<USVString, Dict>",
allBut(nonUserObjects + unionsWithCallback + callbacks, undefineds),
)
# JSString not supported in records
setDistinguishable(
"record<ByteString, long>",
allBut(nonUserObjects + unionsWithCallback + callbacks, undefineds),
)
setDistinguishable(
"record<UTF8String, long>",
allBut(nonUserObjects + unionsWithCallback + callbacks, undefineds),
)
setDistinguishable("any", [])
setDistinguishable("Promise<any>", [])
setDistinguishable("Promise<any>?", [])
setDistinguishable("ArrayBuffer", allBut(argTypes, ["ArrayBuffer", "object"]))
setDistinguishable(
"ArrayBufferView",
allBut(argTypes, ["ArrayBufferView", "Uint8Array", "Uint16Array", "object"]),
)
setDistinguishable(
"Uint8Array", allBut(argTypes, ["ArrayBufferView", "Uint8Array", "object"])
)
setDistinguishable(
"Uint16Array", allBut(argTypes, ["ArrayBufferView", "Uint16Array", "object"])
)
setDistinguishable(
"(long or Callback)",
allBut(nonUserObjects + dicts + records + callbackInterfaces, numerics),
)
setDistinguishable(
"(long or Dict)",
allBut(nonUserObjects + callbacks, numerics + nullables + undefineds),
)
def areDistinguishable(type1, type2):
return data[type1].get(type2, False)
def checkDistinguishability(parser, type1, type2):
idlTemplate = """
enum Enum { "a", "b" };
enum Enum2 { "c", "d" };
interface Interface : AncestorInterface {};
interface AncestorInterface {};
interface UnrelatedInterface {};
callback interface CallbackInterface {};
callback interface CallbackInterface2 {};
callback Callback = any();
callback Callback2 = long(short arg);
[LegacyTreatNonObjectAsNull] callback LegacyCallback1 = any();
// Give our dictionaries required members so we don't need to
// mess with optional and default values.
dictionary Dict { required long member; };
dictionary Dict2 { required long member; };
interface TestInterface {%s
};
"""
if type1 in undefineds or type2 in undefineds:
methods = """
(%s or %s) myMethod();""" % (
type1,
type2,
)
else:
methodTemplate = """
undefined myMethod(%s arg);"""
methods = (methodTemplate % type1) + (methodTemplate % type2)
idl = idlTemplate % methods
parser = parser.reset()
threw = False
try:
parser.parse(idl)
parser.finish()
except WebIDL.WebIDLError:
threw = True
if areDistinguishable(type1, type2):
harness.ok(
not threw,
"Should not throw for '%s' and '%s' because they are distinguishable"
% (type1, type2),
)
else:
harness.ok(
threw,
"Should throw for '%s' and '%s' because they are not distinguishable"
% (type1, type2),
)
# Enumerate over everything in both orders, since order matters in
# terms of our implementation of distinguishability checks
for type1 in argTypes:
for type2 in argTypes:
checkDistinguishability(parser, type1, type2)
|