typescript-to-lua
Version:
A generic TypeScript to Lua transpiler. Write your code in TypeScript and publish Lua!
87 lines • 4.67 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.transformFunctionPrototypeCall = transformFunctionPrototypeCall;
exports.transformFunctionProperty = transformFunctionProperty;
const CompilerOptions_1 = require("../../CompilerOptions");
const lua = __importStar(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