Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/LuaLib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export enum LuaLibFeature {
Iterator = "Iterator",
Map = "Map",
MathAtan2 = "MathAtan2",
MathSign = "MathSign",
New = "New",
Number = "Number",
NumberIsFinite = "NumberIsFinite",
Expand Down
9 changes: 9 additions & 0 deletions src/lualib/MathSign.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function __TS__MathSign(this: void, val: number) {
if (val > 0) {
return 1;
} else if (val < 0) {
return -1;
}

return 0;
}
4 changes: 4 additions & 0 deletions src/transformation/builtins/math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ export function transformMathCall(
return lua.createCallExpression(lua.createTableIndexExpression(math, floor), [add], node);
}

case "sign": {
return transformLuaLibFunction(context, LuaLibFeature.MathSign, node, ...params);
}

case "abs":
case "acos":
case "asin":
Expand Down
81 changes: 0 additions & 81 deletions test/unit/builtins/__snapshots__/math.spec.ts.snap

This file was deleted.

69 changes: 58 additions & 11 deletions test/unit/builtins/math.spec.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,75 @@
import * as tstl from "../../../src";
import * as util from "../../util";

// These test are kept to a minimum,
// because we just want to confirm translation is correct
// and not test Lua's built in math functions.
// Differences in math implementations between JS & Lua cause inaccuracies
// therefore test input numbers are "carefully" selected to always match accuratly.
// Lualib implementations are tested separately.
test.each([
"Math.cos()",
"Math.sin()",
"Math.min()",
"Math.log2(3)",
"Math.log10(3)",
"const x = Math.log2(3)",
"const x = Math.log10(3)",
"Math.log1p(3)",
"Math.round(3.3)",
// log
"Math.log(42)",
"Math.log10(10)",
"Math.log2(42)",
"Math.log1p(42)",
// round
"Math.round(0.1)",
"Math.round(0.9)",
"Math.round(0.5)",
// abs
"Math.abs(-42)",
"Math.abs(42)",
// trigometric
"Math.acos(0.42)",
"Math.asin(0.42)",
"Math.atan(0.42)",
"Math.cos(42)",
"Math.sin(42)",
"Math.tan(42)",
"Math.PI",
// ceil & floor
"Math.ceil(42.42)",
"Math.floor(42.42)",
// exp
"Math.exp(42)",
// max & min
"Math.max(-42, 42)",
"Math.max(42, -42)",
"Math.max(42, 42)",
"Math.max(-42, -42)",
"Math.min(42, -42)",
"Math.min(-42, 42)",
"Math.min(42, 42)",
"Math.min(-42, -42)",
// pow
"Math.pow(4.2, 4.2)",
"Math.pow(4.2, -4.2)",
"Math.pow(-4.2, -4.2)",
// random
// "Math.random()",
// sqrt
"Math.sqrt(2)",
"Math.sqrt(-2)",
])("%s", code => {
// TODO: Remove?
util.testFunction(code).disableSemanticCheck().expectLuaToMatchSnapshot();
util.testExpression(code).expectToMatchJsResult();
});

// Hard to test properly
util.testExpression("Math.random()").expectNoExecutionError();

test.each(["E", "LN10", "LN2", "LOG10E", "LOG2E", "SQRT1_2", "SQRT2"])("Math.%s", constant => {
util.testExpression`Math.${constant}`.tap(builder => {
expect(builder.getLuaExecutionResult()).toBeCloseTo(builder.getJsExecutionResult());
});
});

// LuaLib MathSign
test.each(["Math.sign(-42)", "Math.sign(42)", "Math.sign(-4.2)", "Math.sign(4.2)", "Math.sign(0)"])("%s", code => {
util.testExpression(code).expectToMatchJsResult();
});

// LuaLib Atan2
const expectMathAtan2: util.TapCallback = builder => expect(builder.getMainLuaCodeChunk()).toContain("math.atan2(");
const expectMathAtan: util.TapCallback = builder => expect(builder.getMainLuaCodeChunk()).toContain("math.atan(");
const expectLualibMathAtan2: util.TapCallback = builder =>
Expand Down