From 9935e8d6e3087beecc5e4895fb20c357ad6f0d97 Mon Sep 17 00:00:00 2001 From: Cold Fry Date: Sun, 29 Mar 2026 20:23:26 +0100 Subject: [PATCH 1/2] fix Math.atan2 emitting math.atan2 instead of math.atan for Lua 5.4+ math.atan2 was removed in Lua 5.3 (replaced by two-arg math.atan). The codegen only special-cased 5.3 but not 5.4 or 5.5, which also lack math.atan2 (5.4 has it behind a compat flag, 5.5 drops it). --- src/transformation/builtins/math.ts | 8 ++++++-- test/unit/builtins/math.spec.ts | 4 ++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/transformation/builtins/math.ts b/src/transformation/builtins/math.ts index ab831d52f..03d3e6993 100644 --- a/src/transformation/builtins/math.ts +++ b/src/transformation/builtins/math.ts @@ -42,14 +42,18 @@ export function transformMathCall( const expressionName = calledMethod.name.text; switch (expressionName) { - // Lua 5.3: math.atan(y, x) + // Lua 5.3+: math.atan(y, x) // Otherwise: math.atan2(y, x) case "atan2": { if (context.luaTarget === LuaTarget.Universal) { return transformLuaLibFunction(context, LuaLibFeature.MathAtan2, node, ...params); } - const method = lua.createStringLiteral(context.luaTarget === LuaTarget.Lua53 ? "atan" : "atan2"); + const useAtan = + context.luaTarget === LuaTarget.Lua53 || + context.luaTarget === LuaTarget.Lua54 || + context.luaTarget === LuaTarget.Lua55; + const method = lua.createStringLiteral(useAtan ? "atan" : "atan2"); return lua.createCallExpression(lua.createTableIndexExpression(math, method), params, node); } diff --git a/test/unit/builtins/math.spec.ts b/test/unit/builtins/math.spec.ts index f3ff1f306..d09b331cd 100644 --- a/test/unit/builtins/math.spec.ts +++ b/test/unit/builtins/math.spec.ts @@ -95,8 +95,8 @@ util.testEachVersion("Math.atan2", () => util.testExpression`Math.atan2(4, 5)`, [tstl.LuaTarget.Lua51]: builder => builder.tap(expectMathAtan2), [tstl.LuaTarget.Lua52]: builder => builder.tap(expectMathAtan2), [tstl.LuaTarget.Lua53]: builder => builder.tap(expectMathAtan), - [tstl.LuaTarget.Lua54]: builder => builder.tap(expectMathAtan2), - [tstl.LuaTarget.Lua55]: builder => builder.tap(expectMathAtan2), + [tstl.LuaTarget.Lua54]: builder => builder.tap(expectMathAtan), + [tstl.LuaTarget.Lua55]: builder => builder.tap(expectMathAtan), [tstl.LuaTarget.Luau]: builder => builder.tap(expectMathAtan2), }); From 93007a61ac43d77452aa609e9d9730e9a6b31283 Mon Sep 17 00:00:00 2001 From: Cold Fry Date: Mon, 30 Mar 2026 02:16:03 +0100 Subject: [PATCH 2/2] invert atan2 condition to default future Lua targets to math.atan Enumerate the older targets that need math.atan2 instead of the newer ones that need math.atan, so any future Lua target (5.6+) defaults to the modern behavior rather than silently regressing. --- src/transformation/builtins/math.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/transformation/builtins/math.ts b/src/transformation/builtins/math.ts index 03d3e6993..82a118ec1 100644 --- a/src/transformation/builtins/math.ts +++ b/src/transformation/builtins/math.ts @@ -49,11 +49,13 @@ export function transformMathCall( return transformLuaLibFunction(context, LuaLibFeature.MathAtan2, node, ...params); } - const useAtan = - context.luaTarget === LuaTarget.Lua53 || - context.luaTarget === LuaTarget.Lua54 || - context.luaTarget === LuaTarget.Lua55; - const method = lua.createStringLiteral(useAtan ? "atan" : "atan2"); + const useAtan2 = + context.luaTarget === LuaTarget.Lua50 || + context.luaTarget === LuaTarget.Lua51 || + context.luaTarget === LuaTarget.Lua52 || + context.luaTarget === LuaTarget.LuaJIT || + context.luaTarget === LuaTarget.Luau; + const method = lua.createStringLiteral(useAtan2 ? "atan2" : "atan"); return lua.createCallExpression(lua.createTableIndexExpression(math, method), params, node); }