Skip to content

Commit 78d7cf7

Browse files
lollekoPerryvw
andauthored
Added Math.sign (#1206)
* Added Math.sign Also improved math tests a bit. * Update test/unit/builtins/math.spec.ts Co-authored-by: Perry van Wesel <Perryvw@users.noreply.github.com> Co-authored-by: Perry van Wesel <Perryvw@users.noreply.github.com>
1 parent 2dd9957 commit 78d7cf7

File tree

5 files changed

+72
-92
lines changed

5 files changed

+72
-92
lines changed

src/LuaLib.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export enum LuaLibFeature {
4444
Iterator = "Iterator",
4545
Map = "Map",
4646
MathAtan2 = "MathAtan2",
47+
MathSign = "MathSign",
4748
New = "New",
4849
Number = "Number",
4950
NumberIsFinite = "NumberIsFinite",

src/lualib/MathSign.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function __TS__MathSign(this: void, val: number) {
2+
if (val > 0) {
3+
return 1;
4+
} else if (val < 0) {
5+
return -1;
6+
}
7+
8+
return 0;
9+
}

src/transformation/builtins/math.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ export function transformMathCall(
7878
return lua.createCallExpression(lua.createTableIndexExpression(math, floor), [add], node);
7979
}
8080

81+
case "sign": {
82+
return transformLuaLibFunction(context, LuaLibFeature.MathSign, node, ...params);
83+
}
84+
8185
case "abs":
8286
case "acos":
8387
case "asin":

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

Lines changed: 0 additions & 81 deletions
This file was deleted.

test/unit/builtins/math.spec.ts

Lines changed: 58 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,75 @@
11
import * as tstl from "../../../src";
22
import * as util from "../../util";
33

4+
// These test are kept to a minimum,
5+
// because we just want to confirm translation is correct
6+
// and not test Lua's built in math functions.
7+
// Differences in math implementations between JS & Lua cause inaccuracies
8+
// therefore test input numbers are "carefully" selected to always match accuratly.
9+
// Lualib implementations are tested separately.
410
test.each([
5-
"Math.cos()",
6-
"Math.sin()",
7-
"Math.min()",
8-
"Math.log2(3)",
9-
"Math.log10(3)",
10-
"const x = Math.log2(3)",
11-
"const x = Math.log10(3)",
12-
"Math.log1p(3)",
13-
"Math.round(3.3)",
11+
// log
12+
"Math.log(42)",
13+
"Math.log10(10)",
14+
"Math.log2(42)",
15+
"Math.log1p(42)",
16+
// round
17+
"Math.round(0.1)",
18+
"Math.round(0.9)",
19+
"Math.round(0.5)",
20+
// abs
21+
"Math.abs(-42)",
22+
"Math.abs(42)",
23+
// trigometric
24+
"Math.acos(0.42)",
25+
"Math.asin(0.42)",
26+
"Math.atan(0.42)",
27+
"Math.cos(42)",
28+
"Math.sin(42)",
29+
"Math.tan(42)",
1430
"Math.PI",
31+
// ceil & floor
32+
"Math.ceil(42.42)",
33+
"Math.floor(42.42)",
34+
// exp
35+
"Math.exp(42)",
36+
// max & min
37+
"Math.max(-42, 42)",
38+
"Math.max(42, -42)",
39+
"Math.max(42, 42)",
40+
"Math.max(-42, -42)",
41+
"Math.min(42, -42)",
42+
"Math.min(-42, 42)",
43+
"Math.min(42, 42)",
44+
"Math.min(-42, -42)",
45+
// pow
46+
"Math.pow(4.2, 4.2)",
47+
"Math.pow(4.2, -4.2)",
48+
"Math.pow(-4.2, -4.2)",
49+
// random
50+
// "Math.random()",
51+
// sqrt
52+
"Math.sqrt(2)",
53+
"Math.sqrt(-2)",
1554
])("%s", code => {
16-
// TODO: Remove?
17-
util.testFunction(code).disableSemanticCheck().expectLuaToMatchSnapshot();
55+
util.testExpression(code).expectToMatchJsResult();
1856
});
1957

58+
// Hard to test properly
59+
util.testExpression("Math.random()").expectNoExecutionError();
60+
2061
test.each(["E", "LN10", "LN2", "LOG10E", "LOG2E", "SQRT1_2", "SQRT2"])("Math.%s", constant => {
2162
util.testExpression`Math.${constant}`.tap(builder => {
2263
expect(builder.getLuaExecutionResult()).toBeCloseTo(builder.getJsExecutionResult());
2364
});
2465
});
2566

67+
// LuaLib MathSign
68+
test.each(["Math.sign(-42)", "Math.sign(42)", "Math.sign(-4.2)", "Math.sign(4.2)", "Math.sign(0)"])("%s", code => {
69+
util.testExpression(code).expectToMatchJsResult();
70+
});
71+
72+
// LuaLib Atan2
2673
const expectMathAtan2: util.TapCallback = builder => expect(builder.getMainLuaCodeChunk()).toContain("math.atan2(");
2774
const expectMathAtan: util.TapCallback = builder => expect(builder.getMainLuaCodeChunk()).toContain("math.atan(");
2875
const expectLualibMathAtan2: util.TapCallback = builder =>

0 commit comments

Comments
 (0)