@idlizer/core 2.1.10-arktscgen-5 → 2.1.10-arktscgen-6

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 (104) hide show
  1. package/build/lib/src/Language.js +1 -1
  2. package/build/lib/src/LanguageWriters/ArgConvertors.d.ts +16 -6
  3. package/build/lib/src/LanguageWriters/ArgConvertors.js +80 -92
  4. package/build/lib/src/LanguageWriters/LanguageWriter.d.ts +8 -6
  5. package/build/lib/src/LanguageWriters/LanguageWriter.js +8 -2
  6. package/build/lib/src/LanguageWriters/common.d.ts +1 -0
  7. package/build/lib/src/LanguageWriters/common.js +2 -1
  8. package/build/lib/src/LanguageWriters/convertors/CppConvertors.d.ts +7 -1
  9. package/build/lib/src/LanguageWriters/convertors/CppConvertors.js +53 -15
  10. package/build/lib/src/LanguageWriters/convertors/ETSConvertors.d.ts +1 -0
  11. package/build/lib/src/LanguageWriters/convertors/ETSConvertors.js +10 -8
  12. package/build/lib/src/LanguageWriters/convertors/InteropConvertors.js +1 -1
  13. package/build/lib/src/LanguageWriters/convertors/TSConvertors.js +25 -14
  14. package/build/lib/src/LanguageWriters/index.d.ts +4 -3
  15. package/build/lib/src/LanguageWriters/index.js +9 -11
  16. package/build/lib/src/LanguageWriters/nameConvertor.d.ts +2 -0
  17. package/build/lib/src/LanguageWriters/nameConvertor.js +11 -0
  18. package/build/lib/src/LanguageWriters/writers/CppLanguageWriter.js +1 -1
  19. package/build/lib/src/LanguageWriters/writers/ETSLanguageWriter.d.ts +1 -8
  20. package/build/lib/src/LanguageWriters/writers/ETSLanguageWriter.js +52 -89
  21. package/build/lib/src/LanguageWriters/writers/TsLanguageWriter.d.ts +3 -0
  22. package/build/lib/src/LanguageWriters/writers/TsLanguageWriter.js +24 -7
  23. package/build/lib/src/config.d.ts +2 -1461
  24. package/build/lib/src/config.js +16 -32
  25. package/build/lib/src/configMerge.d.ts +3 -0
  26. package/build/lib/src/configMerge.js +63 -0
  27. package/build/lib/src/diagnostictypes.d.ts +1 -5
  28. package/build/lib/src/diagnostictypes.js +1 -27
  29. package/build/lib/src/from-idl/DtsPrinter.js +12 -5
  30. package/build/lib/src/from-idl/IDLLinter.d.ts +3 -4
  31. package/build/lib/src/from-idl/IDLLinter.js +30 -32
  32. package/build/lib/src/from-idl/deserialize.d.ts +3 -4
  33. package/build/lib/src/from-idl/deserialize.js +29 -641
  34. package/build/lib/src/from-idl/parser.d.ts +20 -2
  35. package/build/lib/src/from-idl/parser.js +97 -29
  36. package/build/lib/src/idl/builders.d.ts +43 -0
  37. package/build/lib/src/idl/builders.js +135 -0
  38. package/build/lib/src/idl/discriminators.d.ts +53 -0
  39. package/build/lib/src/idl/discriminators.js +232 -0
  40. package/build/lib/src/idl/dump.d.ts +48 -0
  41. package/build/lib/src/idl/dump.js +327 -0
  42. package/build/lib/src/idl/index.d.ts +9 -0
  43. package/build/lib/src/idl/index.js +23 -0
  44. package/build/lib/src/idl/keywords.d.ts +2 -0
  45. package/build/lib/src/{options.js → idl/keywords.js} +7 -3
  46. package/build/lib/src/idl/node.d.ts +233 -0
  47. package/build/lib/src/idl/node.js +103 -0
  48. package/build/lib/src/idl/stdlib.d.ts +34 -0
  49. package/build/lib/src/idl/stdlib.js +54 -0
  50. package/build/lib/src/idl/utils.d.ts +44 -0
  51. package/build/lib/src/idl/utils.js +215 -0
  52. package/build/lib/src/idl/visitors.d.ts +15 -0
  53. package/build/lib/src/idl/visitors.js +593 -0
  54. package/build/lib/src/index.d.ts +4 -2
  55. package/build/lib/src/index.js +5 -3
  56. package/build/lib/src/inputPaths.d.ts +11 -0
  57. package/build/lib/src/inputPaths.js +81 -0
  58. package/build/lib/src/languageSpecificKeywords.d.ts +0 -1
  59. package/build/lib/src/languageSpecificKeywords.js +0 -5
  60. package/build/lib/src/peer-generation/LayoutManager.d.ts +1 -1
  61. package/build/lib/src/peer-generation/Materialized.d.ts +8 -2
  62. package/build/lib/src/peer-generation/Materialized.js +24 -12
  63. package/build/lib/src/peer-generation/PeerLibrary.d.ts +10 -15
  64. package/build/lib/src/peer-generation/PeerLibrary.js +35 -215
  65. package/build/lib/src/peer-generation/ReferenceResolver.d.ts +6 -1
  66. package/build/lib/src/peer-generation/ReferenceResolver.js +93 -2
  67. package/build/lib/src/peer-generation/idl/common.d.ts +6 -2
  68. package/build/lib/src/peer-generation/idl/common.js +7 -7
  69. package/build/lib/src/peer-generation/isMaterialized.js +2 -3
  70. package/build/lib/src/peer-generation/modules.js +2 -0
  71. package/build/lib/src/peer-generation/toDeclaration.d.ts +4 -0
  72. package/build/lib/src/peer-generation/toDeclaration.js +53 -0
  73. package/build/lib/src/peer-generation/unions.d.ts +1 -1
  74. package/build/lib/src/peer-generation/unions.js +15 -7
  75. package/build/lib/src/resolveNamedNode.d.ts +1 -0
  76. package/build/lib/src/resolveNamedNode.js +7 -0
  77. package/build/lib/src/transformers/FqnTransformer.d.ts +1 -1
  78. package/build/lib/src/transformers/FqnTransformer.js +20 -12
  79. package/build/lib/src/transformers/GenericTransformer.d.ts +4 -3
  80. package/build/lib/src/transformers/GenericTransformer.js +252 -158
  81. package/build/lib/src/transformers/IdlTransformer.d.ts +6 -0
  82. package/build/lib/src/transformers/IdlTransformer.js +7 -0
  83. package/build/lib/src/transformers/NullTransformer.d.ts +1 -1
  84. package/build/lib/src/transformers/NullTransformer.js +27 -21
  85. package/build/lib/src/transformers/OnSerializeTransformer.d.ts +1 -1
  86. package/build/lib/src/transformers/OnSerializeTransformer.js +28 -13
  87. package/build/lib/src/util.d.ts +4 -0
  88. package/build/lib/src/util.js +22 -0
  89. package/package.json +6 -7
  90. package/build/lib/src/LanguageWriters/convertors/JavaConvertors.d.ts +0 -37
  91. package/build/lib/src/LanguageWriters/convertors/JavaConvertors.js +0 -210
  92. package/build/lib/src/LanguageWriters/writers/JavaLanguageWriter.d.ts +0 -85
  93. package/build/lib/src/LanguageWriters/writers/JavaLanguageWriter.js +0 -306
  94. package/build/lib/src/from-idl/webidl2-utils.d.ts +0 -21
  95. package/build/lib/src/from-idl/webidl2-utils.js +0 -87
  96. package/build/lib/src/idl.d.ts +0 -444
  97. package/build/lib/src/idl.js +0 -1385
  98. package/build/lib/src/idlize.d.ts +0 -25
  99. package/build/lib/src/idlize.js +0 -198
  100. package/build/lib/src/options.d.ts +0 -13
  101. package/build/lib/src/peer-generation/BuilderClass.d.ts +0 -20
  102. package/build/lib/src/peer-generation/BuilderClass.js +0 -68
  103. package/webidl2.js/dist/webidl2.js +0 -4622
  104. package/webidl2.js/package.json +0 -55
@@ -1,1385 +0,0 @@
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["Interface"] = "Interface";
22
- IDLKind["Import"] = "Import";
23
- IDLKind["Callback"] = "Callback";
24
- IDLKind["Const"] = "Const";
25
- IDLKind["Property"] = "Property";
26
- IDLKind["Parameter"] = "Parameter";
27
- IDLKind["Method"] = "Method";
28
- IDLKind["Callable"] = "Callable";
29
- IDLKind["Constructor"] = "Constructor";
30
- IDLKind["Enum"] = "Enum";
31
- IDLKind["EnumMember"] = "EnumMember";
32
- IDLKind["Typedef"] = "Typedef";
33
- IDLKind["PrimitiveType"] = "PrimitiveType";
34
- IDLKind["ContainerType"] = "ContainerType";
35
- IDLKind["UnspecifiedGenericType"] = "UnspecifiedGenericType";
36
- IDLKind["ReferenceType"] = "ReferenceType";
37
- IDLKind["UnionType"] = "UnionType";
38
- IDLKind["TypeParameterType"] = "TypeParameterType";
39
- IDLKind["OptionalType"] = "OptionalType";
40
- IDLKind["Version"] = "Version";
41
- IDLKind["Namespace"] = "Namespace";
42
- IDLKind["File"] = "File";
43
- })(IDLKind || (IDLKind = {}));
44
- export var IDLEntity;
45
- (function (IDLEntity) {
46
- IDLEntity["Class"] = "Class";
47
- IDLEntity["Interface"] = "Interface";
48
- IDLEntity["Import"] = "Import";
49
- IDLEntity["Intersection"] = "Intersection";
50
- IDLEntity["Literal"] = "Literal";
51
- IDLEntity["NamedTuple"] = "NamedTuple";
52
- IDLEntity["Tuple"] = "Tuple";
53
- })(IDLEntity || (IDLEntity = {}));
54
- export var IDLExtendedAttributes;
55
- (function (IDLExtendedAttributes) {
56
- IDLExtendedAttributes["Accessor"] = "Accessor";
57
- IDLExtendedAttributes["Annotations"] = "Annotations";
58
- IDLExtendedAttributes["Async"] = "Async";
59
- IDLExtendedAttributes["CallSignature"] = "CallSignature";
60
- IDLExtendedAttributes["CommonMethod"] = "CommonMethod";
61
- IDLExtendedAttributes["Component"] = "Component";
62
- IDLExtendedAttributes["ComponentInterface"] = "ComponentInterface";
63
- IDLExtendedAttributes["ComponentModifier"] = "ComponentModifier";
64
- IDLExtendedAttributes["Deprecated"] = "Deprecated";
65
- IDLExtendedAttributes["Documentation"] = "Documentation";
66
- IDLExtendedAttributes["DtsName"] = "DtsName";
67
- IDLExtendedAttributes["DtsTag"] = "DtsTag";
68
- IDLExtendedAttributes["Entity"] = "Entity";
69
- IDLExtendedAttributes["Extends"] = "Extends";
70
- IDLExtendedAttributes["ExtensionMethod"] = "ExtensionMethod";
71
- IDLExtendedAttributes["Import"] = "Import";
72
- IDLExtendedAttributes["DefaultExport"] = "DefaultExport";
73
- IDLExtendedAttributes["IndexSignature"] = "IndexSignature";
74
- IDLExtendedAttributes["Interfaces"] = "Interfaces";
75
- IDLExtendedAttributes["NativeModule"] = "NativeModule";
76
- IDLExtendedAttributes["Optional"] = "Optional";
77
- IDLExtendedAttributes["UnionOnlyNull"] = "OptionalOnlyNull";
78
- IDLExtendedAttributes["UnionWithNull"] = "OptionalWithNull";
79
- IDLExtendedAttributes["OriginalEnumMemberName"] = "OriginalEnumMemberName";
80
- IDLExtendedAttributes["OriginalGenericName"] = "OriginalGenericName";
81
- IDLExtendedAttributes["Predefined"] = "Predefined";
82
- IDLExtendedAttributes["Protected"] = "Protected";
83
- IDLExtendedAttributes["Abstract"] = "Abstract";
84
- IDLExtendedAttributes["Synthetic"] = "Synthetic";
85
- IDLExtendedAttributes["Throws"] = "Throws";
86
- IDLExtendedAttributes["TraceKey"] = "TraceKey";
87
- IDLExtendedAttributes["TypeAnnotations"] = "TypeAnnotations";
88
- IDLExtendedAttributes["TypeArguments"] = "TypeArguments";
89
- IDLExtendedAttributes["TypeParameters"] = "TypeParameters";
90
- IDLExtendedAttributes["TypeParametersDefaults"] = "TypeParametersDefaults";
91
- IDLExtendedAttributes["VerbatimDts"] = "VerbatimDts";
92
- IDLExtendedAttributes["HandWrittenImplementation"] = "HandWrittenImplementation";
93
- IDLExtendedAttributes["ExtraMethod"] = "ExtraMethod";
94
- IDLExtendedAttributes["OverloadAlias"] = "OverloadAlias";
95
- IDLExtendedAttributes["OverloadPriority"] = "OverloadPriority";
96
- IDLExtendedAttributes["TransformOnSerialize"] = "TransformOnSerialize";
97
- })(IDLExtendedAttributes || (IDLExtendedAttributes = {}));
98
- export var IDLAccessorAttribute;
99
- (function (IDLAccessorAttribute) {
100
- IDLAccessorAttribute["Getter"] = "Getter";
101
- IDLAccessorAttribute["Setter"] = "Setter";
102
- })(IDLAccessorAttribute || (IDLAccessorAttribute = {}));
103
- const innerIdlSymbol = Symbol("innerIdlSymbol");
104
- export var IDLInterfaceSubkind;
105
- (function (IDLInterfaceSubkind) {
106
- IDLInterfaceSubkind[IDLInterfaceSubkind["Interface"] = 0] = "Interface";
107
- IDLInterfaceSubkind[IDLInterfaceSubkind["Class"] = 1] = "Class";
108
- IDLInterfaceSubkind[IDLInterfaceSubkind["AnonymousInterface"] = 2] = "AnonymousInterface";
109
- IDLInterfaceSubkind[IDLInterfaceSubkind["Tuple"] = 3] = "Tuple";
110
- })(IDLInterfaceSubkind || (IDLInterfaceSubkind = {}));
111
- export function forEachChild(node, cbEnter, cbLeave) {
112
- var _a, _b, _c;
113
- const cleanup = cbEnter(node);
114
- switch (node.kind) {
115
- case IDLKind.File:
116
- node.entries.forEach((value) => forEachChild(value, cbEnter, cbLeave));
117
- break;
118
- case IDLKind.Namespace:
119
- node.members.forEach((value) => forEachChild(value, cbEnter, cbLeave));
120
- break;
121
- case IDLKind.Interface: {
122
- let concrete = node;
123
- concrete.inheritance.forEach((value) => forEachChild(value, cbEnter, cbLeave));
124
- concrete.constructors.forEach((value) => forEachChild(value, cbEnter, cbLeave));
125
- concrete.properties.forEach((value) => forEachChild(value, cbEnter, cbLeave));
126
- concrete.methods.forEach((value) => forEachChild(value, cbEnter, cbLeave));
127
- concrete.callables.forEach((value) => forEachChild(value, cbEnter, cbLeave));
128
- break;
129
- }
130
- case IDLKind.Method:
131
- case IDLKind.Callable:
132
- case IDLKind.Callback:
133
- case IDLKind.Constructor: {
134
- let concrete = node;
135
- (_a = concrete.parameters) === null || _a === void 0 ? void 0 : _a.forEach((value) => forEachChild(value, cbEnter, cbLeave));
136
- if (concrete.returnType)
137
- forEachChild(concrete.returnType, cbEnter, cbLeave);
138
- break;
139
- }
140
- case IDLKind.UnionType: {
141
- let concrete = node;
142
- (_b = concrete.types) === null || _b === void 0 ? void 0 : _b.forEach((value) => forEachChild(value, cbEnter, cbLeave));
143
- break;
144
- }
145
- case IDLKind.OptionalType: {
146
- let concrete = node;
147
- forEachChild(concrete.type, cbEnter, cbLeave);
148
- break;
149
- }
150
- case IDLKind.Const: {
151
- forEachChild(node.type, cbEnter, cbLeave);
152
- break;
153
- }
154
- case IDLKind.Enum: {
155
- node.elements.forEach((value) => forEachChild(value, cbEnter, cbLeave));
156
- break;
157
- }
158
- case IDLKind.Property: {
159
- forEachChild(node.type, cbEnter, cbLeave);
160
- break;
161
- }
162
- case IDLKind.Parameter: {
163
- const concrete = node;
164
- if (concrete.type)
165
- forEachChild(concrete.type, cbEnter, cbLeave);
166
- break;
167
- }
168
- case IDLKind.Typedef: {
169
- forEachChild(node.type, cbEnter, cbLeave);
170
- break;
171
- }
172
- case IDLKind.ContainerType: {
173
- node.elementType.forEach((value) => forEachChild(value, cbEnter, cbLeave));
174
- break;
175
- }
176
- case IDLKind.UnspecifiedGenericType: {
177
- node.typeArguments.forEach((value) => forEachChild(value, cbEnter, cbLeave));
178
- break;
179
- }
180
- case IDLKind.ReferenceType: {
181
- (_c = node.typeArguments) === null || _c === void 0 ? void 0 : _c.forEach((value) => forEachChild(value, cbEnter, cbLeave));
182
- break;
183
- }
184
- case IDLKind.TypeParameterType:
185
- case IDLKind.EnumMember:
186
- case IDLKind.Import:
187
- case IDLKind.PrimitiveType:
188
- case IDLKind.Version:
189
- break;
190
- default: {
191
- throw new Error(`Unhandled ${node.kind}`);
192
- }
193
- }
194
- cbLeave === null || cbLeave === void 0 ? void 0 : cbLeave(node);
195
- cleanup === null || cleanup === void 0 ? void 0 : cleanup();
196
- }
197
- /** Updates tree in place! */
198
- export function updateEachChild(node, op, cbLeave) {
199
- var _a;
200
- const old = node;
201
- node = op(old);
202
- if (node.kind !== old.kind && !(isType(old) && isType(node))) {
203
- throw new Error("Kinds must be the same!");
204
- }
205
- switch (node.kind) {
206
- case IDLKind.File: {
207
- const concrete = node;
208
- concrete.entries = concrete.entries.map(it => updateEachChild(it, op, cbLeave));
209
- break;
210
- }
211
- case IDLKind.Namespace: {
212
- const concrete = node;
213
- concrete.members = concrete.members.map((it) => updateEachChild(it, op, cbLeave));
214
- break;
215
- }
216
- case IDLKind.Interface: {
217
- const concrete = node;
218
- concrete.inheritance = concrete.inheritance.map((it) => updateEachChild(it, op, cbLeave));
219
- concrete.constructors = concrete.constructors.map((it) => updateEachChild(it, op, cbLeave));
220
- concrete.properties = concrete.properties.map((it) => updateEachChild(it, op, cbLeave));
221
- concrete.methods = concrete.methods.map((it) => updateEachChild(it, op, cbLeave));
222
- concrete.callables = concrete.callables.map((it) => updateEachChild(it, op, cbLeave));
223
- break;
224
- }
225
- case IDLKind.Method:
226
- case IDLKind.Callable:
227
- case IDLKind.Callback:
228
- case IDLKind.Constructor: {
229
- const concrete = node;
230
- concrete.parameters = concrete.parameters.map((it) => updateEachChild(it, op, cbLeave));
231
- if (concrete.returnType) {
232
- concrete.returnType = updateEachChild(concrete.returnType, op, cbLeave);
233
- }
234
- break;
235
- }
236
- case IDLKind.UnionType: {
237
- const concrete = node;
238
- concrete.types = concrete.types.map((it) => updateEachChild(it, op, cbLeave));
239
- break;
240
- }
241
- case IDLKind.OptionalType: {
242
- const concrete = node;
243
- concrete.type = updateEachChild(concrete.type, op, cbLeave);
244
- break;
245
- }
246
- case IDLKind.Const: {
247
- const concrete = node;
248
- concrete.type = updateEachChild(concrete.type, op, cbLeave);
249
- break;
250
- }
251
- case IDLKind.Enum: {
252
- const concrete = node;
253
- concrete.elements = concrete.elements.map((it) => updateEachChild(it, op, cbLeave));
254
- break;
255
- }
256
- case IDLKind.Property: {
257
- const concrete = node;
258
- concrete.type = updateEachChild(concrete.type, op, cbLeave);
259
- break;
260
- }
261
- case IDLKind.Parameter: {
262
- const concrete = node;
263
- if (concrete.type)
264
- concrete.type = updateEachChild(concrete.type, op, cbLeave);
265
- break;
266
- }
267
- case IDLKind.Typedef: {
268
- const concrete = node;
269
- concrete.type = updateEachChild(concrete.type, op, cbLeave);
270
- break;
271
- }
272
- case IDLKind.ContainerType: {
273
- const concrete = node;
274
- concrete.elementType = concrete.elementType.map(it => updateEachChild(it, op, cbLeave));
275
- break;
276
- }
277
- case IDLKind.UnspecifiedGenericType: {
278
- const concrete = node;
279
- concrete.typeArguments = concrete.typeArguments.map(it => updateEachChild(it, op, cbLeave));
280
- break;
281
- }
282
- case IDLKind.ReferenceType: {
283
- const concrete = node;
284
- concrete.typeArguments = (_a = concrete.typeArguments) === null || _a === void 0 ? void 0 : _a.map(it => updateEachChild(it, op, cbLeave));
285
- break;
286
- }
287
- case IDLKind.TypeParameterType:
288
- case IDLKind.EnumMember:
289
- case IDLKind.Import:
290
- case IDLKind.PrimitiveType:
291
- case IDLKind.Version:
292
- break;
293
- default: {
294
- throw new Error(`Unhandled ${node.kind}`);
295
- }
296
- }
297
- if (cbLeave) {
298
- cbLeave === null || cbLeave === void 0 ? void 0 : cbLeave(node);
299
- }
300
- return node;
301
- }
302
- export function isNamedNode(type) {
303
- return "_idlNamedNodeBrand" in type;
304
- }
305
- export function forceAsNamedNode(type) {
306
- if (!isNamedNode(type)) {
307
- throw new Error(`Expected to be an IDLNamedNode, but got '${IDLKind[type.kind]}'`);
308
- }
309
- return type;
310
- }
311
- export function isFile(node) {
312
- return node.kind === IDLKind.File;
313
- }
314
- export function isUndefinedType(type) {
315
- return isPrimitiveType(type) && type.name === IDLUndefinedType.name;
316
- }
317
- export function isVoidType(type) {
318
- return isPrimitiveType(type) && type.name === IDLVoidType.name;
319
- }
320
- export function isPrimitiveType(type) {
321
- return type.kind == IDLKind.PrimitiveType;
322
- }
323
- export function isContainerType(type) {
324
- return type.kind == IDLKind.ContainerType;
325
- }
326
- export function isReferenceType(type) {
327
- return type.kind == IDLKind.ReferenceType;
328
- }
329
- export function isUnspecifiedGenericType(type) {
330
- return type.kind == IDLKind.UnspecifiedGenericType;
331
- }
332
- export function isEnum(type) {
333
- return type.kind == IDLKind.Enum;
334
- }
335
- export function isEnumMember(type) {
336
- return type.kind == IDLKind.EnumMember;
337
- }
338
- export function isUnionType(type) {
339
- return type.kind == IDLKind.UnionType;
340
- }
341
- export function isTypeParameterType(type) {
342
- return type.kind == IDLKind.TypeParameterType;
343
- }
344
- export function isInterface(node) {
345
- return node.kind === IDLKind.Interface;
346
- }
347
- export function isImport(type) {
348
- return type.kind == IDLKind.Import;
349
- }
350
- export function isCallable(node) {
351
- return node.kind === IDLKind.Callable;
352
- }
353
- export function isMethod(node) {
354
- return node.kind === IDLKind.Method;
355
- }
356
- export function isParameter(node) {
357
- return node.kind === IDLKind.Parameter;
358
- }
359
- export function isConstructor(node) {
360
- return node.kind === IDLKind.Constructor;
361
- }
362
- export function isProperty(node) {
363
- return node.kind === IDLKind.Property;
364
- }
365
- export function isCallback(node) {
366
- return node.kind === IDLKind.Callback;
367
- }
368
- export function isInterfaceSubkind(idl) {
369
- return idl.subkind === IDLInterfaceSubkind.Interface;
370
- }
371
- export function isClassSubkind(idl) {
372
- return idl.subkind === IDLInterfaceSubkind.Class;
373
- }
374
- export function isConstant(node) {
375
- return node.kind === IDLKind.Const;
376
- }
377
- export function isTypedef(node) {
378
- return node.kind === IDLKind.Typedef;
379
- }
380
- export function isType(node) {
381
- return "_idlTypeBrand" in node;
382
- }
383
- export function isEntry(node) {
384
- return "_idlEntryBrand" in node;
385
- }
386
- export function isNamespace(node) {
387
- return node.kind === IDLKind.Namespace;
388
- }
389
- export function isSyntheticEntry(node) {
390
- var _a;
391
- return isDefined((_a = node.extendedAttributes) === null || _a === void 0 ? void 0 : _a.find(it => it.name === IDLExtendedAttributes.Synthetic));
392
- }
393
- export function isOptionalType(type) {
394
- return type.kind === IDLKind.OptionalType;
395
- }
396
- export function isVersion(node) {
397
- return node.kind === IDLKind.Version;
398
- }
399
- function createPrimitiveType(name) {
400
- return {
401
- kind: IDLKind.PrimitiveType,
402
- name: name,
403
- _idlNodeBrand: innerIdlSymbol,
404
- _idlTypeBrand: innerIdlSymbol,
405
- _idlNamedNodeBrand: innerIdlSymbol,
406
- };
407
- }
408
- export function createOptionalType(element, nodeInitializer) {
409
- if (isOptionalType(element) && !nodeInitializer) {
410
- return element;
411
- }
412
- if (isOptionalType(element)) {
413
- return Object.assign(Object.assign({ kind: IDLKind.OptionalType, type: element.type }, nodeInitializer), { _idlNodeBrand: innerIdlSymbol, _idlTypeBrand: innerIdlSymbol });
414
- }
415
- return Object.assign(Object.assign({ kind: IDLKind.OptionalType, type: element }, nodeInitializer), { _idlNodeBrand: innerIdlSymbol, _idlTypeBrand: innerIdlSymbol });
416
- }
417
- // must match with toIDLType in deserialize.ts
418
- export const IDLPointerType = createPrimitiveType('pointer');
419
- export const IDLVoidType = createPrimitiveType('void');
420
- export const IDLBooleanType = createPrimitiveType('boolean');
421
- export const IDLI8Type = createPrimitiveType('i8');
422
- export const IDLU8Type = createPrimitiveType('u8');
423
- export const IDLI16Type = createPrimitiveType('i16');
424
- export const IDLU16Type = createPrimitiveType('u16');
425
- export const IDLI32Type = createPrimitiveType('i32');
426
- export const IDLU32Type = createPrimitiveType('u32');
427
- export const IDLI64Type = createPrimitiveType('i64');
428
- export const IDLU64Type = createPrimitiveType('u64');
429
- export const IDLF16Type = createPrimitiveType('f16');
430
- export const IDLF32Type = createPrimitiveType('f32');
431
- export const IDLF64Type = createPrimitiveType('f64');
432
- export const IDLBigintType = createPrimitiveType("bigint");
433
- export const IDLNumberType = createPrimitiveType('number');
434
- export const IDLStringType = createPrimitiveType('String');
435
- export const IDLAnyType = createPrimitiveType('any');
436
- export const IDLUndefinedType = createPrimitiveType('undefined');
437
- export const IDLUnknownType = createPrimitiveType('unknown');
438
- export const IDLObjectType = createPrimitiveType('Object');
439
- export const IDLThisType = createPrimitiveType('this');
440
- export const IDLDate = createPrimitiveType('date');
441
- export const IDLBufferType = createPrimitiveType('buffer');
442
- export const IDLUint8ArrayType = createContainerType('sequence', [IDLU8Type]);
443
- export const IDLSerializerBuffer = createPrimitiveType('SerializerBuffer');
444
- // Stub for IdlPeerLibrary
445
- export const IDLFunctionType = createPrimitiveType('Function');
446
- export const IDLCustomObjectType = createPrimitiveType('CustomObject');
447
- export const IDLInteropReturnBufferType = createPrimitiveType('InteropReturnBuffer');
448
- export const IDLNullTypeName = "idlize.stdlib.Null";
449
- export function createNamespace(name, members, nodeInitializer) {
450
- return Object.assign(Object.assign({ kind: IDLKind.Namespace, members: members !== null && members !== void 0 ? members : [], name: name }, nodeInitializer), { _idlNodeBrand: innerIdlSymbol, _idlEntryBrand: innerIdlSymbol, _idlNamedNodeBrand: innerIdlSymbol });
451
- }
452
- export function linkParentBack(node) {
453
- const parentStack = [];
454
- updateEachChild(node, (node) => {
455
- if (isPrimitiveType(node)) {
456
- return node;
457
- }
458
- if (parentStack.length) {
459
- const top = parentStack[parentStack.length - 1];
460
- if (node.parent !== undefined && node.parent !== top) {
461
- node = clone(node);
462
- }
463
- node.parent = top;
464
- }
465
- parentStack.push(node);
466
- return node;
467
- }, (node) => {
468
- if (isPrimitiveType(node)) {
469
- return;
470
- }
471
- parentStack.pop();
472
- });
473
- return node;
474
- }
475
- export function getNamespacesPathFor(node) {
476
- let iterator = node.parent;
477
- const result = [];
478
- while (iterator) {
479
- if (isNamespace(iterator))
480
- result.unshift(iterator);
481
- iterator = iterator.parent;
482
- }
483
- return result;
484
- }
485
- const nodesWithoutIDLFiles = new Set();
486
- export function getFileFor(node) {
487
- let iterator = node;
488
- while (iterator) {
489
- if (isFile(iterator))
490
- return iterator;
491
- iterator = iterator.parent;
492
- }
493
- const name = getQualifiedName(node, "namespace.name");
494
- if (!nodesWithoutIDLFiles.has(name)) {
495
- console.warn(`Node ${name} does not have IDLFile in parents`);
496
- nodesWithoutIDLFiles.add(name);
497
- }
498
- return undefined;
499
- }
500
- export function isEqualByQualifedName(a, b, pattern = "package.namespace.name") {
501
- if (a === b)
502
- return true;
503
- if (!a || !b)
504
- return false;
505
- if (a.kind !== b.kind || a.name !== b.name)
506
- return false;
507
- return getQualifiedName(a, pattern) === getQualifiedName(b, pattern);
508
- }
509
- export function getPackageClause(node) {
510
- var _a;
511
- const file = getFileFor(node);
512
- return (_a = file === null || file === void 0 ? void 0 : file.packageClause) !== null && _a !== void 0 ? _a : [];
513
- }
514
- export function getPackageName(node) {
515
- return getPackageClause(node).join(".");
516
- }
517
- export function isInPackage(entry, packageName, exactMatch = false) {
518
- const entryPackageName = getPackageName(entry);
519
- return exactMatch
520
- ? entryPackageName === packageName
521
- : entryPackageName.startsWith(packageName);
522
- }
523
- export function getNamespaceName(a) {
524
- return getNamespacesPathFor(a).map(it => it.name).join('.');
525
- }
526
- export function deriveQualifiedNameFrom(name, from) {
527
- return [...getPackageClause(from), ...getNamespacesPathFor(from).map(it => it.name), name].join(".");
528
- }
529
- export function getQualifiedName(a, pattern) {
530
- const result = [];
531
- if ("package.namespace.name" === pattern)
532
- result.push(...getPackageClause(a), ...getNamespacesPathFor(a).map(it => it.name));
533
- else if ("namespace.name" === pattern)
534
- result.push(...getNamespacesPathFor(a).map(it => it.name));
535
- if (isNamedNode(a) && a.name)
536
- result.push(a.name);
537
- return result.join(".");
538
- }
539
- export function getFQName(a) {
540
- return getQualifiedName(a, "package.namespace.name");
541
- }
542
- export function createVersion(value, nodeInitializer) {
543
- return Object.assign(Object.assign({ kind: IDLKind.Version, value, name: "version" }, nodeInitializer), { _idlNodeBrand: innerIdlSymbol, _idlEntryBrand: innerIdlSymbol, _idlNamedNodeBrand: innerIdlSymbol });
544
- }
545
- export function fetchNamespaceFrom(pointOfView) {
546
- let node = pointOfView;
547
- while (node) {
548
- if (isNamespace(node))
549
- return node;
550
- node = node.parent;
551
- }
552
- return undefined;
553
- }
554
- export function createReferenceType(nameOrSource, typeArguments, nodeInitializer) {
555
- let name;
556
- if (typeof nameOrSource === 'string') {
557
- name = nameOrSource;
558
- }
559
- else {
560
- name = getFQName(nameOrSource);
561
- }
562
- return Object.assign(Object.assign({ kind: IDLKind.ReferenceType, name,
563
- typeArguments }, nodeInitializer), { _idlNodeBrand: innerIdlSymbol, _idlTypeBrand: innerIdlSymbol, _idlNamedNodeBrand: innerIdlSymbol });
564
- }
565
- export function createUnspecifiedGenericType(name, typeArguments, nodeInitializer) {
566
- return Object.assign(Object.assign({ kind: IDLKind.UnspecifiedGenericType, name,
567
- typeArguments }, nodeInitializer), { _idlNodeBrand: innerIdlSymbol, _idlTypeBrand: innerIdlSymbol, _idlNamedNodeBrand: innerIdlSymbol });
568
- }
569
- export function entityToType(entity) {
570
- if (isType(entity)) {
571
- return entity;
572
- }
573
- else if (isEntry(entity)) {
574
- return createReferenceType(entity);
575
- }
576
- else {
577
- throw new Error(`Expected to have IDLType or IDLEntry, got ${entity}`);
578
- }
579
- }
580
- export function createContainerType(container, element, nodeInitializer) {
581
- return Object.assign(Object.assign({ kind: IDLKind.ContainerType, containerKind: container, elementType: element }, nodeInitializer), { _idlNodeBrand: innerIdlSymbol, _idlTypeBrand: innerIdlSymbol });
582
- }
583
- export function createUnionType(types, name, nodeInitializer) {
584
- if (types.length < 2)
585
- throw new Error("IDLUnionType should contain at least 2 types");
586
- return Object.assign(Object.assign({ kind: IDLKind.UnionType, name: name !== null && name !== void 0 ? name : "Union_" + types.map(it => generateSyntheticIdlNodeName(it)).join("_"), types: types }, nodeInitializer), { _idlNodeBrand: innerIdlSymbol, _idlTypeBrand: innerIdlSymbol, _idlNamedNodeBrand: innerIdlSymbol });
587
- }
588
- export function createFile(entries, fileName, packageClause = [], nodeInitializer) {
589
- return Object.assign(Object.assign({ kind: IDLKind.File, packageClause, entries: entries, fileName }, nodeInitializer), { _idlNodeBrand: innerIdlSymbol });
590
- }
591
- export function createImport(clause, name, nodeInitializer) {
592
- return Object.assign(Object.assign({ kind: IDLKind.Import, name: name !== null && name !== void 0 ? name : "", clause }, nodeInitializer), { _idlNodeBrand: innerIdlSymbol, _idlEntryBrand: innerIdlSymbol, _idlNamedNodeBrand: innerIdlSymbol });
593
- }
594
- export function createEnum(name, elements, nodeInitializer) {
595
- return Object.assign(Object.assign({ kind: IDLKind.Enum, name: name, elements: elements }, nodeInitializer), { _idlNodeBrand: innerIdlSymbol, _idlEntryBrand: innerIdlSymbol, _idlNamedNodeBrand: innerIdlSymbol });
596
- }
597
- export function createEnumMember(name, parent, type, initializer, nodeInitializer = {}) {
598
- return Object.assign(Object.assign({ kind: IDLKind.EnumMember, name: name, parent,
599
- type,
600
- initializer }, nodeInitializer), { _idlNodeBrand: innerIdlSymbol, _idlEntryBrand: innerIdlSymbol, _idlNamedNodeBrand: innerIdlSymbol });
601
- }
602
- export function createInterface(name, subkind, inheritance = [], constructors = [], constants = [], properties = [], methods = [], callables = [], typeParameters = [], nodeInitializer = {}) {
603
- return Object.assign(Object.assign({ kind: IDLKind.Interface, name,
604
- subkind,
605
- typeParameters,
606
- inheritance,
607
- constructors,
608
- constants,
609
- properties,
610
- methods,
611
- callables }, nodeInitializer), { _idlNodeBrand: innerIdlSymbol, _idlEntryBrand: innerIdlSymbol, _idlNamedNodeBrand: innerIdlSymbol });
612
- }
613
- export function createProperty(name, type, isReadonly = false, isStatic = false, isOptional = false, nodeInitializer = {}) {
614
- return Object.assign(Object.assign({ name, kind: IDLKind.Property, type,
615
- isReadonly,
616
- isStatic,
617
- isOptional }, nodeInitializer), { _idlNodeBrand: innerIdlSymbol, _idlEntryBrand: innerIdlSymbol, _idlNamedNodeBrand: innerIdlSymbol });
618
- }
619
- export function createParameter(name, type, isOptional = false, isVariadic = false, nodeInitializer = {}) {
620
- return Object.assign(Object.assign({ kind: IDLKind.Parameter, name: name, type: type, isOptional,
621
- isVariadic }, nodeInitializer), { _idlNodeBrand: innerIdlSymbol, _idlEntryBrand: innerIdlSymbol, _idlNamedNodeBrand: innerIdlSymbol });
622
- }
623
- export function createMethod(name, parameters, returnType, methodInitializer = {
624
- isAsync: false,
625
- isStatic: false,
626
- isOptional: false,
627
- isFree: false,
628
- }, nodeInitializer = {}, typeParameters = []) {
629
- return Object.assign(Object.assign(Object.assign({ kind: IDLKind.Method, name,
630
- parameters,
631
- returnType,
632
- typeParameters }, methodInitializer), nodeInitializer), { _idlNodeBrand: innerIdlSymbol, _idlEntryBrand: innerIdlSymbol, _idlNamedNodeBrand: innerIdlSymbol });
633
- }
634
- export function createCallable(
635
- // TODO name here seems useless
636
- name, parameters, returnType, callableInitializer, nodeInitializer, typeParameters = []) {
637
- return Object.assign(Object.assign(Object.assign({ kind: IDLKind.Callable, name,
638
- parameters,
639
- returnType }, callableInitializer), nodeInitializer), { _idlNodeBrand: innerIdlSymbol, _idlEntryBrand: innerIdlSymbol, _idlNamedNodeBrand: innerIdlSymbol });
640
- }
641
- export function createConstructor(parameters, returnType, nodeInitializer = {}) {
642
- return Object.assign(Object.assign({ kind: IDLKind.Constructor, name: "$CONSTRUCTOR%", parameters,
643
- returnType }, nodeInitializer), { _idlNodeBrand: innerIdlSymbol, _idlEntryBrand: innerIdlSymbol, _idlNamedNodeBrand: innerIdlSymbol });
644
- }
645
- export function createCallback(name, parameters, returnType, nodeInitializer = {}, typeParameters = []) {
646
- return Object.assign(Object.assign({ kind: IDLKind.Callback, name, parameters, returnType, typeParameters }, nodeInitializer), { _idlNodeBrand: innerIdlSymbol, _idlEntryBrand: innerIdlSymbol, _idlNamedNodeBrand: innerIdlSymbol });
647
- }
648
- export function createTypeParameterReference(name, nodeInitializer) {
649
- return Object.assign(Object.assign({ kind: IDLKind.TypeParameterType, name: name }, nodeInitializer), { _idlNodeBrand: innerIdlSymbol, _idlTypeBrand: innerIdlSymbol, _idlNamedNodeBrand: innerIdlSymbol });
650
- }
651
- export function createTypedef(name, type, typeParameters = [], nodeInitializer = {}) {
652
- return Object.assign(Object.assign({ name, type, typeParameters, kind: IDLKind.Typedef }, nodeInitializer), { _idlNodeBrand: innerIdlSymbol, _idlEntryBrand: innerIdlSymbol, _idlNamedNodeBrand: innerIdlSymbol });
653
- }
654
- export function createConstant(name, type, value, nodeInitializer = {}) {
655
- return Object.assign(Object.assign({ kind: IDLKind.Const, name,
656
- type,
657
- value }, nodeInitializer), { _idlNodeBrand: innerIdlSymbol, _idlEntryBrand: innerIdlSymbol, _idlNamedNodeBrand: innerIdlSymbol });
658
- }
659
- export function clone(node) {
660
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0;
661
- const make = (newnode) => {
662
- if (node.nodeLocation) {
663
- newnode.nodeLocation = node.nodeLocation;
664
- }
665
- if (node.nameLocation) {
666
- newnode.nameLocation = node.nameLocation;
667
- }
668
- if (node.valueLocation) {
669
- newnode.valueLocation = node.valueLocation;
670
- }
671
- return newnode;
672
- };
673
- const get = (node) => node;
674
- switch (node.kind) {
675
- case IDLKind.Interface: {
676
- const entry = get(node);
677
- return make(createInterface(entry.name, entry.subkind, (_a = entry.inheritance) === null || _a === void 0 ? void 0 : _a.map(clone), (_b = entry.constructors) === null || _b === void 0 ? void 0 : _b.map(clone), entry.constants.map(clone), entry.properties.map(clone), entry.methods.map(clone), entry.callables.map(clone), (_c = entry.typeParameters) === null || _c === void 0 ? void 0 : _c.map(it => it), {
678
- documentation: node.documentation,
679
- extendedAttributes: (_d = node.extendedAttributes) === null || _d === void 0 ? void 0 : _d.slice(),
680
- fileName: node.fileName
681
- }));
682
- }
683
- case IDLKind.Import: {
684
- const entry = get(node);
685
- return make(createImport(entry.clause, entry.name, {
686
- documentation: entry.documentation,
687
- extendedAttributes: (_e = entry.extendedAttributes) === null || _e === void 0 ? void 0 : _e.slice(),
688
- fileName: entry.fileName
689
- }));
690
- }
691
- case IDLKind.Callback: {
692
- const entry = get(node);
693
- return make(createCallback(entry.name, entry.parameters.map(clone), clone(entry.returnType), {
694
- documentation: entry.documentation,
695
- extendedAttributes: (_f = entry.extendedAttributes) === null || _f === void 0 ? void 0 : _f.slice(),
696
- fileName: entry.fileName
697
- }, entry.typeParameters));
698
- }
699
- case IDLKind.Const: {
700
- const entry = get(node);
701
- return make(createConstant(entry.name, clone(entry.type), entry.value, {
702
- documentation: entry.documentation,
703
- extendedAttributes: (_g = entry.extendedAttributes) === null || _g === void 0 ? void 0 : _g.slice(),
704
- fileName: entry.fileName
705
- }));
706
- }
707
- case IDLKind.Property: {
708
- const entry = get(node);
709
- return make(createProperty(entry.name, clone(entry.type), entry.isReadonly, entry.isStatic, entry.isOptional, {
710
- documentation: entry.documentation,
711
- extendedAttributes: (_h = entry.extendedAttributes) === null || _h === void 0 ? void 0 : _h.slice(),
712
- fileName: entry.fileName
713
- }));
714
- }
715
- case IDLKind.Parameter: {
716
- const entry = get(node);
717
- return make(createParameter(entry.name, clone(entry.type), entry.isOptional, entry.isVariadic, {
718
- documentation: entry.documentation,
719
- extendedAttributes: (_j = entry.extendedAttributes) === null || _j === void 0 ? void 0 : _j.slice(),
720
- fileName: entry.fileName
721
- }));
722
- }
723
- case IDLKind.Method: {
724
- const entry = get(node);
725
- return make(createMethod(entry.name, entry.parameters.map(clone), clone(entry.returnType), {
726
- isAsync: entry.isAsync,
727
- isFree: entry.isFree,
728
- isOptional: entry.isOptional,
729
- isStatic: entry.isStatic
730
- }, {
731
- documentation: entry.documentation,
732
- extendedAttributes: (_k = entry.extendedAttributes) === null || _k === void 0 ? void 0 : _k.slice(),
733
- fileName: entry.fileName
734
- }, entry.typeParameters));
735
- }
736
- case IDLKind.Callable: {
737
- const entry = get(node);
738
- return make(createCallable(entry.name, entry.parameters.map(clone), clone(entry.returnType), {
739
- isAsync: entry.isAsync,
740
- isStatic: entry.isStatic
741
- }, {
742
- documentation: entry.documentation,
743
- extendedAttributes: (_l = entry.extendedAttributes) === null || _l === void 0 ? void 0 : _l.slice(),
744
- fileName: entry.documentation
745
- }, entry.typeParameters));
746
- }
747
- case IDLKind.Constructor: {
748
- const entry = get(node);
749
- return make(createConstructor(entry.parameters.map(clone), entry.returnType ? clone(entry.returnType) : undefined, {
750
- documentation: entry.documentation,
751
- extendedAttributes: (_m = entry.extendedAttributes) === null || _m === void 0 ? void 0 : _m.slice(),
752
- fileName: entry.fileName
753
- }));
754
- }
755
- case IDLKind.Enum: {
756
- const entry = get(node);
757
- const cloned = createEnum(entry.name, entry.elements.map(clone), {
758
- documentation: entry.documentation,
759
- extendedAttributes: (_o = entry.extendedAttributes) === null || _o === void 0 ? void 0 : _o.slice(),
760
- fileName: entry.fileName
761
- });
762
- cloned.elements.forEach(it => {
763
- it.parent = cloned;
764
- });
765
- return make(cloned);
766
- }
767
- case IDLKind.EnumMember: {
768
- const entry = get(node);
769
- return make(createEnumMember(entry.name, entry.parent, clone(entry.type), entry.initializer, {
770
- documentation: entry.documentation,
771
- extendedAttributes: (_p = entry.extendedAttributes) === null || _p === void 0 ? void 0 : _p.slice(),
772
- fileName: entry.fileName
773
- }));
774
- }
775
- case IDLKind.Typedef: {
776
- const entry = get(node);
777
- return make(createTypedef(entry.name, clone(entry.type), entry.typeParameters, {
778
- documentation: entry.documentation,
779
- extendedAttributes: (_q = entry.extendedAttributes) === null || _q === void 0 ? void 0 : _q.slice(),
780
- fileName: entry.fileName
781
- }));
782
- }
783
- case IDLKind.PrimitiveType: {
784
- return node;
785
- }
786
- case IDLKind.ContainerType: {
787
- const type = get(node);
788
- return make(createContainerType(type.containerKind, type.elementType.map(clone), {
789
- documentation: type.documentation,
790
- extendedAttributes: (_r = type.extendedAttributes) === null || _r === void 0 ? void 0 : _r.slice(),
791
- fileName: type.fileName
792
- }));
793
- }
794
- case IDLKind.UnspecifiedGenericType: {
795
- const type = get(node);
796
- return make(createUnspecifiedGenericType(type.name, type.typeArguments.map(clone), {
797
- documentation: type.documentation,
798
- extendedAttributes: (_s = type.extendedAttributes) === null || _s === void 0 ? void 0 : _s.slice(),
799
- fileName: type.fileName
800
- }));
801
- }
802
- case IDLKind.ReferenceType: {
803
- const type = get(node);
804
- return make(createReferenceType(type.name, (_t = type.typeArguments) === null || _t === void 0 ? void 0 : _t.map(clone), {
805
- documentation: type.documentation,
806
- extendedAttributes: (_u = type.extendedAttributes) === null || _u === void 0 ? void 0 : _u.slice(),
807
- fileName: type.fileName
808
- }));
809
- }
810
- case IDLKind.UnionType: {
811
- const type = get(node);
812
- return make(createUnionType(type.types.map(clone), type.name, {
813
- documentation: type.documentation,
814
- extendedAttributes: (_v = type.extendedAttributes) === null || _v === void 0 ? void 0 : _v.slice(),
815
- fileName: type.fileName
816
- }));
817
- }
818
- case IDLKind.TypeParameterType: {
819
- const type = get(node);
820
- return make(createTypeParameterReference(type.name, {
821
- documentation: type.documentation,
822
- extendedAttributes: (_w = type.extendedAttributes) === null || _w === void 0 ? void 0 : _w.slice(),
823
- fileName: type.fileName
824
- }));
825
- }
826
- case IDLKind.OptionalType: {
827
- const type = get(node);
828
- return make(createOptionalType(clone(type.type), {
829
- documentation: type.documentation,
830
- extendedAttributes: (_x = type.extendedAttributes) === null || _x === void 0 ? void 0 : _x.slice(),
831
- fileName: type.fileName
832
- }));
833
- }
834
- case IDLKind.Version: {
835
- const entry = get(node);
836
- return make(createVersion(entry.value, {
837
- documentation: entry.documentation,
838
- extendedAttributes: (_y = entry.extendedAttributes) === null || _y === void 0 ? void 0 : _y.slice(),
839
- fileName: entry.fileName
840
- }));
841
- }
842
- case IDLKind.Namespace: {
843
- const ns = get(node);
844
- return make(createNamespace(ns.name, ns.members.map(clone), {
845
- documentation: ns.documentation,
846
- extendedAttributes: (_z = ns.extendedAttributes) === null || _z === void 0 ? void 0 : _z.slice(),
847
- fileName: ns.fileName
848
- }));
849
- }
850
- case IDLKind.File: {
851
- const file = get(node);
852
- return make(createFile(file.entries.map(clone), file.fileName, file.packageClause, {
853
- documentation: file.documentation,
854
- extendedAttributes: (_0 = file.extendedAttributes) === null || _0 === void 0 ? void 0 : _0.slice(),
855
- fileName: file.fileName
856
- }));
857
- }
858
- }
859
- }
860
- export function hasTypeParameters(entry) {
861
- let foundTypeParameter = false;
862
- forEachChild(entry, n => {
863
- if (isTypeParameterType(n)) {
864
- foundTypeParameter = true;
865
- }
866
- });
867
- return foundTypeParameter;
868
- }
869
- export function escapeIDLKeyword(name) {
870
- return name + (IDLKeywords.has(name) ? "_" : "");
871
- }
872
- export function unescapeKeyword(name) {
873
- if (name.endsWith("_")) {
874
- const unwrapped = name.slice(0, -1);
875
- if (IDLKeywords.has(unwrapped))
876
- return unwrapped;
877
- }
878
- return name;
879
- }
880
- const printedIndentInc = "[[indent-inc]]";
881
- const printedIndentDec = "[[indent-dec]]";
882
- export function printType(type, options) {
883
- var _a;
884
- if (!type)
885
- throw new Error("Missing type");
886
- if (isInterface(type))
887
- return type.name;
888
- if (isOptionalType(type)) {
889
- if (hasExtAttribute(type, IDLExtendedAttributes.UnionOnlyNull))
890
- return `(${printType(type.type)} or ${IDLNullTypeName})`;
891
- else if (hasExtAttribute(type, IDLExtendedAttributes.UnionWithNull))
892
- return `(${printType(type.type)} or ${IDLUndefinedType.name} or${IDLNullTypeName})`;
893
- else
894
- return `(${printType(type.type)} or ${IDLUndefinedType.name})`;
895
- }
896
- if (isPrimitiveType(type))
897
- return type.name;
898
- if (isContainerType(type))
899
- return `${type.containerKind}<${type.elementType.map(it => printType(it)).join(", ")}>`;
900
- if (isReferenceType(type)) {
901
- if (type.typeArguments)
902
- updateExtAttribute(type, IDLExtendedAttributes.TypeArguments, type.typeArguments.map(it => printType(it)).join(","));
903
- if (!((_a = type.extendedAttributes) === null || _a === void 0 ? void 0 : _a.length))
904
- return type.name;
905
- let res = `[${quoteAttributeValues(type.extendedAttributes)}] ${type.name}`;
906
- if (options === null || options === void 0 ? void 0 : options.bracketsAroundReferenceTypeWithExtAttrs)
907
- return `(${res})`;
908
- return res;
909
- }
910
- if (isUnspecifiedGenericType(type))
911
- return `${type.name}<${type.typeArguments.map(it => printType(it)).join(", ")}>`;
912
- if (isUnionType(type))
913
- return `(${type.types.map(it => printType(it)).join(" or ")})`;
914
- if (isTypeParameterType(type))
915
- return type.name;
916
- throw new Error(`Cannot map type: ${IDLKind[type.kind]}`);
917
- }
918
- export function printReturnType(type) {
919
- return printType(type, { bracketsAroundReferenceTypeWithExtAttrs: true });
920
- }
921
- export function printParameters(parameters) {
922
- var _a, _b;
923
- 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 : "";
924
- }
925
- export function printConstructor(idl) {
926
- return [
927
- ...printExtendedAttributes(idl, 1),
928
- `constructor(${printParameters(idl.parameters)});`
929
- ];
930
- }
931
- export function nameWithType(idl, isVariadic = false, isOptional = false) {
932
- const type = printType(idl.type);
933
- const variadic = isVariadic ? "..." : "";
934
- const optional = isOptional ? "optional " : "";
935
- return `${optional}${type}${variadic} ${escapeIDLKeyword(idl.name)}`;
936
- }
937
- export function printConstant(idl) {
938
- return [
939
- ...printExtendedAttributes(idl, 1),
940
- `const ${nameWithType(idl)} = ${idl.value};`
941
- ];
942
- }
943
- export function printProperty(idl) {
944
- const staticMod = idl.isStatic ? "static " : "";
945
- const readonlyMod = idl.isReadonly ? "readonly " : "";
946
- return [
947
- ...printExtendedAttributes(idl, 1),
948
- `${staticMod}${readonlyMod}attribute ${nameWithType(idl)};`
949
- ];
950
- }
951
- export function printExtendedAttributes(idl, indentLevel) {
952
- let typeParameters;
953
- let typeArguments;
954
- switch (idl.kind) {
955
- case IDLKind.Interface:
956
- typeParameters = idl.typeParameters;
957
- break;
958
- case IDLKind.Callback:
959
- case IDLKind.Method:
960
- case IDLKind.Callable:
961
- case IDLKind.Constructor:
962
- typeParameters = idl.typeParameters;
963
- break;
964
- case IDLKind.Typedef:
965
- typeParameters = idl.typeParameters;
966
- break;
967
- case IDLKind.ReferenceType:
968
- typeArguments = idl.typeArguments;
969
- break;
970
- case IDLKind.UnspecifiedGenericType:
971
- typeArguments = idl.typeArguments;
972
- break;
973
- }
974
- const attributes = Array.from(idl.extendedAttributes || []);
975
- if ((typeParameters === null || typeParameters === void 0 ? void 0 : typeParameters.length) && !attributes.find(x => x.name === IDLExtendedAttributes.TypeParameters))
976
- attributes.push({ name: IDLExtendedAttributes.TypeParameters, value: typeParameters.join(",") });
977
- if ((typeArguments === null || typeArguments === void 0 ? void 0 : typeArguments.length) && !attributes.find(x => x.name === IDLExtendedAttributes.TypeArguments))
978
- attributes.push({ name: IDLExtendedAttributes.TypeArguments, value: typeArguments.map(it => printType(it)).join(",") });
979
- if (idl.documentation) {
980
- let docs = {
981
- name: IDLExtendedAttributes.Documentation,
982
- value: idl.documentation
983
- };
984
- attributes.unshift(docs);
985
- }
986
- // Deduplicate
987
- const names = new Set();
988
- const actualAttributes = [];
989
- for (const attr of attributes) {
990
- if (names.has(attr.name)) {
991
- continue;
992
- }
993
- names.add(attr.name);
994
- actualAttributes.push(attr);
995
- }
996
- if (actualAttributes.length == 0) {
997
- return [];
998
- }
999
- const attrSpec = quoteAttributeValues(actualAttributes);
1000
- return attrSpec ? [`[${attrSpec}]`] : [];
1001
- }
1002
- export const attributesToQuote = new Set([
1003
- IDLExtendedAttributes.Documentation,
1004
- IDLExtendedAttributes.DtsName,
1005
- IDLExtendedAttributes.DtsTag,
1006
- IDLExtendedAttributes.Import,
1007
- IDLExtendedAttributes.Interfaces,
1008
- IDLExtendedAttributes.TraceKey,
1009
- IDLExtendedAttributes.TypeArguments,
1010
- IDLExtendedAttributes.TypeParameters,
1011
- IDLExtendedAttributes.TypeParametersDefaults,
1012
- ]);
1013
- function quoteAttributeValues(attributes) {
1014
- return attributes === null || attributes === void 0 ? void 0 : attributes.map(it => {
1015
- let attr = it.name;
1016
- if (it.value) {
1017
- let value = it.value;
1018
- if (value.includes('"') && !value.includes("'"))
1019
- value = value.replaceAll('"', "'");
1020
- value = value.replaceAll('\\', '\\\\').replaceAll('"', '\\"');
1021
- attr += `=${attributesToQuote.has(it.name) ? `"${value}"` : it.value}`;
1022
- }
1023
- return attr;
1024
- }).join(", ");
1025
- }
1026
- export function printFunction(idl) {
1027
- var _a;
1028
- if ((_a = idl.name) === null || _a === void 0 ? void 0 : _a.startsWith("__")) {
1029
- console.log(`Ignore ${idl.name}`);
1030
- return [];
1031
- }
1032
- return [
1033
- ...printExtendedAttributes(idl, 1),
1034
- `${idl.isAsync ? "async " : ""}${printReturnType(idl.returnType)} ${idl.name}(${printParameters(idl.parameters)});`
1035
- ];
1036
- }
1037
- export function printMethod(idl) {
1038
- var _a;
1039
- if ((_a = idl.name) === null || _a === void 0 ? void 0 : _a.startsWith("__")) {
1040
- console.log(`Ignore ${idl.name}`);
1041
- return [];
1042
- }
1043
- return [
1044
- ...printExtendedAttributes(idl, 1),
1045
- `${idl.isStatic ? "static " : ""}${idl.isAsync ? "async " : ""}${printReturnType(idl.returnType)} ${idl.name}(${printParameters(idl.parameters)});`
1046
- ];
1047
- }
1048
- export function printPackage(idl) {
1049
- const effectiveClause = idl.packageClause.filter(it => !!it);
1050
- if (!effectiveClause.length)
1051
- return [];
1052
- return [
1053
- `package ${effectiveClause.join(".")};`
1054
- ];
1055
- }
1056
- export function printImport(idl) {
1057
- const effectiveClause = idl.clause.filter(it => !!it);
1058
- return [
1059
- `import ${effectiveClause.join(".") || "NULL_IMPORT"}${idl.name ? " as " : ""}${idl.name};`
1060
- ];
1061
- }
1062
- export function printNamespace(idl) {
1063
- return [
1064
- ...printExtendedAttributes(idl, 0),
1065
- `namespace ${idl.name} {`,
1066
- printedIndentInc,
1067
- ...idl.members.map(member => printIDL(member)).flat(),
1068
- printedIndentDec,
1069
- "};"
1070
- ];
1071
- }
1072
- export function printCallback(idl) {
1073
- return [
1074
- ...printExtendedAttributes(idl, 0),
1075
- `callback ${idl.name} = ${printReturnType(idl.returnType)} (${printParameters(idl.parameters)});`
1076
- ];
1077
- }
1078
- export function printScoped(idl) {
1079
- if (idl.kind == IDLKind.Callback)
1080
- return printCallback(idl);
1081
- if (idl.kind === IDLKind.Interface)
1082
- return printInterface(idl);
1083
- throw new Error(`Unexpected scoped: ${idl.kind} ${idl.name}`);
1084
- }
1085
- function printInterfaceInherit(idl) {
1086
- if (idl.inheritance.length === 0) {
1087
- return "";
1088
- }
1089
- const types = idl.inheritance.map(type => printType(type));
1090
- return ": " + types.join(', ');
1091
- }
1092
- export function printInterface(idl) {
1093
- return [
1094
- ...printExtendedAttributes(idl, 0),
1095
- `interface ${idl.name}${printInterfaceInherit(idl)} {`,
1096
- // TODO: type system hack!
1097
- ]
1098
- .concat(printedIndentInc)
1099
- .concat(idl.constructors.map(printConstructor).flat())
1100
- .concat(idl.constants.map(printConstant).flat())
1101
- .concat(idl.properties.map(printProperty).flat())
1102
- .concat(idl.methods.map(printMethod).flat())
1103
- .concat(idl.callables.map(printFunction).flat())
1104
- .concat(printedIndentDec)
1105
- .concat(["};"]);
1106
- }
1107
- export function printEnumMember(idl) {
1108
- const type = printType(idl.type);
1109
- const initializer = idl.initializer === undefined
1110
- ? ''
1111
- : ' = ' + (type === IDLStringType.name
1112
- ? `"${String(idl.initializer).replaceAll('"', "'")}"`
1113
- : idl.initializer);
1114
- return [
1115
- idl.documentation,
1116
- ...printExtendedAttributes(idl, 0),
1117
- `${type} ${idl.name}${initializer};`
1118
- ];
1119
- }
1120
- export function printEnum(idl, skipInitializers) {
1121
- if (skipInitializers) {
1122
- return [
1123
- idl.documentation,
1124
- ...printExtendedAttributes(idl, 0),
1125
- `enum ${idl.name} {`,
1126
- printedIndentInc,
1127
- ...idl.elements.map(it => `${it.name} ${(it.initializer !== undefined ? " /* " + it.initializer + " */" : "")}`),
1128
- printedIndentDec,
1129
- "};"
1130
- ];
1131
- }
1132
- else {
1133
- return [
1134
- idl.documentation,
1135
- ...printExtendedAttributes(idl, 0),
1136
- `dictionary ${idl.name} {`,
1137
- printedIndentInc,
1138
- ...idl.elements.map(printEnumMember),
1139
- printedIndentDec,
1140
- "};"
1141
- ].flat();
1142
- }
1143
- }
1144
- export function printTypedef(idl) {
1145
- return [
1146
- idl.documentation,
1147
- ...printExtendedAttributes(idl, 0),
1148
- `typedef ${printType(idl.type)} ${idl.name};`
1149
- ];
1150
- }
1151
- // TODO: use IndentedPrinter instead!
1152
- export function printIDL(idl, options) {
1153
- var _a;
1154
- if (idl.kind == IDLKind.Interface)
1155
- return printInterface(idl);
1156
- if (idl.kind == IDLKind.Enum)
1157
- return printEnum(idl, (_a = options === null || options === void 0 ? void 0 : options.disableEnumInitializers) !== null && _a !== void 0 ? _a : false);
1158
- if (idl.kind == IDLKind.Typedef)
1159
- return printTypedef(idl);
1160
- if (idl.kind == IDLKind.Callback)
1161
- return printCallback(idl);
1162
- if (idl.kind == IDLKind.Import)
1163
- return printImport(idl);
1164
- if (idl.kind == IDLKind.Namespace)
1165
- return printNamespace(idl);
1166
- if (idl.kind == IDLKind.Method)
1167
- return printMethod(idl);
1168
- if (idl.kind == IDLKind.Const)
1169
- return printConstant(idl);
1170
- if (options === null || options === void 0 ? void 0 : options.allowUnknownKinds) {
1171
- return [`${IDLKind[idl.kind]} ${"name" in idl ? idl.name : ""}`];
1172
- }
1173
- else {
1174
- throw new Error(`unexpected kind: ${idl.kind}`);
1175
- }
1176
- }
1177
- export function toIDLString(file, options) {
1178
- let indent = 0;
1179
- const generated = printPackage(file);
1180
- return generated.concat(file.entries
1181
- .map(it => printIDL(it, options))
1182
- .flat()
1183
- .filter(isDefined)
1184
- .filter(it => it.length > 0)
1185
- .map(it => {
1186
- if (it === printedIndentInc) {
1187
- ++indent;
1188
- return undefined;
1189
- }
1190
- else if (it === printedIndentDec) {
1191
- --indent;
1192
- return undefined;
1193
- }
1194
- else
1195
- return indentedBy(it, indent);
1196
- })
1197
- .filter(isDefined)).join("\n");
1198
- }
1199
- // throws validation error
1200
- export function verifyIDLString(source) {
1201
- webidl2.validate(webidl2.parse(source));
1202
- return true;
1203
- }
1204
- export function hasExtAttribute(node, attribute) {
1205
- var _a;
1206
- return ((_a = node.extendedAttributes) === null || _a === void 0 ? void 0 : _a.find((it) => it.name == attribute)) != undefined;
1207
- }
1208
- export function getExtAttribute(node, name) {
1209
- var _a, _b;
1210
- 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;
1211
- }
1212
- export function removeExtAttribute(node, name) {
1213
- if (node.extendedAttributes) {
1214
- node.extendedAttributes = node.extendedAttributes.filter(it => it.name !== name);
1215
- }
1216
- }
1217
- export function updateExtAttribute(node, name, value) {
1218
- var _a;
1219
- removeExtAttribute(node, name);
1220
- (_a = node.extendedAttributes) !== null && _a !== void 0 ? _a : (node.extendedAttributes = []);
1221
- node.extendedAttributes.push({ name, value });
1222
- }
1223
- export function getVerbatimDts(node) {
1224
- let value = getExtAttribute(node, IDLExtendedAttributes.VerbatimDts);
1225
- return value ? value.substring(1, value.length - 1) : undefined;
1226
- }
1227
- export const IDLContainerUtils = {
1228
- isRecord: (x) => isContainerType(x) && x.containerKind === 'record',
1229
- isSequence: (x) => isContainerType(x) && x.containerKind === 'sequence',
1230
- isPromise: (x) => isContainerType(x) && x.containerKind === 'Promise'
1231
- };
1232
- /**
1233
- * @returns tuple of qualifier name and real type name
1234
- */
1235
- export function decomposeQualifiedName(type) {
1236
- const typeName = type.name;
1237
- const lastDot = typeName.lastIndexOf(".");
1238
- if (lastDot >= 0) {
1239
- const qualifier = typeName.slice(0, lastDot);
1240
- const realTypeName = typeName.slice(lastDot + 1);
1241
- return [qualifier, realTypeName];
1242
- }
1243
- return [undefined, typeName];
1244
- }
1245
- export function qualifiedNameStartsWith(node, template) {
1246
- const name = Array.isArray(node) ? node : getFQName(node).split(".");
1247
- if (name.length < template.length)
1248
- return false;
1249
- for (let i = 0; i < template.length; i++) {
1250
- if (name[i] != template[i])
1251
- return false;
1252
- }
1253
- return true;
1254
- }
1255
- export function maybeUnwrapOptionalType(type) {
1256
- if (isOptionalType(type)) {
1257
- return type.type;
1258
- }
1259
- return type;
1260
- }
1261
- export function maybeOptional(type, optional = false) {
1262
- if (optional) {
1263
- return createOptionalType(type);
1264
- }
1265
- return type;
1266
- }
1267
- export const DebugUtils = {
1268
- debugPrintType: (type) => {
1269
- if (isContainerType(type)) {
1270
- return `[IDLType, name: '${printType(type)}', kind: '${IDLKind[type.kind]}', elements: [${type.elementType.map(DebugUtils.debugPrintType).join(', ')}]]`;
1271
- }
1272
- return `[IDLType, name: '${printType(type)}', kind: '${IDLKind[type.kind]}']`;
1273
- },
1274
- };
1275
- export function forEachFunction(node, cb) {
1276
- forEachChild(node, child => {
1277
- if (child.kind === IDLKind.Method || child.kind === IDLKind.Callable)
1278
- cb(child);
1279
- });
1280
- }
1281
- export function asPromise(type) {
1282
- if (!type)
1283
- return undefined;
1284
- if (!isContainerType(type))
1285
- return undefined;
1286
- const container = type;
1287
- if (!IDLContainerUtils.isPromise(container))
1288
- return undefined;
1289
- return container;
1290
- }
1291
- export function transformMethodsAsync2ReturnPromise(entry) {
1292
- forEachFunction(entry, function_ => {
1293
- var _a;
1294
- if (function_.isAsync) {
1295
- function_.isAsync = false;
1296
- if (!asPromise(function_.returnType))
1297
- function_.returnType = createContainerType("Promise", [(_a = function_.returnType) !== null && _a !== void 0 ? _a : IDLVoidType]);
1298
- }
1299
- });
1300
- }
1301
- export function transformMethodsReturnPromise2Async(entry) {
1302
- forEachFunction(entry, function_ => {
1303
- const promise = asPromise(function_.returnType);
1304
- if (promise) {
1305
- function_.returnType = promise.elementType[0];
1306
- function_.isAsync = true;
1307
- }
1308
- });
1309
- }
1310
- export function fetchSignatureTags(node) {
1311
- if (!node.extendedAttributes)
1312
- return [];
1313
- return node.extendedAttributes
1314
- .filter((ea) => ea.name === IDLExtendedAttributes.DtsTag)
1315
- .map((ea) => {
1316
- if (!ea.value)
1317
- throw new Error('Empty DtsTag is not allowed');
1318
- let indexNameValue = ea.value.split('|');
1319
- if (indexNameValue.length === 1) {
1320
- return {
1321
- index: 0,
1322
- name: 'type',
1323
- value: indexNameValue[0],
1324
- };
1325
- }
1326
- if (indexNameValue.length !== 3)
1327
- throw new Error(`Malformed DtsTag: "${ea.value}"`);
1328
- return {
1329
- index: Number(indexNameValue[0]),
1330
- name: indexNameValue[1],
1331
- value: indexNameValue[2],
1332
- };
1333
- })
1334
- .sort((a, b) => a.index - b.index);
1335
- }
1336
- export function mixMethodParametersAndTags(node) {
1337
- let mix = node.parameters.slice(0);
1338
- for (const tag of fetchSignatureTags(node))
1339
- mix.splice(tag.index, 0, tag);
1340
- return mix;
1341
- }
1342
- export function isHandwritten(decl) {
1343
- return hasExtAttribute(decl, IDLExtendedAttributes.HandWrittenImplementation);
1344
- }
1345
- export function isStringEnum(decl) {
1346
- return decl.elements.some(e => e.type === IDLStringType);
1347
- }
1348
- export function linearizeNamespaceMembers(entries) {
1349
- const linearized = [];
1350
- for (const entry of entries) {
1351
- linearized.push(entry);
1352
- if (isNamespace(entry))
1353
- linearized.push(...linearizeNamespaceMembers(entry.members));
1354
- }
1355
- return linearized;
1356
- }
1357
- export function extremumOfOrdinals(enumEntry) {
1358
- let low = 0;
1359
- let high = 0;
1360
- enumEntry.elements.forEach((member, index) => {
1361
- let value = index;
1362
- if ((typeof member.initializer === 'number') && !isStringEnum(enumEntry)) {
1363
- value = member.initializer;
1364
- }
1365
- if (low > value)
1366
- low = value;
1367
- if (high < value)
1368
- high = value;
1369
- });
1370
- return { low, high };
1371
- }
1372
- export const PACKAGE_IDLIZE_INTERNAL = "idlize.internal";
1373
- export function isInIdlize(entry) {
1374
- return isInPackage(entry, "idlize");
1375
- }
1376
- export function isInIdlizeInterop(entry) {
1377
- return isInPackage(entry, `${PACKAGE_IDLIZE_INTERNAL}.interop`);
1378
- }
1379
- export function isInIdlizeInternal(entry) {
1380
- return isInPackage(entry, PACKAGE_IDLIZE_INTERNAL);
1381
- }
1382
- export function isInIdlizeStdlib(entry) {
1383
- return isInPackage(entry, "idlize.stdlib");
1384
- }
1385
- //# sourceMappingURL=idl.js.map