@idlizer/core 2.1.10-arktscgen-4 → 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 (122) hide show
  1. package/build/lib/src/Language.d.ts +0 -1
  2. package/build/lib/src/Language.js +1 -3
  3. package/build/lib/src/LanguageWriters/ArgConvertors.d.ts +18 -7
  4. package/build/lib/src/LanguageWriters/ArgConvertors.js +95 -103
  5. package/build/lib/src/LanguageWriters/LanguageWriter.d.ts +13 -7
  6. package/build/lib/src/LanguageWriters/LanguageWriter.js +10 -2
  7. package/build/lib/src/LanguageWriters/common.d.ts +1 -0
  8. package/build/lib/src/LanguageWriters/common.js +2 -1
  9. package/build/lib/src/LanguageWriters/convertors/CJConvertors.d.ts +0 -1
  10. package/build/lib/src/LanguageWriters/convertors/CJConvertors.js +1 -3
  11. package/build/lib/src/LanguageWriters/convertors/CppConvertors.d.ts +14 -8
  12. package/build/lib/src/LanguageWriters/convertors/CppConvertors.js +88 -36
  13. package/build/lib/src/LanguageWriters/convertors/ETSConvertors.d.ts +1 -0
  14. package/build/lib/src/LanguageWriters/convertors/ETSConvertors.js +10 -8
  15. package/build/lib/src/LanguageWriters/convertors/InteropConvertors.js +1 -1
  16. package/build/lib/src/LanguageWriters/convertors/KotlinConvertors.js +9 -5
  17. package/build/lib/src/LanguageWriters/convertors/TSConvertors.d.ts +4 -4
  18. package/build/lib/src/LanguageWriters/convertors/TSConvertors.js +98 -44
  19. package/build/lib/src/LanguageWriters/index.d.ts +5 -4
  20. package/build/lib/src/LanguageWriters/index.js +8 -15
  21. package/build/lib/src/LanguageWriters/nameConvertor.d.ts +2 -0
  22. package/build/lib/src/LanguageWriters/nameConvertor.js +11 -0
  23. package/build/lib/src/LanguageWriters/writers/CJLanguageWriter.d.ts +2 -0
  24. package/build/lib/src/LanguageWriters/writers/CJLanguageWriter.js +13 -1
  25. package/build/lib/src/LanguageWriters/writers/CppLanguageWriter.d.ts +2 -0
  26. package/build/lib/src/LanguageWriters/writers/CppLanguageWriter.js +8 -2
  27. package/build/lib/src/LanguageWriters/writers/ETSLanguageWriter.d.ts +2 -10
  28. package/build/lib/src/LanguageWriters/writers/ETSLanguageWriter.js +52 -92
  29. package/build/lib/src/LanguageWriters/writers/KotlinLanguageWriter.d.ts +8 -0
  30. package/build/lib/src/LanguageWriters/writers/KotlinLanguageWriter.js +75 -27
  31. package/build/lib/src/LanguageWriters/writers/TsLanguageWriter.d.ts +5 -0
  32. package/build/lib/src/LanguageWriters/writers/TsLanguageWriter.js +58 -18
  33. package/build/lib/src/LibraryInterface.d.ts +0 -1
  34. package/build/lib/src/config.d.ts +2 -1461
  35. package/build/lib/src/config.js +16 -32
  36. package/build/lib/src/configMerge.d.ts +3 -0
  37. package/build/lib/src/configMerge.js +63 -0
  38. package/build/lib/src/diagnostictypes.d.ts +1 -5
  39. package/build/lib/src/diagnostictypes.js +1 -27
  40. package/build/lib/src/from-idl/DtsPrinter.js +13 -6
  41. package/build/lib/src/from-idl/IDLLinter.d.ts +3 -4
  42. package/build/lib/src/from-idl/IDLLinter.js +30 -32
  43. package/build/lib/src/from-idl/deserialize.d.ts +3 -4
  44. package/build/lib/src/from-idl/deserialize.js +30 -642
  45. package/build/lib/src/from-idl/parser.d.ts +20 -2
  46. package/build/lib/src/from-idl/parser.js +97 -29
  47. package/build/lib/src/idl/builders.d.ts +43 -0
  48. package/build/lib/src/idl/builders.js +135 -0
  49. package/build/lib/src/idl/discriminators.d.ts +53 -0
  50. package/build/lib/src/idl/discriminators.js +232 -0
  51. package/build/lib/src/idl/dump.d.ts +48 -0
  52. package/build/lib/src/idl/dump.js +327 -0
  53. package/build/lib/src/idl/index.d.ts +9 -0
  54. package/build/lib/src/idl/index.js +23 -0
  55. package/build/lib/src/idl/keywords.d.ts +2 -0
  56. package/build/lib/src/{options.js → idl/keywords.js} +7 -3
  57. package/build/lib/src/idl/node.d.ts +233 -0
  58. package/build/lib/src/idl/node.js +103 -0
  59. package/build/lib/src/idl/stdlib.d.ts +34 -0
  60. package/build/lib/src/idl/stdlib.js +54 -0
  61. package/build/lib/src/idl/utils.d.ts +44 -0
  62. package/build/lib/src/idl/utils.js +215 -0
  63. package/build/lib/src/idl/visitors.d.ts +15 -0
  64. package/build/lib/src/idl/visitors.js +593 -0
  65. package/build/lib/src/index.d.ts +5 -6
  66. package/build/lib/src/index.js +6 -7
  67. package/build/lib/src/inheritance.d.ts +0 -2
  68. package/build/lib/src/inheritance.js +0 -17
  69. package/build/lib/src/inputPaths.d.ts +11 -0
  70. package/build/lib/src/inputPaths.js +81 -0
  71. package/build/lib/src/languageSpecificKeywords.d.ts +0 -1
  72. package/build/lib/src/languageSpecificKeywords.js +0 -5
  73. package/build/lib/src/peer-generation/ConflictingDeclarations.d.ts +6 -0
  74. package/build/lib/src/peer-generation/ConflictingDeclarations.js +43 -0
  75. package/build/lib/src/peer-generation/LayoutManager.d.ts +3 -1
  76. package/build/lib/src/peer-generation/LayoutManager.js +15 -0
  77. package/build/lib/src/peer-generation/Materialized.d.ts +8 -0
  78. package/build/lib/src/peer-generation/Materialized.js +26 -1
  79. package/build/lib/src/peer-generation/PeerLibrary.d.ts +10 -18
  80. package/build/lib/src/peer-generation/PeerLibrary.js +63 -227
  81. package/build/lib/src/peer-generation/ReferenceResolver.d.ts +6 -1
  82. package/build/lib/src/peer-generation/ReferenceResolver.js +93 -2
  83. package/build/lib/src/peer-generation/idl/IdlNameConvertor.d.ts +6 -0
  84. package/build/lib/src/peer-generation/idl/IdlNameConvertor.js +15 -3
  85. package/build/lib/src/peer-generation/idl/common.d.ts +6 -2
  86. package/build/lib/src/peer-generation/idl/common.js +7 -7
  87. package/build/lib/src/peer-generation/isMaterialized.js +2 -3
  88. package/build/lib/src/peer-generation/modules.js +2 -0
  89. package/build/lib/src/peer-generation/toDeclaration.d.ts +4 -0
  90. package/build/lib/src/peer-generation/toDeclaration.js +53 -0
  91. package/build/lib/src/peer-generation/unions.d.ts +1 -1
  92. package/build/lib/src/peer-generation/unions.js +15 -7
  93. package/build/lib/src/resolveNamedNode.d.ts +1 -0
  94. package/build/lib/src/resolveNamedNode.js +7 -0
  95. package/build/lib/src/transformers/FqnTransformer.d.ts +1 -1
  96. package/build/lib/src/transformers/FqnTransformer.js +20 -12
  97. package/build/lib/src/transformers/GenericTransformer.d.ts +4 -3
  98. package/build/lib/src/transformers/GenericTransformer.js +252 -158
  99. package/build/lib/src/transformers/IdlTransformer.d.ts +6 -0
  100. package/build/lib/src/transformers/IdlTransformer.js +7 -0
  101. package/build/lib/src/transformers/NullTransformer.d.ts +1 -1
  102. package/build/lib/src/transformers/NullTransformer.js +27 -21
  103. package/build/lib/src/transformers/OnSerializeTransformer.d.ts +1 -1
  104. package/build/lib/src/transformers/OnSerializeTransformer.js +28 -13
  105. package/build/lib/src/util.d.ts +9 -39
  106. package/build/lib/src/util.js +65 -369
  107. package/package.json +44 -49
  108. package/build/lib/src/LanguageWriters/convertors/JavaConvertors.d.ts +0 -37
  109. package/build/lib/src/LanguageWriters/convertors/JavaConvertors.js +0 -210
  110. package/build/lib/src/LanguageWriters/writers/JavaLanguageWriter.d.ts +0 -83
  111. package/build/lib/src/LanguageWriters/writers/JavaLanguageWriter.js +0 -295
  112. package/build/lib/src/from-idl/webidl2-utils.d.ts +0 -21
  113. package/build/lib/src/from-idl/webidl2-utils.js +0 -87
  114. package/build/lib/src/idl.d.ts +0 -439
  115. package/build/lib/src/idl.js +0 -1367
  116. package/build/lib/src/idlize.d.ts +0 -25
  117. package/build/lib/src/idlize.js +0 -198
  118. package/build/lib/src/options.d.ts +0 -13
  119. package/build/lib/src/peer-generation/BuilderClass.d.ts +0 -20
  120. package/build/lib/src/peer-generation/BuilderClass.js +0 -68
  121. package/webidl2.js/dist/webidl2.js +0 -4622
  122. package/webidl2.js/package.json +0 -55
@@ -1,1367 +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
- export function getFileFor(node) {
486
- let iterator = node;
487
- while (iterator) {
488
- if (isFile(iterator))
489
- return iterator;
490
- iterator = iterator.parent;
491
- }
492
- console.warn(`Node ${getQualifiedName(node, "namespace.name")} does not have IDLFile in parents`);
493
- return undefined;
494
- }
495
- export function isEqualByQualifedName(a, b, pattern = "package.namespace.name") {
496
- if (a === b)
497
- return true;
498
- if (!a || !b)
499
- return false;
500
- if (a.kind !== b.kind || a.name !== b.name)
501
- return false;
502
- return getQualifiedName(a, pattern) === getQualifiedName(b, pattern);
503
- }
504
- export function getPackageClause(node) {
505
- var _a;
506
- const file = getFileFor(node);
507
- return (_a = file === null || file === void 0 ? void 0 : file.packageClause) !== null && _a !== void 0 ? _a : [];
508
- }
509
- export function getPackageName(node) {
510
- return getPackageClause(node).join(".");
511
- }
512
- export function isInPackage(entry, packageName, exactMatch = false) {
513
- const entryPackageName = getPackageName(entry);
514
- return exactMatch
515
- ? entryPackageName === packageName
516
- : entryPackageName.startsWith(packageName);
517
- }
518
- export function getNamespaceName(a) {
519
- return getNamespacesPathFor(a).map(it => it.name).join('.');
520
- }
521
- export function deriveQualifiedNameFrom(name, from) {
522
- return [...getPackageClause(from), ...getNamespacesPathFor(from).map(it => it.name), name].join(".");
523
- }
524
- export function getQualifiedName(a, pattern) {
525
- const result = [];
526
- if ("package.namespace.name" === pattern)
527
- result.push(...getPackageClause(a), ...getNamespacesPathFor(a).map(it => it.name));
528
- else if ("namespace.name" === pattern)
529
- result.push(...getNamespacesPathFor(a).map(it => it.name));
530
- if (isNamedNode(a) && a.name)
531
- result.push(a.name);
532
- return result.join(".");
533
- }
534
- export function getFQName(a) {
535
- return getQualifiedName(a, "package.namespace.name");
536
- }
537
- export function createVersion(value, nodeInitializer) {
538
- return Object.assign(Object.assign({ kind: IDLKind.Version, value, name: "version" }, nodeInitializer), { _idlNodeBrand: innerIdlSymbol, _idlEntryBrand: innerIdlSymbol, _idlNamedNodeBrand: innerIdlSymbol });
539
- }
540
- export function fetchNamespaceFrom(pointOfView) {
541
- let node = pointOfView;
542
- while (node) {
543
- if (isNamespace(node))
544
- return node;
545
- node = node.parent;
546
- }
547
- return undefined;
548
- }
549
- export function createReferenceType(nameOrSource, typeArguments, nodeInitializer) {
550
- let name;
551
- if (typeof nameOrSource === 'string') {
552
- name = nameOrSource;
553
- }
554
- else {
555
- name = getFQName(nameOrSource);
556
- }
557
- return Object.assign(Object.assign({ kind: IDLKind.ReferenceType, name,
558
- typeArguments }, nodeInitializer), { _idlNodeBrand: innerIdlSymbol, _idlTypeBrand: innerIdlSymbol, _idlNamedNodeBrand: innerIdlSymbol });
559
- }
560
- export function createUnspecifiedGenericType(name, typeArguments, nodeInitializer) {
561
- return Object.assign(Object.assign({ kind: IDLKind.UnspecifiedGenericType, name,
562
- typeArguments }, nodeInitializer), { _idlNodeBrand: innerIdlSymbol, _idlTypeBrand: innerIdlSymbol, _idlNamedNodeBrand: innerIdlSymbol });
563
- }
564
- export function entityToType(entity) {
565
- if (isType(entity)) {
566
- return entity;
567
- }
568
- else if (isEntry(entity)) {
569
- return createReferenceType(entity);
570
- }
571
- else {
572
- throw new Error(`Expected to have IDLType or IDLEntry, got ${entity}`);
573
- }
574
- }
575
- export function createContainerType(container, element, nodeInitializer) {
576
- return Object.assign(Object.assign({ kind: IDLKind.ContainerType, containerKind: container, elementType: element }, nodeInitializer), { _idlNodeBrand: innerIdlSymbol, _idlTypeBrand: innerIdlSymbol });
577
- }
578
- export function createUnionType(types, name, nodeInitializer) {
579
- if (types.length < 2)
580
- throw new Error("IDLUnionType should contain at least 2 types");
581
- 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 });
582
- }
583
- export function createFile(entries, fileName, packageClause = [], nodeInitializer) {
584
- return Object.assign(Object.assign({ kind: IDLKind.File, packageClause, entries: entries, fileName }, nodeInitializer), { _idlNodeBrand: innerIdlSymbol });
585
- }
586
- export function createImport(clause, name, nodeInitializer) {
587
- return Object.assign(Object.assign({ kind: IDLKind.Import, name: name !== null && name !== void 0 ? name : "", clause }, nodeInitializer), { _idlNodeBrand: innerIdlSymbol, _idlEntryBrand: innerIdlSymbol, _idlNamedNodeBrand: innerIdlSymbol });
588
- }
589
- export function createEnum(name, elements, nodeInitializer) {
590
- return Object.assign(Object.assign({ kind: IDLKind.Enum, name: name, elements: elements }, nodeInitializer), { _idlNodeBrand: innerIdlSymbol, _idlEntryBrand: innerIdlSymbol, _idlNamedNodeBrand: innerIdlSymbol });
591
- }
592
- export function createEnumMember(name, parent, type, initializer, nodeInitializer = {}) {
593
- return Object.assign(Object.assign({ kind: IDLKind.EnumMember, name: name, parent,
594
- type,
595
- initializer }, nodeInitializer), { _idlNodeBrand: innerIdlSymbol, _idlEntryBrand: innerIdlSymbol, _idlNamedNodeBrand: innerIdlSymbol });
596
- }
597
- export function createInterface(name, subkind, inheritance = [], constructors = [], constants = [], properties = [], methods = [], callables = [], typeParameters = [], nodeInitializer = {}) {
598
- return Object.assign(Object.assign({ kind: IDLKind.Interface, name,
599
- subkind,
600
- typeParameters,
601
- inheritance,
602
- constructors,
603
- constants,
604
- properties,
605
- methods,
606
- callables }, nodeInitializer), { _idlNodeBrand: innerIdlSymbol, _idlEntryBrand: innerIdlSymbol, _idlNamedNodeBrand: innerIdlSymbol });
607
- }
608
- export function createProperty(name, type, isReadonly = false, isStatic = false, isOptional = false, nodeInitializer = {}) {
609
- return Object.assign(Object.assign({ name, kind: IDLKind.Property, type,
610
- isReadonly,
611
- isStatic,
612
- isOptional }, nodeInitializer), { _idlNodeBrand: innerIdlSymbol, _idlEntryBrand: innerIdlSymbol, _idlNamedNodeBrand: innerIdlSymbol });
613
- }
614
- export function createParameter(name, type, isOptional = false, isVariadic = false, nodeInitializer = {}) {
615
- return Object.assign(Object.assign({ kind: IDLKind.Parameter, name: name, type: type, isOptional,
616
- isVariadic }, nodeInitializer), { _idlNodeBrand: innerIdlSymbol, _idlEntryBrand: innerIdlSymbol, _idlNamedNodeBrand: innerIdlSymbol });
617
- }
618
- export function createMethod(name, parameters, returnType, methodInitializer = {
619
- isAsync: false,
620
- isStatic: false,
621
- isOptional: false,
622
- isFree: false,
623
- }, nodeInitializer = {}, typeParameters = []) {
624
- return Object.assign(Object.assign(Object.assign({ kind: IDLKind.Method, name,
625
- parameters,
626
- returnType,
627
- typeParameters }, methodInitializer), nodeInitializer), { _idlNodeBrand: innerIdlSymbol, _idlEntryBrand: innerIdlSymbol, _idlNamedNodeBrand: innerIdlSymbol });
628
- }
629
- export function createCallable(
630
- // TODO name here seems useless
631
- name, parameters, returnType, callableInitializer, nodeInitializer, typeParameters = []) {
632
- return Object.assign(Object.assign(Object.assign({ kind: IDLKind.Callable, name,
633
- parameters,
634
- returnType }, callableInitializer), nodeInitializer), { _idlNodeBrand: innerIdlSymbol, _idlEntryBrand: innerIdlSymbol, _idlNamedNodeBrand: innerIdlSymbol });
635
- }
636
- export function createConstructor(parameters, returnType, nodeInitializer = {}) {
637
- return Object.assign(Object.assign({ kind: IDLKind.Constructor, name: "$CONSTRUCTOR%", parameters,
638
- returnType }, nodeInitializer), { _idlNodeBrand: innerIdlSymbol, _idlEntryBrand: innerIdlSymbol, _idlNamedNodeBrand: innerIdlSymbol });
639
- }
640
- export function createCallback(name, parameters, returnType, nodeInitializer = {}, typeParameters = []) {
641
- return Object.assign(Object.assign({ kind: IDLKind.Callback, name, parameters, returnType, typeParameters }, nodeInitializer), { _idlNodeBrand: innerIdlSymbol, _idlEntryBrand: innerIdlSymbol, _idlNamedNodeBrand: innerIdlSymbol });
642
- }
643
- export function createTypeParameterReference(name, nodeInitializer) {
644
- return Object.assign(Object.assign({ kind: IDLKind.TypeParameterType, name: name }, nodeInitializer), { _idlNodeBrand: innerIdlSymbol, _idlTypeBrand: innerIdlSymbol, _idlNamedNodeBrand: innerIdlSymbol });
645
- }
646
- export function createTypedef(name, type, typeParameters = [], nodeInitializer = {}) {
647
- return Object.assign(Object.assign({ name, type, typeParameters, kind: IDLKind.Typedef }, nodeInitializer), { _idlNodeBrand: innerIdlSymbol, _idlEntryBrand: innerIdlSymbol, _idlNamedNodeBrand: innerIdlSymbol });
648
- }
649
- export function createConstant(name, type, value, nodeInitializer = {}) {
650
- return Object.assign(Object.assign({ kind: IDLKind.Const, name,
651
- type,
652
- value }, nodeInitializer), { _idlNodeBrand: innerIdlSymbol, _idlEntryBrand: innerIdlSymbol, _idlNamedNodeBrand: innerIdlSymbol });
653
- }
654
- export function clone(node) {
655
- 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;
656
- const make = (newnode) => {
657
- if (node.nodeLocation) {
658
- newnode.nodeLocation = node.nodeLocation;
659
- }
660
- if (node.nameLocation) {
661
- newnode.nameLocation = node.nameLocation;
662
- }
663
- if (node.valueLocation) {
664
- newnode.valueLocation = node.valueLocation;
665
- }
666
- return newnode;
667
- };
668
- const get = (node) => node;
669
- switch (node.kind) {
670
- case IDLKind.Interface: {
671
- const entry = get(node);
672
- 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), {
673
- documentation: node.documentation,
674
- extendedAttributes: (_d = node.extendedAttributes) === null || _d === void 0 ? void 0 : _d.slice(),
675
- fileName: node.fileName
676
- }));
677
- }
678
- case IDLKind.Import: {
679
- const entry = get(node);
680
- return make(createImport(entry.clause, entry.name, {
681
- documentation: entry.documentation,
682
- extendedAttributes: (_e = entry.extendedAttributes) === null || _e === void 0 ? void 0 : _e.slice(),
683
- fileName: entry.fileName
684
- }));
685
- }
686
- case IDLKind.Callback: {
687
- const entry = get(node);
688
- return make(createCallback(entry.name, entry.parameters.map(clone), clone(entry.returnType), {
689
- documentation: entry.documentation,
690
- extendedAttributes: (_f = entry.extendedAttributes) === null || _f === void 0 ? void 0 : _f.slice(),
691
- fileName: entry.fileName
692
- }, entry.typeParameters));
693
- }
694
- case IDLKind.Const: {
695
- const entry = get(node);
696
- return make(createConstant(entry.name, clone(entry.type), entry.value, {
697
- documentation: entry.documentation,
698
- extendedAttributes: (_g = entry.extendedAttributes) === null || _g === void 0 ? void 0 : _g.slice(),
699
- fileName: entry.fileName
700
- }));
701
- }
702
- case IDLKind.Property: {
703
- const entry = get(node);
704
- return make(createProperty(entry.name, clone(entry.type), entry.isReadonly, entry.isStatic, entry.isOptional, {
705
- documentation: entry.documentation,
706
- extendedAttributes: (_h = entry.extendedAttributes) === null || _h === void 0 ? void 0 : _h.slice(),
707
- fileName: entry.fileName
708
- }));
709
- }
710
- case IDLKind.Parameter: {
711
- const entry = get(node);
712
- return make(createParameter(entry.name, clone(entry.type), entry.isOptional, entry.isVariadic, {
713
- documentation: entry.documentation,
714
- extendedAttributes: (_j = entry.extendedAttributes) === null || _j === void 0 ? void 0 : _j.slice(),
715
- fileName: entry.fileName
716
- }));
717
- }
718
- case IDLKind.Method: {
719
- const entry = get(node);
720
- return make(createMethod(entry.name, entry.parameters.map(clone), clone(entry.returnType), {
721
- isAsync: entry.isAsync,
722
- isFree: entry.isFree,
723
- isOptional: entry.isOptional,
724
- isStatic: entry.isStatic
725
- }, {
726
- documentation: entry.documentation,
727
- extendedAttributes: (_k = entry.extendedAttributes) === null || _k === void 0 ? void 0 : _k.slice(),
728
- fileName: entry.fileName
729
- }, entry.typeParameters));
730
- }
731
- case IDLKind.Callable: {
732
- const entry = get(node);
733
- return make(createCallable(entry.name, entry.parameters.map(clone), clone(entry.returnType), {
734
- isAsync: entry.isAsync,
735
- isStatic: entry.isStatic
736
- }, {
737
- documentation: entry.documentation,
738
- extendedAttributes: (_l = entry.extendedAttributes) === null || _l === void 0 ? void 0 : _l.slice(),
739
- fileName: entry.documentation
740
- }, entry.typeParameters));
741
- }
742
- case IDLKind.Constructor: {
743
- const entry = get(node);
744
- return make(createConstructor(entry.parameters.map(clone), entry.returnType ? clone(entry.returnType) : undefined, {
745
- documentation: entry.documentation,
746
- extendedAttributes: (_m = entry.extendedAttributes) === null || _m === void 0 ? void 0 : _m.slice(),
747
- fileName: entry.fileName
748
- }));
749
- }
750
- case IDLKind.Enum: {
751
- const entry = get(node);
752
- const cloned = createEnum(entry.name, entry.elements.map(clone), {
753
- documentation: entry.documentation,
754
- extendedAttributes: (_o = entry.extendedAttributes) === null || _o === void 0 ? void 0 : _o.slice(),
755
- fileName: entry.fileName
756
- });
757
- cloned.elements.forEach(it => {
758
- it.parent = cloned;
759
- });
760
- return make(cloned);
761
- }
762
- case IDLKind.EnumMember: {
763
- const entry = get(node);
764
- return make(createEnumMember(entry.name, entry.parent, clone(entry.type), entry.initializer, {
765
- documentation: entry.documentation,
766
- extendedAttributes: (_p = entry.extendedAttributes) === null || _p === void 0 ? void 0 : _p.slice(),
767
- fileName: entry.fileName
768
- }));
769
- }
770
- case IDLKind.Typedef: {
771
- const entry = get(node);
772
- return make(createTypedef(entry.name, clone(entry.type), entry.typeParameters, {
773
- documentation: entry.documentation,
774
- extendedAttributes: (_q = entry.extendedAttributes) === null || _q === void 0 ? void 0 : _q.slice(),
775
- fileName: entry.fileName
776
- }));
777
- }
778
- case IDLKind.PrimitiveType: {
779
- return node;
780
- }
781
- case IDLKind.ContainerType: {
782
- const type = get(node);
783
- return make(createContainerType(type.containerKind, type.elementType.map(clone), {
784
- documentation: type.documentation,
785
- extendedAttributes: (_r = type.extendedAttributes) === null || _r === void 0 ? void 0 : _r.slice(),
786
- fileName: type.fileName
787
- }));
788
- }
789
- case IDLKind.UnspecifiedGenericType: {
790
- const type = get(node);
791
- return make(createUnspecifiedGenericType(type.name, type.typeArguments.map(clone), {
792
- documentation: type.documentation,
793
- extendedAttributes: (_s = type.extendedAttributes) === null || _s === void 0 ? void 0 : _s.slice(),
794
- fileName: type.fileName
795
- }));
796
- }
797
- case IDLKind.ReferenceType: {
798
- const type = get(node);
799
- return make(createReferenceType(type.name, (_t = type.typeArguments) === null || _t === void 0 ? void 0 : _t.map(clone), {
800
- documentation: type.documentation,
801
- extendedAttributes: (_u = type.extendedAttributes) === null || _u === void 0 ? void 0 : _u.slice(),
802
- fileName: type.fileName
803
- }));
804
- }
805
- case IDLKind.UnionType: {
806
- const type = get(node);
807
- return make(createUnionType(type.types.map(clone), type.name, {
808
- documentation: type.documentation,
809
- extendedAttributes: (_v = type.extendedAttributes) === null || _v === void 0 ? void 0 : _v.slice(),
810
- fileName: type.fileName
811
- }));
812
- }
813
- case IDLKind.TypeParameterType: {
814
- const type = get(node);
815
- return make(createTypeParameterReference(type.name, {
816
- documentation: type.documentation,
817
- extendedAttributes: (_w = type.extendedAttributes) === null || _w === void 0 ? void 0 : _w.slice(),
818
- fileName: type.fileName
819
- }));
820
- }
821
- case IDLKind.OptionalType: {
822
- const type = get(node);
823
- return make(createOptionalType(clone(type.type), {
824
- documentation: type.documentation,
825
- extendedAttributes: (_x = type.extendedAttributes) === null || _x === void 0 ? void 0 : _x.slice(),
826
- fileName: type.fileName
827
- }));
828
- }
829
- case IDLKind.Version: {
830
- const entry = get(node);
831
- return make(createVersion(entry.value, {
832
- documentation: entry.documentation,
833
- extendedAttributes: (_y = entry.extendedAttributes) === null || _y === void 0 ? void 0 : _y.slice(),
834
- fileName: entry.fileName
835
- }));
836
- }
837
- case IDLKind.Namespace: {
838
- const ns = get(node);
839
- return make(createNamespace(ns.name, ns.members.map(clone), {
840
- documentation: ns.documentation,
841
- extendedAttributes: (_z = ns.extendedAttributes) === null || _z === void 0 ? void 0 : _z.slice(),
842
- fileName: ns.fileName
843
- }));
844
- }
845
- case IDLKind.File: {
846
- const file = get(node);
847
- return make(createFile(file.entries.map(clone), file.fileName, file.packageClause, {
848
- documentation: file.documentation,
849
- extendedAttributes: (_0 = file.extendedAttributes) === null || _0 === void 0 ? void 0 : _0.slice(),
850
- fileName: file.fileName
851
- }));
852
- }
853
- }
854
- }
855
- export function hasTypeParameters(entry) {
856
- let foundTypeParameter = false;
857
- forEachChild(entry, n => {
858
- if (isTypeParameterType(n)) {
859
- foundTypeParameter = true;
860
- }
861
- });
862
- return foundTypeParameter;
863
- }
864
- export function escapeIDLKeyword(name) {
865
- return name + (IDLKeywords.has(name) ? "_" : "");
866
- }
867
- export function unescapeKeyword(name) {
868
- if (name.endsWith("_")) {
869
- const unwrapped = name.slice(0, -1);
870
- if (IDLKeywords.has(unwrapped))
871
- return unwrapped;
872
- }
873
- return name;
874
- }
875
- const printedIndentInc = "[[indent-inc]]";
876
- const printedIndentDec = "[[indent-dec]]";
877
- export function printType(type, options) {
878
- var _a;
879
- if (!type)
880
- throw new Error("Missing type");
881
- if (isInterface(type))
882
- return type.name;
883
- if (isOptionalType(type)) {
884
- if (hasExtAttribute(type, IDLExtendedAttributes.UnionOnlyNull))
885
- return `(${printType(type.type)} or ${IDLNullTypeName})`;
886
- else if (hasExtAttribute(type, IDLExtendedAttributes.UnionWithNull))
887
- return `(${printType(type.type)} or ${IDLUndefinedType.name} or${IDLNullTypeName})`;
888
- else
889
- return `(${printType(type.type)} or ${IDLUndefinedType.name})`;
890
- }
891
- if (isPrimitiveType(type))
892
- return type.name;
893
- if (isContainerType(type))
894
- return `${type.containerKind}<${type.elementType.map(it => printType(it)).join(", ")}>`;
895
- if (isReferenceType(type)) {
896
- if (type.typeArguments)
897
- updateExtAttribute(type, IDLExtendedAttributes.TypeArguments, type.typeArguments.map(it => printType(it)).join(","));
898
- if (!((_a = type.extendedAttributes) === null || _a === void 0 ? void 0 : _a.length))
899
- return type.name;
900
- let res = `[${quoteAttributeValues(type.extendedAttributes)}] ${type.name}`;
901
- if (options === null || options === void 0 ? void 0 : options.bracketsAroundReferenceTypeWithExtAttrs)
902
- return `(${res})`;
903
- return res;
904
- }
905
- if (isUnspecifiedGenericType(type))
906
- return `${type.name}<${type.typeArguments.map(it => printType(it)).join(", ")}>`;
907
- if (isUnionType(type))
908
- return `(${type.types.map(it => printType(it)).join(" or ")})`;
909
- if (isTypeParameterType(type))
910
- return type.name;
911
- throw new Error(`Cannot map type: ${IDLKind[type.kind]}`);
912
- }
913
- export function printReturnType(type) {
914
- return printType(type, { bracketsAroundReferenceTypeWithExtAttrs: true });
915
- }
916
- export function printParameters(parameters) {
917
- var _a, _b;
918
- 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 : "";
919
- }
920
- export function printConstructor(idl) {
921
- return [
922
- ...printExtendedAttributes(idl, 1),
923
- `constructor(${printParameters(idl.parameters)});`
924
- ];
925
- }
926
- export function nameWithType(idl, isVariadic = false, isOptional = false) {
927
- const type = printType(idl.type);
928
- const variadic = isVariadic ? "..." : "";
929
- const optional = isOptional ? "optional " : "";
930
- return `${optional}${type}${variadic} ${escapeIDLKeyword(idl.name)}`;
931
- }
932
- export function printConstant(idl) {
933
- return [
934
- ...printExtendedAttributes(idl, 1),
935
- `const ${nameWithType(idl)} = ${idl.value};`
936
- ];
937
- }
938
- export function printProperty(idl) {
939
- const staticMod = idl.isStatic ? "static " : "";
940
- const readonlyMod = idl.isReadonly ? "readonly " : "";
941
- return [
942
- ...printExtendedAttributes(idl, 1),
943
- `${staticMod}${readonlyMod}attribute ${nameWithType(idl)};`
944
- ];
945
- }
946
- export function printExtendedAttributes(idl, indentLevel) {
947
- let typeParameters;
948
- let typeArguments;
949
- switch (idl.kind) {
950
- case IDLKind.Interface:
951
- typeParameters = idl.typeParameters;
952
- break;
953
- case IDLKind.Callback:
954
- case IDLKind.Method:
955
- case IDLKind.Callable:
956
- case IDLKind.Constructor:
957
- typeParameters = idl.typeParameters;
958
- break;
959
- case IDLKind.Typedef:
960
- typeParameters = idl.typeParameters;
961
- break;
962
- case IDLKind.ReferenceType:
963
- typeArguments = idl.typeArguments;
964
- break;
965
- case IDLKind.UnspecifiedGenericType:
966
- typeArguments = idl.typeArguments;
967
- break;
968
- }
969
- const attributes = Array.from(idl.extendedAttributes || []);
970
- if ((typeParameters === null || typeParameters === void 0 ? void 0 : typeParameters.length) && !attributes.find(x => x.name === IDLExtendedAttributes.TypeParameters))
971
- attributes.push({ name: IDLExtendedAttributes.TypeParameters, value: typeParameters.join(",") });
972
- if ((typeArguments === null || typeArguments === void 0 ? void 0 : typeArguments.length) && !attributes.find(x => x.name === IDLExtendedAttributes.TypeArguments))
973
- attributes.push({ name: IDLExtendedAttributes.TypeArguments, value: typeArguments.map(it => printType(it)).join(",") });
974
- if (idl.documentation) {
975
- let docs = {
976
- name: IDLExtendedAttributes.Documentation,
977
- value: idl.documentation
978
- };
979
- attributes.unshift(docs);
980
- }
981
- // Deduplicate
982
- const names = new Set();
983
- const actualAttributes = [];
984
- for (const attr of attributes) {
985
- if (names.has(attr.name)) {
986
- continue;
987
- }
988
- names.add(attr.name);
989
- actualAttributes.push(attr);
990
- }
991
- if (actualAttributes.length == 0) {
992
- return [];
993
- }
994
- const attrSpec = quoteAttributeValues(actualAttributes);
995
- return attrSpec ? [`[${attrSpec}]`] : [];
996
- }
997
- export const attributesToQuote = new Set([
998
- IDLExtendedAttributes.Documentation,
999
- IDLExtendedAttributes.DtsName,
1000
- IDLExtendedAttributes.DtsTag,
1001
- IDLExtendedAttributes.Import,
1002
- IDLExtendedAttributes.Interfaces,
1003
- IDLExtendedAttributes.TraceKey,
1004
- IDLExtendedAttributes.TypeArguments,
1005
- IDLExtendedAttributes.TypeParameters,
1006
- IDLExtendedAttributes.TypeParametersDefaults,
1007
- ]);
1008
- function quoteAttributeValues(attributes) {
1009
- return attributes === null || attributes === void 0 ? void 0 : attributes.map(it => {
1010
- let attr = it.name;
1011
- if (it.value) {
1012
- let value = it.value;
1013
- if (value.includes('"') && !value.includes("'"))
1014
- value = value.replaceAll('"', "'");
1015
- value = value.replaceAll('\\', '\\\\').replaceAll('"', '\\"');
1016
- attr += `=${attributesToQuote.has(it.name) ? `"${value}"` : it.value}`;
1017
- }
1018
- return attr;
1019
- }).join(", ");
1020
- }
1021
- export function printFunction(idl) {
1022
- var _a;
1023
- if ((_a = idl.name) === null || _a === void 0 ? void 0 : _a.startsWith("__")) {
1024
- console.log(`Ignore ${idl.name}`);
1025
- return [];
1026
- }
1027
- return [
1028
- ...printExtendedAttributes(idl, 1),
1029
- `${idl.isAsync ? "async " : ""}${printReturnType(idl.returnType)} ${idl.name}(${printParameters(idl.parameters)});`
1030
- ];
1031
- }
1032
- export function printMethod(idl) {
1033
- var _a;
1034
- if ((_a = idl.name) === null || _a === void 0 ? void 0 : _a.startsWith("__")) {
1035
- console.log(`Ignore ${idl.name}`);
1036
- return [];
1037
- }
1038
- return [
1039
- ...printExtendedAttributes(idl, 1),
1040
- `${idl.isStatic ? "static " : ""}${idl.isAsync ? "async " : ""}${printReturnType(idl.returnType)} ${idl.name}(${printParameters(idl.parameters)});`
1041
- ];
1042
- }
1043
- export function printPackage(idl) {
1044
- const effectiveClause = idl.packageClause.filter(it => !!it);
1045
- if (!effectiveClause.length)
1046
- return [];
1047
- return [
1048
- `package ${effectiveClause.join(".")};`
1049
- ];
1050
- }
1051
- export function printImport(idl) {
1052
- const effectiveClause = idl.clause.filter(it => !!it);
1053
- return [
1054
- `import ${effectiveClause.join(".") || "NULL_IMPORT"}${idl.name ? " as " : ""}${idl.name};`
1055
- ];
1056
- }
1057
- export function printNamespace(idl) {
1058
- return [
1059
- ...printExtendedAttributes(idl, 0),
1060
- `namespace ${idl.name} {`,
1061
- printedIndentInc,
1062
- ...idl.members.map(member => printIDL(member)).flat(),
1063
- printedIndentDec,
1064
- "};"
1065
- ];
1066
- }
1067
- export function printCallback(idl) {
1068
- return [
1069
- ...printExtendedAttributes(idl, 0),
1070
- `callback ${idl.name} = ${printReturnType(idl.returnType)} (${printParameters(idl.parameters)});`
1071
- ];
1072
- }
1073
- export function printScoped(idl) {
1074
- if (idl.kind == IDLKind.Callback)
1075
- return printCallback(idl);
1076
- if (idl.kind === IDLKind.Interface)
1077
- return printInterface(idl);
1078
- throw new Error(`Unexpected scoped: ${idl.kind} ${idl.name}`);
1079
- }
1080
- function printInterfaceInherit(idl) {
1081
- if (idl.inheritance.length === 0) {
1082
- return "";
1083
- }
1084
- const types = idl.inheritance.map(type => printType(type));
1085
- return ": " + types.join(', ');
1086
- }
1087
- export function printInterface(idl) {
1088
- return [
1089
- ...printExtendedAttributes(idl, 0),
1090
- `interface ${idl.name}${printInterfaceInherit(idl)} {`,
1091
- // TODO: type system hack!
1092
- ]
1093
- .concat(printedIndentInc)
1094
- .concat(idl.constructors.map(printConstructor).flat())
1095
- .concat(idl.constants.map(printConstant).flat())
1096
- .concat(idl.properties.map(printProperty).flat())
1097
- .concat(idl.methods.map(printMethod).flat())
1098
- .concat(idl.callables.map(printFunction).flat())
1099
- .concat(printedIndentDec)
1100
- .concat(["};"]);
1101
- }
1102
- export function printEnumMember(idl) {
1103
- const type = printType(idl.type);
1104
- const initializer = idl.initializer === undefined
1105
- ? ''
1106
- : ' = ' + (type === IDLStringType.name
1107
- ? `"${String(idl.initializer).replaceAll('"', "'")}"`
1108
- : idl.initializer);
1109
- return [
1110
- idl.documentation,
1111
- ...printExtendedAttributes(idl, 0),
1112
- `${type} ${idl.name}${initializer};`
1113
- ];
1114
- }
1115
- export function printEnum(idl, skipInitializers) {
1116
- if (skipInitializers) {
1117
- return [
1118
- idl.documentation,
1119
- ...printExtendedAttributes(idl, 0),
1120
- `enum ${idl.name} {`,
1121
- printedIndentInc,
1122
- ...idl.elements.map(it => `${it.name} ${(it.initializer !== undefined ? " /* " + it.initializer + " */" : "")}`),
1123
- printedIndentDec,
1124
- "};"
1125
- ];
1126
- }
1127
- else {
1128
- return [
1129
- idl.documentation,
1130
- ...printExtendedAttributes(idl, 0),
1131
- `dictionary ${idl.name} {`,
1132
- printedIndentInc,
1133
- ...idl.elements.map(printEnumMember),
1134
- printedIndentDec,
1135
- "};"
1136
- ].flat();
1137
- }
1138
- }
1139
- export function printTypedef(idl) {
1140
- return [
1141
- idl.documentation,
1142
- ...printExtendedAttributes(idl, 0),
1143
- `typedef ${printType(idl.type)} ${idl.name};`
1144
- ];
1145
- }
1146
- // TODO: use IndentedPrinter instead!
1147
- export function printIDL(idl, options) {
1148
- var _a;
1149
- if (idl.kind == IDLKind.Interface)
1150
- return printInterface(idl);
1151
- if (idl.kind == IDLKind.Enum)
1152
- return printEnum(idl, (_a = options === null || options === void 0 ? void 0 : options.disableEnumInitializers) !== null && _a !== void 0 ? _a : false);
1153
- if (idl.kind == IDLKind.Typedef)
1154
- return printTypedef(idl);
1155
- if (idl.kind == IDLKind.Callback)
1156
- return printCallback(idl);
1157
- if (idl.kind == IDLKind.Import)
1158
- return printImport(idl);
1159
- if (idl.kind == IDLKind.Namespace)
1160
- return printNamespace(idl);
1161
- if (idl.kind == IDLKind.Method)
1162
- return printMethod(idl);
1163
- if (idl.kind == IDLKind.Const)
1164
- return printConstant(idl);
1165
- if (options === null || options === void 0 ? void 0 : options.allowUnknownKinds) {
1166
- return [`${IDLKind[idl.kind]} ${"name" in idl ? idl.name : ""}`];
1167
- }
1168
- else {
1169
- throw new Error(`unexpected kind: ${idl.kind}`);
1170
- }
1171
- }
1172
- export function toIDLString(file, options) {
1173
- let indent = 0;
1174
- const generated = printPackage(file);
1175
- return generated.concat(file.entries
1176
- .map(it => printIDL(it, options))
1177
- .flat()
1178
- .filter(isDefined)
1179
- .filter(it => it.length > 0)
1180
- .map(it => {
1181
- if (it === printedIndentInc) {
1182
- ++indent;
1183
- return undefined;
1184
- }
1185
- else if (it === printedIndentDec) {
1186
- --indent;
1187
- return undefined;
1188
- }
1189
- else
1190
- return indentedBy(it, indent);
1191
- })
1192
- .filter(isDefined)).join("\n");
1193
- }
1194
- // throws validation error
1195
- export function verifyIDLString(source) {
1196
- webidl2.validate(webidl2.parse(source));
1197
- return true;
1198
- }
1199
- export function hasExtAttribute(node, attribute) {
1200
- var _a;
1201
- return ((_a = node.extendedAttributes) === null || _a === void 0 ? void 0 : _a.find((it) => it.name == attribute)) != undefined;
1202
- }
1203
- export function getExtAttribute(node, name) {
1204
- var _a, _b;
1205
- 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;
1206
- }
1207
- export function removeExtAttribute(node, name) {
1208
- if (node.extendedAttributes) {
1209
- node.extendedAttributes = node.extendedAttributes.filter(it => it.name !== name);
1210
- }
1211
- }
1212
- export function updateExtAttribute(node, name, value) {
1213
- var _a;
1214
- removeExtAttribute(node, name);
1215
- (_a = node.extendedAttributes) !== null && _a !== void 0 ? _a : (node.extendedAttributes = []);
1216
- node.extendedAttributes.push({ name, value });
1217
- }
1218
- export function getVerbatimDts(node) {
1219
- let value = getExtAttribute(node, IDLExtendedAttributes.VerbatimDts);
1220
- return value ? value.substring(1, value.length - 1) : undefined;
1221
- }
1222
- export const IDLContainerUtils = {
1223
- isRecord: (x) => isContainerType(x) && x.containerKind === 'record',
1224
- isSequence: (x) => isContainerType(x) && x.containerKind === 'sequence',
1225
- isPromise: (x) => isContainerType(x) && x.containerKind === 'Promise'
1226
- };
1227
- /**
1228
- * @returns tuple of qualifier name and real type name
1229
- */
1230
- export function decomposeQualifiedName(type) {
1231
- const typeName = type.name;
1232
- const lastDot = typeName.lastIndexOf(".");
1233
- if (lastDot >= 0) {
1234
- const qualifier = typeName.slice(0, lastDot);
1235
- const realTypeName = typeName.slice(lastDot + 1);
1236
- return [qualifier, realTypeName];
1237
- }
1238
- return [undefined, typeName];
1239
- }
1240
- export function qualifiedNameStartsWith(node, template) {
1241
- const name = Array.isArray(node) ? node : getFQName(node).split(".");
1242
- if (name.length < template.length)
1243
- return false;
1244
- for (let i = 0; i < template.length; i++) {
1245
- if (name[i] != template[i])
1246
- return false;
1247
- }
1248
- return true;
1249
- }
1250
- export function maybeUnwrapOptionalType(type) {
1251
- if (isOptionalType(type)) {
1252
- return type.type;
1253
- }
1254
- return type;
1255
- }
1256
- export function maybeOptional(type, optional = false) {
1257
- if (optional) {
1258
- return createOptionalType(type);
1259
- }
1260
- return type;
1261
- }
1262
- export const DebugUtils = {
1263
- debugPrintType: (type) => {
1264
- if (isContainerType(type)) {
1265
- return `[IDLType, name: '${printType(type)}', kind: '${IDLKind[type.kind]}', elements: [${type.elementType.map(DebugUtils.debugPrintType).join(', ')}]]`;
1266
- }
1267
- return `[IDLType, name: '${printType(type)}', kind: '${IDLKind[type.kind]}']`;
1268
- },
1269
- };
1270
- export function forEachFunction(node, cb) {
1271
- forEachChild(node, child => {
1272
- if (child.kind === IDLKind.Method || child.kind === IDLKind.Callable)
1273
- cb(child);
1274
- });
1275
- }
1276
- export function asPromise(type) {
1277
- if (!type)
1278
- return undefined;
1279
- if (!isContainerType(type))
1280
- return undefined;
1281
- const container = type;
1282
- if (!IDLContainerUtils.isPromise(container))
1283
- return undefined;
1284
- return container;
1285
- }
1286
- export function transformMethodsAsync2ReturnPromise(entry) {
1287
- forEachFunction(entry, function_ => {
1288
- var _a;
1289
- if (function_.isAsync) {
1290
- function_.isAsync = false;
1291
- if (!asPromise(function_.returnType))
1292
- function_.returnType = createContainerType("Promise", [(_a = function_.returnType) !== null && _a !== void 0 ? _a : IDLVoidType]);
1293
- }
1294
- });
1295
- }
1296
- export function transformMethodsReturnPromise2Async(entry) {
1297
- forEachFunction(entry, function_ => {
1298
- const promise = asPromise(function_.returnType);
1299
- if (promise) {
1300
- function_.returnType = promise.elementType[0];
1301
- function_.isAsync = true;
1302
- }
1303
- });
1304
- }
1305
- export function fetchSignatureTags(node) {
1306
- if (!node.extendedAttributes)
1307
- return [];
1308
- return node.extendedAttributes
1309
- .filter((ea) => ea.name === IDLExtendedAttributes.DtsTag)
1310
- .map((ea) => {
1311
- if (!ea.value)
1312
- throw new Error('Empty DtsTag is not allowed');
1313
- let indexNameValue = ea.value.split('|');
1314
- if (indexNameValue.length === 1) {
1315
- return {
1316
- index: 0,
1317
- name: 'type',
1318
- value: indexNameValue[0],
1319
- };
1320
- }
1321
- if (indexNameValue.length !== 3)
1322
- throw new Error(`Malformed DtsTag: "${ea.value}"`);
1323
- return {
1324
- index: Number(indexNameValue[0]),
1325
- name: indexNameValue[1],
1326
- value: indexNameValue[2],
1327
- };
1328
- })
1329
- .sort((a, b) => a.index - b.index);
1330
- }
1331
- export function mixMethodParametersAndTags(node) {
1332
- let mix = node.parameters.slice(0);
1333
- for (const tag of fetchSignatureTags(node))
1334
- mix.splice(tag.index, 0, tag);
1335
- return mix;
1336
- }
1337
- export function isHandwritten(decl) {
1338
- return hasExtAttribute(decl, IDLExtendedAttributes.HandWrittenImplementation);
1339
- }
1340
- export function isStringEnum(decl) {
1341
- return decl.elements.some(e => e.type === IDLStringType);
1342
- }
1343
- export function linearizeNamespaceMembers(entries) {
1344
- const linearized = [];
1345
- for (const entry of entries) {
1346
- linearized.push(entry);
1347
- if (isNamespace(entry))
1348
- linearized.push(...linearizeNamespaceMembers(entry.members));
1349
- }
1350
- return linearized;
1351
- }
1352
- export function extremumOfOrdinals(enumEntry) {
1353
- let low = 0;
1354
- let high = 0;
1355
- enumEntry.elements.forEach((member, index) => {
1356
- let value = index;
1357
- if ((typeof member.initializer === 'number') && !isStringEnum(enumEntry)) {
1358
- value = member.initializer;
1359
- }
1360
- if (low > value)
1361
- low = value;
1362
- if (high < value)
1363
- high = value;
1364
- });
1365
- return { low, high };
1366
- }
1367
- //# sourceMappingURL=idl.js.map