From 129623e5fa91cc6b9a7221ac167a932601a7f2c7 Mon Sep 17 00:00:00 2001 From: hazzard993 Date: Mon, 24 Feb 2020 15:49:48 +1000 Subject: [PATCH] Print numbers overflowing the float limit as math.huge --- src/transformation/visitors/literal.ts | 12 +++++++++++- test/unit/builtins/numbers.spec.ts | 4 ++++ 2 files changed, 15 insertions(+), 1 deletion(-) 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(); +});