diff --git a/src/transformation/visitors/literal.ts b/src/transformation/visitors/literal.ts index 281f70ec6..6149e476f 100644 --- a/src/transformation/visitors/literal.ts +++ b/src/transformation/visitors/literal.ts @@ -60,6 +60,16 @@ export function createShorthandIdentifier( return identifier; } +const transformNumericLiteralExpression: FunctionVisitor = expression => { + if (expression.text === "Infinity") { + const math = lua.createIdentifier("math"); + const huge = lua.createStringLiteral("huge"); + return lua.createTableIndexExpression(math, huge, expression); + } + + return lua.createNumericLiteral(Number(expression.text), expression); +}; + const transformObjectLiteralExpression: FunctionVisitor = (expression, context) => { let properties: lua.TableFieldExpression[] = []; const tableExpressions: lua.Expression[] = []; @@ -142,7 +152,7 @@ export const literalVisitors: Visitors = { [ts.SyntaxKind.NullKeyword]: node => lua.createNilLiteral(node), [ts.SyntaxKind.TrueKeyword]: node => lua.createBooleanLiteral(true, node), [ts.SyntaxKind.FalseKeyword]: node => lua.createBooleanLiteral(false, node), - [ts.SyntaxKind.NumericLiteral]: node => lua.createNumericLiteral(Number(node.text), node), + [ts.SyntaxKind.NumericLiteral]: transformNumericLiteralExpression, [ts.SyntaxKind.StringLiteral]: node => lua.createStringLiteral(node.text, node), [ts.SyntaxKind.NoSubstitutionTemplateLiteral]: node => lua.createStringLiteral(node.text, node), [ts.SyntaxKind.ObjectLiteralExpression]: transformObjectLiteralExpression, diff --git a/test/unit/builtins/numbers.spec.ts b/test/unit/builtins/numbers.spec.ts index ac4303c18..2f52c1d7d 100644 --- a/test/unit/builtins/numbers.spec.ts +++ b/test/unit/builtins/numbers.spec.ts @@ -74,3 +74,7 @@ test("number intersected method", () => { return ({ normalize: () => 3 } as Vector).normalize(); `.expectToMatchJsResult(); }); + +test("numbers overflowing the float limit become math.huge", () => { + util.testExpression`1e309`.expectToMatchJsResult(); +});