Skip to content

Commit 9935e8d

Browse files
committed
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).
1 parent 57459c6 commit 9935e8d

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

src/transformation/builtins/math.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,18 @@ 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 useAtan =
53+
context.luaTarget === LuaTarget.Lua53 ||
54+
context.luaTarget === LuaTarget.Lua54 ||
55+
context.luaTarget === LuaTarget.Lua55;
56+
const method = lua.createStringLiteral(useAtan ? "atan" : "atan2");
5357
return lua.createCallExpression(lua.createTableIndexExpression(math, method), params, node);
5458
}
5559

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)