-
-
Notifications
You must be signed in to change notification settings - Fork 185
Expand file tree
/
Copy pathcall.ts
More file actions
293 lines (254 loc) · 12.1 KB
/
call.ts
File metadata and controls
293 lines (254 loc) · 12.1 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
import * as ts from "typescript";
import * as lua from "../../LuaAST";
import { transformBuiltinCallExpression } from "../builtins";
import { FunctionVisitor, TransformationContext } from "../context";
import { validateAssignment } from "../utils/assignment-validation";
import { ContextType, getCallContextType } from "../utils/function-context";
import { wrapInTable } from "../utils/lua-ast";
import { isValidLuaIdentifier } from "../utils/safe-names";
import { isExpressionWithEvaluationEffect } from "../utils/typescript";
import { transformElementAccessArgument } from "./access";
import { isMultiReturnCall, shouldMultiReturnCallBeWrapped } from "./language-extensions/multi";
import { unsupportedBuiltinOptionalCall } from "../utils/diagnostics";
import { moveToPrecedingTemp, transformExpressionList } from "./expression-list";
import { transformInPrecedingStatementScope } from "../utils/preceding-statements";
import { getOptionalContinuationData, transformOptionalChain } from "./optional-chaining";
import { transformImportExpression } from "./modules/import";
import { transformLanguageExtensionCallExpression } from "./language-extensions/call-extension";
import { getCustomNameFromSymbol } from "./identifier";
export function validateArguments(
context: TransformationContext,
params: readonly ts.Expression[],
signature?: ts.Signature
) {
if (!signature || signature.parameters.length < params.length) {
return;
}
for (const [index, param] of params.entries()) {
const signatureParameter = signature.parameters[index];
if (signatureParameter.valueDeclaration !== undefined) {
const signatureType = context.checker.getTypeAtLocation(signatureParameter.valueDeclaration);
const paramType = context.checker.getTypeAtLocation(param);
validateAssignment(context, param, paramType, signatureType, signatureParameter.name);
}
}
}
export function transformArguments(
context: TransformationContext,
params: readonly ts.Expression[],
signature?: ts.Signature,
callContext?: ts.Expression
): lua.Expression[] {
validateArguments(context, params, signature);
return transformExpressionList(context, callContext ? [callContext, ...params] : params);
}
function transformCallWithArguments(
context: TransformationContext,
callExpression: ts.Expression,
transformedArguments: lua.Expression[],
argPrecedingStatements: lua.Statement[],
callContext?: ts.Expression
): [lua.Expression, lua.Expression[]] {
let call = context.transformExpression(callExpression);
let transformedContext: lua.Expression | undefined;
if (callContext) {
transformedContext = context.transformExpression(callContext);
}
if (argPrecedingStatements.length > 0) {
if (transformedContext) {
transformedContext = moveToPrecedingTemp(context, transformedContext, callContext);
}
call = moveToPrecedingTemp(context, call, callExpression);
context.addPrecedingStatements(argPrecedingStatements);
}
if (transformedContext) {
transformedArguments.unshift(transformedContext);
}
return [call, transformedArguments];
}
export function transformCallAndArguments(
context: TransformationContext,
callExpression: ts.Expression,
params: readonly ts.Expression[],
signature?: ts.Signature,
callContext?: ts.Expression
): [lua.Expression, lua.Expression[]] {
const { precedingStatements: argPrecedingStatements, result: transformedArguments } =
transformInPrecedingStatementScope(context, () => transformArguments(context, params, signature, callContext));
return transformCallWithArguments(context, callExpression, transformedArguments, argPrecedingStatements);
}
function transformElementAccessCall(
context: TransformationContext,
left: ts.PropertyAccessExpression | ts.ElementAccessExpression,
transformedArguments: lua.Expression[],
argPrecedingStatements: lua.Statement[]
) {
// Cache left-side if it has effects
// local ____self = context; return ____self[argument](parameters);
const selfIdentifier = lua.createIdentifier(context.createTempName("self"));
const callContext = context.transformExpression(left.expression);
const selfAssignment = lua.createVariableDeclarationStatement(selfIdentifier, callContext);
context.addPrecedingStatements(selfAssignment);
const argument = ts.isElementAccessExpression(left)
? transformElementAccessArgument(context, left)
: lua.createStringLiteral(left.name.text);
let index: lua.Expression = lua.createTableIndexExpression(selfIdentifier, argument);
if (argPrecedingStatements.length > 0) {
// Cache index in temp if args had preceding statements
index = moveToPrecedingTemp(context, index);
context.addPrecedingStatements(argPrecedingStatements);
}
return lua.createCallExpression(index, [selfIdentifier, ...transformedArguments]);
}
export function transformContextualCallExpression(
context: TransformationContext,
node: ts.CallExpression | ts.TaggedTemplateExpression,
args: ts.Expression[] | ts.NodeArray<ts.Expression>
): lua.Expression {
if (ts.isOptionalChain(node)) {
return transformOptionalChain(context, node);
}
const left = ts.isCallExpression(node) ? getCalledExpression(node) : node.tag;
let { precedingStatements: argPrecedingStatements, result: transformedArguments } =
transformInPrecedingStatementScope(context, () => transformArguments(context, args));
if (
ts.isPropertyAccessExpression(left) &&
ts.isIdentifier(left.name) &&
isValidLuaIdentifier(left.name.text, context.options) &&
argPrecedingStatements.length === 0
) {
// table:name()
const table = context.transformExpression(left.expression);
let name = left.name.text;
const symbol = context.checker.getSymbolAtLocation(left);
const customName = getCustomNameFromSymbol(context, symbol);
if (customName) {
name = customName;
}
return lua.createMethodCallExpression(table, lua.createIdentifier(name, left.name), transformedArguments, node);
} else if (ts.isElementAccessExpression(left) || ts.isPropertyAccessExpression(left)) {
if (isExpressionWithEvaluationEffect(left.expression)) {
return transformElementAccessCall(context, left, transformedArguments, argPrecedingStatements);
} else {
let expression: lua.Expression;
[expression, transformedArguments] = transformCallWithArguments(
context,
left,
transformedArguments,
argPrecedingStatements,
left.expression
);
return lua.createCallExpression(expression, transformedArguments, node);
}
} else if (ts.isIdentifier(left) || ts.isCallExpression(left)) {
const callContext = context.isStrict ? ts.factory.createNull() : ts.factory.createIdentifier("_G");
let expression: lua.Expression;
[expression, transformedArguments] = transformCallWithArguments(
context,
left,
transformedArguments,
argPrecedingStatements,
callContext
);
return lua.createCallExpression(expression, transformedArguments, node);
} else {
throw new Error(`Unsupported LeftHandSideExpression kind: ${ts.SyntaxKind[left.kind]}`);
}
}
function transformPropertyCall(
context: TransformationContext,
node: ts.CallExpression,
calledMethod: ts.PropertyAccessExpression
): lua.Expression {
const signature = context.checker.getResolvedSignature(node);
if (calledMethod.expression.kind === ts.SyntaxKind.SuperKeyword) {
// Super calls take the format of super.call(self,...)
const parameters = transformArguments(context, node.arguments, signature, ts.factory.createThis());
return lua.createCallExpression(context.transformExpression(node.expression), parameters, node);
}
if (getCallContextType(context, node) !== ContextType.Void) {
// table:name()
return transformContextualCallExpression(context, node, node.arguments);
} else {
// table.name()
const [callPath, parameters] = transformCallAndArguments(context, node.expression, node.arguments, signature);
return lua.createCallExpression(callPath, parameters, node);
}
}
function transformElementCall(context: TransformationContext, node: ts.CallExpression): lua.Expression {
if (getCallContextType(context, node) !== ContextType.Void) {
// A contextual parameter must be given to this call expression
return transformContextualCallExpression(context, node, node.arguments);
} else {
// No context
const [expression, parameters] = transformCallAndArguments(context, node.expression, node.arguments);
return lua.createCallExpression(expression, parameters, node);
}
}
export const transformCallExpression: FunctionVisitor<ts.CallExpression> = (node, context) => {
const calledExpression = getCalledExpression(node);
if (calledExpression.kind === ts.SyntaxKind.ImportKeyword) {
return transformImportExpression(node, context);
}
if (ts.isOptionalChain(node)) {
return transformOptionalChain(context, node);
}
const optionalContinuation = ts.isIdentifier(calledExpression)
? getOptionalContinuationData(calledExpression)
: undefined;
const wrapResultInTable = isMultiReturnCall(context, node) && shouldMultiReturnCallBeWrapped(context, node);
const builtinOrExtensionResult =
transformBuiltinCallExpression(context, node) ?? transformLanguageExtensionCallExpression(context, node);
if (builtinOrExtensionResult) {
if (optionalContinuation !== undefined) {
context.diagnostics.push(unsupportedBuiltinOptionalCall(node));
}
return wrapResultInTable ? wrapInTable(builtinOrExtensionResult) : builtinOrExtensionResult;
}
if (ts.isPropertyAccessExpression(calledExpression)) {
const result = transformPropertyCall(context, node, calledExpression);
return wrapResultInTable ? wrapInTable(result) : result;
}
if (ts.isElementAccessExpression(calledExpression)) {
const result = transformElementCall(context, node);
return wrapResultInTable ? wrapInTable(result) : result;
}
const signature = context.checker.getResolvedSignature(node);
// Handle super calls properly
if (calledExpression.kind === ts.SyntaxKind.SuperKeyword) {
const parameters = transformArguments(context, node.arguments, signature, ts.factory.createThis());
return lua.createCallExpression(
lua.createTableIndexExpression(
context.transformExpression(ts.factory.createSuper()),
lua.createStringLiteral("____constructor")
),
parameters,
node
);
}
let callPath: lua.Expression;
let parameters: lua.Expression[];
const isContextualCall = getCallContextType(context, node) !== ContextType.Void;
if (!isContextualCall) {
[callPath, parameters] = transformCallAndArguments(context, calledExpression, node.arguments, signature);
} else {
// if is optionalContinuation, context will be handled by transformOptionalChain.
const useGlobalContext = !context.isStrict && optionalContinuation === undefined;
const callContext = useGlobalContext ? ts.factory.createIdentifier("_G") : ts.factory.createNull();
[callPath, parameters] = transformCallAndArguments(
context,
calledExpression,
node.arguments,
signature,
callContext
);
}
const callExpression = lua.createCallExpression(callPath, parameters, node);
if (optionalContinuation && isContextualCall) {
optionalContinuation.contextualCall = callExpression;
}
return wrapResultInTable ? wrapInTable(callExpression) : callExpression;
};
export function getCalledExpression(node: ts.CallExpression): ts.Expression {
return ts.skipOuterExpressions(node.expression);
}