UNPKG

typescript-to-lua

Version:

A generic TypeScript to Lua transpiler. Write your code in TypeScript and publish Lua!

54 lines 3.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.transformFunctionPrototypeCall = transformFunctionPrototypeCall; exports.transformFunctionProperty = transformFunctionProperty; const CompilerOptions_1 = require("../../CompilerOptions"); const lua = require("../../LuaAST"); const diagnostics_1 = require("../utils/diagnostics"); const function_context_1 = require("../utils/function-context"); const lualib_1 = require("../utils/lualib"); const call_1 = require("../visitors/call"); const lua_ast_1 = require("../utils/lua-ast"); function transformFunctionPrototypeCall(context, node, calledMethod) { const callerType = context.checker.getTypeAtLocation(calledMethod.expression); if ((0, function_context_1.getFunctionContextType)(context, callerType) === function_context_1.ContextType.Void) { context.diagnostics.push((0, diagnostics_1.unsupportedSelfFunctionConversion)(node)); } const signature = context.checker.getResolvedSignature(node); const [caller, params] = (0, call_1.transformCallAndArguments)(context, calledMethod.expression, node.arguments, signature); const expressionName = calledMethod.name.text; switch (expressionName) { case "apply": const nonContextArgs = params.length > 1 ? [(0, lua_ast_1.createUnpackCall)(context, params[1], node.arguments[1])] : []; return lua.createCallExpression(caller, [params[0], ...nonContextArgs], node); case "bind": return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.FunctionBind, node, caller, ...params); case "call": return lua.createCallExpression(caller, params, node); case "toString": context.diagnostics.push((0, diagnostics_1.unsupportedProperty)(calledMethod.name, "function", expressionName)); } } function transformFunctionProperty(context, node) { switch (node.name.text) { case "length": if (context.luaTarget === CompilerOptions_1.LuaTarget.Lua50 || context.luaTarget === CompilerOptions_1.LuaTarget.Lua51 || context.luaTarget === CompilerOptions_1.LuaTarget.Universal) { context.diagnostics.push((0, diagnostics_1.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 = (0, function_context_1.getFunctionContextType)(context, context.checker.getTypeAtLocation(node.expression)); return contextType === function_context_1.ContextType.NonVoid ? lua.createBinaryExpression(nparams, lua.createNumericLiteral(1), lua.SyntaxKind.SubtractionOperator) : nparams; case "arguments": case "caller": case "displayName": case "name": context.diagnostics.push((0, diagnostics_1.unsupportedProperty)(node.name, "function", node.name.text)); } } //# sourceMappingURL=function.js.map