forked from MCJack123/TypeScriptToLua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction.ts
More file actions
70 lines (63 loc) · 3.22 KB
/
function.ts
File metadata and controls
70 lines (63 loc) · 3.22 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
import * as ts from "typescript";
import { LuaTarget } from "../../CompilerOptions";
import * as lua from "../../LuaAST";
import { TransformationContext } from "../context";
import { unsupportedForTarget, unsupportedProperty, unsupportedSelfFunctionConversion } from "../utils/diagnostics";
import { ContextType, getFunctionContextType } from "../utils/function-context";
import { LuaLibFeature, transformLuaLibFunction } from "../utils/lualib";
import { transformCallAndArguments } from "../visitors/call";
import { createUnpackCall } from "../utils/lua-ast";
export function transformFunctionPrototypeCall(
context: TransformationContext,
node: ts.CallExpression,
calledMethod: ts.PropertyAccessExpression
): lua.CallExpression | undefined {
const callerType = context.checker.getTypeAtLocation(calledMethod.expression);
if (getFunctionContextType(context, callerType) === ContextType.Void) {
context.diagnostics.push(unsupportedSelfFunctionConversion(node));
}
const signature = context.checker.getResolvedSignature(node);
const [caller, params] = transformCallAndArguments(context, calledMethod.expression, node.arguments, signature);
const expressionName = calledMethod.name.text;
switch (expressionName) {
case "apply":
const nonContextArgs = params.length > 1 ? [createUnpackCall(context, params[1], node.arguments[1])] : [];
return lua.createCallExpression(caller, [params[0], ...nonContextArgs], node);
case "bind":
return transformLuaLibFunction(context, LuaLibFeature.FunctionBind, node, caller, ...params);
case "call":
return lua.createCallExpression(caller, params, node);
case "toString":
context.diagnostics.push(unsupportedProperty(calledMethod.name, "function", expressionName));
}
}
export function transformFunctionProperty(
context: TransformationContext,
node: ts.PropertyAccessExpression
): lua.Expression | undefined {
switch (node.name.text) {
case "length":
if (
context.luaTarget === LuaTarget.Lua50 ||
context.luaTarget === LuaTarget.Lua51 ||
context.luaTarget === LuaTarget.Universal
) {
context.diagnostics.push(unsupportedForTarget(node, "function.length", context.luaTarget));
}
// debug.getinfo(fn)
const getInfoCall = lua.createCallExpression(
lua.createTableIndexExpression(lua.createIdentifier("debug"), lua.createStringLiteral("getinfo")),
[context.transformExpression(node.expression)]
);
const nparams = lua.createTableIndexExpression(getInfoCall, lua.createStringLiteral("nparams"));
const contextType = getFunctionContextType(context, context.checker.getTypeAtLocation(node.expression));
return contextType === ContextType.NonVoid
? lua.createBinaryExpression(nparams, lua.createNumericLiteral(1), lua.SyntaxKind.SubtractionOperator)
: nparams;
case "arguments":
case "caller":
case "displayName":
case "name":
context.diagnostics.push(unsupportedProperty(node.name, "function", node.name.text));
}
}