Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Unreleased

- `Function.length` is supported now

## 0.34.0

- Added new `"luaTarget"` option value - `"universal"`. Choosing this target makes TypeScriptToLua generate code compatible with all supported Lua targets.
Expand Down
32 changes: 31 additions & 1 deletion src/transformation/builtins/function.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import * as ts from "typescript";
import { LuaTarget } from "../../CompilerOptions";
import * as lua from "../../LuaAST";
import { TransformationContext } from "../context";
import { unsupportedProperty, unsupportedSelfFunctionConversion } from "../utils/diagnostics";
import { unsupportedForTarget, unsupportedProperty, unsupportedSelfFunctionConversion } from "../utils/diagnostics";
import { ContextType, getFunctionContextType } from "../utils/function-context";
import { createUnpackCall } from "../utils/lua-ast";
import { LuaLibFeature, transformLuaLibFunction } from "../utils/lualib";
Expand Down Expand Up @@ -35,3 +37,31 @@ export function transformFunctionPrototypeCall(
context.diagnostics.push(unsupportedProperty(expression.name, "function", expressionName));
}
}

export function transformFunctionProperty(
context: TransformationContext,
node: ts.PropertyAccessExpression
): lua.Expression | undefined {
switch (node.name.text) {
case "length":
if (context.luaTarget === LuaTarget.Lua51 || context.luaTarget === LuaTarget.Universal) {
context.diagnostics.push(unsupportedForTarget(node, "function.length", LuaTarget.Lua51));
}

// 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;

default:
context.diagnostics.push(unsupportedProperty(node.name, "function", node.name.text));
}
}
6 changes: 5 additions & 1 deletion src/transformation/builtins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { PropertyCallExpression } from "../visitors/call";
import { checkForLuaLibType } from "../visitors/class/new";
import { transformArrayProperty, transformArrayPrototypeCall } from "./array";
import { transformConsoleCall } from "./console";
import { transformFunctionPrototypeCall } from "./function";
import { transformFunctionPrototypeCall, transformFunctionProperty } from "./function";
import { transformGlobalCall } from "./global";
import { transformMathCall, transformMathProperty } from "./math";
import { transformNumberConstructorCall, transformNumberPrototypeCall } from "./number";
Expand All @@ -38,6 +38,10 @@ export function transformBuiltinPropertyAccessExpression(
return transformArrayProperty(context, node);
}

if (isFunctionType(context, ownerType)) {
return transformFunctionProperty(context, node);
}

if (ts.isIdentifier(node.expression) && isStandardLibraryType(context, ownerType, undefined)) {
switch (node.expression.text) {
case "Math":
Expand Down
24 changes: 24 additions & 0 deletions test/unit/functions/__snapshots__/functions.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`function.length unsupported ("5.1"): code 1`] = `
"local ____exports = {}
function ____exports.__main(self)
local function fn(self)
end
return debug.getinfo(fn).nparams - 1
end
return ____exports"
`;

exports[`function.length unsupported ("5.1"): diagnostics 1`] = `"main.ts(3,16): error TSTL: function.length is/are not supported for target Lua 5.1."`;

exports[`function.length unsupported ("universal"): code 1`] = `
"local ____exports = {}
function ____exports.__main(self)
local function fn(self)
end
return debug.getinfo(fn).nparams - 1
end
return ____exports"
`;

exports[`function.length unsupported ("universal"): diagnostics 1`] = `"main.ts(3,16): error TSTL: function.length is/are not supported for target Lua 5.1."`;

exports[`missing declaration name: code 1`] = `
"function ____(self)
end"
Expand Down
27 changes: 27 additions & 0 deletions test/unit/functions/functions.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import * as tstl from "../../../src";
import * as util from "../../util";
import { unsupportedForTarget } from "../../../src/transformation/utils/diagnostics";

test("Arrow Function Expression", () => {
util.testFunction`
Expand Down Expand Up @@ -185,6 +187,31 @@ test("Function call", () => {
`.expectToMatchJsResult();
});

test.each([
"function fn() {}",
"function fn(x, y, z) {}",
"function fn(x, y, z, ...args) {}",
"function fn(...args) {}",
"function fn(this: void) {}",
"function fn(this: void, x, y, z) {}",
"function fnReference(x, y, z) {} const fn = fnReference;",
"const wrap = (fn: (...args: any[]) => any) => (...args: any[]) => fn(...args); const fn = wrap((x, y, z) => {});",
])("function.length (%p)", declaration => {
util.testFunction`
${declaration}
return fn.length;
`.expectToMatchJsResult();
});

test.each([tstl.LuaTarget.Lua51, tstl.LuaTarget.Universal])("function.length unsupported (%p)", luaTarget => {
util.testFunction`
function fn() {}
return fn.length;
`
.setOptions({ luaTarget })
.expectDiagnosticsToMatchSnapshot([unsupportedForTarget.code]);
});

test("Recursive function definition", () => {
util.testFunction`
function f() { return typeof f; };
Expand Down