@idlizer/core 2.0.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (78) hide show
  1. package/build/lib/src/IndentedPrinter.d.ts +28 -0
  2. package/build/lib/src/IndentedPrinter.js +81 -0
  3. package/build/lib/src/Language.d.ts +15 -0
  4. package/build/lib/src/Language.js +42 -0
  5. package/build/lib/src/LanguageWriters/ArgConvertors.d.ts +318 -0
  6. package/build/lib/src/LanguageWriters/ArgConvertors.js +1015 -0
  7. package/build/lib/src/LanguageWriters/InteropConvertor.d.ts +36 -0
  8. package/build/lib/src/LanguageWriters/InteropConvertor.js +174 -0
  9. package/build/lib/src/LanguageWriters/LanguageWriter.d.ts +386 -0
  10. package/build/lib/src/LanguageWriters/LanguageWriter.js +746 -0
  11. package/build/lib/src/LanguageWriters/common.d.ts +17 -0
  12. package/build/lib/src/LanguageWriters/common.js +33 -0
  13. package/build/lib/src/LanguageWriters/convertors/CppConvertors.d.ts +8 -0
  14. package/build/lib/src/LanguageWriters/convertors/CppConvertors.js +35 -0
  15. package/build/lib/src/LanguageWriters/index.d.ts +3 -0
  16. package/build/lib/src/LanguageWriters/index.js +3 -0
  17. package/build/lib/src/LanguageWriters/nameConvertor.d.ts +28 -0
  18. package/build/lib/src/LanguageWriters/nameConvertor.js +61 -0
  19. package/build/lib/src/LanguageWriters/writers/CJLanguageWriter.d.ts +147 -0
  20. package/build/lib/src/LanguageWriters/writers/CJLanguageWriter.js +578 -0
  21. package/build/lib/src/LanguageWriters/writers/CLikeLanguageWriter.d.ts +46 -0
  22. package/build/lib/src/LanguageWriters/writers/CLikeLanguageWriter.js +121 -0
  23. package/build/lib/src/LanguageWriters/writers/CppLanguageWriter.d.ts +116 -0
  24. package/build/lib/src/LanguageWriters/writers/CppLanguageWriter.js +472 -0
  25. package/build/lib/src/LanguageWriters/writers/ETSLanguageWriter.d.ts +72 -0
  26. package/build/lib/src/LanguageWriters/writers/ETSLanguageWriter.js +329 -0
  27. package/build/lib/src/LanguageWriters/writers/JavaLanguageWriter.d.ts +83 -0
  28. package/build/lib/src/LanguageWriters/writers/JavaLanguageWriter.js +275 -0
  29. package/build/lib/src/LanguageWriters/writers/TsLanguageWriter.d.ts +93 -0
  30. package/build/lib/src/LanguageWriters/writers/TsLanguageWriter.js +390 -0
  31. package/build/lib/src/LibraryInterface.d.ts +21 -0
  32. package/build/lib/src/LibraryInterface.js +2 -0
  33. package/build/lib/src/config.d.ts +8 -0
  34. package/build/lib/src/config.js +20 -0
  35. package/build/lib/src/from-idl/DtsPrinter.d.ts +35 -0
  36. package/build/lib/src/from-idl/DtsPrinter.js +375 -0
  37. package/build/lib/src/from-idl/common.d.ts +5 -0
  38. package/build/lib/src/from-idl/common.js +83 -0
  39. package/build/lib/src/from-idl/deserialize.d.ts +9 -0
  40. package/build/lib/src/from-idl/deserialize.js +384 -0
  41. package/build/lib/src/from-idl/toString.d.ts +2 -0
  42. package/build/lib/src/from-idl/toString.js +18 -0
  43. package/build/lib/src/from-idl/webidl2-utils.d.ts +20 -0
  44. package/build/lib/src/from-idl/webidl2-utils.js +84 -0
  45. package/build/lib/src/idl.d.ts +404 -0
  46. package/build/lib/src/idl.js +1015 -0
  47. package/build/lib/src/idlize.d.ts +4 -0
  48. package/build/lib/src/idlize.js +52 -0
  49. package/build/lib/src/index.d.ts +34 -0
  50. package/build/lib/src/index.js +48 -0
  51. package/build/lib/src/inheritance.d.ts +16 -0
  52. package/build/lib/src/inheritance.js +76 -0
  53. package/build/lib/src/languageSpecificKeywords.d.ts +5 -0
  54. package/build/lib/src/languageSpecificKeywords.js +51 -0
  55. package/build/lib/src/library.d.ts +67 -0
  56. package/build/lib/src/library.js +190 -0
  57. package/build/lib/src/options.d.ts +11 -0
  58. package/build/lib/src/options.js +16 -0
  59. package/build/lib/src/peer-generation/PrimitiveType.d.ts +24 -0
  60. package/build/lib/src/peer-generation/PrimitiveType.js +52 -0
  61. package/build/lib/src/peer-generation/ReferenceResolver.d.ts +9 -0
  62. package/build/lib/src/peer-generation/ReferenceResolver.js +37 -0
  63. package/build/lib/src/peer-generation/idl/IdlNameConvertor.d.ts +28 -0
  64. package/build/lib/src/peer-generation/idl/IdlNameConvertor.js +82 -0
  65. package/build/lib/src/peer-generation/idl/common.d.ts +9 -0
  66. package/build/lib/src/peer-generation/idl/common.js +70 -0
  67. package/build/lib/src/peer-generation/unions.d.ts +29 -0
  68. package/build/lib/src/peer-generation/unions.js +111 -0
  69. package/build/lib/src/rand_utils.d.ts +6 -0
  70. package/build/lib/src/rand_utils.js +100 -0
  71. package/build/lib/src/util.d.ts +75 -0
  72. package/build/lib/src/util.js +622 -0
  73. package/package.json +49 -0
  74. package/webidl2.js/LICENSE +21 -0
  75. package/webidl2.js/README.md +827 -0
  76. package/webidl2.js/dist/package.json +3 -0
  77. package/webidl2.js/dist/webidl2.js +4547 -0
  78. package/webidl2.js/package.json +55 -0
@@ -0,0 +1,1015 @@
1
+ /*
2
+ * Copyright (c) 2024 Huawei Device Co., Ltd.
3
+ * Licensed under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License.
5
+ * You may obtain a copy of the License at
6
+ *
7
+ * http://www.apache.org/licenses/LICENSE-2.0
8
+ *
9
+ * Unless required by applicable law or agreed to in writing, software
10
+ * distributed under the License is distributed on an "AS IS" BASIS,
11
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ * See the License for the specific language governing permissions and
13
+ * limitations under the License.
14
+ */
15
+ import * as webidl2 from "webidl2";
16
+ import { indentedBy, isDefined } from "./util";
17
+ import { generateSyntheticIdlNodeName } from "./peer-generation/idl/common";
18
+ import { IDLKeywords } from "./languageSpecificKeywords";
19
+ export var IDLKind;
20
+ (function (IDLKind) {
21
+ IDLKind[IDLKind["Interface"] = 0] = "Interface";
22
+ IDLKind[IDLKind["Package"] = 1] = "Package";
23
+ IDLKind[IDLKind["Import"] = 2] = "Import";
24
+ IDLKind[IDLKind["Callback"] = 3] = "Callback";
25
+ IDLKind[IDLKind["Const"] = 4] = "Const";
26
+ IDLKind[IDLKind["Property"] = 5] = "Property";
27
+ IDLKind[IDLKind["Parameter"] = 6] = "Parameter";
28
+ IDLKind[IDLKind["Method"] = 7] = "Method";
29
+ IDLKind[IDLKind["Callable"] = 8] = "Callable";
30
+ IDLKind[IDLKind["Constructor"] = 9] = "Constructor";
31
+ IDLKind[IDLKind["Enum"] = 10] = "Enum";
32
+ IDLKind[IDLKind["EnumMember"] = 11] = "EnumMember";
33
+ IDLKind[IDLKind["Typedef"] = 12] = "Typedef";
34
+ IDLKind[IDLKind["PrimitiveType"] = 13] = "PrimitiveType";
35
+ IDLKind[IDLKind["ContainerType"] = 14] = "ContainerType";
36
+ IDLKind[IDLKind["UnspecifiedGenericType"] = 15] = "UnspecifiedGenericType";
37
+ IDLKind[IDLKind["ReferenceType"] = 16] = "ReferenceType";
38
+ IDLKind[IDLKind["UnionType"] = 17] = "UnionType";
39
+ IDLKind[IDLKind["TypeParameterType"] = 18] = "TypeParameterType";
40
+ IDLKind[IDLKind["OptionalType"] = 19] = "OptionalType";
41
+ IDLKind[IDLKind["Version"] = 20] = "Version";
42
+ IDLKind[IDLKind["Namespace"] = 21] = "Namespace";
43
+ })(IDLKind || (IDLKind = {}));
44
+ export var IDLEntity;
45
+ (function (IDLEntity) {
46
+ IDLEntity["Class"] = "Class";
47
+ IDLEntity["Interface"] = "Interface";
48
+ IDLEntity["Package"] = "Package";
49
+ IDLEntity["Import"] = "Import";
50
+ IDLEntity["Intersection"] = "Intersection";
51
+ IDLEntity["Literal"] = "Literal";
52
+ IDLEntity["NamedTuple"] = "NamedTuple";
53
+ IDLEntity["Tuple"] = "Tuple";
54
+ })(IDLEntity || (IDLEntity = {}));
55
+ export var IDLExtendedAttributes;
56
+ (function (IDLExtendedAttributes) {
57
+ IDLExtendedAttributes["Accessor"] = "Accessor";
58
+ IDLExtendedAttributes["Async"] = "Async";
59
+ IDLExtendedAttributes["CallSignature"] = "CallSignature";
60
+ IDLExtendedAttributes["CJType"] = "CJType";
61
+ IDLExtendedAttributes["CommonMethod"] = "CommonMethod";
62
+ IDLExtendedAttributes["Component"] = "Component";
63
+ IDLExtendedAttributes["ComponentInterface"] = "ComponentInterface";
64
+ IDLExtendedAttributes["CPPType"] = "CPPType";
65
+ IDLExtendedAttributes["Deprecated"] = "Deprecated";
66
+ IDLExtendedAttributes["Documentation"] = "Documentation";
67
+ IDLExtendedAttributes["DtsName"] = "DtsName";
68
+ IDLExtendedAttributes["DtsTag"] = "DtsTag";
69
+ IDLExtendedAttributes["Entity"] = "Entity";
70
+ IDLExtendedAttributes["GlobalScope"] = "GlobalScope";
71
+ IDLExtendedAttributes["Import"] = "Import";
72
+ IDLExtendedAttributes["IndexSignature"] = "IndexSignature";
73
+ IDLExtendedAttributes["Interfaces"] = "Interfaces";
74
+ IDLExtendedAttributes["NativeModule"] = "NativeModule";
75
+ IDLExtendedAttributes["Optional"] = "Optional";
76
+ IDLExtendedAttributes["OriginalEnumMemberName"] = "OriginalEnumMemberName";
77
+ IDLExtendedAttributes["Protected"] = "Protected";
78
+ IDLExtendedAttributes["Synthetic"] = "Synthetic";
79
+ IDLExtendedAttributes["TSType"] = "TSType";
80
+ IDLExtendedAttributes["TypeArguments"] = "TypeArguments";
81
+ IDLExtendedAttributes["TypeParameters"] = "TypeParameters";
82
+ IDLExtendedAttributes["VerbatimDts"] = "VerbatimDts";
83
+ IDLExtendedAttributes["HandWrittenImplementation"] = "HandWrittenImplementation";
84
+ IDLExtendedAttributes["Predefined"] = "Predefined";
85
+ })(IDLExtendedAttributes || (IDLExtendedAttributes = {}));
86
+ export var IDLAccessorAttribute;
87
+ (function (IDLAccessorAttribute) {
88
+ IDLAccessorAttribute["Getter"] = "Getter";
89
+ IDLAccessorAttribute["Setter"] = "Setter";
90
+ })(IDLAccessorAttribute || (IDLAccessorAttribute = {}));
91
+ const innerIdlSymbol = Symbol("innerIdlSymbol");
92
+ export var IDLInterfaceSubkind;
93
+ (function (IDLInterfaceSubkind) {
94
+ IDLInterfaceSubkind[IDLInterfaceSubkind["Interface"] = 0] = "Interface";
95
+ IDLInterfaceSubkind[IDLInterfaceSubkind["Class"] = 1] = "Class";
96
+ IDLInterfaceSubkind[IDLInterfaceSubkind["AnonymousInterface"] = 2] = "AnonymousInterface";
97
+ IDLInterfaceSubkind[IDLInterfaceSubkind["Tuple"] = 3] = "Tuple";
98
+ })(IDLInterfaceSubkind || (IDLInterfaceSubkind = {}));
99
+ export function forEachChild(node, cbEnter, cbLeave) {
100
+ var _a, _b, _c;
101
+ cbEnter(node);
102
+ switch (node.kind) {
103
+ case IDLKind.Namespace:
104
+ node.members.forEach((value) => forEachChild(value, cbEnter, cbLeave));
105
+ break;
106
+ case IDLKind.Interface: {
107
+ let concrete = node;
108
+ concrete.inheritance.forEach((value) => forEachChild(value, cbEnter, cbLeave));
109
+ concrete.constructors.forEach((value) => forEachChild(value, cbEnter, cbLeave));
110
+ concrete.properties.forEach((value) => forEachChild(value, cbEnter, cbLeave));
111
+ concrete.methods.forEach((value) => forEachChild(value, cbEnter, cbLeave));
112
+ concrete.callables.forEach((value) => forEachChild(value, cbEnter, cbLeave));
113
+ (_a = concrete.scope) === null || _a === void 0 ? void 0 : _a.forEach((value) => forEachChild(value, cbEnter, cbLeave));
114
+ break;
115
+ }
116
+ case IDLKind.Method:
117
+ case IDLKind.Callable:
118
+ case IDLKind.Callback:
119
+ case IDLKind.Constructor: {
120
+ let concrete = node;
121
+ (_b = concrete.parameters) === null || _b === void 0 ? void 0 : _b.forEach((value) => forEachChild(value, cbEnter, cbLeave));
122
+ if (concrete.returnType)
123
+ forEachChild(concrete.returnType, cbEnter, cbLeave);
124
+ break;
125
+ }
126
+ case IDLKind.UnionType: {
127
+ let concrete = node;
128
+ (_c = concrete.types) === null || _c === void 0 ? void 0 : _c.forEach((value) => forEachChild(value, cbEnter, cbLeave));
129
+ break;
130
+ }
131
+ case IDLKind.OptionalType: {
132
+ let concrete = node;
133
+ forEachChild(concrete.type, cbEnter, cbLeave);
134
+ break;
135
+ }
136
+ case IDLKind.Const: {
137
+ forEachChild(node.type, cbEnter, cbLeave);
138
+ break;
139
+ }
140
+ case IDLKind.Enum: {
141
+ node.elements.forEach((value) => forEachChild(value, cbEnter, cbLeave));
142
+ break;
143
+ }
144
+ case IDLKind.Property: {
145
+ forEachChild(node.type, cbEnter, cbLeave);
146
+ break;
147
+ }
148
+ case IDLKind.Parameter: {
149
+ const concrete = node;
150
+ if (concrete.type)
151
+ forEachChild(concrete.type, cbEnter, cbLeave);
152
+ break;
153
+ }
154
+ case IDLKind.Typedef: {
155
+ forEachChild(node.type, cbEnter, cbLeave);
156
+ break;
157
+ }
158
+ case IDLKind.ContainerType: {
159
+ node.elementType.forEach((value) => forEachChild(value, cbEnter, cbLeave));
160
+ break;
161
+ }
162
+ case IDLKind.UnspecifiedGenericType: {
163
+ node.typeArguments.forEach((value) => forEachChild(value, cbEnter, cbLeave));
164
+ break;
165
+ }
166
+ case IDLKind.ReferenceType:
167
+ case IDLKind.TypeParameterType:
168
+ case IDLKind.EnumMember:
169
+ case IDLKind.Package:
170
+ case IDLKind.Import:
171
+ case IDLKind.PrimitiveType:
172
+ case IDLKind.Version:
173
+ break;
174
+ default: {
175
+ throw new Error(`Unhandled ${node.kind}`);
176
+ }
177
+ }
178
+ if (cbLeave)
179
+ cbLeave(node);
180
+ }
181
+ export function isNamedNode(type) {
182
+ return "_idlNamedNodeBrand" in type;
183
+ }
184
+ export function forceAsNamedNode(type) {
185
+ if (!isNamedNode(type)) {
186
+ throw new Error(`Expected to be an IDLNamedNode, but got '${IDLKind[type.kind]}'`);
187
+ }
188
+ return type;
189
+ }
190
+ export function isUndefinedType(type) {
191
+ return isPrimitiveType(type) && type.name === IDLUndefinedType.name;
192
+ }
193
+ export function isVoidType(type) {
194
+ return isPrimitiveType(type) && type.name === IDLVoidType.name;
195
+ }
196
+ export function isPrimitiveType(type) {
197
+ return type.kind == IDLKind.PrimitiveType;
198
+ }
199
+ export function isContainerType(type) {
200
+ return type.kind == IDLKind.ContainerType;
201
+ }
202
+ export function isReferenceType(type) {
203
+ return type.kind == IDLKind.ReferenceType;
204
+ }
205
+ export function isUnspecifiedGenericType(type) {
206
+ return type.kind == IDLKind.UnspecifiedGenericType;
207
+ }
208
+ export function isEnum(type) {
209
+ return type.kind == IDLKind.Enum;
210
+ }
211
+ export function isEnumMember(type) {
212
+ return type.kind == IDLKind.EnumMember;
213
+ }
214
+ export function isUnionType(type) {
215
+ return type.kind == IDLKind.UnionType;
216
+ }
217
+ export function isTypeParameterType(type) {
218
+ return type.kind == IDLKind.TypeParameterType;
219
+ }
220
+ export function isInterface(node) {
221
+ return node.kind === IDLKind.Interface;
222
+ }
223
+ export function isPackage(type) {
224
+ return type.kind == IDLKind.Package;
225
+ }
226
+ export function isImport(type) {
227
+ return type.kind == IDLKind.Import;
228
+ }
229
+ export function isCallable(node) {
230
+ return node.kind === IDLKind.Callable;
231
+ }
232
+ export function isMethod(node) {
233
+ return node.kind === IDLKind.Method;
234
+ }
235
+ export function isParameter(node) {
236
+ return node.kind === IDLKind.Parameter;
237
+ }
238
+ export function isConstructor(node) {
239
+ return node.kind === IDLKind.Constructor;
240
+ }
241
+ export function isProperty(node) {
242
+ return node.kind === IDLKind.Property;
243
+ }
244
+ export function isCallback(node) {
245
+ return node.kind === IDLKind.Callback;
246
+ }
247
+ export function isInterfaceSubkind(idl) {
248
+ return idl.subkind === IDLInterfaceSubkind.Interface;
249
+ }
250
+ export function isClassSubkind(idl) {
251
+ return idl.subkind === IDLInterfaceSubkind.Class;
252
+ }
253
+ export function isConstant(node) {
254
+ return node.kind === IDLKind.Const;
255
+ }
256
+ export function isTypedef(node) {
257
+ return node.kind === IDLKind.Typedef;
258
+ }
259
+ export function isType(node) {
260
+ return "_idlTypeBrand" in node;
261
+ }
262
+ export function isEntry(node) {
263
+ return "_idlEntryBrand" in node;
264
+ }
265
+ export function isNamespace(node) {
266
+ return node.kind === IDLKind.Namespace;
267
+ }
268
+ export function isSyntheticEntry(node) {
269
+ var _a;
270
+ return isDefined((_a = node.extendedAttributes) === null || _a === void 0 ? void 0 : _a.find(it => it.name === IDLExtendedAttributes.Synthetic));
271
+ }
272
+ export function isOptionalType(type) {
273
+ return type.kind === IDLKind.OptionalType;
274
+ }
275
+ export function isVersion(node) {
276
+ return node.kind === IDLKind.Version;
277
+ }
278
+ function createPrimitiveType(name) {
279
+ return {
280
+ kind: IDLKind.PrimitiveType,
281
+ name: name,
282
+ _idlNodeBrand: innerIdlSymbol,
283
+ _idlTypeBrand: innerIdlSymbol,
284
+ _idlNamedNodeBrand: innerIdlSymbol,
285
+ };
286
+ }
287
+ export function createOptionalType(element) {
288
+ if (isOptionalType(element)) {
289
+ return element;
290
+ }
291
+ return {
292
+ kind: IDLKind.OptionalType,
293
+ type: element,
294
+ _idlNodeBrand: innerIdlSymbol,
295
+ _idlTypeBrand: innerIdlSymbol,
296
+ };
297
+ }
298
+ /**
299
+ * This placeholder is used when a class has no superclass.
300
+ * Examples:
301
+ * class definition: inheritance:
302
+ * `C extends T` : [T]
303
+ * `C implements T` : [Top, T]
304
+ * `C extends T implements I, J` : [T, I, J]
305
+ */
306
+ export const IDLTopType = createReferenceType("__TOP__");
307
+ export const IDLPointerType = createPrimitiveType('pointer');
308
+ export const IDLVoidType = createPrimitiveType('void');
309
+ export const IDLBooleanType = createPrimitiveType('boolean');
310
+ export const IDLI8Type = createPrimitiveType('i8');
311
+ export const IDLU8Type = createPrimitiveType('u8');
312
+ export const IDLI16Type = createPrimitiveType('i16');
313
+ export const IDLU16Type = createPrimitiveType('u16');
314
+ export const IDLI32Type = createPrimitiveType('i32');
315
+ export const IDLU32Type = createPrimitiveType('u32');
316
+ export const IDLI64Type = createPrimitiveType('i64');
317
+ export const IDLU64Type = createPrimitiveType('u64');
318
+ export const IDLF16Type = createPrimitiveType('f16');
319
+ export const IDLF32Type = createPrimitiveType('f32');
320
+ export const IDLF64Type = createPrimitiveType('f64');
321
+ export const IDLBigintType = createPrimitiveType("bigint");
322
+ export const IDLNumberType = createPrimitiveType('number');
323
+ export const IDLStringType = createPrimitiveType('String');
324
+ export const IDLAnyType = createPrimitiveType('any');
325
+ export const IDLUndefinedType = createPrimitiveType('undefined');
326
+ export const IDLUnknownType = createPrimitiveType('unknown');
327
+ export const IDLObjectType = createReferenceType('Object');
328
+ export const IDLThisType = createPrimitiveType('this');
329
+ export const IDLDate = createPrimitiveType('date');
330
+ export const IDLBufferType = createPrimitiveType('buffer');
331
+ export const IDLUint8ArrayType = createContainerType('sequence', [IDLU8Type]);
332
+ // Stub for IdlPeerLibrary
333
+ export const IDLFunctionType = createPrimitiveType('Function');
334
+ export const IDLLengthType = createPrimitiveType('Length');
335
+ export const IDLCustomObjectType = createPrimitiveType('CustomObject');
336
+ export function createNamespace(name, extendedAttributes, fileName) {
337
+ return {
338
+ kind: IDLKind.Namespace,
339
+ members: [],
340
+ name: name,
341
+ extendedAttributes,
342
+ fileName,
343
+ _idlNodeBrand: innerIdlSymbol,
344
+ _idlEntryBrand: innerIdlSymbol,
345
+ _idlNamedNodeBrand: innerIdlSymbol,
346
+ };
347
+ }
348
+ export function linkNamespacesBack(node) {
349
+ let namespacePath = [];
350
+ forEachChild(node, child => {
351
+ if (isEntry(child) || isReferenceType(child)) {
352
+ if (!child.namespace)
353
+ child.namespace = namespacePath.length ? namespacePath[namespacePath.length - 1] : undefined;
354
+ }
355
+ if (isNamespace(child))
356
+ namespacePath.push(child);
357
+ }, child => {
358
+ if (isNamespace(child))
359
+ namespacePath.pop();
360
+ });
361
+ }
362
+ export function getNamespacesPathFor(entry) {
363
+ let iterator = entry.namespace;
364
+ const result = [];
365
+ while (iterator) {
366
+ result.unshift(iterator);
367
+ iterator = iterator.namespace;
368
+ }
369
+ return result;
370
+ }
371
+ export function isEqualByQualifedName(a, b) {
372
+ if (a === b)
373
+ return true;
374
+ if (!a || !b)
375
+ return false;
376
+ if (a.kind !== b.kind || a.name !== b.name)
377
+ return false;
378
+ return isEqualByQualifedName(a.namespace, b.namespace);
379
+ }
380
+ export function createVersion(value, extendedAttributes, fileName) {
381
+ return {
382
+ kind: IDLKind.Version,
383
+ value,
384
+ name: "version",
385
+ extendedAttributes,
386
+ fileName,
387
+ _idlNodeBrand: innerIdlSymbol,
388
+ _idlEntryBrand: innerIdlSymbol,
389
+ _idlNamedNodeBrand: innerIdlSymbol,
390
+ };
391
+ }
392
+ export function fetchNamespaceFrom(pointOfView) {
393
+ if (pointOfView) {
394
+ if (isNamespace(pointOfView))
395
+ return pointOfView;
396
+ if (isEntry(pointOfView) || isReferenceType(pointOfView))
397
+ return pointOfView.namespace;
398
+ }
399
+ return undefined;
400
+ }
401
+ export function createReferenceType(name, typeArguments, pointOfView) {
402
+ return {
403
+ kind: IDLKind.ReferenceType,
404
+ name,
405
+ typeArguments,
406
+ namespace: fetchNamespaceFrom(pointOfView),
407
+ _idlNodeBrand: innerIdlSymbol,
408
+ _idlTypeBrand: innerIdlSymbol,
409
+ _idlNamedNodeBrand: innerIdlSymbol,
410
+ };
411
+ }
412
+ export function createUnspecifiedGenericType(name, typeArguments) {
413
+ return {
414
+ kind: IDLKind.UnspecifiedGenericType,
415
+ name,
416
+ typeArguments,
417
+ _idlNodeBrand: innerIdlSymbol,
418
+ _idlTypeBrand: innerIdlSymbol,
419
+ _idlNamedNodeBrand: innerIdlSymbol,
420
+ };
421
+ }
422
+ export function entityToType(entity) {
423
+ if (isType(entity)) {
424
+ return entity;
425
+ }
426
+ return createReferenceType(forceAsNamedNode(entity).name, undefined, entity);
427
+ }
428
+ export function createContainerType(container, element) {
429
+ return {
430
+ kind: IDLKind.ContainerType,
431
+ containerKind: container,
432
+ elementType: element,
433
+ _idlNodeBrand: innerIdlSymbol,
434
+ _idlTypeBrand: innerIdlSymbol,
435
+ };
436
+ }
437
+ export function createUnionType(types, name) {
438
+ if (types.length < 2)
439
+ throw new Error("IDLUnionType should contain at least 2 types");
440
+ return {
441
+ kind: IDLKind.UnionType,
442
+ name: name !== null && name !== void 0 ? name : "Union_" + types.map(it => generateSyntheticIdlNodeName(it)).join("_"),
443
+ types: types,
444
+ _idlNodeBrand: innerIdlSymbol,
445
+ _idlTypeBrand: innerIdlSymbol,
446
+ _idlNamedNodeBrand: innerIdlSymbol,
447
+ };
448
+ }
449
+ export function createPackage(name) {
450
+ return {
451
+ kind: IDLKind.Package,
452
+ name,
453
+ _idlNodeBrand: innerIdlSymbol,
454
+ _idlEntryBrand: innerIdlSymbol,
455
+ _idlNamedNodeBrand: innerIdlSymbol,
456
+ };
457
+ }
458
+ export function createImport(name, importClause) {
459
+ return {
460
+ kind: IDLKind.Import,
461
+ name,
462
+ importClause: importClause,
463
+ _idlNodeBrand: innerIdlSymbol,
464
+ _idlEntryBrand: innerIdlSymbol,
465
+ _idlNamedNodeBrand: innerIdlSymbol,
466
+ };
467
+ }
468
+ export function createEnum(name, elements, nodeInitializer) {
469
+ return Object.assign(Object.assign({ kind: IDLKind.Enum, name: name, elements: elements }, nodeInitializer), { _idlNodeBrand: innerIdlSymbol, _idlEntryBrand: innerIdlSymbol, _idlNamedNodeBrand: innerIdlSymbol });
470
+ }
471
+ export function createEnumMember(name, parent, type, initializer, nodeInitializer = {}) {
472
+ return Object.assign(Object.assign({ kind: IDLKind.EnumMember, name: name, parent,
473
+ type,
474
+ initializer }, nodeInitializer), { _idlNodeBrand: innerIdlSymbol, _idlEntryBrand: innerIdlSymbol, _idlNamedNodeBrand: innerIdlSymbol });
475
+ }
476
+ export function createInterface(name, subkind, inheritance = [], constructors = [], constants = [], properties = [], methods = [], callables = [], typeParameters = [], nodeInitializer = {}) {
477
+ return Object.assign(Object.assign({ kind: IDLKind.Interface, name,
478
+ subkind,
479
+ typeParameters,
480
+ inheritance,
481
+ constructors,
482
+ constants,
483
+ properties,
484
+ methods,
485
+ callables }, nodeInitializer), { _idlNodeBrand: innerIdlSymbol, _idlEntryBrand: innerIdlSymbol, _idlNamedNodeBrand: innerIdlSymbol });
486
+ }
487
+ export function createProperty(name, type, isReadonly = false, isStatic = false, isOptional = false, nodeInitializer = {}) {
488
+ return Object.assign(Object.assign({ name, kind: IDLKind.Property, type,
489
+ isReadonly,
490
+ isStatic,
491
+ isOptional }, nodeInitializer), { _idlNodeBrand: innerIdlSymbol, _idlEntryBrand: innerIdlSymbol, _idlNamedNodeBrand: innerIdlSymbol });
492
+ }
493
+ export function createParameter(name, type, isOptional = false, isVariadic = false, nodeInitializer = {}) {
494
+ return Object.assign(Object.assign({ kind: IDLKind.Parameter, name: name, type: type, isOptional,
495
+ isVariadic }, nodeInitializer), { _idlNodeBrand: innerIdlSymbol, _idlEntryBrand: innerIdlSymbol, _idlNamedNodeBrand: innerIdlSymbol });
496
+ }
497
+ export function createMethod(name, parameters, returnType, methodInitializer = {
498
+ isAsync: false,
499
+ isStatic: false,
500
+ isOptional: false,
501
+ isFree: false,
502
+ }, nodeInitializer = {}, typeParameters = []) {
503
+ return Object.assign(Object.assign(Object.assign({ kind: IDLKind.Method, name,
504
+ parameters,
505
+ returnType,
506
+ typeParameters }, methodInitializer), nodeInitializer), { _idlNodeBrand: innerIdlSymbol, _idlEntryBrand: innerIdlSymbol, _idlNamedNodeBrand: innerIdlSymbol });
507
+ }
508
+ export function createCallable(
509
+ // TODO name here seems useless
510
+ name, parameters, returnType, callableInitializer, nodeInitializer, typeParameters = []) {
511
+ return Object.assign(Object.assign(Object.assign({ kind: IDLKind.Callable, name,
512
+ parameters,
513
+ returnType }, callableInitializer), nodeInitializer), { _idlNodeBrand: innerIdlSymbol, _idlEntryBrand: innerIdlSymbol, _idlNamedNodeBrand: innerIdlSymbol });
514
+ }
515
+ export function createConstructor(parameters, returnType, nodeInitializer = {}) {
516
+ return Object.assign(Object.assign({ kind: IDLKind.Constructor, name: "$CONSTRUCTOR%", parameters,
517
+ returnType }, nodeInitializer), { _idlNodeBrand: innerIdlSymbol, _idlEntryBrand: innerIdlSymbol, _idlNamedNodeBrand: innerIdlSymbol });
518
+ }
519
+ export function createCallback(name, parameters, returnType, nodeInitializer = {}, typeParameters = []) {
520
+ if (returnType === IDLThisType)
521
+ returnType = IDLAnyType;
522
+ parameters = parameters.map(it => {
523
+ if (it.type && isNamedNode(it.type) && (it.type.name === "T" || it.type.name === "this"))
524
+ return createParameter(it.name, IDLAnyType, it.isOptional, it.isVariadic, { fileName: it.fileName });
525
+ return it;
526
+ });
527
+ return Object.assign(Object.assign({ kind: IDLKind.Callback, name, parameters, returnType, typeParameters }, nodeInitializer), { _idlNodeBrand: innerIdlSymbol, _idlEntryBrand: innerIdlSymbol, _idlNamedNodeBrand: innerIdlSymbol });
528
+ }
529
+ export function createTypeParameterReference(name) {
530
+ return {
531
+ kind: IDLKind.TypeParameterType,
532
+ name: name,
533
+ _idlNodeBrand: innerIdlSymbol,
534
+ _idlTypeBrand: innerIdlSymbol,
535
+ _idlNamedNodeBrand: innerIdlSymbol,
536
+ };
537
+ }
538
+ export function createTypedef(name, type, typeParameters = [], nodeInitializer = {}) {
539
+ return Object.assign(Object.assign({ name, type, typeParameters, kind: IDLKind.Typedef }, nodeInitializer), { _idlNodeBrand: innerIdlSymbol, _idlEntryBrand: innerIdlSymbol, _idlNamedNodeBrand: innerIdlSymbol });
540
+ }
541
+ export function createConstant(name, type, value, nodeInitializer = {}) {
542
+ return Object.assign(Object.assign({ kind: IDLKind.Const, name,
543
+ type,
544
+ value }, nodeInitializer), { _idlNodeBrand: innerIdlSymbol, _idlEntryBrand: innerIdlSymbol, _idlNamedNodeBrand: innerIdlSymbol });
545
+ }
546
+ export function escapeIDLKeyword(name) {
547
+ return name + (IDLKeywords.has(name) ? "_" : "");
548
+ }
549
+ export function unescapeKeyword(name) {
550
+ if (name.endsWith("_")) {
551
+ const unwrapped = name.slice(0, -1);
552
+ if (IDLKeywords.has(unwrapped))
553
+ return unwrapped;
554
+ }
555
+ return name;
556
+ }
557
+ const printedIndentInc = "[[indent-inc]]";
558
+ const printedIndentDec = "[[indent-dec]]";
559
+ export function printType(type, options) {
560
+ if (!type)
561
+ throw new Error("Missing type");
562
+ if (isInterface(type))
563
+ return type.name;
564
+ if (isOptionalType(type))
565
+ return `(${printType(type.type)} or ${IDLUndefinedType.name})`;
566
+ if (isPrimitiveType(type))
567
+ return type.name;
568
+ if (isContainerType(type))
569
+ return `${type.containerKind}<${type.elementType.map(it => printType(it)).join(", ")}>`;
570
+ if (isReferenceType(type)) {
571
+ const extAttrs = type.extendedAttributes ? Array.from(type.extendedAttributes) : [];
572
+ if (type.typeArguments)
573
+ extAttrs.push({ name: IDLExtendedAttributes.TypeArguments, value: type.typeArguments.map(it => printType(it)).join(",") });
574
+ if (!extAttrs.length)
575
+ return type.name;
576
+ let res = `[${quoteAttributeValues(extAttrs)}] ${type.name}`;
577
+ if (options === null || options === void 0 ? void 0 : options.bracketsAroundReferenceTypeWithExtAttrs)
578
+ return `(${res})`;
579
+ return res;
580
+ }
581
+ if (isUnspecifiedGenericType(type))
582
+ return `${type.name}<${type.typeArguments.map(it => printType(it)).join(", ")}>`;
583
+ if (isUnionType(type))
584
+ return `(${type.types.map(it => printType(it)).join(" or ")})`;
585
+ if (isTypeParameterType(type))
586
+ return type.name;
587
+ throw new Error(`Cannot map type: ${IDLKind[type.kind]}`);
588
+ }
589
+ export function printReturnType(type) {
590
+ return printType(type, { bracketsAroundReferenceTypeWithExtAttrs: true });
591
+ }
592
+ export function printParameters(parameters) {
593
+ var _a, _b;
594
+ return (_b = (_a = parameters === null || parameters === void 0 ? void 0 : parameters.map(it => nameWithType(it, it.isVariadic, it.isOptional))) === null || _a === void 0 ? void 0 : _a.join(", ")) !== null && _b !== void 0 ? _b : "";
595
+ }
596
+ export function printConstructor(idl) {
597
+ return [`constructor(${printParameters(idl.parameters)});`];
598
+ }
599
+ export function nameWithType(idl, isVariadic = false, isOptional = false) {
600
+ const type = printType(idl.type);
601
+ const variadic = isVariadic ? "..." : "";
602
+ const optional = isOptional ? "optional " : "";
603
+ return `${optional}${type}${variadic} ${escapeIDLKeyword(idl.name)}`;
604
+ }
605
+ export function printConstant(idl) {
606
+ return [
607
+ ...printExtendedAttributes(idl, 1),
608
+ `const ${nameWithType(idl)} = ${idl.value};`
609
+ ];
610
+ }
611
+ export function printProperty(idl) {
612
+ const staticMod = idl.isStatic ? "static " : "";
613
+ const readonlyMod = idl.isReadonly ? "readonly " : "";
614
+ return [
615
+ ...printExtendedAttributes(idl, 1),
616
+ `${staticMod}${readonlyMod}attribute ${nameWithType(idl)};`
617
+ ];
618
+ }
619
+ export function printExtendedAttributes(idl, indentLevel) {
620
+ let typeParameters;
621
+ let typeArguments;
622
+ switch (idl.kind) {
623
+ case IDLKind.Interface:
624
+ typeParameters = idl.typeParameters;
625
+ break;
626
+ case IDLKind.Callback:
627
+ case IDLKind.Method:
628
+ case IDLKind.Callable:
629
+ case IDLKind.Constructor:
630
+ typeParameters = idl.typeParameters;
631
+ break;
632
+ case IDLKind.Typedef:
633
+ typeParameters = idl.typeParameters;
634
+ break;
635
+ case IDLKind.ReferenceType:
636
+ typeArguments = idl.typeArguments;
637
+ break;
638
+ case IDLKind.UnspecifiedGenericType:
639
+ typeArguments = idl.typeArguments;
640
+ break;
641
+ }
642
+ const attributes = Array.from(idl.extendedAttributes || []);
643
+ if (typeParameters === null || typeParameters === void 0 ? void 0 : typeParameters.length)
644
+ attributes.push({ name: IDLExtendedAttributes.TypeParameters, value: typeParameters.join(",") });
645
+ if (typeArguments === null || typeArguments === void 0 ? void 0 : typeArguments.length)
646
+ attributes.push({ name: IDLExtendedAttributes.TypeArguments, value: typeArguments.map(it => printType(it)).join(",") });
647
+ if (idl.documentation) {
648
+ let docs = {
649
+ name: IDLExtendedAttributes.Documentation,
650
+ value: idl.documentation
651
+ };
652
+ attributes.push(docs);
653
+ }
654
+ const attrSpec = quoteAttributeValues(attributes);
655
+ return attrSpec ? [`[${attrSpec}]`] : [];
656
+ }
657
+ export const attributesToQuote = new Set([
658
+ IDLExtendedAttributes.Documentation,
659
+ IDLExtendedAttributes.DtsName,
660
+ IDLExtendedAttributes.DtsTag,
661
+ IDLExtendedAttributes.Import,
662
+ IDLExtendedAttributes.Interfaces,
663
+ IDLExtendedAttributes.TypeArguments,
664
+ IDLExtendedAttributes.TypeParameters,
665
+ ]);
666
+ function quoteAttributeValues(attributes) {
667
+ return attributes === null || attributes === void 0 ? void 0 : attributes.map(it => {
668
+ let attr = it.name;
669
+ if (it.value) {
670
+ let value = it.value;
671
+ if (value.includes('"') && !value.includes("'"))
672
+ value = value.replaceAll('"', "'");
673
+ value = value.replaceAll('\\', '\\\\').replaceAll('"', '\\"');
674
+ attr += `=${attributesToQuote.has(it.name) ? `"${value}"` : it.value}`;
675
+ }
676
+ return attr;
677
+ }).join(", ");
678
+ }
679
+ export function printFunction(idl) {
680
+ var _a;
681
+ if ((_a = idl.name) === null || _a === void 0 ? void 0 : _a.startsWith("__")) {
682
+ console.log(`Ignore ${idl.name}`);
683
+ return [];
684
+ }
685
+ return [
686
+ ...printExtendedAttributes(idl, 1),
687
+ `${idl.isAsync ? "async " : ""}${printReturnType(idl.returnType)} ${idl.name}(${printParameters(idl.parameters)});`
688
+ ];
689
+ }
690
+ export function printMethod(idl) {
691
+ var _a;
692
+ if ((_a = idl.name) === null || _a === void 0 ? void 0 : _a.startsWith("__")) {
693
+ console.log(`Ignore ${idl.name}`);
694
+ return [];
695
+ }
696
+ return [
697
+ ...printExtendedAttributes(idl, 1),
698
+ `${idl.isStatic ? "static " : ""}${idl.isAsync ? "async " : ""}${printReturnType(idl.returnType)} ${idl.name}(${printParameters(idl.parameters)});`
699
+ ];
700
+ }
701
+ export function printPackage(idl) {
702
+ return [
703
+ `package "${idl.name}";`
704
+ ];
705
+ }
706
+ export function printImport(idl) {
707
+ return [
708
+ `import "${idl.name}";`
709
+ ];
710
+ }
711
+ export function printNamespace(idl) {
712
+ return [
713
+ ...printExtendedAttributes(idl, 0),
714
+ `namespace ${idl.name} {`,
715
+ printedIndentInc,
716
+ ...idl.members.map(member => printIDL(member)).flat(),
717
+ printedIndentDec,
718
+ "};"
719
+ ];
720
+ }
721
+ export function printCallback(idl) {
722
+ return [
723
+ ...printExtendedAttributes(idl, 0),
724
+ `callback ${idl.name} = ${printReturnType(idl.returnType)} (${printParameters(idl.parameters)});`
725
+ ];
726
+ }
727
+ export function printScoped(idl) {
728
+ if (idl.kind == IDLKind.Callback)
729
+ return printCallback(idl);
730
+ if (idl.kind === IDLKind.Interface)
731
+ return printInterface(idl);
732
+ throw new Error(`Unexpected scoped: ${idl.kind} ${idl.name}`);
733
+ }
734
+ export function printInterface(idl) {
735
+ idl.methods
736
+ .map(it => {
737
+ let result = it.scope;
738
+ it.scope = undefined;
739
+ return result;
740
+ })
741
+ .filter(isDefined)
742
+ .forEach(scope => idl.scope ? idl.scope.push(...scope) : idl.scope = scope);
743
+ return [
744
+ ...printExtendedAttributes(idl, 0),
745
+ `interface ${idl.name}${hasSuperType(idl) ? ": " + printType(idl.inheritance[0]) : ""} {`,
746
+ // TODO: type system hack!
747
+ ]
748
+ .concat(printedIndentInc)
749
+ .concat(idl.constructors.map(printConstructor).flat())
750
+ .concat(idl.constants.map(printConstant).flat())
751
+ .concat(idl.properties.map(printProperty).flat())
752
+ .concat(idl.methods.map(printMethod).flat())
753
+ .concat(idl.callables.map(printFunction).flat())
754
+ .concat(printedIndentDec)
755
+ .concat(["};"]);
756
+ }
757
+ export function getSuperType(idl) {
758
+ if (!idl.inheritance)
759
+ return undefined;
760
+ const parent = idl.inheritance[0];
761
+ return parent && parent !== IDLTopType ? parent : undefined;
762
+ }
763
+ export function hasSuperType(idl) {
764
+ return isDefined(getSuperType(idl));
765
+ }
766
+ export function printEnumMember(idl) {
767
+ const type = printType(idl.type);
768
+ const initializer = idl.initializer === undefined
769
+ ? ''
770
+ : ' = ' + (type === IDLStringType.name
771
+ ? `"${String(idl.initializer).replaceAll('"', "'")}"`
772
+ : idl.initializer);
773
+ return [
774
+ idl.documentation,
775
+ ...printExtendedAttributes(idl, 0),
776
+ `${type} ${idl.name}${initializer};`
777
+ ];
778
+ }
779
+ export function printEnum(idl, skipInitializers) {
780
+ if (skipInitializers) {
781
+ return [
782
+ idl.documentation,
783
+ ...printExtendedAttributes(idl, 0),
784
+ `enum ${idl.name} {`,
785
+ printedIndentInc,
786
+ ...idl.elements.map(it => `${it.name} ${(it.initializer !== undefined ? " /* " + it.initializer + " */" : "")}`),
787
+ printedIndentDec,
788
+ "};"
789
+ ];
790
+ }
791
+ else {
792
+ return [
793
+ idl.documentation,
794
+ ...printExtendedAttributes(idl, 0),
795
+ `dictionary ${idl.name} {`,
796
+ printedIndentInc,
797
+ ...idl.elements.map(printEnumMember),
798
+ printedIndentDec,
799
+ "};"
800
+ ].flat();
801
+ }
802
+ }
803
+ export function printTypedef(idl) {
804
+ return [
805
+ idl.documentation,
806
+ ...printExtendedAttributes(idl, 0),
807
+ `typedef ${printType(idl.type)} ${idl.name};`
808
+ ];
809
+ }
810
+ export function printIDL(idl, options) {
811
+ var _a;
812
+ if (idl.kind == IDLKind.Interface)
813
+ return printInterface(idl);
814
+ if (idl.kind == IDLKind.Enum)
815
+ return printEnum(idl, (_a = options === null || options === void 0 ? void 0 : options.disableEnumInitializers) !== null && _a !== void 0 ? _a : false);
816
+ if (idl.kind == IDLKind.Typedef)
817
+ return printTypedef(idl);
818
+ if (idl.kind == IDLKind.Callback)
819
+ return printCallback(idl);
820
+ if (idl.kind == IDLKind.Package)
821
+ return printPackage(idl);
822
+ if (idl.kind == IDLKind.Import)
823
+ return printImport(idl);
824
+ if (idl.kind == IDLKind.Namespace)
825
+ return printNamespace(idl);
826
+ if (idl.kind == IDLKind.Method)
827
+ return printMethod(idl);
828
+ if (idl.kind == IDLKind.Const)
829
+ return printConstant(idl);
830
+ if (options === null || options === void 0 ? void 0 : options.allowUnknownKinds) {
831
+ return [`${IDLKind[idl.kind]} ${"name" in idl ? idl.name : ""}`];
832
+ }
833
+ else {
834
+ throw new Error(`unexpected kind: ${idl.kind}`);
835
+ }
836
+ }
837
+ export function toIDLString(entries, options) {
838
+ let indent = 0;
839
+ const generatedIdl = entries
840
+ .map(it => printIDL(it, options))
841
+ .concat(printScopes(entries))
842
+ .flat()
843
+ .filter(isDefined)
844
+ .filter(it => it.length > 0)
845
+ .map(it => {
846
+ if (it === printedIndentInc)
847
+ ++indent;
848
+ else if (it === printedIndentDec)
849
+ --indent;
850
+ else
851
+ return indentedBy(it, indent);
852
+ })
853
+ .join("\n");
854
+ return generatedIdl;
855
+ }
856
+ // throws validation error
857
+ export function verifyIDLString(source) {
858
+ webidl2.validate(webidl2.parse(source));
859
+ return true;
860
+ }
861
+ function printScopes(entries) {
862
+ return entries
863
+ .map((it) => it.scope)
864
+ .filter(isDefined)
865
+ .flatMap((it) => it.map(printScoped));
866
+ }
867
+ export function hasExtAttribute(node, attribute) {
868
+ var _a;
869
+ return ((_a = node.extendedAttributes) === null || _a === void 0 ? void 0 : _a.find((it) => it.name == attribute)) != undefined;
870
+ }
871
+ export function getExtAttribute(node, name) {
872
+ var _a, _b;
873
+ return (_b = (_a = node.extendedAttributes) === null || _a === void 0 ? void 0 : _a.find(it => it.name === name)) === null || _b === void 0 ? void 0 : _b.value;
874
+ }
875
+ export function getVerbatimDts(node) {
876
+ let value = getExtAttribute(node, IDLExtendedAttributes.VerbatimDts);
877
+ return value ? value.substring(1, value.length - 1) : undefined;
878
+ }
879
+ export const IDLContainerUtils = {
880
+ isRecord: (x) => isContainerType(x) && x.containerKind === 'record',
881
+ isSequence: (x) => isContainerType(x) && x.containerKind === 'sequence',
882
+ isPromise: (x) => isContainerType(x) && x.containerKind === 'Promise'
883
+ };
884
+ /**
885
+ * @returns tuple of qualifier name and real type name
886
+ */
887
+ export function decomposeQualifiedName(type) {
888
+ const typeName = type.name;
889
+ const lastDot = typeName.lastIndexOf(".");
890
+ if (lastDot >= 0) {
891
+ const qualifier = typeName.slice(0, lastDot);
892
+ const realTypeName = typeName.slice(lastDot + 1);
893
+ return [qualifier, realTypeName];
894
+ }
895
+ return [undefined, typeName];
896
+ }
897
+ export function maybeUnwrapOptionalType(type) {
898
+ if (isOptionalType(type)) {
899
+ return type.type;
900
+ }
901
+ return type;
902
+ }
903
+ export function maybeOptional(type, optional = false) {
904
+ if (optional) {
905
+ return createOptionalType(type);
906
+ }
907
+ return type;
908
+ }
909
+ export const DebugUtils = {
910
+ debugPrintType: (type) => {
911
+ if (isContainerType(type)) {
912
+ return `[IDLType, name: '${printType(type)}', kind: '${IDLKind[type.kind]}', elements: [${type.elementType.map(DebugUtils.debugPrintType).join(', ')}]]`;
913
+ }
914
+ return `[IDLType, name: '${printType(type)}', kind: '${IDLKind[type.kind]}']`;
915
+ },
916
+ };
917
+ export function forEachFunction(node, cb) {
918
+ forEachChild(node, child => {
919
+ if (child.kind === IDLKind.Method || child.kind === IDLKind.Callable)
920
+ cb(child);
921
+ });
922
+ }
923
+ export function asPromise(type) {
924
+ if (!type)
925
+ return;
926
+ if (!isContainerType(type))
927
+ return;
928
+ const container = type;
929
+ if (!IDLContainerUtils.isPromise(container))
930
+ return;
931
+ return container;
932
+ }
933
+ export function transformMethodsAsync2ReturnPromise(entry) {
934
+ forEachFunction(entry, function_ => {
935
+ var _a;
936
+ if (function_.isAsync) {
937
+ function_.isAsync = false;
938
+ if (!asPromise(function_.returnType))
939
+ function_.returnType = createContainerType("Promise", [(_a = function_.returnType) !== null && _a !== void 0 ? _a : IDLVoidType]);
940
+ }
941
+ });
942
+ }
943
+ export function transformMethodsReturnPromise2Async(entry) {
944
+ forEachFunction(entry, function_ => {
945
+ const promise = asPromise(function_.returnType);
946
+ if (promise) {
947
+ function_.returnType = promise.elementType[0];
948
+ function_.isAsync = true;
949
+ }
950
+ });
951
+ }
952
+ export function fetchSignatureTags(node) {
953
+ if (!node.extendedAttributes)
954
+ return [];
955
+ return node.extendedAttributes
956
+ .filter((ea) => ea.name === IDLExtendedAttributes.DtsTag)
957
+ .map((ea) => {
958
+ if (!ea.value)
959
+ throw new Error('Empty DtsTag is not allowed');
960
+ let indexNameValue = ea.value.split('|');
961
+ if (indexNameValue.length === 1) {
962
+ return {
963
+ index: 0,
964
+ name: 'type',
965
+ value: indexNameValue[0],
966
+ };
967
+ }
968
+ if (indexNameValue.length !== 3)
969
+ throw new Error(`Malformed DtsTag: "${ea.value}"`);
970
+ return {
971
+ index: Number(indexNameValue[0]),
972
+ name: indexNameValue[1],
973
+ value: indexNameValue[2],
974
+ };
975
+ })
976
+ .sort((a, b) => a.index - b.index);
977
+ }
978
+ export function mixMethodParametersAndTags(node) {
979
+ let mix = node.parameters.slice(0);
980
+ for (const tag of fetchSignatureTags(node))
981
+ mix.splice(tag.index, 0, tag);
982
+ return mix;
983
+ }
984
+ export function isHandwritten(decl) {
985
+ return hasExtAttribute(decl, IDLExtendedAttributes.HandWrittenImplementation);
986
+ }
987
+ export function isStringEnum(decl) {
988
+ return decl.elements.some(e => e.type === IDLStringType);
989
+ }
990
+ export function linearizeNamespaceMembers(entries) {
991
+ const linearized = [];
992
+ for (const entry of entries) {
993
+ if (isNamespace(entry))
994
+ linearized.push(...linearizeNamespaceMembers(entry.members));
995
+ else
996
+ linearized.push(entry);
997
+ }
998
+ return linearized;
999
+ }
1000
+ export function extremumOfOrdinals(enumEntry) {
1001
+ let low = 0;
1002
+ let high = 0;
1003
+ enumEntry.elements.forEach((member, index) => {
1004
+ let value = index;
1005
+ if ((typeof member.initializer === 'number') && !isStringEnum(enumEntry)) {
1006
+ value = member.initializer;
1007
+ }
1008
+ if (low > value)
1009
+ low = value;
1010
+ if (high < value)
1011
+ high = value;
1012
+ });
1013
+ return { low, high };
1014
+ }
1015
+ //# sourceMappingURL=idl.js.map