@idlizer/core 2.0.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/lib/src/IndentedPrinter.d.ts +28 -0
- package/build/lib/src/IndentedPrinter.js +81 -0
- package/build/lib/src/Language.d.ts +15 -0
- package/build/lib/src/Language.js +42 -0
- package/build/lib/src/LanguageWriters/ArgConvertors.d.ts +318 -0
- package/build/lib/src/LanguageWriters/ArgConvertors.js +1015 -0
- package/build/lib/src/LanguageWriters/InteropConvertor.d.ts +36 -0
- package/build/lib/src/LanguageWriters/InteropConvertor.js +174 -0
- package/build/lib/src/LanguageWriters/LanguageWriter.d.ts +386 -0
- package/build/lib/src/LanguageWriters/LanguageWriter.js +746 -0
- package/build/lib/src/LanguageWriters/common.d.ts +17 -0
- package/build/lib/src/LanguageWriters/common.js +33 -0
- package/build/lib/src/LanguageWriters/convertors/CppConvertors.d.ts +8 -0
- package/build/lib/src/LanguageWriters/convertors/CppConvertors.js +35 -0
- package/build/lib/src/LanguageWriters/index.d.ts +3 -0
- package/build/lib/src/LanguageWriters/index.js +3 -0
- package/build/lib/src/LanguageWriters/nameConvertor.d.ts +28 -0
- package/build/lib/src/LanguageWriters/nameConvertor.js +61 -0
- package/build/lib/src/LanguageWriters/writers/CJLanguageWriter.d.ts +147 -0
- package/build/lib/src/LanguageWriters/writers/CJLanguageWriter.js +578 -0
- package/build/lib/src/LanguageWriters/writers/CLikeLanguageWriter.d.ts +46 -0
- package/build/lib/src/LanguageWriters/writers/CLikeLanguageWriter.js +121 -0
- package/build/lib/src/LanguageWriters/writers/CppLanguageWriter.d.ts +116 -0
- package/build/lib/src/LanguageWriters/writers/CppLanguageWriter.js +472 -0
- package/build/lib/src/LanguageWriters/writers/ETSLanguageWriter.d.ts +72 -0
- package/build/lib/src/LanguageWriters/writers/ETSLanguageWriter.js +329 -0
- package/build/lib/src/LanguageWriters/writers/JavaLanguageWriter.d.ts +83 -0
- package/build/lib/src/LanguageWriters/writers/JavaLanguageWriter.js +275 -0
- package/build/lib/src/LanguageWriters/writers/TsLanguageWriter.d.ts +93 -0
- package/build/lib/src/LanguageWriters/writers/TsLanguageWriter.js +390 -0
- package/build/lib/src/LibraryInterface.d.ts +21 -0
- package/build/lib/src/LibraryInterface.js +2 -0
- package/build/lib/src/config.d.ts +8 -0
- package/build/lib/src/config.js +20 -0
- package/build/lib/src/from-idl/DtsPrinter.d.ts +35 -0
- package/build/lib/src/from-idl/DtsPrinter.js +375 -0
- package/build/lib/src/from-idl/common.d.ts +5 -0
- package/build/lib/src/from-idl/common.js +83 -0
- package/build/lib/src/from-idl/deserialize.d.ts +9 -0
- package/build/lib/src/from-idl/deserialize.js +384 -0
- package/build/lib/src/from-idl/toString.d.ts +2 -0
- package/build/lib/src/from-idl/toString.js +18 -0
- package/build/lib/src/from-idl/webidl2-utils.d.ts +20 -0
- package/build/lib/src/from-idl/webidl2-utils.js +84 -0
- package/build/lib/src/idl.d.ts +404 -0
- package/build/lib/src/idl.js +1015 -0
- package/build/lib/src/idlize.d.ts +4 -0
- package/build/lib/src/idlize.js +52 -0
- package/build/lib/src/index.d.ts +34 -0
- package/build/lib/src/index.js +48 -0
- package/build/lib/src/inheritance.d.ts +16 -0
- package/build/lib/src/inheritance.js +76 -0
- package/build/lib/src/languageSpecificKeywords.d.ts +5 -0
- package/build/lib/src/languageSpecificKeywords.js +51 -0
- package/build/lib/src/library.d.ts +67 -0
- package/build/lib/src/library.js +190 -0
- package/build/lib/src/options.d.ts +11 -0
- package/build/lib/src/options.js +16 -0
- package/build/lib/src/peer-generation/PrimitiveType.d.ts +24 -0
- package/build/lib/src/peer-generation/PrimitiveType.js +52 -0
- package/build/lib/src/peer-generation/ReferenceResolver.d.ts +9 -0
- package/build/lib/src/peer-generation/ReferenceResolver.js +37 -0
- package/build/lib/src/peer-generation/idl/IdlNameConvertor.d.ts +28 -0
- package/build/lib/src/peer-generation/idl/IdlNameConvertor.js +82 -0
- package/build/lib/src/peer-generation/idl/common.d.ts +9 -0
- package/build/lib/src/peer-generation/idl/common.js +70 -0
- package/build/lib/src/peer-generation/unions.d.ts +29 -0
- package/build/lib/src/peer-generation/unions.js +111 -0
- package/build/lib/src/rand_utils.d.ts +6 -0
- package/build/lib/src/rand_utils.js +100 -0
- package/build/lib/src/util.d.ts +75 -0
- package/build/lib/src/util.js +622 -0
- package/package.json +49 -0
- 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 +4547 -0
- package/webidl2.js/package.json +55 -0
|
@@ -0,0 +1,746 @@
|
|
|
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 idl from "../idl";
|
|
16
|
+
import * as fs from "fs";
|
|
17
|
+
import { RuntimeType } from "./common";
|
|
18
|
+
export class TernaryExpression {
|
|
19
|
+
constructor(condition, trueExpression, falseExpression) {
|
|
20
|
+
this.condition = condition;
|
|
21
|
+
this.trueExpression = trueExpression;
|
|
22
|
+
this.falseExpression = falseExpression;
|
|
23
|
+
}
|
|
24
|
+
asString() {
|
|
25
|
+
return `(${this.condition.asString()}) ? (${this.trueExpression.asString()}) : (${this.falseExpression.asString()})`;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
export class NaryOpExpression {
|
|
29
|
+
constructor(op, args) {
|
|
30
|
+
this.op = op;
|
|
31
|
+
this.args = args;
|
|
32
|
+
}
|
|
33
|
+
asString() {
|
|
34
|
+
return `${this.args.map(arg => `(${arg.asString()})`).join(` ${this.op} `)}`;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
export class StringExpression {
|
|
38
|
+
constructor(value) {
|
|
39
|
+
this.value = value;
|
|
40
|
+
}
|
|
41
|
+
asString() {
|
|
42
|
+
return this.value;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
export class NewObjectExpression {
|
|
46
|
+
constructor(objectName, params) {
|
|
47
|
+
this.objectName = objectName;
|
|
48
|
+
this.params = params;
|
|
49
|
+
}
|
|
50
|
+
asString() {
|
|
51
|
+
return `new ${this.objectName}(${this.params.map(it => it.asString()).join(", ")})`;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
export class FunctionCallExpression {
|
|
55
|
+
constructor(name, params) {
|
|
56
|
+
this.name = name;
|
|
57
|
+
this.params = params;
|
|
58
|
+
}
|
|
59
|
+
asString() {
|
|
60
|
+
return `${this.name}(${this.params.map(it => it.asString()).join(", ")})`;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
export class MethodCallExpression extends FunctionCallExpression {
|
|
64
|
+
constructor(receiver, method, params, nullable = false) {
|
|
65
|
+
super(method, params);
|
|
66
|
+
this.receiver = receiver;
|
|
67
|
+
this.nullable = nullable;
|
|
68
|
+
}
|
|
69
|
+
asString() {
|
|
70
|
+
return `${this.receiver}${this.nullable ? "?" : ""}.${super.asString()}`;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
export class FieldAccessExpression {
|
|
74
|
+
constructor(receiver, field, nullable = false) {
|
|
75
|
+
this.receiver = receiver;
|
|
76
|
+
this.field = field;
|
|
77
|
+
this.nullable = nullable;
|
|
78
|
+
}
|
|
79
|
+
asString() {
|
|
80
|
+
return `${this.receiver}${this.nullable ? "?" : ""}.${this.field}`;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
export class CheckDefinedExpression {
|
|
84
|
+
constructor(value) {
|
|
85
|
+
this.value = value;
|
|
86
|
+
}
|
|
87
|
+
asString() {
|
|
88
|
+
return `${this.value} != undefined`;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
export class ProxyStatement {
|
|
92
|
+
constructor(cb) {
|
|
93
|
+
this.cb = cb;
|
|
94
|
+
}
|
|
95
|
+
write(writer) {
|
|
96
|
+
this.cb(writer);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
export class AssignStatement {
|
|
100
|
+
constructor(variableName, type, expression, isDeclared = true, isConst = true, options) {
|
|
101
|
+
this.variableName = variableName;
|
|
102
|
+
this.type = type;
|
|
103
|
+
this.expression = expression;
|
|
104
|
+
this.isDeclared = isDeclared;
|
|
105
|
+
this.isConst = isConst;
|
|
106
|
+
this.options = options;
|
|
107
|
+
}
|
|
108
|
+
write(writer) {
|
|
109
|
+
var _a, _b;
|
|
110
|
+
if (this.isDeclared) {
|
|
111
|
+
const typeSpec = ((_a = this.options) === null || _a === void 0 ? void 0 : _a.overrideTypeName)
|
|
112
|
+
? `: ${this.options.overrideTypeName}`
|
|
113
|
+
: this.type
|
|
114
|
+
? `: ${writer.getNodeName(this.type)}${ /*SHOULD BE REMOVED*/idl.isOptionalType(this.type) ? "|undefined" : ""}`
|
|
115
|
+
: "";
|
|
116
|
+
const initValue = this.expression ? `= ${this.expression.asString()}` : "";
|
|
117
|
+
const constSpec = this.isConst ? "const" : "let";
|
|
118
|
+
writer.print(`${constSpec} ${this.variableName}${typeSpec} ${initValue}`);
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
writer.print(`${this.variableName} = ${(_b = this.expression) === null || _b === void 0 ? void 0 : _b.asString()}`);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
export class ExpressionStatement {
|
|
126
|
+
constructor(expression) {
|
|
127
|
+
this.expression = expression;
|
|
128
|
+
}
|
|
129
|
+
write(writer) {
|
|
130
|
+
const text = this.expression.asString();
|
|
131
|
+
if (text.length > 0) {
|
|
132
|
+
writer.print(`${this.expression.asString()}${writer.maybeSemicolon()}`);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
export class BlockStatement {
|
|
137
|
+
constructor(statements, inScope = true) {
|
|
138
|
+
this.statements = statements;
|
|
139
|
+
this.inScope = inScope;
|
|
140
|
+
}
|
|
141
|
+
write(writer) {
|
|
142
|
+
if (this.inScope) {
|
|
143
|
+
writer.print("{");
|
|
144
|
+
writer.pushIndent();
|
|
145
|
+
}
|
|
146
|
+
this.statements.forEach(s => s.write(writer));
|
|
147
|
+
if (this.inScope) {
|
|
148
|
+
writer.popIndent();
|
|
149
|
+
writer.print("}");
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
export class IfStatement {
|
|
154
|
+
constructor(condition, thenStatement, elseStatement, insideIfOp, insideElseOp) {
|
|
155
|
+
this.condition = condition;
|
|
156
|
+
this.thenStatement = thenStatement;
|
|
157
|
+
this.elseStatement = elseStatement;
|
|
158
|
+
this.insideIfOp = insideIfOp;
|
|
159
|
+
this.insideElseOp = insideElseOp;
|
|
160
|
+
}
|
|
161
|
+
write(writer) {
|
|
162
|
+
writer.print(`if (${this.condition.asString()})`);
|
|
163
|
+
this.writeBody(writer, this.thenStatement, () => {
|
|
164
|
+
if (this.insideIfOp) {
|
|
165
|
+
this.insideIfOp();
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
if (this.elseStatement !== undefined) {
|
|
169
|
+
writer.print("else");
|
|
170
|
+
this.writeBody(writer, this.elseStatement, () => {
|
|
171
|
+
if (this.insideElseOp) {
|
|
172
|
+
this.insideElseOp();
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
writeBody(writer, body, op) {
|
|
178
|
+
if (!(body instanceof BlockStatement)) {
|
|
179
|
+
writer.pushIndent();
|
|
180
|
+
}
|
|
181
|
+
body.write(writer);
|
|
182
|
+
op();
|
|
183
|
+
if (!(body instanceof BlockStatement)) {
|
|
184
|
+
writer.popIndent();
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
export class MultiBranchIfStatement {
|
|
189
|
+
constructor(statements, elseStatement) {
|
|
190
|
+
this.statements = statements;
|
|
191
|
+
this.elseStatement = elseStatement;
|
|
192
|
+
}
|
|
193
|
+
write(writer) {
|
|
194
|
+
this.statements.forEach((value, index) => {
|
|
195
|
+
const { expr, stmt } = value;
|
|
196
|
+
if (index == 0) {
|
|
197
|
+
writer.print(`if (${expr.asString()}) {`);
|
|
198
|
+
}
|
|
199
|
+
else {
|
|
200
|
+
writer.print(`else if (${expr.asString()}) {`);
|
|
201
|
+
}
|
|
202
|
+
writer.pushIndent();
|
|
203
|
+
stmt.write(writer);
|
|
204
|
+
writer.popIndent();
|
|
205
|
+
writer.print("}");
|
|
206
|
+
});
|
|
207
|
+
if (this.statements.length > 0 && this.elseStatement !== undefined) {
|
|
208
|
+
writer.print("else {");
|
|
209
|
+
writer.pushIndent();
|
|
210
|
+
this.elseStatement.write(writer);
|
|
211
|
+
writer.popIndent();
|
|
212
|
+
writer.print("}");
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
export class CheckOptionalStatement {
|
|
217
|
+
constructor(undefinedValue, optionalExpression, doStatement) {
|
|
218
|
+
this.undefinedValue = undefinedValue;
|
|
219
|
+
this.optionalExpression = optionalExpression;
|
|
220
|
+
this.doStatement = doStatement;
|
|
221
|
+
}
|
|
222
|
+
write(writer) {
|
|
223
|
+
writer.print(`if (${this.optionalExpression.asString()} != ${this.undefinedValue})`);
|
|
224
|
+
writer.pushIndent();
|
|
225
|
+
this.doStatement.write(writer);
|
|
226
|
+
writer.popIndent();
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
// maybe rename or move of fix
|
|
230
|
+
export class TsEnumEntityStatement {
|
|
231
|
+
constructor(enumEntity, isExport) {
|
|
232
|
+
this.enumEntity = enumEntity;
|
|
233
|
+
this.isExport = isExport;
|
|
234
|
+
}
|
|
235
|
+
write(writer) {
|
|
236
|
+
// writer.print(this.enumEntity.comment)
|
|
237
|
+
idl.getNamespacesPathFor(this.enumEntity).forEach(it => writer.pushNamespace(it.name));
|
|
238
|
+
writer.print(`${this.isExport ? "export " : ""}enum ${this.enumEntity.name} {`);
|
|
239
|
+
writer.pushIndent();
|
|
240
|
+
this.enumEntity.elements.forEach((member, index) => {
|
|
241
|
+
// writer.print(member.comment)
|
|
242
|
+
const initValue = member.initializer
|
|
243
|
+
? ` = ${this.maybeQuoted(member.initializer)}` : ``;
|
|
244
|
+
writer.print(`${member.name}${initValue},`);
|
|
245
|
+
let originalName = idl.getExtAttribute(member, idl.IDLExtendedAttributes.OriginalEnumMemberName);
|
|
246
|
+
if (originalName) {
|
|
247
|
+
const initValue = ` = ${member.name}`;
|
|
248
|
+
writer.print(`${originalName}${initValue},`);
|
|
249
|
+
}
|
|
250
|
+
});
|
|
251
|
+
writer.popIndent();
|
|
252
|
+
writer.print(`}`);
|
|
253
|
+
idl.getNamespacesPathFor(this.enumEntity).forEach(it => writer.popNamespace());
|
|
254
|
+
}
|
|
255
|
+
maybeQuoted(value) {
|
|
256
|
+
if (typeof value == "string")
|
|
257
|
+
return `"${value}"`;
|
|
258
|
+
else
|
|
259
|
+
return `${value}`;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
export class ReturnStatement {
|
|
263
|
+
constructor(expression) {
|
|
264
|
+
this.expression = expression;
|
|
265
|
+
}
|
|
266
|
+
write(writer) {
|
|
267
|
+
writer.print(this.expression ? `return ${this.expression.asString()}` : "return");
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
export class LambdaExpression {
|
|
271
|
+
constructor(originalWriter, signature, resolver, body) {
|
|
272
|
+
this.originalWriter = originalWriter;
|
|
273
|
+
this.signature = signature;
|
|
274
|
+
this.resolver = resolver;
|
|
275
|
+
this.body = body;
|
|
276
|
+
}
|
|
277
|
+
bodyAsString() {
|
|
278
|
+
const writer = this.originalWriter.fork();
|
|
279
|
+
if (this.body) {
|
|
280
|
+
for (const stmt of this.body) {
|
|
281
|
+
stmt.write(writer);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
return writer.printer.getOutput()
|
|
285
|
+
.map(line => line.trim())
|
|
286
|
+
.filter(line => line !== "")
|
|
287
|
+
.map(line => line === "{" || line === "}" || this.statementHasSemicolon ? line : `${line};`)
|
|
288
|
+
.join(" ");
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
////////////////////////////////////////////////////////////////
|
|
292
|
+
// SIGNATURES //
|
|
293
|
+
////////////////////////////////////////////////////////////////
|
|
294
|
+
export var FieldModifier;
|
|
295
|
+
(function (FieldModifier) {
|
|
296
|
+
FieldModifier[FieldModifier["READONLY"] = 0] = "READONLY";
|
|
297
|
+
FieldModifier[FieldModifier["PRIVATE"] = 1] = "PRIVATE";
|
|
298
|
+
FieldModifier[FieldModifier["PUBLIC"] = 2] = "PUBLIC";
|
|
299
|
+
FieldModifier[FieldModifier["STATIC"] = 3] = "STATIC";
|
|
300
|
+
FieldModifier[FieldModifier["PROTECTED"] = 4] = "PROTECTED";
|
|
301
|
+
FieldModifier[FieldModifier["FINAL"] = 5] = "FINAL";
|
|
302
|
+
FieldModifier[FieldModifier["VOLATILE"] = 6] = "VOLATILE";
|
|
303
|
+
FieldModifier[FieldModifier["INTERNAL"] = 7] = "INTERNAL";
|
|
304
|
+
})(FieldModifier || (FieldModifier = {}));
|
|
305
|
+
export var MethodModifier;
|
|
306
|
+
(function (MethodModifier) {
|
|
307
|
+
MethodModifier[MethodModifier["PUBLIC"] = 0] = "PUBLIC";
|
|
308
|
+
MethodModifier[MethodModifier["PRIVATE"] = 1] = "PRIVATE";
|
|
309
|
+
MethodModifier[MethodModifier["STATIC"] = 2] = "STATIC";
|
|
310
|
+
MethodModifier[MethodModifier["NATIVE"] = 3] = "NATIVE";
|
|
311
|
+
MethodModifier[MethodModifier["INLINE"] = 4] = "INLINE";
|
|
312
|
+
MethodModifier[MethodModifier["GETTER"] = 5] = "GETTER";
|
|
313
|
+
MethodModifier[MethodModifier["SETTER"] = 6] = "SETTER";
|
|
314
|
+
MethodModifier[MethodModifier["PROTECTED"] = 7] = "PROTECTED";
|
|
315
|
+
MethodModifier[MethodModifier["FREE"] = 8] = "FREE";
|
|
316
|
+
})(MethodModifier || (MethodModifier = {}));
|
|
317
|
+
export var ClassModifier;
|
|
318
|
+
(function (ClassModifier) {
|
|
319
|
+
ClassModifier[ClassModifier["PUBLIC"] = 0] = "PUBLIC";
|
|
320
|
+
ClassModifier[ClassModifier["PRIVATE"] = 1] = "PRIVATE";
|
|
321
|
+
ClassModifier[ClassModifier["PROTECTED"] = 2] = "PROTECTED";
|
|
322
|
+
})(ClassModifier || (ClassModifier = {}));
|
|
323
|
+
export class Field {
|
|
324
|
+
constructor(name, type, modifiers = []) {
|
|
325
|
+
this.name = name;
|
|
326
|
+
this.type = type;
|
|
327
|
+
this.modifiers = modifiers;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
export class Method {
|
|
331
|
+
constructor(name, signature, modifiers = undefined, generics) {
|
|
332
|
+
this.name = name;
|
|
333
|
+
this.signature = signature;
|
|
334
|
+
this.modifiers = modifiers;
|
|
335
|
+
this.generics = generics;
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
export class PrintHint {
|
|
339
|
+
constructor(hint) {
|
|
340
|
+
this.hint = hint;
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
PrintHint.AsPointer = new PrintHint('AsPointer');
|
|
344
|
+
PrintHint.AsConstPointer = new PrintHint('AsConstPointer');
|
|
345
|
+
PrintHint.AsValue = new PrintHint('AsValue');
|
|
346
|
+
PrintHint.AsConstReference = new PrintHint('AsConstReference');
|
|
347
|
+
export class MethodSignature {
|
|
348
|
+
constructor(returnType, args, defaults = undefined, printHints, argNames) {
|
|
349
|
+
this.returnType = returnType;
|
|
350
|
+
this.args = args;
|
|
351
|
+
this.defaults = defaults;
|
|
352
|
+
this.printHints = printHints;
|
|
353
|
+
this.argNames = argNames;
|
|
354
|
+
}
|
|
355
|
+
argName(index) {
|
|
356
|
+
var _a, _b;
|
|
357
|
+
return (_b = (_a = this === null || this === void 0 ? void 0 : this.argNames) === null || _a === void 0 ? void 0 : _a.at(index)) !== null && _b !== void 0 ? _b : `arg${index}`;
|
|
358
|
+
}
|
|
359
|
+
argDefault(index) {
|
|
360
|
+
var _a;
|
|
361
|
+
return (_a = this.defaults) === null || _a === void 0 ? void 0 : _a[index];
|
|
362
|
+
}
|
|
363
|
+
retHint() {
|
|
364
|
+
var _a;
|
|
365
|
+
return (_a = this.printHints) === null || _a === void 0 ? void 0 : _a[0];
|
|
366
|
+
}
|
|
367
|
+
argHint(index) {
|
|
368
|
+
var _a;
|
|
369
|
+
return (_a = this.printHints) === null || _a === void 0 ? void 0 : _a[index + 1];
|
|
370
|
+
}
|
|
371
|
+
toString() {
|
|
372
|
+
return `${this.args.map(it => idl.forceAsNamedNode(it).name)} => ${this.returnType}`;
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
export class NamedMethodSignature extends MethodSignature {
|
|
376
|
+
constructor(returnType, args = [], argsNames = [], defaults = undefined, printHints) {
|
|
377
|
+
super(returnType, args, defaults, printHints);
|
|
378
|
+
this.argsNames = argsNames;
|
|
379
|
+
}
|
|
380
|
+
static make(returnType, args) {
|
|
381
|
+
return new NamedMethodSignature(returnType, args.map(it => it.type), args.map(it => it.name));
|
|
382
|
+
}
|
|
383
|
+
argName(index) {
|
|
384
|
+
return this.argsNames[index];
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
////////////////////////////////////////////////////////////////
|
|
388
|
+
// LANGUAGE WRITER //
|
|
389
|
+
////////////////////////////////////////////////////////////////
|
|
390
|
+
export class LanguageWriter {
|
|
391
|
+
constructor(printer, resolver, language) {
|
|
392
|
+
this.printer = printer;
|
|
393
|
+
this.resolver = resolver;
|
|
394
|
+
this.language = language;
|
|
395
|
+
}
|
|
396
|
+
indentDepth() {
|
|
397
|
+
return this.printer.indentDepth();
|
|
398
|
+
}
|
|
399
|
+
maybeSemicolon() { return ";"; }
|
|
400
|
+
// version of makeCast which uses TypeCheck.typeCast<T>(value) call for ETS language writer
|
|
401
|
+
// Use it only if TypeChecker class is added as import to the generated file
|
|
402
|
+
makeTypeCast(value, type, options) {
|
|
403
|
+
return this.makeCast(value, type, options);
|
|
404
|
+
}
|
|
405
|
+
makeUnwrapOptional(expression) {
|
|
406
|
+
return expression;
|
|
407
|
+
}
|
|
408
|
+
concat(other) {
|
|
409
|
+
other.getOutput().forEach(it => this.print(it));
|
|
410
|
+
return this;
|
|
411
|
+
}
|
|
412
|
+
printTo(file) {
|
|
413
|
+
fs.writeFileSync(file, this.getOutput().join("\n"));
|
|
414
|
+
}
|
|
415
|
+
writeLines(lines) {
|
|
416
|
+
lines.split("\n").forEach(it => this.print(it));
|
|
417
|
+
}
|
|
418
|
+
writeGetterImplementation(method, op) {
|
|
419
|
+
var _a;
|
|
420
|
+
this.writeMethodImplementation(new Method(method.name, method.signature, [MethodModifier.GETTER].concat((_a = method.modifiers) !== null && _a !== void 0 ? _a : [])), op);
|
|
421
|
+
}
|
|
422
|
+
writeSetterImplementation(method, op) {
|
|
423
|
+
var _a;
|
|
424
|
+
this.writeMethodImplementation(new Method(method.name, method.signature, [MethodModifier.SETTER].concat((_a = method.modifiers) !== null && _a !== void 0 ? _a : [])), op);
|
|
425
|
+
}
|
|
426
|
+
writeSuperCall(params) {
|
|
427
|
+
this.printer.print(`super(${params.join(", ")})${this.maybeSemicolon()}`);
|
|
428
|
+
}
|
|
429
|
+
writeMethodCall(receiver, method, params, nullable = false) {
|
|
430
|
+
this.printer.print(`${receiver}${nullable ? "?" : ""}.${method}(${params.join(", ")})`);
|
|
431
|
+
}
|
|
432
|
+
writeStatement(stmt) {
|
|
433
|
+
stmt.write(this);
|
|
434
|
+
}
|
|
435
|
+
writeStatements(...statements) {
|
|
436
|
+
statements.forEach(it => this.writeStatement(it));
|
|
437
|
+
}
|
|
438
|
+
writeExpressionStatement(smth) {
|
|
439
|
+
this.writeStatement(new ExpressionStatement(smth));
|
|
440
|
+
}
|
|
441
|
+
makeRef(type, _options) {
|
|
442
|
+
return type;
|
|
443
|
+
}
|
|
444
|
+
makeThis() {
|
|
445
|
+
return new StringExpression("this");
|
|
446
|
+
}
|
|
447
|
+
makeNull(value) {
|
|
448
|
+
return new StringExpression("null");
|
|
449
|
+
}
|
|
450
|
+
makeVoid() {
|
|
451
|
+
return this.makeUndefined();
|
|
452
|
+
}
|
|
453
|
+
makeRuntimeTypeCondition(typeVarName, equals, type, varName) {
|
|
454
|
+
const op = equals ? "==" : "!=";
|
|
455
|
+
return this.makeNaryOp(op, [this.makeRuntimeType(type), this.makeString(typeVarName)]);
|
|
456
|
+
}
|
|
457
|
+
makeValueFromOption(value, destinationConvertor) {
|
|
458
|
+
return this.makeString(`${value}!`);
|
|
459
|
+
}
|
|
460
|
+
makeNewObject(objectName, params = []) {
|
|
461
|
+
return new NewObjectExpression(objectName, params);
|
|
462
|
+
}
|
|
463
|
+
makeFunctionCall(name, params) {
|
|
464
|
+
if (typeof name === "string") {
|
|
465
|
+
return new FunctionCallExpression(name, params);
|
|
466
|
+
}
|
|
467
|
+
return new FunctionCallExpression(name.asString(), params);
|
|
468
|
+
}
|
|
469
|
+
makeMethodCall(receiver, method, params, nullable) {
|
|
470
|
+
return new MethodCallExpression(receiver, method, params, nullable);
|
|
471
|
+
}
|
|
472
|
+
makeFieldAccess(receiver, method, nullable) {
|
|
473
|
+
return new FieldAccessExpression(receiver, method, nullable);
|
|
474
|
+
}
|
|
475
|
+
makeNativeCall(nativeModule, method, params, nullable) {
|
|
476
|
+
return new MethodCallExpression(this.nativeReceiver(nativeModule), method, params, nullable);
|
|
477
|
+
}
|
|
478
|
+
makeBlock(statements, inScope = true) {
|
|
479
|
+
return new BlockStatement(statements, inScope);
|
|
480
|
+
}
|
|
481
|
+
nativeReceiver(nativeModule) {
|
|
482
|
+
return nativeModule.name;
|
|
483
|
+
}
|
|
484
|
+
makeDefinedCheck(value) {
|
|
485
|
+
return new CheckDefinedExpression(value);
|
|
486
|
+
}
|
|
487
|
+
makeRuntimeTypeDefinedCheck(runtimeType) {
|
|
488
|
+
return this.makeRuntimeTypeCondition(runtimeType, false, RuntimeType.UNDEFINED);
|
|
489
|
+
}
|
|
490
|
+
makeCondition(condition, thenStatement, elseStatement, insideIfOp, insideElseOp) {
|
|
491
|
+
return new IfStatement(condition, thenStatement, elseStatement, insideIfOp, insideElseOp);
|
|
492
|
+
}
|
|
493
|
+
makeMultiBranchCondition(conditions, elseStatement) {
|
|
494
|
+
return new MultiBranchIfStatement(conditions, elseStatement);
|
|
495
|
+
}
|
|
496
|
+
makeTernary(condition, trueExpression, falseExpression) {
|
|
497
|
+
return new TernaryExpression(condition, trueExpression, falseExpression);
|
|
498
|
+
}
|
|
499
|
+
makeArrayLength(array, length) {
|
|
500
|
+
return this.makeString(`${array}.length`);
|
|
501
|
+
}
|
|
502
|
+
makeArrayAccess(value, indexVar) {
|
|
503
|
+
return this.makeString(`${value}[${indexVar}]`);
|
|
504
|
+
}
|
|
505
|
+
makeTupleAccess(value, index) {
|
|
506
|
+
return this.makeString(`${value}[${index}]`);
|
|
507
|
+
}
|
|
508
|
+
makeUnionSelector(value, valueType) {
|
|
509
|
+
return this.makeAssign(valueType, undefined, this.makeString(`runtimeType(${value})`), false);
|
|
510
|
+
}
|
|
511
|
+
makeUnionVariantCondition(_convertor, _valueName, valueType, type, _convertorIndex, _runtimeTypeIndex) {
|
|
512
|
+
return this.makeString(`RuntimeType.${type.toUpperCase()} == ${valueType}`);
|
|
513
|
+
}
|
|
514
|
+
makeUnionVariantCast(value, type, convertor, index) {
|
|
515
|
+
return this.makeString(`unsafeCast<${type}>(${value})`);
|
|
516
|
+
}
|
|
517
|
+
makeUnionTypeDefaultInitializer() {
|
|
518
|
+
return this.makeRuntimeType(RuntimeType.UNDEFINED);
|
|
519
|
+
}
|
|
520
|
+
makeRuntimeTypeGetterCall(value) {
|
|
521
|
+
return this.makeFunctionCall("runtimeType", [this.makeString(value)]);
|
|
522
|
+
}
|
|
523
|
+
makeArrayResize(array, arrayType, length, deserializer) {
|
|
524
|
+
return new ExpressionStatement(new StringExpression(""));
|
|
525
|
+
}
|
|
526
|
+
makeMapResize(mapTypeName, keyType, valueType, map, size, deserializer) {
|
|
527
|
+
return new ExpressionStatement(new StringExpression("// TODO: TS map resize"));
|
|
528
|
+
}
|
|
529
|
+
makeMapSize(map) {
|
|
530
|
+
return this.makeString(`${map}.size`);
|
|
531
|
+
}
|
|
532
|
+
makeTupleAlloc(option) {
|
|
533
|
+
return new ExpressionStatement(new StringExpression(""));
|
|
534
|
+
}
|
|
535
|
+
makeSetUnionSelector(value, index) {
|
|
536
|
+
// empty expression
|
|
537
|
+
return new ExpressionStatement(new StringExpression(""));
|
|
538
|
+
}
|
|
539
|
+
makeSetOptionTag(value, tag) {
|
|
540
|
+
// empty expression
|
|
541
|
+
return new ExpressionStatement(new StringExpression(""));
|
|
542
|
+
}
|
|
543
|
+
makeString(value) {
|
|
544
|
+
return new StringExpression(value);
|
|
545
|
+
}
|
|
546
|
+
makeNaryOp(op, args) {
|
|
547
|
+
return new NaryOpExpression(op, args);
|
|
548
|
+
}
|
|
549
|
+
makeStatement(expr) {
|
|
550
|
+
return new ExpressionStatement(expr);
|
|
551
|
+
}
|
|
552
|
+
writeNativeMethodDeclaration(name, signature, isNative) {
|
|
553
|
+
this.writeMethodDeclaration(name, signature);
|
|
554
|
+
}
|
|
555
|
+
writeUnsafeNativeMethodDeclaration(name, signature) {
|
|
556
|
+
return;
|
|
557
|
+
}
|
|
558
|
+
pushIndent() {
|
|
559
|
+
this.printer.pushIndent();
|
|
560
|
+
}
|
|
561
|
+
popIndent() {
|
|
562
|
+
this.printer.popIndent();
|
|
563
|
+
}
|
|
564
|
+
print(string) {
|
|
565
|
+
this.printer.print(string);
|
|
566
|
+
}
|
|
567
|
+
getOutput() {
|
|
568
|
+
return this.printer.getOutput();
|
|
569
|
+
}
|
|
570
|
+
makeSignature(returnType, parameters) {
|
|
571
|
+
return new MethodSignature(returnType, parameters.map(it => it.type));
|
|
572
|
+
}
|
|
573
|
+
makeNamedSignature(returnType, parameters) {
|
|
574
|
+
return NamedMethodSignature.make(returnType, parameters.map(it => ({
|
|
575
|
+
name: it.name,
|
|
576
|
+
type: it.isOptional ? idl.createOptionalType(it.type) : it.type
|
|
577
|
+
})));
|
|
578
|
+
}
|
|
579
|
+
makeNativeMethodNamedSignature(returnType, parameters) {
|
|
580
|
+
return this.makeNamedSignature(returnType, parameters);
|
|
581
|
+
}
|
|
582
|
+
makeSerializerConstructorSignature() {
|
|
583
|
+
return undefined;
|
|
584
|
+
}
|
|
585
|
+
mapFieldModifier(modifier) {
|
|
586
|
+
return `${FieldModifier[modifier].toLowerCase()}`;
|
|
587
|
+
}
|
|
588
|
+
mapMethodModifier(modifier) {
|
|
589
|
+
return `${MethodModifier[modifier].toLowerCase()}`;
|
|
590
|
+
}
|
|
591
|
+
/**
|
|
592
|
+
* TODO: replace me with {@link makeUnsafeCast_}
|
|
593
|
+
*/
|
|
594
|
+
makeUnsafeCast(convertor, param) {
|
|
595
|
+
return `unsafeCast<int32>(${param})`;
|
|
596
|
+
}
|
|
597
|
+
makeUnsafeCast_(value, type, typeOptions) {
|
|
598
|
+
return `(${value.asString()} as ${this.getNodeName(type)})`;
|
|
599
|
+
}
|
|
600
|
+
runtimeType(param, valueType, value) {
|
|
601
|
+
this.writeStatement(this.makeAssign(valueType, idl.IDLI32Type, this.makeFunctionCall("runtimeType", [this.makeString(value)]), false));
|
|
602
|
+
}
|
|
603
|
+
makeDiscriminatorFromFields(convertor, value, accessors, duplicates) {
|
|
604
|
+
return this.makeString(`(${this.makeNaryOp("||", accessors.map(it => this.makeString(`${value}!.hasOwnProperty("${it}")`))).asString()})`);
|
|
605
|
+
}
|
|
606
|
+
makeCallIsResource(value) {
|
|
607
|
+
return this.makeString(`isResource(${value})`);
|
|
608
|
+
}
|
|
609
|
+
makeEnumEntity(enumEntity, isExport) {
|
|
610
|
+
return new TsEnumEntityStatement(enumEntity, isExport);
|
|
611
|
+
}
|
|
612
|
+
makeFieldModifiersList(modifiers, customFieldFilter) {
|
|
613
|
+
let allowedModifiers = this.supportedFieldModifiers;
|
|
614
|
+
let modifierFilter = customFieldFilter ? customFieldFilter : function (field) {
|
|
615
|
+
return allowedModifiers.includes(field);
|
|
616
|
+
};
|
|
617
|
+
let prefix = modifiers === null || modifiers === void 0 ? void 0 : modifiers.filter(modifierFilter).map(it => this.mapFieldModifier(it)).join(" ");
|
|
618
|
+
return prefix ? prefix : "";
|
|
619
|
+
}
|
|
620
|
+
escapeKeyword(keyword) {
|
|
621
|
+
return keyword;
|
|
622
|
+
}
|
|
623
|
+
makeCastCustomObject(customName, _isGenericType) {
|
|
624
|
+
return this.makeString(customName);
|
|
625
|
+
}
|
|
626
|
+
makeHasOwnProperty(value, _valueTypeName, property, propertyTypeName) {
|
|
627
|
+
const expressions = [this.makeString(`${value}.hasOwnProperty("${property}")`)];
|
|
628
|
+
if (propertyTypeName) {
|
|
629
|
+
expressions.push(this.makeString(`isInstanceOf("${propertyTypeName}", ${value}.${property})`));
|
|
630
|
+
}
|
|
631
|
+
return this.makeNaryOp("&&", expressions);
|
|
632
|
+
}
|
|
633
|
+
discriminatorFromExpressions(value, runtimeType, exprs) {
|
|
634
|
+
return this.makeNaryOp("&&", [
|
|
635
|
+
this.makeNaryOp("==", [this.makeRuntimeType(runtimeType), this.makeString(`${value}_type`)]),
|
|
636
|
+
...exprs
|
|
637
|
+
]);
|
|
638
|
+
}
|
|
639
|
+
makeDiscriminatorConvertor(_convertor, _value, _index) {
|
|
640
|
+
return undefined;
|
|
641
|
+
}
|
|
642
|
+
makeNot(expr) {
|
|
643
|
+
return this.makeString(`!(${expr.asString()})`);
|
|
644
|
+
}
|
|
645
|
+
makeEquals(args) {
|
|
646
|
+
return this.makeNaryOp("===", args);
|
|
647
|
+
}
|
|
648
|
+
castToInt(value, bitness) { return value; }
|
|
649
|
+
castToBoolean(value) { return value; }
|
|
650
|
+
makeCallIsObject(value) {
|
|
651
|
+
return this.makeString(`typeof ${value} === "object"`);
|
|
652
|
+
}
|
|
653
|
+
makeCallIsArrayBuffer(value) {
|
|
654
|
+
return this.makeString(`${value} instanceof ArrayBuffer`);
|
|
655
|
+
}
|
|
656
|
+
instanceOf(convertor, value, _duplicateMembers) {
|
|
657
|
+
return this.makeString(`${value} instanceof ${this.getNodeName(convertor.idlType)}`);
|
|
658
|
+
}
|
|
659
|
+
// The version of instanceOf() which does not use ArgConvertors
|
|
660
|
+
typeInstanceOf(type, value, members) {
|
|
661
|
+
return this.makeString(`${value} instanceof ${this.getNodeName(type)}`);
|
|
662
|
+
}
|
|
663
|
+
makeLengthSerializer(serializer, value) {
|
|
664
|
+
const valueType = "valueType";
|
|
665
|
+
return this.makeBlock([
|
|
666
|
+
this.makeAssign(valueType, undefined, this.makeFunctionCall("runtimeType", [this.makeString(value)]), true),
|
|
667
|
+
this.makeStatement(this.makeMethodCall(serializer, "writeInt8", [this.makeString(valueType)])),
|
|
668
|
+
this.makeMultiBranchCondition([
|
|
669
|
+
{
|
|
670
|
+
expr: this.makeRuntimeTypeCondition(valueType, true, RuntimeType.NUMBER),
|
|
671
|
+
stmt: this.makeStatement(this.makeMethodCall(serializer, "writeFloat32", [this.makeString(`${value} as float32`)]))
|
|
672
|
+
},
|
|
673
|
+
{
|
|
674
|
+
expr: this.makeRuntimeTypeCondition(valueType, true, RuntimeType.STRING),
|
|
675
|
+
stmt: this.makeStatement(this.makeMethodCall(serializer, "writeString", [this.makeString(`${value} as string`)]))
|
|
676
|
+
},
|
|
677
|
+
{
|
|
678
|
+
expr: this.makeRuntimeTypeCondition(valueType, true, RuntimeType.OBJECT),
|
|
679
|
+
stmt: this.makeStatement(this.makeMethodCall(serializer, "writeInt32", [this.makeString(`(${value} as Resource).id as int32`)]))
|
|
680
|
+
},
|
|
681
|
+
]),
|
|
682
|
+
], false);
|
|
683
|
+
}
|
|
684
|
+
makeLengthDeserializer(deserializer) {
|
|
685
|
+
const valueType = "valueType";
|
|
686
|
+
return this.makeBlock([
|
|
687
|
+
this.makeAssign(valueType, undefined, this.makeMethodCall(deserializer, "readInt8", []), true),
|
|
688
|
+
this.makeMultiBranchCondition([{
|
|
689
|
+
expr: this.makeRuntimeTypeCondition(valueType, true, RuntimeType.NUMBER),
|
|
690
|
+
stmt: this.makeReturn(this.makeString(`${deserializer}.readFloat32() as number`))
|
|
691
|
+
},
|
|
692
|
+
{
|
|
693
|
+
expr: this.makeRuntimeTypeCondition(valueType, true, RuntimeType.STRING),
|
|
694
|
+
stmt: this.makeReturn(this.makeMethodCall(deserializer, "readString", []))
|
|
695
|
+
},
|
|
696
|
+
{
|
|
697
|
+
expr: this.makeRuntimeTypeCondition(valueType, true, RuntimeType.OBJECT),
|
|
698
|
+
stmt: this.makeReturn(this.makeString(`({id: ${deserializer}.readInt32(), bundleName: "", moduleName: ""}) as Resource`))
|
|
699
|
+
}], this.makeReturn(this.makeUndefined())),
|
|
700
|
+
], false);
|
|
701
|
+
}
|
|
702
|
+
stringifyTypeOrEmpty(type) {
|
|
703
|
+
if (type === undefined)
|
|
704
|
+
return "";
|
|
705
|
+
return this.getNodeName(type);
|
|
706
|
+
}
|
|
707
|
+
/**
|
|
708
|
+
* Writes `namespace <namespace> {` and adds extra indent
|
|
709
|
+
* @param namespace Namespace to begin
|
|
710
|
+
*/
|
|
711
|
+
pushNamespace(namespace, ident = true) {
|
|
712
|
+
this.print(`namespace ${namespace} {`);
|
|
713
|
+
if (ident)
|
|
714
|
+
this.pushIndent();
|
|
715
|
+
}
|
|
716
|
+
/**
|
|
717
|
+
* Writes closing brace of namespace block and removes one level of indent
|
|
718
|
+
*/
|
|
719
|
+
popNamespace(ident = true) {
|
|
720
|
+
if (ident)
|
|
721
|
+
this.popIndent();
|
|
722
|
+
this.print(`}`);
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
export function mangleMethodName(method, id) {
|
|
726
|
+
return `${method.name}${id !== null && id !== void 0 ? id : ""}`;
|
|
727
|
+
}
|
|
728
|
+
export function printMethodDeclaration(printer, retType, methodName, apiParameters, postfix = "") {
|
|
729
|
+
if (apiParameters.length > 1) {
|
|
730
|
+
const methodTypeName = `${retType} ${methodName}`;
|
|
731
|
+
const indent = ` `.repeat(methodTypeName.length + 1);
|
|
732
|
+
printer.print(`${methodTypeName}(${apiParameters[0]},`);
|
|
733
|
+
for (let i = 1; i < apiParameters.length; i++) {
|
|
734
|
+
printer.print(indent + apiParameters[i] + ((i === apiParameters.length - 1) ? `)${postfix}` : ","));
|
|
735
|
+
}
|
|
736
|
+
}
|
|
737
|
+
else {
|
|
738
|
+
const signature = `${retType} ${methodName}(${apiParameters.join(", ")})${postfix}`;
|
|
739
|
+
printer.print(signature);
|
|
740
|
+
}
|
|
741
|
+
}
|
|
742
|
+
export function copyMethod(method, overrides) {
|
|
743
|
+
var _a, _b, _c, _d;
|
|
744
|
+
return new Method((_a = overrides.name) !== null && _a !== void 0 ? _a : method.name, (_b = overrides.signature) !== null && _b !== void 0 ? _b : method.signature, (_c = overrides.modifiers) !== null && _c !== void 0 ? _c : method.modifiers, (_d = overrides.generics) !== null && _d !== void 0 ? _d : method.generics);
|
|
745
|
+
}
|
|
746
|
+
//# sourceMappingURL=LanguageWriter.js.map
|