Skip to content

Commit 7abca43

Browse files
authored
Use power operator instead of math.pow (#1210)
* Use power operator instead of math.pow in lua 5.3 and later * Always use ^ instead of math.pow
1 parent 61bfff3 commit 7abca43

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

src/transformation/builtins/math.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ export function transformMathCall(
7070
return lua.createCallExpression(lua.createTableIndexExpression(math, log), [add], node);
7171
}
7272

73+
case "pow": {
74+
// Translate to base ^ power
75+
return lua.createBinaryExpression(params[0], params[1], lua.SyntaxKind.PowerOperator, node);
76+
}
77+
7378
// math.floor(x + 0.5)
7479
case "round": {
7580
const floor = lua.createStringLiteral("floor");
@@ -93,7 +98,6 @@ export function transformMathCall(
9398
case "log":
9499
case "max":
95100
case "min":
96-
case "pow":
97101
case "random":
98102
case "sin":
99103
case "sqrt":

test/unit/builtins/math.spec.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,14 @@ util.testEachVersion("Math.atan2", () => util.testExpression`Math.atan2(4, 5)`,
8484
[tstl.LuaTarget.Lua54]: builder => builder.tap(expectMathAtan2),
8585
});
8686

87-
util.testEachVersion("Math.atan2(4, 5)", () => util.testExpression`Math.atan2(4, 5)`, {
88-
[tstl.LuaTarget.Universal]: builder => builder.expectToMatchJsResult(),
89-
[tstl.LuaTarget.LuaJIT]: false,
90-
[tstl.LuaTarget.Lua51]: builder => builder.expectToMatchJsResult(),
91-
[tstl.LuaTarget.Lua52]: builder => builder.expectToMatchJsResult(),
92-
[tstl.LuaTarget.Lua53]: builder => builder.expectToMatchJsResult(),
93-
[tstl.LuaTarget.Lua54]: builder => builder.expectToMatchJsResult(),
94-
});
87+
util.testEachVersion(
88+
"Math.atan2(4, 5)",
89+
() => util.testExpression`Math.atan2(4, 5)`,
90+
util.expectEachVersionExceptJit(builder => builder.expectToMatchJsResult())
91+
);
92+
93+
util.testEachVersion(
94+
"Math.pow(3, 5)",
95+
() => util.testExpression`Math.pow(3, 5)`,
96+
util.expectEachVersionExceptJit(builder => builder.expectToMatchJsResult())
97+
);

0 commit comments

Comments
 (0)