Skip to content

Commit 10ff5f7

Browse files
hazzard993ark120202
authored andcommitted
Use lua's math.atan2 for Math.atan2 (#765)
* use lua's math.atan2 for Math.atan2 * use math.atan for Lua 5.3 * use snapshots for tests * use tap and testEachVersion for math.atan testing * Move Math.atan2 transformation down to switch case * Move math atan2 transformation back to top of switch * Remove unneeded whitespace change * Improve math.atan2 comment Co-Authored-By: Perry van Wesel <Perryvw@users.noreply.github.com> * Use methodName for Math method transformation * Add Math.atan2 Lua5.3 comparison test, fix math.atan taps * Update test/unit/builtins/math.spec.ts Co-Authored-By: ark120202 <ark120202@gmail.com> * Update test/unit/builtins/math.spec.ts Co-Authored-By: ark120202 <ark120202@gmail.com>
1 parent c1485af commit 10ff5f7

File tree

3 files changed

+21
-13
lines changed

3 files changed

+21
-13
lines changed

src/transformation/builtins/math.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as ts from "typescript";
22
import * as lua from "../../LuaAST";
3+
import { LuaTarget } from "../../CompilerOptions";
34
import { TransformationContext } from "../context";
45
import { UnsupportedProperty } from "../utils/errors";
56
import { PropertyCallExpression, transformArguments } from "../visitors/call";
@@ -33,12 +34,13 @@ export function transformMathCall(context: TransformationContext, node: Property
3334

3435
const expressionName = expression.name.text;
3536
switch (expressionName) {
36-
// math.tan(x / y)
37+
// Lua 5.3: math.atan(y, x)
38+
// Otherwise: math.atan2(y, x)
3739
case "atan2": {
3840
const math = lua.createIdentifier("math");
39-
const atan = lua.createStringLiteral("atan");
40-
const div = lua.createBinaryExpression(params[0], params[1], lua.SyntaxKind.DivisionOperator);
41-
return lua.createCallExpression(lua.createTableIndexExpression(math, atan), [div], node);
41+
const methodName = context.options.luaTarget === LuaTarget.Lua53 ? "atan" : expressionName;
42+
const method = lua.createStringLiteral(methodName);
43+
return lua.createCallExpression(lua.createTableIndexExpression(math, method), params, node);
4244
}
4345

4446
// (math.log(x) / Math.LNe)

test/unit/builtins/__snapshots__/math.spec.ts.snap

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,6 @@ end
88
return ____exports"
99
`;
1010

11-
exports[`Math.atan2(2, 3) 1`] = `
12-
"local ____exports = {}
13-
function ____exports.__main(self)
14-
math.atan(2 / 3)
15-
end
16-
return ____exports"
17-
`;
18-
1911
exports[`Math.cos() 1`] = `
2012
"local ____exports = {}
2113
function ____exports.__main(self)

test/unit/builtins/math.spec.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
import * as tstl from "../../../src";
12
import * as util from "../../util";
23

34
test.each([
45
"Math.cos()",
56
"Math.sin()",
67
"Math.min()",
7-
"Math.atan2(2, 3)",
88
"Math.log2(3)",
99
"Math.log10(3)",
1010
"const x = Math.log2(3)",
@@ -24,3 +24,17 @@ test.each(["E", "LN10", "LN2", "LOG10E", "LOG2E", "SQRT1_2", "SQRT2"])("Math.%s"
2424
expect(builder.getLuaExecutionResult()).toBeCloseTo(builder.getJsExecutionResult());
2525
});
2626
});
27+
28+
const expectMathAtan2: util.TapCallback = builder => expect(builder.getMainLuaCodeChunk()).toContain("math.atan2(");
29+
const expectMathAtan: util.TapCallback = builder => expect(builder.getMainLuaCodeChunk()).toContain("math.atan(");
30+
31+
util.testEachVersion("Math.atan2", () => util.testExpression`Math.atan2(4, 5)`, {
32+
[tstl.LuaTarget.LuaJIT]: builder => builder.tap(expectMathAtan2),
33+
[tstl.LuaTarget.Lua51]: builder => builder.tap(expectMathAtan2),
34+
[tstl.LuaTarget.Lua52]: builder => builder.tap(expectMathAtan2),
35+
[tstl.LuaTarget.Lua53]: builder => builder.tap(expectMathAtan),
36+
});
37+
38+
test("Math.atan2(4, 5)", () => {
39+
util.testExpression`Math.atan2(4, 5)`.expectToMatchJsResult();
40+
});

0 commit comments

Comments
 (0)