Skip to content

Commit 5ba1fac

Browse files
authored
Add lualib support for Math.trunc (#1564)
Also includes a small fix that makes `Math.sign` ECMA compliant Closes #1550
1 parent d6f1bb0 commit 5ba1fac

File tree

5 files changed

+39
-5
lines changed

5 files changed

+39
-5
lines changed

src/LuaLib.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ export enum LuaLibFeature {
6363
MathAtan2 = "MathAtan2",
6464
MathModf = "MathModf",
6565
MathSign = "MathSign",
66+
MathTrunc = "MathTrunc",
6667
New = "New",
6768
Number = "Number",
6869
NumberIsFinite = "NumberIsFinite",

src/lualib/MathSign.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1+
// https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-math.sign
2+
3+
import { __TS__NumberIsNaN } from "./NumberIsNaN";
4+
15
export function __TS__MathSign(this: void, val: number) {
2-
if (val > 0) {
3-
return 1;
4-
} else if (val < 0) {
6+
if (__TS__NumberIsNaN(val) || val === 0) {
7+
return val;
8+
}
9+
10+
if (val < 0) {
511
return -1;
612
}
713

8-
return 0;
14+
return 1;
915
}

src/lualib/MathTrunc.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-math.trunc
2+
3+
import { __TS__NumberIsFinite } from "./NumberIsFinite";
4+
export function __TS__MathTrunc(this: void, val: number) {
5+
if (!__TS__NumberIsFinite(val) || val === 0) {
6+
return val;
7+
}
8+
9+
return val > 0 ? math.floor(val) : math.ceil(val);
10+
}

src/transformation/builtins/math.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ export function transformMathCall(
100100
return transformLuaLibFunction(context, LuaLibFeature.MathSign, node, ...params);
101101
}
102102

103+
case "trunc": {
104+
return transformLuaLibFunction(context, LuaLibFeature.MathTrunc, node, ...params);
105+
}
106+
103107
case "abs":
104108
case "acos":
105109
case "asin":

test/unit/builtins/math.spec.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ test.each([
5151
// sqrt
5252
"Math.sqrt(2)",
5353
"Math.sqrt(-2)",
54+
// trunc
55+
"Math.trunc(42.42)",
56+
"Math.trunc(-42.42)",
57+
"Math.trunc(0)",
58+
"Math.trunc(Infinity)",
59+
"Math.trunc(-Infinity)",
5460
])("%s", code => {
5561
util.testExpression(code).expectToMatchJsResult();
5662
});
@@ -65,7 +71,14 @@ test.each(["E", "LN10", "LN2", "LOG10E", "LOG2E", "SQRT1_2", "SQRT2"])("Math.%s"
6571
});
6672

6773
// LuaLib MathSign
68-
test.each(["Math.sign(-42)", "Math.sign(42)", "Math.sign(-4.2)", "Math.sign(4.2)", "Math.sign(0)"])("%s", code => {
74+
test.each([
75+
"Math.sign(-42)",
76+
"Math.sign(42)",
77+
"Math.sign(-4.2)",
78+
"Math.sign(4.2)",
79+
"Math.sign(0)",
80+
"Math.sign(-Infinity)",
81+
])("%s", code => {
6982
util.testExpression(code).expectToMatchJsResult();
7083
});
7184

0 commit comments

Comments
 (0)