-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathpreprocessorlua.ts
More file actions
443 lines (371 loc) · 19.5 KB
/
Copy pathpreprocessorlua.ts
File metadata and controls
443 lines (371 loc) · 19.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
import * as ts from 'typescript';
import { IdentifierResolver } from './resolvers';
import { TypeInfo } from './typeInfo';
export class PreprocessorLua {
public constructor(private resolver: IdentifierResolver, private typeInfo: TypeInfo) {
}
public preprocessStatement(node: ts.Statement): ts.Statement {
if (!node) {
throw new Error('node is null or undefined');
}
return node;
}
public preprocessExpression(node: ts.Expression): ts.Expression {
if (!node) {
throw new Error('node is null or undefined');
}
let newExpression: ts.Expression;
switch (node.kind) {
case ts.SyntaxKind.NewExpression:
newExpression = this.preprocessNewExpression(<ts.NewExpression>node);
break;
case ts.SyntaxKind.CallExpression:
newExpression = this.preprocessCallExpression(<ts.CallExpression>node);
break;
case ts.SyntaxKind.PropertyAccessExpression:
newExpression = this.preprocessPropertyAccessExpression(<ts.PropertyAccessExpression>node);
break;
case ts.SyntaxKind.ElementAccessExpression:
newExpression = this.preprocessElementAccessExpression(<ts.ElementAccessExpression>node);
break;
case ts.SyntaxKind.TypeAssertionExpression:
newExpression = this.preprocessTypeAssertionExpression(<ts.TypeAssertion>node);
break;
}
if (newExpression) {
return newExpression;
}
return node;
}
private preprocessNewExpression(newExpression: ts.NewExpression): ts.Expression {
if (newExpression.arguments.length === 0) {
return;
}
const methodDelc = this.typeInfo.getTypeObject(newExpression.expression);
const constructs = methodDelc
&& methodDelc.symbol
&& methodDelc.symbol.valueDeclaration
&& methodDelc.symbol.valueDeclaration.members.filter(m => m.kind === ts.SyntaxKind.Constructor);
const parameters = constructs && constructs.length > 0 && constructs[0].parameters;
const newArgs = <any>this.preprocessCallParameters(newExpression.arguments, parameters);
if (newArgs) {
newExpression.arguments = newArgs;
}
return undefined;
}
private preprocessCallExpression(callExpression: ts.CallExpression): ts.Expression {
// String -> tostring, Number -> tonumber
if (callExpression.expression.kind === ts.SyntaxKind.Identifier && callExpression.arguments.length === 1) {
const name = callExpression.expression.kind === ts.SyntaxKind.Identifier ? (<ts.Identifier>callExpression.expression).text : '';
if (name === 'String' || name === 'Number') {
const identExpr = ts.createIdentifier('to' + name.toLowerCase());
identExpr.parent = callExpression.expression.parent;
callExpression.expression = identExpr;
}
}
// preprocess method paremeters when const string cast to String object required
this.preprocessMethodCallParameters(callExpression);
if (callExpression.expression.kind === ts.SyntaxKind.PropertyAccessExpression) {
const newExpression =
this.preprocessBindingApplyingOrCalling(callExpression)
|| this.preprocessConstCast(callExpression);
if (newExpression) {
return newExpression;
}
}
this.preprocessSuperCalls(callExpression);
return undefined;
}
// we need to convert all method returns (such as <THIS>.<METHOD>)
// into __wrapper(this, method) to be able to call method without providing <THIS>
private preprocessPropertyAccessExpression(propertyAccessExpression: ts.PropertyAccessExpression): ts.Expression {
if (propertyAccessExpression.parent
&& (propertyAccessExpression.parent.kind === ts.SyntaxKind.VariableDeclaration
|| propertyAccessExpression.parent.kind === ts.SyntaxKind.PropertyDeclaration
|| propertyAccessExpression.parent.kind === ts.SyntaxKind.BinaryExpression
|| propertyAccessExpression.parent.kind === ts.SyntaxKind.CallExpression)) {
const isRightOfBinaryExpression =
propertyAccessExpression.parent.kind === ts.SyntaxKind.BinaryExpression
&& (<ts.BinaryExpression>propertyAccessExpression.parent).operatorToken.kind === ts.SyntaxKind.EqualsToken
&& (<ts.BinaryExpression>propertyAccessExpression.parent).right === propertyAccessExpression;
const isCallParameter =
propertyAccessExpression.parent.kind === ts.SyntaxKind.CallExpression
&& (<ts.CallExpression>propertyAccessExpression.parent).expression !== propertyAccessExpression;
const declar =
propertyAccessExpression.parent.kind === ts.SyntaxKind.VariableDeclaration
|| propertyAccessExpression.parent.kind === ts.SyntaxKind.PropertyDeclaration;
// in case of getting method
if ((isRightOfBinaryExpression || isCallParameter || declar)
/*&& this.typeInfo.isResultNonStaticMethodReference(propertyAccessExpression)*/
&& this.typeInfo.isResultMethodReference(propertyAccessExpression)
&& !(<any>propertyAccessExpression).__self_call_required) {
// wrap it into method
(<any>propertyAccessExpression).__self_call_required = true;
const methodWrapCall = ts.createCall(ts.createIdentifier('__wrapper'), undefined, [propertyAccessExpression, propertyAccessExpression.expression]);
methodWrapCall.parent = propertyAccessExpression.parent;
return methodWrapCall;
} else if (this.typeInfo.isResultFunctioinType(propertyAccessExpression)) {
// propertyAccessExpression2.parent.kind === ts.SyntaxKind.CallExpression
// suppress SELF calls
(<any>propertyAccessExpression).__self_call_required = false;
}
}
if (this.typeInfo.isTypeOfNode(propertyAccessExpression.expression, 'string')
&& propertyAccessExpression.name.text === 'length'
&& !(<any>propertyAccessExpression.name).__len) {
const getLengthOfString = ts.createCall(
ts.createPropertyAccess(
ts.createIdentifier('StringHelper'), 'getLength'), undefined, [propertyAccessExpression.expression]);
getLengthOfString.parent = propertyAccessExpression.parent;
return getLengthOfString;
}
// replace <XXX>.prototype to <XXX>.__proto
if (propertyAccessExpression.name.text === 'prototype') {
// if access object is Type then prototype is Type thus access to prototype object should be removed
const exprTypeInfo = this.typeInfo.getVariableDeclarationOfTypeOfNode(propertyAccessExpression.expression);
if (exprTypeInfo && exprTypeInfo.kind === ts.SyntaxKind.ClassDeclaration) {
return propertyAccessExpression.expression;
}
// otherwise rename it into __proto
const protoIdentifier = ts.createIdentifier('__proto');
protoIdentifier.parent = propertyAccessExpression.name.parent;
propertyAccessExpression.name = protoIdentifier;
}
return undefined;
}
private preprocessTypeAssertionExpression(typeAssertion: ts.TypeAssertion): ts.Expression {
if (this.typeInfo.isTypeOfNode(typeAssertion.type, 'string') && !this.typeInfo.isTypeOfNode(typeAssertion.expression, 'string')) {
const castExpr = ts.createCall(
ts.createIdentifier('tostring'),
undefined,
[typeAssertion.expression]);
castExpr.parent = typeAssertion.parent;
return castExpr;
}
if (this.typeInfo.isTypeOfNode(typeAssertion.type, 'number') && !this.typeInfo.isTypeOfNode(typeAssertion.expression, 'number')) {
const castExpr = ts.createCall(
ts.createIdentifier('tonumber'),
undefined,
[typeAssertion.expression]);
castExpr.parent = typeAssertion.parent;
return castExpr;
}
return typeAssertion;
}
private preprocessElementAccessExpression(elementAccessExpression: ts.ElementAccessExpression): ts.Expression {
// support string access 'asd'[xxx];
const isConstStringAndNumberElementAccess =
this.typeInfo.isTypeOfNode(elementAccessExpression.expression, 'string')
&& this.typeInfo.isTypeOfNode(elementAccessExpression.argumentExpression, 'number');
if (!isConstStringAndNumberElementAccess) {
return undefined;
}
const stringIdent = ts.createIdentifier('string');
const charIdent = ts.createIdentifier('char');
const byteIdent = ts.createIdentifier('byte');
const decrIndex = ts.createBinary(
elementAccessExpression.argumentExpression, ts.SyntaxKind.PlusToken, ts.createNumericLiteral('1'));
const getByteExpr = ts.createCall(ts.createPropertyAccess(stringIdent, byteIdent), undefined,
[
elementAccessExpression.expression,
decrIndex
]);
decrIndex.parent = getByteExpr;
const expr = ts.createCall(
ts.createPropertyAccess(stringIdent, charIdent),
undefined,
[getByteExpr]);
expr.parent = elementAccessExpression.parent;
getByteExpr.parent = expr;
return expr;
}
// BIND
// convert <xxx>.bind(this) into __bind(<xxx>, this, ...); +apply, +call
// check if end propertyaccess is 'bind'
private preprocessBindingApplyingOrCalling(callExpression: ts.CallExpression): ts.Expression {
const propertyAccessExpression = <ts.PropertyAccessExpression>callExpression.expression;
const propertyName = propertyAccessExpression.name.text;
const isBindCallOfMethod =
(propertyName === 'bind' || propertyName === 'apply' || propertyName === 'call')
&& this.typeInfo.isResultMethodReferenceOrFunctionTypeOrAny(propertyAccessExpression.expression);
if (!isBindCallOfMethod) {
return undefined;
}
const methodBindCall = ts.createCall(
ts.createIdentifier('__' + propertyAccessExpression.name.text),
undefined,
[propertyAccessExpression.expression, ...callExpression.arguments]);
// do not use METHOD as parent, otherwise processCallExpression will mess up with return pareneters
methodBindCall.parent = propertyAccessExpression.parent.parent;
(<any>methodBindCall).__bind_call = true;
return methodBindCall;
}
private preprocessConstCast(callExpression: ts.CallExpression) {
const propertyAccessExpression = <ts.PropertyAccessExpression>callExpression.expression;
// STRING & NUMBER
// string "...".<function>() => new String("...").<function>()
let isConstString = propertyAccessExpression.expression.kind === ts.SyntaxKind.StringLiteral;
let isConstNumber = propertyAccessExpression.expression.kind === ts.SyntaxKind.NumericLiteral;
if (!isConstString && !isConstNumber) {
try {
const typeResult = this.typeInfo.getTypeObject(propertyAccessExpression.expression);
if (typeResult) {
isConstString = (typeResult.intrinsicName || typeof (typeResult.value)) === 'string';
isConstNumber = (typeResult.intrinsicName || typeof (typeResult.value)) === 'number';
}
} catch (e) {
console.warn('Can\'t get type of "' + callExpression.getText() + '"');
}
}
if (isConstString || isConstNumber) {
const methodCall = ts.createCall(
ts.createPropertyAccess(
ts.createIdentifier(
isConstString ? 'StringHelper' : (isConstNumber ? 'NumberHelper' : '')), propertyAccessExpression.name),
undefined,
[propertyAccessExpression.expression, ...callExpression.arguments]);
methodCall.parent = callExpression;
return methodCall;
}
}
private preprocessSuperCalls(callExpression: ts.CallExpression) {
let memberAccess = callExpression.expression;
// SUPER
// convert super.xxx(...) into <Type>.xxx(this, ...);
let lastPropertyAccess: ts.PropertyAccessExpression;
while (memberAccess.kind === ts.SyntaxKind.PropertyAccessExpression) {
lastPropertyAccess = <ts.PropertyAccessExpression>memberAccess;
memberAccess = lastPropertyAccess.expression;
}
if (memberAccess.kind === ts.SyntaxKind.SuperKeyword) {
(<any>callExpression.expression).__self_call_required = false;
// add 'this' parameter
callExpression.arguments = <ts.NodeArray<ts.Expression>><any>[ts.createThis(), ...callExpression.arguments];
}
}
private preprocessMethodCallParameters(callExpression: ts.CallExpression) {
if (callExpression.arguments.length === 0) {
return;
}
const methodDelc = this.typeInfo.getTypeObject(callExpression.expression);
const parameters = methodDelc
&& methodDelc.symbol
&& methodDelc.symbol.valueDeclaration
&& methodDelc.symbol.valueDeclaration.parameters;
const newArgs = <any>this.preprocessCallParameters(callExpression.arguments, parameters);
if (newArgs) {
callExpression.arguments = newArgs;
}
}
private preprocessCallParameters(
args: ts.NodeArray<ts.Expression>,
parameters: ts.NodeArray<ts.ParameterDeclaration>): ts.Expression[] {
if (!args || args.length === 0) {
return null;
}
if (!parameters || parameters.length === 0) {
return null;
}
const newArguments = new Array<ts.Expression>();
const length = args.length;
let anyNewArgument = false;
for (let i = 0; i < length; i++) {
let currentOrNewArgument = args[i];
const parameter = parameters[i];
if (!parameter || parameter.dotDotDotToken) {
continue;
}
let typeOfParameter = parameter.type;
if (typeOfParameter
&& typeOfParameter.kind === ts.SyntaxKind.ArrayType
&& parameter.dotDotDotToken
&& parameter.dotDotDotToken.kind === ts.SyntaxKind.DotDotDotToken) {
typeOfParameter = (<ts.ArrayTypeNode>typeOfParameter).elementType;
}
// if paraneter is NULL treaqt it as "custom code which is not required correction, for example rowget(t, '__get__')"
// case 'Any'
const any = typeOfParameter && typeOfParameter.kind === ts.SyntaxKind.AnyKeyword;
if (any) {
// if string
const typeName = this.typeInfo.getTypeNameOfNode(currentOrNewArgument);
switch (typeName) {
case 'string':
const identString = ts.createIdentifier('String');
const newString = ts.createNew(identString, undefined, [ currentOrNewArgument ]);
newString.parent = currentOrNewArgument.parent;
identString.parent = newString;
currentOrNewArgument = newString;
anyNewArgument = true;
break;
case 'number':
const identNumber = ts.createIdentifier('Number');
const newNumber = ts.createNew(identNumber, undefined, [ currentOrNewArgument ]);
newNumber.parent = currentOrNewArgument.parent;
identNumber.parent = newNumber;
currentOrNewArgument = newNumber;
anyNewArgument = true;
break;
case '__object':
// we need to have Dynamic object which converts all const values such as string into String classes
// and numbers into Number classes as 'any' can't tell us which type we are using
const identObject = ts.createIdentifier('Object');
const newObject = ts.createNew(identObject, undefined, [ currentOrNewArgument ]);
newObject.parent = currentOrNewArgument.parent;
identObject.parent = newObject;
currentOrNewArgument = newObject;
anyNewArgument = true;
break;
}
}
// case 'string'
const stringConstType = typeOfParameter && typeOfParameter.kind === ts.SyntaxKind.StringKeyword;
if (stringConstType) {
// if string
const typeName = this.typeInfo.getTypeNameOfNode(currentOrNewArgument);
switch (typeName) {
case 'string':
case '__function':
// nothing to do
break;
case 'any':
default:
const ident = ts.createIdentifier('__tostring');
const tostringCall = ts.createCall(ident, undefined, [ currentOrNewArgument ]);
ident.parent = tostringCall;
tostringCall.parent = currentOrNewArgument.parent;
currentOrNewArgument = tostringCall;
anyNewArgument = true;
break;
}
}
const numberConstType = parameter.type && parameter.type.kind === ts.SyntaxKind.NumberKeyword;
if (numberConstType) {
// if string
const typeName = this.typeInfo.getTypeNameOfNode(currentOrNewArgument);
switch (typeName) {
case 'number':
case '__function':
// nothing to do
break;
case 'any':
default:
const ident = ts.createIdentifier('tonumber');
const tonumberCall = ts.createCall(ident, undefined, [ currentOrNewArgument ]);
ident.parent = tonumberCall;
tonumberCall.parent = currentOrNewArgument.parent;
currentOrNewArgument = tonumberCall;
anyNewArgument = true;
break;
}
}
if (!currentOrNewArgument) {
throw Error('current argument is undefined/null');
}
newArguments.push(currentOrNewArgument);
}
if (anyNewArgument) {
return newArguments;
}
return null;
}
}