Skip to content

Commit 84ef0b2

Browse files
authored
fix Math.atan2 emitting math.atan2 instead of math.atan for Lua 5.4+ (#1700)
* 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). * 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.
1 parent ec26bdf commit 84ef0b2

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

src/transformation/builtins/math.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,20 @@ export function transformMathCall(
4242

4343
const expressionName = calledMethod.name.text;
4444
switch (expressionName) {
45-
// Lua 5.3: math.atan(y, x)
45+
// Lua 5.3+: math.atan(y, x)
4646
// Otherwise: math.atan2(y, x)
4747
case "atan2": {
4848
if (context.luaTarget === LuaTarget.Universal) {
4949
return transformLuaLibFunction(context, LuaLibFeature.MathAtan2, node, ...params);
5050
}
5151

52-
const method = lua.createStringLiteral(context.luaTarget === LuaTarget.Lua53 ? "atan" : "atan2");
52+
const useAtan2 =
53+
context.luaTarget === LuaTarget.Lua50 ||
54+
context.luaTarget === LuaTarget.Lua51 ||
55+
context.luaTarget === LuaTarget.Lua52 ||
56+
context.luaTarget === LuaTarget.LuaJIT ||
57+
context.luaTarget === LuaTarget.Luau;
58+
const method = lua.createStringLiteral(useAtan2 ? "atan2" : "atan");
5359
return lua.createCallExpression(lua.createTableIndexExpression(math, method), params, node);
5460
}
5561

test/unit/builtins/math.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ util.testEachVersion("Math.atan2", () => util.testExpression`Math.atan2(4, 5)`,
9595
[tstl.LuaTarget.Lua51]: builder => builder.tap(expectMathAtan2),
9696
[tstl.LuaTarget.Lua52]: builder => builder.tap(expectMathAtan2),
9797
[tstl.LuaTarget.Lua53]: builder => builder.tap(expectMathAtan),
98-
[tstl.LuaTarget.Lua54]: builder => builder.tap(expectMathAtan2),
99-
[tstl.LuaTarget.Lua55]: builder => builder.tap(expectMathAtan2),
98+
[tstl.LuaTarget.Lua54]: builder => builder.tap(expectMathAtan),
99+
[tstl.LuaTarget.Lua55]: builder => builder.tap(expectMathAtan),
100100
[tstl.LuaTarget.Luau]: builder => builder.tap(expectMathAtan2),
101101
});
102102

0 commit comments

Comments
 (0)