Skip to content

Commit bb02542

Browse files
authored
Print numbers overflowing the float limit as math.huge (#828)
1 parent f98a33e commit bb02542

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/transformation/visitors/literal.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,16 @@ export function createShorthandIdentifier(
6060
return identifier;
6161
}
6262

63+
const transformNumericLiteralExpression: FunctionVisitor<ts.NumericLiteral> = expression => {
64+
if (expression.text === "Infinity") {
65+
const math = lua.createIdentifier("math");
66+
const huge = lua.createStringLiteral("huge");
67+
return lua.createTableIndexExpression(math, huge, expression);
68+
}
69+
70+
return lua.createNumericLiteral(Number(expression.text), expression);
71+
};
72+
6373
const transformObjectLiteralExpression: FunctionVisitor<ts.ObjectLiteralExpression> = (expression, context) => {
6474
let properties: lua.TableFieldExpression[] = [];
6575
const tableExpressions: lua.Expression[] = [];
@@ -142,7 +152,7 @@ export const literalVisitors: Visitors = {
142152
[ts.SyntaxKind.NullKeyword]: node => lua.createNilLiteral(node),
143153
[ts.SyntaxKind.TrueKeyword]: node => lua.createBooleanLiteral(true, node),
144154
[ts.SyntaxKind.FalseKeyword]: node => lua.createBooleanLiteral(false, node),
145-
[ts.SyntaxKind.NumericLiteral]: node => lua.createNumericLiteral(Number(node.text), node),
155+
[ts.SyntaxKind.NumericLiteral]: transformNumericLiteralExpression,
146156
[ts.SyntaxKind.StringLiteral]: node => lua.createStringLiteral(node.text, node),
147157
[ts.SyntaxKind.NoSubstitutionTemplateLiteral]: node => lua.createStringLiteral(node.text, node),
148158
[ts.SyntaxKind.ObjectLiteralExpression]: transformObjectLiteralExpression,

test/unit/builtins/numbers.spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,7 @@ test("number intersected method", () => {
7474
return ({ normalize: () => 3 } as Vector).normalize();
7575
`.expectToMatchJsResult();
7676
});
77+
78+
test("numbers overflowing the float limit become math.huge", () => {
79+
util.testExpression`1e309`.expectToMatchJsResult();
80+
});

0 commit comments

Comments
 (0)