-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathparse-webidl.js
446 lines (409 loc) · 15.1 KB
/
parse-webidl.js
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
#!/usr/bin/env node
/**
* The WebIDL parser takes the URL of a spec as input and outputs a JSON
* structure that describes the WebIDL term definitions and references that the
* spec contains.
*
* The WebIDL parser uses the [WebIDL extractor]{@link module:webidlExtractor}
* to fetch and extract the WebIDL definitions contained in the given spec, and
* analyzes that spec with [WebIDL2]{@link
* https://github.com/darobin/webidl2.js}
*
* The WebIDL parser can be called directly through:
*
* `node parse-webidl.js [url]`
*
* where `url` is the URL of the spec to fetch and parse.
*
* @module webidlParser
*/
import * as WebIDL2 from 'webidl2';
import { fileURLToPath } from 'node:url';
import process from 'node:process';
/**
* Update obsolete WebIDL constructs to make them compatible with latest
* version of WebIDL
*/
function normalizeWebIDL1to2(idl) {
return idl
// Use "FrozenArray" instead of "[]"
.replace(/attribute +([^\[ ]*)\[\]/g, "attribute FrozenArray<$1>")
// Use the default toJSON operation instead of serializers
.replace(/serializer\s*=\s*{[^}]*}/g, "[Default] object toJSON()");
}
/**
* Checks whether the given IDL uses WebIDL Level 1 constructs that are no
* longer valid in WebIDL Level 2.
*
* Note a smarter method would typically return the list of obsolete constructs
* instead of just a boolean flag. To be considered...
*
* @function
* @public
* @param {String} idl IDL content to check
* @return {boolean} True when the IDL string contains obsolete constructs,
* false otherwise.
*/
function hasObsoleteIdl(idl) {
return (idl !== normalizeWebIDL1to2(idl));
}
/**
* Serialize an IDL AST node to a JSON object that contains both a textual
* serialization of the IDL fragment and a tree serialization of the AST node
*
* @function
* @private
* @param {Object} def The parsed IDL node returned by the webidl2.js parser
* @return {Object} An object with a "value" and "parsedValue" properties
*/
function serialize(def) {
return Object.assign(
{ fragment: WebIDL2.write([def]).trim() },
JSON.parse(JSON.stringify(def)));
}
/**
* Main method that takes IDL definitions and parses that IDL to compute
* the list of internal/external dependencies.
*
* @function
* @public
* @param {String} idl IDL content, typically returned by the WebIDL extractor
* @return {Promise} The promise to a parsed IDL structure that includes
* information about dependencies, both at the interface level and at the
* global level.
*/
function parse(idl) {
idl = normalizeWebIDL1to2(idl);
return new Promise(function (resolve, reject) {
var idlTree;
var idlReport = {
// List of names available to global interfaces, either as
// objects that can be constructed or as objects that can be
// returned from functions
jsNames: {
constructors: {},
functions: {}
},
// List of IDL names defined in the IDL content, indexed by name
idlNames: {},
// List of partial IDL name definitions, indexed by name
idlExtendedNames: {},
// List of globals defined by the IDL content and the name of the
// underlying interfaces (e.g. { "Worker":
// ["DedicatedWorkerGlobalScope", "SharedWorkerGlobalScope"] })
globals: {},
// List of globals on which interfaces are defined, along with the
// name of the underlying interfaces (e.g. { "Window":
// ["ServiceWorker", "ServiceWorkerRegistration", ...]})
exposed: {},
// List of dependencies (both internal and external) per interface
dependencies: {},
// IDL names referenced by the IDL content but defined elsewhere
externalDependencies: []
};
try {
idlTree = WebIDL2.parse(idl);
} catch (e) {
return reject(e);
}
idlTree.forEach(parseIdlAstTree(idlReport));
idlReport.externalDependencies = idlReport.externalDependencies
.filter(n => !idlReport.idlNames[n]);
resolve(idlReport);
});
}
/**
* Function that generates a parsing method that may be applied to nodes of
* the IDL AST tree generated by the webidl2 and that completes the tree
* structure with dependencies information.
*
* Note that the method recursively calls itself when it parses interfaces or
* dictionaries.
*
* @function
* @private
* @param {Object} idlReport The IDL report to fill out, see structure
* definition in parse function
* @param {String} contextName The current interface context, used to compute
* dependencies at the interface level
* @return A function that can be applied to all nodes of an IDL AST tree and
* that fills up the above sets.
*/
function parseIdlAstTree(idlReport, contextName) {
const { idlNames, idlExtendedNames, dependencies, externalDependencies } = idlReport;
return function (def) {
switch(def.type) {
case "namespace":
case "interface":
case "interface mixin":
case "dictionary":
case "callback interface":
parseInterfaceOrDictionary(def, idlReport);
break;
case "enum":
idlNames[def.name] = serialize(def);
break;
case "operation":
if (def.stringifier || (def.special && def.special === 'stringifier')) return;
parseType(def.idlType, idlReport, contextName);
def.arguments.forEach(a => parseType(a.idlType, idlReport, contextName));
break;
case "attribute":
case "field":
parseType(def.idlType, idlReport, contextName);
break;
case 'constructor':
def.arguments.forEach(a => parseType(a.idlType, idlReport, contextName));
break;
case "includes":
case "implements":
parseType(def.target, idlReport);
parseType(def[def.type], idlReport);
if (!dependencies[def.target]) {
dependencies[def.target] = [];
}
addDependency(def[def.type], {}, dependencies[def.target]);
if (!idlExtendedNames[def.target]) {
idlExtendedNames[def.target] = [];
}
const mixin = {name: def.target, type: "interface", includes: def.includes};
idlExtendedNames[def.target].push(serialize(def));
break;
case "typedef":
parseType(def.idlType, idlReport);
idlNames[def.name] = serialize(def);
break;
case "callback":
idlNames[def.name] = serialize(def);
def.arguments.forEach(a => parseType(a.idlType, idlReport));
break;
case "iterable":
case "setlike":
case "maplike":
var type = def.idlType;
if (!Array.isArray(type)) {
type = [def.idlType];
}
type.forEach(a => parseType(a, idlReport, contextName));
break;
case "serializer":
case "stringifier":
case "const":
case "eof":
break;
default:
throw new Error("Unhandled IDL type: " + def.type + " in " +JSON.stringify(def));
}
};
}
/**
* Parse an IDL AST node that defines an interface or dictionary, and compute
* dependencies information.
*
* @function
* @private
* @param {Object} def The IDL AST node to parse
* @see parseIdlAstTree for other parameters
* @return {void} The function updates the contents of its parameters and does
* not return anything
*/
function parseInterfaceOrDictionary(def, idlReport) {
const { idlNames, idlExtendedNames, globals, exposed, jsNames, dependencies, externalDependencies } = idlReport;
if (!dependencies[def.name]) {
dependencies[def.name] = [];
}
if (def.partial) {
if (!idlExtendedNames[def.name]) {
idlExtendedNames[def.name] = [];
}
idlExtendedNames[def.name].push(serialize(def));
addDependency(def.name, idlNames, externalDependencies);
}
else {
idlNames[def.name] = serialize(def);
}
if (def.inheritance) {
addDependency(def.inheritance, idlNames, externalDependencies);
addDependency(def.inheritance, {}, dependencies[def.name]);
}
const globalEA = def.extAttrs.find(ea => ea.name === "Global");
if (globalEA && globalEA.rhs) {
const globalNames = (globalEA.rhs.type === "identifier") ?
[globalEA.rhs.value] : globalEA.rhs.value.map(c => c.value);
globalNames.forEach(name => {
if (!globals[name]) {
globals[name] = [];
}
globals[name].push(def.name);
});
}
const exposedEA = def.extAttrs.find(ea => ea.name === "Exposed");
if (exposedEA && exposedEA.rhs) {
let exposedNames = [];
if (exposedEA.rhs.type === "*") {
exposedNames.push("*");
} else if (exposedEA.rhs.type === "identifier") {
exposedNames.push(exposedEA.rhs.value);
} else {
exposedNames = exposedEA.rhs.value.map(c => c.value);
}
exposedNames.forEach(name => {
if (!exposed[name]) {
exposed[name] = [];
}
exposed[name].push(def.name);
});
}
if (def.extAttrs.some(ea => ea.name === "Constructor")) {
addToJSContext(def.extAttrs, jsNames, def.name, "constructors");
def.extAttrs.filter(ea => ea.name === "Constructor").forEach(function(constructor) {
if (constructor.arguments) {
constructor.arguments.forEach(a => parseType(a.idlType, idlReport, def.name));
}
});
} else if (def.extAttrs.some(ea => ea.name === "NamedConstructor")) {
def.extAttrs.filter(ea => ea.name === "NamedConstructor").forEach(function(constructor) {
idlNames[constructor.rhs.value] = constructor;
addToJSContext(def.extAttrs, jsNames, def.name, "constructors");
if (constructor.arguments) {
constructor.arguments.forEach(a => parseType(a.idlType, idlReport, def.name));
}
});
} else if (def.members.find(member => member.type === 'constructor')) {
addToJSContext(def.extAttrs, jsNames, def.name, "constructors");
} else if (def.type === "interface") {
addToJSContext(def.extAttrs, jsNames, def.name, "functions");
}
def.members.forEach(parseIdlAstTree(idlReport, def.name));
}
/**
* Add the given IDL name to the set of objects that are exposed to JS, for the
* right contexts.
*
* @function
* @private
* @param {Object} eas The extended attributes that may qualify that IDL name
* @param {Object} jsNames See parseIdlAstTree params
* @param {String} name The IDL name to add to the jsNames set
* @param {String} type The type of exposure (constructor, function, object)
* @return {void} The function updates jsNames
*/
function addToJSContext(eas, jsNames, name, type) {
var contexts = [];
var exposed = eas && eas.some(ea => ea.name === "Exposed");
if (exposed) {
var exposedEa = eas.find(ea => ea.name === "Exposed");
if (exposedEa.rhs.type === "*") {
contexts = ["*"];
} else if (exposedEa.rhs.type === "identifier") {
contexts = [exposedEa.rhs.value];
} else {
contexts = exposedEa.rhs.value.map(c => c.value);
}
}
contexts.forEach(c => { if (!jsNames[type][c]) jsNames[type][c] = []; jsNames[type][c].push(name)});
}
/**
* Parse the given IDL type and update external dependencies accordingly
*
* @function
* @private
* @param {Object} idltype The IDL AST node that defines/references the type
* @see parseIdlAstTree for other parameters
* @return {void} The function updates externalDependencies
*/
function parseType(idltype, idlReport, contextName) {
// For some reasons, webidl2 sometimes returns the name of the IDL type
// instead of an IDL construct for array constructs. For example:
// Constructor(DOMString[] urls) interface toto;
// ... will create an array IDL node that directly point to "DOMString" and
// not to a node that describes the "DOMString" type.
if (isString(idltype)) {
idltype = { idlType: 'DOMString' };
}
if (idltype.union || (idltype.generic && Array.isArray(idltype.idlType))) {
idltype.idlType.forEach(t => parseType(t, idlReport, contextName));
return;
}
if (idltype.sequence || idltype.array || idltype.generic) {
parseType(idltype.idlType, idlReport, contextName);
return;
}
var wellKnownTypes = [
"undefined", "any",
"boolean",
"byte", "octet",
"short", "unsigned short",
"long", "unsigned long", "long long", "unsigned long long",
"float", "unrestricted float", "double", "unrestricted double",
"bigint",
"DOMString", "ByteString", "USVString",
"object"
];
if (wellKnownTypes.indexOf(idltype.idlType) === -1) {
addDependency(idltype.idlType, idlReport.idlNames, idlReport.externalDependencies);
if (contextName) {
addDependency(idltype.idlType, {}, idlReport.dependencies[contextName]);
}
}
}
/**
* Returns true if given object is a String
*
* @function
* @private
* @param {any} obj The object to test
* @return {bool} true is object is a String, false otherwise
*/
function isString(obj) {
return Object.prototype.toString.call(obj) === '[object String]';
}
/**
* Add the given name to the list of external dependencies, unless it already
* appears in the set of IDL names defined by the IDL content
*
* @function
* @private
* @param {String} name The IDL name to consider as a potential external dependency
* @param {Array(String)} idlNames The set of IDL names set by the IDL content
* @param {Array(String)} externalDependencies The set of external dependencies
* @return {void} The function updates externalDependencies as needed
*/
function addDependency(name, idlNames, externalDependencies) {
if ((Object.keys(idlNames).indexOf(name) === -1) &&
(externalDependencies.indexOf(name) === -1)) {
externalDependencies.push(name);
}
}
/**************************************************
Export the Web IDL parser
**************************************************/
const webidlParser = {
parse,
hasObsoleteIdl
};
export {
webidlParser as default,
parse,
hasObsoleteIdl
};
/**************************************************
Code run if the code is run as a stand-alone module
**************************************************/
if (process.argv[1] === fileURLToPath(import.meta.url)) {
const fs = await import('node:fs');
const idlFile = process.argv[2];
if (!idlFile) {
console.error("No IDL file to parse");
process.exit(2);
}
const idl = fs.readFileSync(idlFile, "utf8");
parse(idl)
.then(function (data) {
console.log(JSON.stringify(data, null, 2));
})
.catch(function (err) {
console.error(err, err.stack);
process.exit(64);
});
}