|
| 1 | +import * as ts from "typescript"; |
| 2 | +import { LuaTarget } from "../../CompilerOptions"; |
1 | 3 | import * as lua from "../../LuaAST"; |
2 | 4 | import { TransformationContext } from "../context"; |
3 | | -import { unsupportedProperty, unsupportedSelfFunctionConversion } from "../utils/diagnostics"; |
| 5 | +import { unsupportedForTarget, unsupportedProperty, unsupportedSelfFunctionConversion } from "../utils/diagnostics"; |
4 | 6 | import { ContextType, getFunctionContextType } from "../utils/function-context"; |
5 | 7 | import { createUnpackCall } from "../utils/lua-ast"; |
6 | 8 | import { LuaLibFeature, transformLuaLibFunction } from "../utils/lualib"; |
@@ -35,3 +37,31 @@ export function transformFunctionPrototypeCall( |
35 | 37 | context.diagnostics.push(unsupportedProperty(expression.name, "function", expressionName)); |
36 | 38 | } |
37 | 39 | } |
| 40 | + |
| 41 | +export function transformFunctionProperty( |
| 42 | + context: TransformationContext, |
| 43 | + node: ts.PropertyAccessExpression |
| 44 | +): lua.Expression | undefined { |
| 45 | + switch (node.name.text) { |
| 46 | + case "length": |
| 47 | + if (context.luaTarget === LuaTarget.Lua51 || context.luaTarget === LuaTarget.Universal) { |
| 48 | + context.diagnostics.push(unsupportedForTarget(node, "function.length", LuaTarget.Lua51)); |
| 49 | + } |
| 50 | + |
| 51 | + // debug.getinfo(fn) |
| 52 | + const getInfoCall = lua.createCallExpression( |
| 53 | + lua.createTableIndexExpression(lua.createIdentifier("debug"), lua.createStringLiteral("getinfo")), |
| 54 | + [context.transformExpression(node.expression)] |
| 55 | + ); |
| 56 | + |
| 57 | + const nparams = lua.createTableIndexExpression(getInfoCall, lua.createStringLiteral("nparams")); |
| 58 | + |
| 59 | + const contextType = getFunctionContextType(context, context.checker.getTypeAtLocation(node.expression)); |
| 60 | + return contextType === ContextType.NonVoid |
| 61 | + ? lua.createBinaryExpression(nparams, lua.createNumericLiteral(1), lua.SyntaxKind.SubtractionOperator) |
| 62 | + : nparams; |
| 63 | + |
| 64 | + default: |
| 65 | + context.diagnostics.push(unsupportedProperty(node.name, "function", node.name.text)); |
| 66 | + } |
| 67 | +} |
0 commit comments