@idlizer/core 2.1.2 → 2.1.7
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.
- package/build/lib/src/LanguageWriters/ArgConvertors.d.ts +3 -4
- package/build/lib/src/LanguageWriters/ArgConvertors.js +43 -32
- package/build/lib/src/LanguageWriters/LanguageWriter.d.ts +35 -17
- package/build/lib/src/LanguageWriters/LanguageWriter.js +30 -56
- package/build/lib/src/LanguageWriters/convertors/CJConvertors.d.ts +3 -1
- package/build/lib/src/LanguageWriters/convertors/CJConvertors.js +32 -11
- package/build/lib/src/LanguageWriters/convertors/CppConvertors.d.ts +5 -2
- package/build/lib/src/LanguageWriters/convertors/CppConvertors.js +71 -62
- package/build/lib/src/LanguageWriters/convertors/ETSConvertors.d.ts +1 -1
- package/build/lib/src/LanguageWriters/convertors/ETSConvertors.js +9 -19
- package/build/lib/src/LanguageWriters/convertors/InteropConvertors.d.ts +4 -2
- package/build/lib/src/LanguageWriters/convertors/InteropConvertors.js +10 -3
- package/build/lib/src/LanguageWriters/convertors/JavaConvertors.d.ts +2 -1
- package/build/lib/src/LanguageWriters/convertors/JavaConvertors.js +24 -4
- package/build/lib/src/LanguageWriters/convertors/TSConvertors.d.ts +8 -4
- package/build/lib/src/LanguageWriters/convertors/TSConvertors.js +87 -33
- package/build/lib/src/LanguageWriters/nameConvertor.d.ts +3 -1
- package/build/lib/src/LanguageWriters/nameConvertor.js +5 -1
- package/build/lib/src/LanguageWriters/writers/CJLanguageWriter.d.ts +19 -11
- package/build/lib/src/LanguageWriters/writers/CJLanguageWriter.js +34 -47
- package/build/lib/src/LanguageWriters/writers/CLikeLanguageWriter.d.ts +4 -1
- package/build/lib/src/LanguageWriters/writers/CLikeLanguageWriter.js +1 -1
- package/build/lib/src/LanguageWriters/writers/CppLanguageWriter.d.ts +9 -9
- package/build/lib/src/LanguageWriters/writers/CppLanguageWriter.js +15 -24
- package/build/lib/src/LanguageWriters/writers/ETSLanguageWriter.d.ts +17 -8
- package/build/lib/src/LanguageWriters/writers/ETSLanguageWriter.js +48 -23
- package/build/lib/src/LanguageWriters/writers/JavaLanguageWriter.d.ts +4 -6
- package/build/lib/src/LanguageWriters/writers/JavaLanguageWriter.js +15 -14
- package/build/lib/src/LanguageWriters/writers/TsLanguageWriter.d.ts +13 -8
- package/build/lib/src/LanguageWriters/writers/TsLanguageWriter.js +33 -52
- package/build/lib/src/LibraryInterface.d.ts +1 -4
- package/build/lib/src/config.d.ts +813 -110
- package/build/lib/src/config.js +25 -3
- package/build/lib/src/configDescriber.d.ts +31 -4
- package/build/lib/src/configDescriber.js +101 -3
- package/build/lib/src/from-idl/DtsPrinter.js +31 -18
- package/build/lib/src/from-idl/IDLLinter.d.ts +40 -7
- package/build/lib/src/from-idl/IDLLinter.js +211 -25
- package/build/lib/src/from-idl/common.js +1 -1
- package/build/lib/src/from-idl/deserialize.d.ts +10 -1
- package/build/lib/src/from-idl/deserialize.js +486 -352
- package/build/lib/src/idl.d.ts +29 -19
- package/build/lib/src/idl.js +404 -119
- package/build/lib/src/idlize.d.ts +2 -1
- package/build/lib/src/idlize.js +82 -26
- package/build/lib/src/index.d.ts +4 -3
- package/build/lib/src/index.js +3 -2
- package/build/lib/src/options.d.ts +1 -1
- package/build/lib/src/peer-generation/BuilderClass.d.ts +0 -2
- package/build/lib/src/peer-generation/BuilderClass.js +0 -8
- package/build/lib/src/peer-generation/LayoutManager.d.ts +10 -3
- package/build/lib/src/peer-generation/LayoutManager.js +3 -2
- package/build/lib/src/peer-generation/Materialized.d.ts +1 -1
- package/build/lib/src/peer-generation/Materialized.js +3 -3
- package/build/lib/src/peer-generation/PeerClass.d.ts +3 -8
- package/build/lib/src/peer-generation/PeerClass.js +0 -1
- package/build/lib/src/peer-generation/PeerFile.d.ts +1 -2
- package/build/lib/src/peer-generation/PeerFile.js +1 -1
- package/build/lib/src/peer-generation/PeerLibrary.d.ts +12 -7
- package/build/lib/src/peer-generation/PeerLibrary.js +131 -67
- package/build/lib/src/peer-generation/ReferenceResolver.d.ts +1 -1
- package/build/lib/src/peer-generation/ReferenceResolver.js +2 -2
- package/build/lib/src/peer-generation/idl/IdlNameConvertor.d.ts +1 -0
- package/build/lib/src/peer-generation/idl/IdlNameConvertor.js +6 -2
- package/build/lib/src/peer-generation/idl/common.d.ts +3 -1
- package/build/lib/src/peer-generation/idl/common.js +21 -4
- package/build/lib/src/peer-generation/isMaterialized.js +28 -7
- package/build/lib/src/peer-generation/unions.d.ts +3 -2
- package/build/lib/src/peer-generation/unions.js +7 -3
- package/build/lib/src/resolveNamedNode.d.ts +3 -0
- package/build/lib/src/resolveNamedNode.js +105 -0
- package/build/lib/src/util.d.ts +8 -1
- package/build/lib/src/util.js +41 -3
- package/build/lib/src/visitor.d.ts +2 -1
- package/build/lib/src/visitor.js +109 -7
- package/package.json +2 -2
- package/webidl2.js/LICENSE +21 -0
- package/webidl2.js/README.md +827 -0
- package/webidl2.js/dist/package.json +3 -0
- package/webidl2.js/dist/webidl2.js +93 -19
|
@@ -18,385 +18,517 @@ import { isAttribute, isCallback, isClass, isConstant, isConstructor, isDictiona
|
|
|
18
18
|
import { toString } from "./toString";
|
|
19
19
|
import * as idl from "../idl";
|
|
20
20
|
import { isDefined, warn } from "../util";
|
|
21
|
-
import { generateSyntheticUnionName } from "../peer-generation/idl/common";
|
|
21
|
+
import { collapseTypes, generateSyntheticUnionName } from "../peer-generation/idl/common";
|
|
22
|
+
function getTokens(node) {
|
|
23
|
+
return node.tokens;
|
|
24
|
+
}
|
|
22
25
|
const syntheticTypes = new Map();
|
|
23
26
|
export function addSyntheticType(name, type) {
|
|
24
|
-
if (syntheticTypes.has(name))
|
|
25
|
-
warn(`duplicate synthetic type name "${name}"`);
|
|
27
|
+
if (syntheticTypes.has(name)) {
|
|
28
|
+
warn(`duplicate synthetic type name "${name}"`);
|
|
29
|
+
}
|
|
26
30
|
syntheticTypes.set(name, type);
|
|
27
31
|
} // check
|
|
28
32
|
export function resolveSyntheticType(type) {
|
|
29
33
|
return syntheticTypes.get(type.name);
|
|
30
34
|
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
function toIDLNodeForward(file, node) {
|
|
35
|
-
if (isEnum(node)) {
|
|
36
|
-
return toIDLEnum(file, node);
|
|
35
|
+
class IDLDeserializer {
|
|
36
|
+
enterGenericScope(generics) {
|
|
37
|
+
this.genericsScopes.push(new Set(generics !== null && generics !== void 0 ? generics : []));
|
|
37
38
|
}
|
|
38
|
-
|
|
39
|
-
|
|
39
|
+
constructor(info, inheritanceMode = 'multiple') {
|
|
40
|
+
this.info = info;
|
|
41
|
+
this.inheritanceMode = inheritanceMode;
|
|
42
|
+
this.namespacePathNames = [];
|
|
43
|
+
this.currentPackage = [];
|
|
44
|
+
this.genericsScopes = [];
|
|
40
45
|
}
|
|
41
|
-
|
|
42
|
-
|
|
46
|
+
///
|
|
47
|
+
withInfo(from, result) {
|
|
48
|
+
this.info.set(result, getTokens(from));
|
|
49
|
+
return result;
|
|
43
50
|
}
|
|
44
|
-
|
|
45
|
-
|
|
51
|
+
setPackage(pkg) {
|
|
52
|
+
this.currentPackage = pkg;
|
|
46
53
|
}
|
|
47
|
-
|
|
48
|
-
|
|
54
|
+
///
|
|
55
|
+
sanitizeTypeParameter(param) {
|
|
56
|
+
const extendsIdx = param.indexOf('extends');
|
|
57
|
+
if (extendsIdx !== -1) {
|
|
58
|
+
return param.substring(0, extendsIdx).trim();
|
|
59
|
+
}
|
|
60
|
+
const eqIdx = param.indexOf('=');
|
|
61
|
+
if (eqIdx !== -1) {
|
|
62
|
+
return param.substring(0, eqIdx).trim();
|
|
63
|
+
}
|
|
64
|
+
return param;
|
|
49
65
|
}
|
|
50
|
-
|
|
51
|
-
|
|
66
|
+
extractGenerics(extAttrs) {
|
|
67
|
+
var _a, _b;
|
|
68
|
+
return (_b = (_a = this.findExtendedAttribute(extAttrs, idl.IDLExtendedAttributes.TypeParameters)) === null || _a === void 0 ? void 0 : _a.split(",")) === null || _b === void 0 ? void 0 : _b.map(it => this.sanitizeTypeParameter(it));
|
|
52
69
|
}
|
|
53
|
-
|
|
54
|
-
|
|
70
|
+
///
|
|
71
|
+
toIDLNode(file, node) {
|
|
72
|
+
return this.toIDLNodeForward(file, node);
|
|
55
73
|
}
|
|
56
|
-
|
|
57
|
-
|
|
74
|
+
toIDLNodeForward(file, node) {
|
|
75
|
+
if (isEnum(node)) {
|
|
76
|
+
return this.toIDLEnum(file, node);
|
|
77
|
+
}
|
|
78
|
+
if (this.isImport(node)) {
|
|
79
|
+
return this.toIDLImport(node);
|
|
80
|
+
}
|
|
81
|
+
if (isClass(node)) {
|
|
82
|
+
return this.toIDLInterface(file, node);
|
|
83
|
+
}
|
|
84
|
+
if (isInterface(node)) {
|
|
85
|
+
return this.toIDLInterface(file, node);
|
|
86
|
+
}
|
|
87
|
+
if (isCallback(node)) {
|
|
88
|
+
return this.toIDLCallback(file, node);
|
|
89
|
+
}
|
|
90
|
+
if (isTypedef(node)) {
|
|
91
|
+
return this.toIDLTypedef(file, node);
|
|
92
|
+
}
|
|
93
|
+
if (isDictionary(node)) {
|
|
94
|
+
return this.toIDLDictionary(file, node);
|
|
95
|
+
}
|
|
96
|
+
if (this.isNamespace(node)) {
|
|
97
|
+
return this.toIDLNamespace(file, node);
|
|
98
|
+
}
|
|
99
|
+
if (this.isVersion(node)) {
|
|
100
|
+
return this.toIDLVersion(file, node);
|
|
101
|
+
}
|
|
102
|
+
if (isAttribute(node)) {
|
|
103
|
+
return this.toIDLProperty(file, node);
|
|
104
|
+
}
|
|
105
|
+
if (isOperation(node)) {
|
|
106
|
+
return this.toIDLMethod(file, node, true);
|
|
107
|
+
}
|
|
108
|
+
if (isConstant(node)) {
|
|
109
|
+
return this.toIDLConstant(file, node);
|
|
110
|
+
}
|
|
111
|
+
throw new Error(`unexpected node type: ${toString(node)}`);
|
|
58
112
|
}
|
|
59
|
-
|
|
60
|
-
return
|
|
113
|
+
toIDLImport(node) {
|
|
114
|
+
return this.withInfo(node, idl.createImport(node.clause.split("."), node.alias || undefined));
|
|
61
115
|
}
|
|
62
|
-
|
|
63
|
-
|
|
116
|
+
interfaceSubkind(node) {
|
|
117
|
+
var _a, _b;
|
|
118
|
+
const nodeIDLEntity = (_b = (_a = node.extAttrs.find(it => it.name === "Entity")) === null || _a === void 0 ? void 0 : _a.rhs) === null || _b === void 0 ? void 0 : _b.value;
|
|
119
|
+
if (nodeIDLEntity == idl.IDLEntity.Class)
|
|
120
|
+
return idl.IDLInterfaceSubkind.Class;
|
|
121
|
+
if (nodeIDLEntity == idl.IDLEntity.Interface)
|
|
122
|
+
return idl.IDLInterfaceSubkind.Interface;
|
|
123
|
+
if (nodeIDLEntity == idl.IDLEntity.Tuple)
|
|
124
|
+
return idl.IDLInterfaceSubkind.Tuple;
|
|
125
|
+
return idl.IDLInterfaceSubkind.Interface;
|
|
64
126
|
}
|
|
65
|
-
|
|
66
|
-
|
|
127
|
+
toIDLInterface(file, node) {
|
|
128
|
+
const generics = this.extractGenerics(node.extAttrs);
|
|
129
|
+
this.enterGenericScope(generics);
|
|
130
|
+
const subkind = this.interfaceSubkind(node);
|
|
131
|
+
const result = idl.createInterface(node.name, subkind, (() => {
|
|
132
|
+
if (!node.inheritance) {
|
|
133
|
+
return [];
|
|
134
|
+
}
|
|
135
|
+
const implementations = [];
|
|
136
|
+
let extension = undefined;
|
|
137
|
+
node.inheritance.forEach(it => {
|
|
138
|
+
var _a;
|
|
139
|
+
const attributes = it.extAttrs;
|
|
140
|
+
const parentTypeArgs = this.extractTypeArguments(file, attributes !== null && attributes !== void 0 ? attributes : [], idl.IDLExtendedAttributes.TypeArguments);
|
|
141
|
+
const attrs = (_a = this.toExtendedAttributes(attributes !== null && attributes !== void 0 ? attributes : [])) === null || _a === void 0 ? void 0 : _a.filter(it => it.name !== idl.IDLExtendedAttributes.TypeArguments);
|
|
142
|
+
const baseClass = attrs === null || attrs === void 0 ? void 0 : attrs.find(it => it.name === idl.IDLExtendedAttributes.Extends);
|
|
143
|
+
const ref = idl.createReferenceType(it.inheritance, parentTypeArgs, {
|
|
144
|
+
extendedAttributes: attrs
|
|
145
|
+
});
|
|
146
|
+
if (baseClass) {
|
|
147
|
+
extension = ref;
|
|
148
|
+
}
|
|
149
|
+
else {
|
|
150
|
+
implementations.push(ref);
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
if (subkind === idl.IDLInterfaceSubkind.Class && extension === undefined && this.inheritanceMode === 'multiple') {
|
|
154
|
+
extension = idl.IDLTopType;
|
|
155
|
+
}
|
|
156
|
+
if (extension) {
|
|
157
|
+
return [extension, ...implementations];
|
|
158
|
+
}
|
|
159
|
+
return implementations;
|
|
160
|
+
})(), node.members
|
|
161
|
+
.filter(isConstructor)
|
|
162
|
+
.map(it => this.toIDLConstructor(file, it)), [], node.members
|
|
163
|
+
.filter(isAttribute)
|
|
164
|
+
.map(it => this.toIDLProperty(file, it)), node.members
|
|
165
|
+
.filter(isOperation)
|
|
166
|
+
.filter(it => !this.isCallable(it))
|
|
167
|
+
.map(it => this.toIDLMethod(file, it, false)), node.members
|
|
168
|
+
.filter(isOperation)
|
|
169
|
+
.filter(it => this.isCallable(it))
|
|
170
|
+
.map(it => this.toIDLCallable(file, it)), generics, {
|
|
171
|
+
fileName: file,
|
|
172
|
+
documentation: this.makeDocs(node),
|
|
173
|
+
extendedAttributes: this.toExtendedAttributes(node.extAttrs),
|
|
174
|
+
});
|
|
175
|
+
this.genericsScopes.pop();
|
|
176
|
+
this.info.set(result, getTokens(node));
|
|
177
|
+
if (node.extAttrs.find(it => it.name === "Synthetic")) {
|
|
178
|
+
const fqName = this.currentPackage.concat(this.namespacePathNames).concat([node.name]).join('.');
|
|
179
|
+
addSyntheticType(fqName, result);
|
|
180
|
+
}
|
|
181
|
+
return result;
|
|
67
182
|
}
|
|
68
|
-
|
|
69
|
-
|
|
183
|
+
toIDLType(file, type, extAttrs, suggestedName) {
|
|
184
|
+
var _a;
|
|
185
|
+
if (typeof type === "string") {
|
|
186
|
+
// is it IDLStringType?
|
|
187
|
+
const refType = idl.createReferenceType(type);
|
|
188
|
+
refType.fileName = file;
|
|
189
|
+
refType.typeArguments = this.extractTypeArguments(file, extAttrs, idl.IDLExtendedAttributes.TypeArguments);
|
|
190
|
+
return refType;
|
|
191
|
+
}
|
|
192
|
+
if (type.nullable) {
|
|
193
|
+
return this.withInfo(type, idl.createOptionalType(this.toIDLType(file, Object.assign(Object.assign({}, type), { nullable: false }), extAttrs)));
|
|
194
|
+
}
|
|
195
|
+
if (isUnionTypeDescription(type)) {
|
|
196
|
+
let types = type.idlType
|
|
197
|
+
.map(it => this.toIDLType(file, it, undefined))
|
|
198
|
+
.filter(isDefined);
|
|
199
|
+
if (types.includes(idl.IDLUndefinedType)) {
|
|
200
|
+
types = types.filter(it => it !== idl.IDLUndefinedType);
|
|
201
|
+
return this.withInfo(type, idl.createOptionalType(collapseTypes(types)));
|
|
202
|
+
}
|
|
203
|
+
const name = suggestedName !== null && suggestedName !== void 0 ? suggestedName : generateSyntheticUnionName(types);
|
|
204
|
+
return this.withInfo(type, idl.createUnionType(types, name));
|
|
205
|
+
}
|
|
206
|
+
if (isSingleTypeDescription(type)) {
|
|
207
|
+
// must match with primitive types in idl.ts
|
|
208
|
+
switch (type.idlType) {
|
|
209
|
+
case idl.IDLPointerType.name: return idl.IDLPointerType;
|
|
210
|
+
case idl.IDLVoidType.name: return idl.IDLVoidType;
|
|
211
|
+
case idl.IDLBooleanType.name: return idl.IDLBooleanType;
|
|
212
|
+
case idl.IDLObjectType.name: return idl.IDLObjectType;
|
|
213
|
+
case idl.IDLI8Type.name: return idl.IDLI8Type;
|
|
214
|
+
case idl.IDLU8Type.name: return idl.IDLU8Type;
|
|
215
|
+
case idl.IDLI16Type.name: return idl.IDLI16Type;
|
|
216
|
+
case idl.IDLU16Type.name: return idl.IDLU16Type;
|
|
217
|
+
case idl.IDLI32Type.name: return idl.IDLI32Type;
|
|
218
|
+
case idl.IDLU32Type.name: return idl.IDLU32Type;
|
|
219
|
+
case idl.IDLI64Type.name: return idl.IDLI64Type;
|
|
220
|
+
case idl.IDLU64Type.name: return idl.IDLU64Type;
|
|
221
|
+
case idl.IDLF32Type.name: return idl.IDLF32Type;
|
|
222
|
+
case idl.IDLF64Type.name: return idl.IDLF64Type;
|
|
223
|
+
case idl.IDLBigintType.name: return idl.IDLBigintType;
|
|
224
|
+
case idl.IDLNumberType.name: return idl.IDLNumberType;
|
|
225
|
+
case idl.IDLStringType.name: return idl.IDLStringType;
|
|
226
|
+
case idl.IDLAnyType.name: return idl.IDLAnyType;
|
|
227
|
+
case idl.IDLUndefinedType.name: return idl.IDLUndefinedType;
|
|
228
|
+
case idl.IDLUnknownType.name: return idl.IDLUnknownType;
|
|
229
|
+
case idl.IDLObjectType.name: return idl.IDLObjectType;
|
|
230
|
+
case idl.IDLThisType.name: return idl.IDLThisType;
|
|
231
|
+
case idl.IDLDate.name: return idl.IDLDate;
|
|
232
|
+
case idl.IDLBufferType.name: return idl.IDLBufferType;
|
|
233
|
+
case idl.IDLSerializerBuffer.name: return idl.IDLSerializerBuffer;
|
|
234
|
+
}
|
|
235
|
+
const combinedExtAttrs = ((_a = type.extAttrs) !== null && _a !== void 0 ? _a : []).concat(extAttrs !== null && extAttrs !== void 0 ? extAttrs : []);
|
|
236
|
+
let idlRefType;
|
|
237
|
+
if (this.genericsScopes.some(it => it.has(type.idlType))) {
|
|
238
|
+
idlRefType = idl.createTypeParameterReference(type.idlType);
|
|
239
|
+
}
|
|
240
|
+
else {
|
|
241
|
+
const ref = idl.createReferenceType(type.idlType);
|
|
242
|
+
ref.typeArguments = this.extractTypeArguments(file, combinedExtAttrs, idl.IDLExtendedAttributes.TypeArguments);
|
|
243
|
+
idlRefType = ref;
|
|
244
|
+
}
|
|
245
|
+
idlRefType.fileName = file;
|
|
246
|
+
idlRefType.extendedAttributes = this.toExtendedAttributes(combinedExtAttrs);
|
|
247
|
+
return this.withInfo(type, idlRefType);
|
|
248
|
+
}
|
|
249
|
+
if (isSequenceTypeDescription(type) || isPromiseTypeDescription(type) || isRecordTypeDescription(type)) {
|
|
250
|
+
return this.withInfo(type, idl.createContainerType(type.generic, type.idlType.map(it => this.toIDLType(file, it, undefined))));
|
|
251
|
+
}
|
|
252
|
+
if (isUnspecifiedGenericTypeDescription(type)) {
|
|
253
|
+
return this.withInfo(type, idl.createUnspecifiedGenericType(type.generic, type.idlType.map(it => this.toIDLType(file, it, undefined))));
|
|
254
|
+
}
|
|
255
|
+
throw new Error(`unexpected type: ${toString(type)}`);
|
|
70
256
|
}
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
function interfaceSubkind(node) {
|
|
93
|
-
var _a, _b;
|
|
94
|
-
const nodeIDLEntity = (_b = (_a = node.extAttrs.find(it => it.name === "Entity")) === null || _a === void 0 ? void 0 : _a.rhs) === null || _b === void 0 ? void 0 : _b.value;
|
|
95
|
-
if (nodeIDLEntity == idl.IDLEntity.Class)
|
|
96
|
-
return idl.IDLInterfaceSubkind.Class;
|
|
97
|
-
if (nodeIDLEntity == idl.IDLEntity.Interface)
|
|
98
|
-
return idl.IDLInterfaceSubkind.Interface;
|
|
99
|
-
if (nodeIDLEntity == idl.IDLEntity.Tuple)
|
|
100
|
-
return idl.IDLInterfaceSubkind.Tuple;
|
|
101
|
-
return idl.IDLInterfaceSubkind.Interface;
|
|
102
|
-
}
|
|
103
|
-
function toIDLInterface(file, node) {
|
|
104
|
-
var _a;
|
|
105
|
-
const result = idl.createInterface(node.name, interfaceSubkind(node), (() => {
|
|
106
|
-
var _a, _b;
|
|
107
|
-
if (!node.inheritance)
|
|
108
|
-
return [];
|
|
109
|
-
const parentTypeArgs = extractTypeArguments(file, (_a = node.inheritanceExtAttrs) !== null && _a !== void 0 ? _a : [], idl.IDLExtendedAttributes.TypeArguments);
|
|
110
|
-
const parentType = idl.createReferenceType(node.inheritance, parentTypeArgs);
|
|
111
|
-
parentType.fileName = file;
|
|
112
|
-
if (node.inheritanceExtAttrs)
|
|
113
|
-
parentType.extendedAttributes = (_b = toExtendedAttributes(node.inheritanceExtAttrs)) === null || _b === void 0 ? void 0 : _b.filter(it => it.name !== idl.IDLExtendedAttributes.TypeArguments);
|
|
114
|
-
return [parentType];
|
|
115
|
-
})(), node.members
|
|
116
|
-
.filter(isConstructor)
|
|
117
|
-
.map(it => toIDLConstructor(file, it)), [], node.members
|
|
118
|
-
.filter(isAttribute)
|
|
119
|
-
.map(it => toIDLProperty(file, it)), node.members
|
|
120
|
-
.filter(isOperation)
|
|
121
|
-
.filter(it => !isCallable(it))
|
|
122
|
-
.map(it => toIDLMethod(file, it)), node.members
|
|
123
|
-
.filter(isOperation)
|
|
124
|
-
.filter(it => isCallable(it))
|
|
125
|
-
.map(it => toIDLCallable(file, it)), (_a = findExtendedAttribute(node.extAttrs, idl.IDLExtendedAttributes.TypeParameters)) === null || _a === void 0 ? void 0 : _a.split(","), {
|
|
126
|
-
fileName: file,
|
|
127
|
-
documentation: makeDocs(node),
|
|
128
|
-
extendedAttributes: toExtendedAttributes(node.extAttrs),
|
|
129
|
-
});
|
|
130
|
-
if (node.extAttrs.find(it => it.name === "Synthetic"))
|
|
131
|
-
addSyntheticType(node.name, result);
|
|
132
|
-
return result;
|
|
133
|
-
}
|
|
134
|
-
function extractTypeArguments(file, extAttrs, attribute) {
|
|
135
|
-
var _a;
|
|
136
|
-
const attr = extAttrs === null || extAttrs === void 0 ? void 0 : extAttrs.find(it => it.name === attribute);
|
|
137
|
-
if (!attr)
|
|
138
|
-
return undefined;
|
|
139
|
-
let value = toExtendedAttributeValue(attr);
|
|
140
|
-
return (_a = value === null || value === void 0 ? void 0 : value.split(",") // TODO need real parsing here. What about "<T, Map<K, Callback<K,R>>, U>"
|
|
141
|
-
) === null || _a === void 0 ? void 0 : _a.map(it => toIDLType(file, it));
|
|
142
|
-
}
|
|
143
|
-
function toIDLType(file, type, extAttrs) {
|
|
144
|
-
var _a;
|
|
145
|
-
if (typeof type === "string") {
|
|
146
|
-
// is it IDLStringType?
|
|
147
|
-
const refType = idl.createReferenceType(type);
|
|
148
|
-
refType.fileName = file;
|
|
149
|
-
refType.typeArguments = extractTypeArguments(file, extAttrs, idl.IDLExtendedAttributes.TypeArguments);
|
|
150
|
-
return refType;
|
|
151
|
-
}
|
|
152
|
-
if (type.nullable) {
|
|
153
|
-
return idl.createOptionalType(toIDLType(file, Object.assign(Object.assign({}, type), { nullable: false }), extAttrs));
|
|
154
|
-
}
|
|
155
|
-
if (isUnionTypeDescription(type)) {
|
|
156
|
-
const types = type.idlType
|
|
157
|
-
.map(it => toIDLType(file, it))
|
|
158
|
-
.filter(isDefined);
|
|
159
|
-
const name = generateSyntheticUnionName(types);
|
|
160
|
-
return idl.createUnionType(types, name);
|
|
161
|
-
}
|
|
162
|
-
if (isSingleTypeDescription(type)) {
|
|
163
|
-
switch (type.idlType) {
|
|
164
|
-
case idl.IDLUnknownType.name: return idl.IDLUnknownType;
|
|
165
|
-
case idl.IDLObjectType.name: return idl.IDLObjectType;
|
|
166
|
-
case idl.IDLAnyType.name: return idl.IDLAnyType;
|
|
167
|
-
case idl.IDLBooleanType.name: return idl.IDLBooleanType;
|
|
168
|
-
case idl.IDLNumberType.name: return idl.IDLNumberType;
|
|
169
|
-
case idl.IDLStringType.name: return idl.IDLStringType;
|
|
170
|
-
case idl.IDLUndefinedType.name: return idl.IDLUndefinedType;
|
|
171
|
-
case idl.IDLVoidType.name: return idl.IDLVoidType;
|
|
172
|
-
case idl.IDLI8Type.name: return idl.IDLI8Type;
|
|
173
|
-
case idl.IDLU8Type.name: return idl.IDLU8Type;
|
|
174
|
-
case idl.IDLI16Type.name: return idl.IDLI16Type;
|
|
175
|
-
case idl.IDLU16Type.name: return idl.IDLU16Type;
|
|
176
|
-
case idl.IDLI32Type.name: return idl.IDLI32Type;
|
|
177
|
-
case idl.IDLU32Type.name: return idl.IDLU32Type;
|
|
178
|
-
case idl.IDLI64Type.name: return idl.IDLI64Type;
|
|
179
|
-
case idl.IDLU64Type.name: return idl.IDLU64Type;
|
|
180
|
-
case idl.IDLF32Type.name: return idl.IDLF32Type;
|
|
181
|
-
case idl.IDLF64Type.name: return idl.IDLF64Type;
|
|
182
|
-
case idl.IDLPointerType.name: return idl.IDLPointerType;
|
|
183
|
-
case idl.IDLBufferType.name: return idl.IDLBufferType;
|
|
184
|
-
}
|
|
185
|
-
const combinedExtAttrs = ((_a = type.extAttrs) !== null && _a !== void 0 ? _a : []).concat(extAttrs !== null && extAttrs !== void 0 ? extAttrs : []);
|
|
186
|
-
const idlRefType = idl.createReferenceType(type.idlType);
|
|
187
|
-
idlRefType.fileName = file;
|
|
188
|
-
idlRefType.typeArguments = extractTypeArguments(file, combinedExtAttrs, idl.IDLExtendedAttributes.TypeArguments);
|
|
189
|
-
idlRefType.extendedAttributes = toExtendedAttributes(combinedExtAttrs);
|
|
190
|
-
return idlRefType;
|
|
191
|
-
}
|
|
192
|
-
if (isSequenceTypeDescription(type) || isPromiseTypeDescription(type) || isRecordTypeDescription(type)) {
|
|
193
|
-
return idl.createContainerType(type.generic, type.idlType.map(it => toIDLType(file, it)));
|
|
194
|
-
}
|
|
195
|
-
if (isUnspecifiedGenericTypeDescription(type)) {
|
|
196
|
-
return idl.createUnspecifiedGenericType(type.generic, type.idlType.map(it => toIDLType(file, it)));
|
|
197
|
-
}
|
|
198
|
-
throw new Error(`unexpected type: ${toString(type)}`);
|
|
199
|
-
}
|
|
200
|
-
function toIDLCallable(file, node) {
|
|
201
|
-
var _a, _b;
|
|
202
|
-
if (!node.idlType) {
|
|
203
|
-
throw new Error(`method with no type ${toString(node)}`);
|
|
204
|
-
}
|
|
205
|
-
const returnType = toIDLType(file, node.idlType, node.extAttrs);
|
|
206
|
-
if (idl.isReferenceType(returnType)) {
|
|
207
|
-
const returnTypeArgs = extractTypeArguments(file, node.extAttrs, idl.IDLExtendedAttributes.TypeArguments);
|
|
208
|
-
returnType.typeArguments = returnTypeArgs;
|
|
209
|
-
}
|
|
210
|
-
return idl.createCallable((_a = node.name) !== null && _a !== void 0 ? _a : "", node.arguments.map(it => toIDLParameter(file, it)), returnType, {
|
|
211
|
-
isStatic: node.special === "static",
|
|
212
|
-
isAsync: node.async,
|
|
213
|
-
}, {
|
|
214
|
-
documentation: makeDocs(node),
|
|
215
|
-
extendedAttributes: toExtendedAttributes(node.extAttrs),
|
|
216
|
-
}, (_b = findExtendedAttribute(node.extAttrs, idl.IDLExtendedAttributes.TypeParameters)) === null || _b === void 0 ? void 0 : _b.split(","));
|
|
217
|
-
}
|
|
218
|
-
function toIDLMethod(file, node) {
|
|
219
|
-
var _a, _b;
|
|
220
|
-
if (!node.idlType) {
|
|
221
|
-
throw new Error(`method with no type ${toString(node)}`);
|
|
222
|
-
}
|
|
223
|
-
const returnType = toIDLType(file, node.idlType, node.extAttrs);
|
|
224
|
-
if (idl.isReferenceType(returnType))
|
|
225
|
-
returnType.typeArguments = extractTypeArguments(file, node.extAttrs, idl.IDLExtendedAttributes.TypeArguments);
|
|
226
|
-
return idl.createMethod((_a = node.name) !== null && _a !== void 0 ? _a : "", node.arguments.map(it => toIDLParameter(file, it)), returnType, {
|
|
227
|
-
isStatic: node.special === "static",
|
|
228
|
-
isAsync: node.async,
|
|
229
|
-
isOptional: isOptional(node),
|
|
230
|
-
isFree: false, // TODO: namespace-related-to-rework
|
|
231
|
-
}, {
|
|
232
|
-
documentation: makeDocs(node),
|
|
233
|
-
extendedAttributes: toExtendedAttributes(node.extAttrs),
|
|
234
|
-
}, (_b = findExtendedAttribute(node.extAttrs, idl.IDLExtendedAttributes.TypeParameters)) === null || _b === void 0 ? void 0 : _b.split(","));
|
|
235
|
-
}
|
|
236
|
-
function toIDLConstructor(file, node) {
|
|
237
|
-
return idl.createConstructor(node.arguments.map(it => toIDLParameter(file, it)), undefined, {
|
|
238
|
-
documentation: makeDocs(node),
|
|
239
|
-
});
|
|
240
|
-
}
|
|
241
|
-
function toIDLParameter(file, node) {
|
|
242
|
-
return idl.createParameter(node.name, toIDLType(file, node.idlType, node.extAttrs), node.optional, node.variadic, {
|
|
243
|
-
fileName: file,
|
|
244
|
-
});
|
|
245
|
-
}
|
|
246
|
-
function toIDLCallback(file, node) {
|
|
247
|
-
const result = idl.createCallback(node.name, node.arguments.map(it => toIDLParameter(file, it)), toIDLType(file, node.idlType), {
|
|
248
|
-
fileName: file,
|
|
249
|
-
extendedAttributes: toExtendedAttributes(node.extAttrs),
|
|
250
|
-
documentation: makeDocs(node),
|
|
251
|
-
});
|
|
252
|
-
if (node.extAttrs.find(it => it.name === "Synthetic"))
|
|
253
|
-
addSyntheticType(node.name, result);
|
|
254
|
-
return result;
|
|
255
|
-
}
|
|
256
|
-
function toIDLTypedef(file, node) {
|
|
257
|
-
var _a;
|
|
258
|
-
return idl.createTypedef(node.name, toIDLType(file, node.idlType), (_a = findExtendedAttribute(node.extAttrs, idl.IDLExtendedAttributes.TypeParameters)) === null || _a === void 0 ? void 0 : _a.split(","), {
|
|
259
|
-
extendedAttributes: toExtendedAttributes(node.extAttrs),
|
|
260
|
-
documentation: makeDocs(node),
|
|
261
|
-
fileName: file,
|
|
262
|
-
});
|
|
263
|
-
}
|
|
264
|
-
function toIDLConstant(file, node) {
|
|
265
|
-
return idl.createConstant(node.name, toIDLType(file, node.idlType), constantValue(node));
|
|
266
|
-
}
|
|
267
|
-
function constantValue(node) {
|
|
268
|
-
switch (node.value.type) {
|
|
269
|
-
case "string":
|
|
270
|
-
return `"${node.value.value}"`;
|
|
271
|
-
case "number":
|
|
272
|
-
return node.value.value;
|
|
273
|
-
case "boolean":
|
|
274
|
-
return node.value.value.toString();
|
|
275
|
-
case "null":
|
|
276
|
-
return "null";
|
|
277
|
-
case "Infinity":
|
|
278
|
-
return "Infinity";
|
|
279
|
-
case "NaN":
|
|
280
|
-
return "NaN";
|
|
281
|
-
case "sequence":
|
|
282
|
-
return `[${node.value.value.join(',')}]`;
|
|
283
|
-
case "dictionary":
|
|
284
|
-
return `new Map()`;
|
|
285
|
-
default:
|
|
286
|
-
return "undefined";
|
|
257
|
+
toIDLCallable(file, node) {
|
|
258
|
+
var _a;
|
|
259
|
+
if (!node.idlType) {
|
|
260
|
+
throw new Error(`method with no type ${toString(node)}`);
|
|
261
|
+
}
|
|
262
|
+
const generics = this.extractGenerics(node.extAttrs);
|
|
263
|
+
this.enterGenericScope(generics);
|
|
264
|
+
const returnType = this.toIDLType(file, node.idlType, node.extAttrs);
|
|
265
|
+
if (idl.isReferenceType(returnType)) {
|
|
266
|
+
const returnTypeArgs = this.extractTypeArguments(file, node.extAttrs, idl.IDLExtendedAttributes.TypeArguments);
|
|
267
|
+
returnType.typeArguments = returnTypeArgs;
|
|
268
|
+
}
|
|
269
|
+
const result = this.withInfo(node, idl.createCallable((_a = node.name) !== null && _a !== void 0 ? _a : "", node.arguments.map(it => this.toIDLParameter(file, it)), returnType, {
|
|
270
|
+
isStatic: node.special === "static",
|
|
271
|
+
isAsync: node.async,
|
|
272
|
+
}, {
|
|
273
|
+
documentation: this.makeDocs(node),
|
|
274
|
+
extendedAttributes: this.toExtendedAttributes(node.extAttrs),
|
|
275
|
+
}, generics));
|
|
276
|
+
this.genericsScopes.pop();
|
|
277
|
+
return result;
|
|
287
278
|
}
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
extendedAttributes: toExtendedAttributes(node.extAttrs),
|
|
293
|
-
fileName: file,
|
|
294
|
-
});
|
|
295
|
-
result.elements = node.members.map(it => toIDLEnumMember(file, it, result));
|
|
296
|
-
return result;
|
|
297
|
-
}
|
|
298
|
-
function toIDLNamespace(file, node) {
|
|
299
|
-
const namespace = idl.createNamespace(node.name, toExtendedAttributes(node.extAttrs), file);
|
|
300
|
-
namespace.members = node.members.map(it => toIDLNodeForward(file, it));
|
|
301
|
-
return namespace;
|
|
302
|
-
}
|
|
303
|
-
function toIDLVersion(file, node) {
|
|
304
|
-
return idl.createVersion(node.value, toExtendedAttributes(node.extAttrs), file);
|
|
305
|
-
}
|
|
306
|
-
function toIDLProperty(file, node) {
|
|
307
|
-
return idl.createProperty(node.name, toIDLType(file, node.idlType), node.readonly, node.special === "static", isOptional(node), {
|
|
308
|
-
documentation: makeDocs(node),
|
|
309
|
-
fileName: file,
|
|
310
|
-
extendedAttributes: toExtendedAttributes(node.extAttrs)
|
|
311
|
-
});
|
|
312
|
-
}
|
|
313
|
-
function unescapeString(value) {
|
|
314
|
-
if (!value.length || value[0] !== '"')
|
|
315
|
-
return value;
|
|
316
|
-
value = value.slice(1, -1);
|
|
317
|
-
value = value.replace(/\\((['"\\bfnrtv])|([0-7]{1-3})|x([0-9a-fA-F]{2})|u([0-9a-fA-F]{4}))/g, (_, all, c, oct, h2, u4) => {
|
|
318
|
-
if (c !== undefined) {
|
|
319
|
-
switch (c) {
|
|
320
|
-
case "'": return "'";
|
|
321
|
-
case '"': return '"';
|
|
322
|
-
case "\\": return "\\";
|
|
323
|
-
case "b": return "\b";
|
|
324
|
-
case "f": return "\f";
|
|
325
|
-
case "n": return "\n";
|
|
326
|
-
case "r": return "\r";
|
|
327
|
-
case "t": return "\t";
|
|
328
|
-
case "v": return "\v";
|
|
329
|
-
}
|
|
279
|
+
toIDLMethod(file, node, isFree = false) {
|
|
280
|
+
var _a;
|
|
281
|
+
if (!node.idlType) {
|
|
282
|
+
throw new Error(`method with no type ${toString(node)}`);
|
|
330
283
|
}
|
|
331
|
-
|
|
332
|
-
|
|
284
|
+
const generics = this.extractGenerics(node.extAttrs);
|
|
285
|
+
this.enterGenericScope(generics);
|
|
286
|
+
const returnType = this.toIDLType(file, node.idlType, node.extAttrs);
|
|
287
|
+
if (idl.isReferenceType(returnType))
|
|
288
|
+
returnType.typeArguments = this.extractTypeArguments(file, node.extAttrs, idl.IDLExtendedAttributes.TypeArguments);
|
|
289
|
+
const result = this.withInfo(node, idl.createMethod((_a = node.name) !== null && _a !== void 0 ? _a : "", node.arguments.map(it => this.toIDLParameter(file, it !== null && it !== void 0 ? it : new Map())), returnType, {
|
|
290
|
+
isStatic: node.special === "static",
|
|
291
|
+
isAsync: node.async,
|
|
292
|
+
isOptional: isOptional(node),
|
|
293
|
+
isFree
|
|
294
|
+
}, {
|
|
295
|
+
documentation: this.makeDocs(node),
|
|
296
|
+
extendedAttributes: this.toExtendedAttributes(node.extAttrs),
|
|
297
|
+
}, generics));
|
|
298
|
+
this.genericsScopes.pop();
|
|
299
|
+
return result;
|
|
300
|
+
}
|
|
301
|
+
toIDLConstructor(file, node) {
|
|
302
|
+
return this.withInfo(node, idl.createConstructor(node.arguments.map(it => this.toIDLParameter(file, it)), undefined, {
|
|
303
|
+
documentation: this.makeDocs(node),
|
|
304
|
+
}));
|
|
305
|
+
}
|
|
306
|
+
toIDLParameter(file, node) {
|
|
307
|
+
return this.withInfo(node, idl.createParameter(node.name, this.toIDLType(file, node.idlType, node.extAttrs), node.optional, node.variadic, {
|
|
308
|
+
fileName: file,
|
|
309
|
+
}));
|
|
310
|
+
}
|
|
311
|
+
toIDLCallback(file, node) {
|
|
312
|
+
const generics = this.extractGenerics(node.extAttrs);
|
|
313
|
+
this.enterGenericScope(generics);
|
|
314
|
+
const result = idl.createCallback(node.name, node.arguments.map(it => this.toIDLParameter(file, it)), this.toIDLType(file, node.idlType, undefined), {
|
|
315
|
+
fileName: file,
|
|
316
|
+
extendedAttributes: this.toExtendedAttributes(node.extAttrs),
|
|
317
|
+
documentation: this.makeDocs(node),
|
|
318
|
+
}, generics);
|
|
319
|
+
if (node.extAttrs.find(it => it.name === "Synthetic")) {
|
|
320
|
+
const fqName = this.currentPackage.concat(this.namespacePathNames).concat([node.name]).join('.');
|
|
321
|
+
addSyntheticType(fqName, result);
|
|
333
322
|
}
|
|
334
|
-
|
|
335
|
-
|
|
323
|
+
this.genericsScopes.pop();
|
|
324
|
+
return this.withInfo(node, result);
|
|
325
|
+
}
|
|
326
|
+
toIDLTypedef(file, node) {
|
|
327
|
+
const generics = this.extractGenerics(node.extAttrs);
|
|
328
|
+
this.enterGenericScope(generics);
|
|
329
|
+
const result = this.withInfo(node, idl.createTypedef(node.name, this.toIDLType(file, node.idlType, undefined, node.name), generics, {
|
|
330
|
+
extendedAttributes: this.toExtendedAttributes(node.extAttrs),
|
|
331
|
+
documentation: this.makeDocs(node),
|
|
332
|
+
fileName: file,
|
|
333
|
+
}));
|
|
334
|
+
this.genericsScopes.pop();
|
|
335
|
+
return result;
|
|
336
|
+
}
|
|
337
|
+
toIDLConstant(file, node) {
|
|
338
|
+
return this.withInfo(node, idl.createConstant(node.name, this.toIDLType(file, node.idlType, undefined), this.constantValue(node)));
|
|
339
|
+
}
|
|
340
|
+
toIDLDictionary(file, node) {
|
|
341
|
+
const result = idl.createEnum(node.name, [], {
|
|
342
|
+
documentation: this.makeDocs(node),
|
|
343
|
+
extendedAttributes: this.toExtendedAttributes(node.extAttrs),
|
|
344
|
+
fileName: file,
|
|
345
|
+
});
|
|
346
|
+
result.elements = node.members.map(it => this.toIDLEnumMember(file, it, result));
|
|
347
|
+
return this.withInfo(node, result);
|
|
348
|
+
}
|
|
349
|
+
toIDLNamespace(file, node) {
|
|
350
|
+
const namespace = idl.createNamespace(node.name, [], {
|
|
351
|
+
extendedAttributes: this.toExtendedAttributes(node.extAttrs),
|
|
352
|
+
fileName: file
|
|
353
|
+
});
|
|
354
|
+
this.namespacePathNames.push(node.name);
|
|
355
|
+
namespace.members = node.members.map(it => this.toIDLNodeForward(file, it));
|
|
356
|
+
this.namespacePathNames.pop();
|
|
357
|
+
return this.withInfo(node, namespace);
|
|
358
|
+
}
|
|
359
|
+
toIDLVersion(file, node) {
|
|
360
|
+
return this.withInfo(node, idl.createVersion(node.value, {
|
|
361
|
+
extendedAttributes: this.toExtendedAttributes(node.extAttrs),
|
|
362
|
+
fileName: file
|
|
363
|
+
}));
|
|
364
|
+
}
|
|
365
|
+
toIDLProperty(file, node) {
|
|
366
|
+
return this.withInfo(node, idl.createProperty(node.name, this.toIDLType(file, node.idlType, undefined), node.readonly, node.special === "static", isOptional(node), {
|
|
367
|
+
documentation: this.makeDocs(node),
|
|
368
|
+
fileName: file,
|
|
369
|
+
extendedAttributes: this.toExtendedAttributes(node.extAttrs)
|
|
370
|
+
}));
|
|
371
|
+
}
|
|
372
|
+
toIDLEnumMember(file, node, parent) {
|
|
373
|
+
var _a, _b, _c;
|
|
374
|
+
let initializer = undefined;
|
|
375
|
+
if (((_a = node.default) === null || _a === void 0 ? void 0 : _a.type) == "string") {
|
|
376
|
+
initializer = this.unescapeString(node.default.value);
|
|
336
377
|
}
|
|
337
|
-
else if (
|
|
338
|
-
|
|
378
|
+
else if (((_b = node.default) === null || _b === void 0 ? void 0 : _b.type) == "number") {
|
|
379
|
+
initializer = +((_c = node.default) === null || _c === void 0 ? void 0 : _c.value);
|
|
339
380
|
}
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
381
|
+
else if (node.default == null) {
|
|
382
|
+
initializer = undefined;
|
|
383
|
+
}
|
|
384
|
+
else {
|
|
385
|
+
throw new Error(`Not representable enum initializer: ${JSON.stringify(node.default)}. Found in ${file}`);
|
|
386
|
+
}
|
|
387
|
+
return this.withInfo(node, idl.createEnumMember(node.name, parent, this.toIDLType(file, node.idlType, undefined), initializer, {
|
|
388
|
+
extendedAttributes: this.toExtendedAttributes(node.extAttrs),
|
|
389
|
+
}));
|
|
349
390
|
}
|
|
350
|
-
|
|
351
|
-
|
|
391
|
+
toExtendedAttributes(extAttrs) {
|
|
392
|
+
return extAttrs.map(it => {
|
|
393
|
+
return this.withInfo(it, { name: it.name, value: this.toExtendedAttributeValue(it) });
|
|
394
|
+
});
|
|
352
395
|
}
|
|
353
|
-
|
|
354
|
-
|
|
396
|
+
toExtendedAttributeValue(attr) {
|
|
397
|
+
var _a, _b;
|
|
398
|
+
// TODO: be smarter about RHS.
|
|
399
|
+
if (((_a = attr.rhs) === null || _a === void 0 ? void 0 : _a.value) instanceof Array)
|
|
400
|
+
return attr.rhs.value.map(v => v.value).join(",");
|
|
401
|
+
if (typeof ((_b = attr.rhs) === null || _b === void 0 ? void 0 : _b.value) === 'string')
|
|
402
|
+
return this.unescapeString(attr.rhs.value);
|
|
403
|
+
return;
|
|
355
404
|
}
|
|
356
|
-
|
|
357
|
-
|
|
405
|
+
toIDLEnum(file, node) {
|
|
406
|
+
const result = idl.createEnum(node.name, [], {
|
|
407
|
+
fileName: file,
|
|
408
|
+
documentation: this.makeDocs(node),
|
|
409
|
+
extendedAttributes: this.toExtendedAttributes(node.extAttrs),
|
|
410
|
+
});
|
|
411
|
+
result.elements = node.values.map((it) => idl.createEnumMember(it.value, result, idl.IDLNumberType, undefined));
|
|
412
|
+
return this.withInfo(node, result);
|
|
358
413
|
}
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
414
|
+
///
|
|
415
|
+
isNamespace(node) {
|
|
416
|
+
return node.type === 'namespace';
|
|
417
|
+
}
|
|
418
|
+
isVersion(node) {
|
|
419
|
+
return node.type === 'version';
|
|
420
|
+
}
|
|
421
|
+
isPackage(node) {
|
|
422
|
+
return node.type === 'package';
|
|
423
|
+
}
|
|
424
|
+
isImport(node) {
|
|
425
|
+
return node.type === 'import';
|
|
426
|
+
}
|
|
427
|
+
isCallable(node) {
|
|
428
|
+
return node.extAttrs.some(it => it.name == idl.IDLExtendedAttributes.CallSignature);
|
|
429
|
+
}
|
|
430
|
+
///
|
|
431
|
+
splitTypeArguments(line) {
|
|
432
|
+
let buffer = "";
|
|
433
|
+
let brackets = 0;
|
|
434
|
+
const result = [];
|
|
435
|
+
for (const letter of line) {
|
|
436
|
+
if (letter === ',' && brackets === 0) {
|
|
437
|
+
result.push(buffer);
|
|
438
|
+
buffer = '';
|
|
439
|
+
continue;
|
|
440
|
+
}
|
|
441
|
+
if (letter === '<') {
|
|
442
|
+
brackets += 1;
|
|
443
|
+
}
|
|
444
|
+
if (letter === '>') {
|
|
445
|
+
brackets -= 1;
|
|
446
|
+
}
|
|
447
|
+
buffer += letter;
|
|
448
|
+
}
|
|
449
|
+
if (buffer.length) {
|
|
450
|
+
result.push(buffer);
|
|
451
|
+
}
|
|
452
|
+
return result;
|
|
453
|
+
}
|
|
454
|
+
extractTypeArguments(file, extAttrs, attribute) {
|
|
380
455
|
var _a;
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
}
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
456
|
+
const attr = extAttrs === null || extAttrs === void 0 ? void 0 : extAttrs.find(it => it.name === attribute);
|
|
457
|
+
if (!attr)
|
|
458
|
+
return undefined;
|
|
459
|
+
let value = this.toExtendedAttributeValue(attr);
|
|
460
|
+
return (_a = this.splitTypeArguments(value)) === null || _a === void 0 ? void 0 : _a.map(it => { var _a; return this.toIDLType(file, (_a = webidl2.parseType(it.replaceAll('\'', '"'), file)) !== null && _a !== void 0 ? _a : it); });
|
|
461
|
+
}
|
|
462
|
+
constantValue(node) {
|
|
463
|
+
switch (node.value.type) {
|
|
464
|
+
case "string":
|
|
465
|
+
return `"${node.value.value}"`;
|
|
466
|
+
case "number":
|
|
467
|
+
return node.value.value;
|
|
468
|
+
case "boolean":
|
|
469
|
+
return node.value.value.toString();
|
|
470
|
+
case "null":
|
|
471
|
+
return "null";
|
|
472
|
+
case "Infinity":
|
|
473
|
+
return "Infinity";
|
|
474
|
+
case "NaN":
|
|
475
|
+
return "NaN";
|
|
476
|
+
case "sequence":
|
|
477
|
+
return `[${node.value.value.join(',')}]`;
|
|
478
|
+
case "dictionary":
|
|
479
|
+
return `new Map()`;
|
|
480
|
+
default:
|
|
481
|
+
return "undefined";
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
unescapeString(value) {
|
|
485
|
+
if (!value.length || value[0] !== '"')
|
|
486
|
+
return value;
|
|
487
|
+
value = value.slice(1, -1);
|
|
488
|
+
value = value.replace(/\\((['"\\bfnrtv])|([0-7]{1-3})|x([0-9a-fA-F]{2})|u([0-9a-fA-F]{4}))/g, (_, all, c, oct, h2, u4) => {
|
|
489
|
+
if (c !== undefined) {
|
|
490
|
+
switch (c) {
|
|
491
|
+
case "'": return "'";
|
|
492
|
+
case '"': return '"';
|
|
493
|
+
case "\\": return "\\";
|
|
494
|
+
case "b": return "\b";
|
|
495
|
+
case "f": return "\f";
|
|
496
|
+
case "n": return "\n";
|
|
497
|
+
case "r": return "\r";
|
|
498
|
+
case "t": return "\t";
|
|
499
|
+
case "v": return "\v";
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
else if (oct !== undefined) {
|
|
503
|
+
return String.fromCharCode(parseInt(oct, 8));
|
|
504
|
+
}
|
|
505
|
+
else if (h2 !== undefined) {
|
|
506
|
+
return String.fromCharCode(parseInt(h2, 16));
|
|
507
|
+
}
|
|
508
|
+
else if (u4 !== undefined) {
|
|
509
|
+
return String.fromCharCode(parseInt(u4, 16));
|
|
510
|
+
}
|
|
511
|
+
throw new Error(`unknown escape sequence: ${_}`);
|
|
512
|
+
});
|
|
513
|
+
return value;
|
|
514
|
+
}
|
|
515
|
+
makeDocs(node) {
|
|
516
|
+
let docs = undefined;
|
|
517
|
+
node.extAttrs.forEach(it => {
|
|
518
|
+
var _a;
|
|
519
|
+
if (it.name == "Documentation")
|
|
520
|
+
docs = (_a = it.rhs) === null || _a === void 0 ? void 0 : _a.value;
|
|
521
|
+
});
|
|
522
|
+
return docs;
|
|
523
|
+
}
|
|
524
|
+
findExtendedAttribute(extAttrs, name) {
|
|
525
|
+
const attr = extAttrs.find(it => it.name === name);
|
|
526
|
+
return attr ? this.toExtendedAttributeValue(attr) : undefined;
|
|
527
|
+
}
|
|
398
528
|
}
|
|
399
|
-
export function toIDLFile(fileName, content) {
|
|
529
|
+
export function toIDLFile(fileName, { content, inheritanceMode = 'multiple' } = {}) {
|
|
530
|
+
const lexicalInfo = new Map();
|
|
531
|
+
const deserializer = new IDLDeserializer(lexicalInfo, inheritanceMode);
|
|
400
532
|
if (undefined === content)
|
|
401
533
|
content = fs.readFileSync(fileName).toString();
|
|
402
534
|
let packageClause = [];
|
|
@@ -404,14 +536,16 @@ export function toIDLFile(fileName, content) {
|
|
|
404
536
|
.filter(it => {
|
|
405
537
|
if (!it.type)
|
|
406
538
|
return false;
|
|
407
|
-
if (isPackage(it)) {
|
|
539
|
+
if (deserializer.isPackage(it)) {
|
|
408
540
|
packageClause = it.clause.split(".");
|
|
541
|
+
deserializer.setPackage(packageClause);
|
|
409
542
|
return false;
|
|
410
543
|
}
|
|
411
544
|
return true;
|
|
412
545
|
})
|
|
413
|
-
.map(it => toIDLNode(fileName, it));
|
|
546
|
+
.map(it => deserializer.toIDLNode(fileName, it));
|
|
414
547
|
const file = idl.createFile(entries, fileName, packageClause);
|
|
415
|
-
|
|
548
|
+
file.text = content;
|
|
549
|
+
return [idl.linkParentBack(file), lexicalInfo];
|
|
416
550
|
}
|
|
417
551
|
//# sourceMappingURL=deserialize.js.map
|