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
12 changes: 11 additions & 1 deletion src/transformation/visitors/literal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ export function createShorthandIdentifier(
return identifier;
}

const transformNumericLiteralExpression: FunctionVisitor<ts.NumericLiteral> = 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<ts.ObjectLiteralExpression> = (expression, context) => {
let properties: lua.TableFieldExpression[] = [];
const tableExpressions: lua.Expression[] = [];
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions test/unit/builtins/numbers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});