11import ts = require( "typescript" ) ;
22import * as lua from "../../LuaAST" ;
3+ import { createNaN } from "../utils/lua-ast" ;
34import { TransformationContext } from "../context" ;
45import { unsupportedProperty } from "../utils/diagnostics" ;
56import { LuaLibFeature , transformLuaLibFunction } from "../utils/lualib" ;
67import { transformArguments } from "../visitors/call" ;
8+ import { LuaTarget } from "../../CompilerOptions" ;
79
810export function transformNumberPrototypeCall (
911 context : TransformationContext ,
@@ -27,6 +29,87 @@ export function transformNumberPrototypeCall(
2729 }
2830}
2931
32+ export function transformNumberProperty (
33+ context : TransformationContext ,
34+ node : ts . PropertyAccessExpression
35+ ) : lua . Expression | undefined {
36+ const name = node . name . text ;
37+
38+ /*
39+ Read the docs on https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number for further info about what these numbers entail.
40+ Most of them should be fairly straight forward base on their name(s) though.
41+ */
42+
43+ switch ( name ) {
44+ case "POSITIVE_INFINITY" :
45+ if ( context . luaTarget === LuaTarget . Lua50 ) {
46+ const one = lua . createNumericLiteral ( 1 ) ;
47+ const zero = lua . createNumericLiteral ( 0 ) ;
48+ return lua . createBinaryExpression ( one , zero , lua . SyntaxKind . DivisionOperator ) ;
49+ } else {
50+ const math = lua . createIdentifier ( "math" ) ;
51+ const huge = lua . createStringLiteral ( "huge" ) ;
52+ return lua . createTableIndexExpression ( math , huge , node ) ;
53+ }
54+ case "NEGATIVE_INFINITY" :
55+ if ( context . luaTarget === LuaTarget . Lua50 ) {
56+ const one = lua . createNumericLiteral ( 1 ) ;
57+ const zero = lua . createNumericLiteral ( 0 ) ;
58+ return lua . createUnaryExpression (
59+ lua . createBinaryExpression ( one , zero , lua . SyntaxKind . DivisionOperator ) ,
60+ lua . SyntaxKind . NegationOperator
61+ ) ;
62+ } else {
63+ const math = lua . createIdentifier ( "math" ) ;
64+ const huge = lua . createStringLiteral ( "huge" ) ;
65+ return lua . createUnaryExpression (
66+ lua . createTableIndexExpression ( math , huge , node ) ,
67+ lua . SyntaxKind . NegationOperator
68+ ) ;
69+ }
70+ case "NaN" :
71+ return createNaN ( node ) ;
72+ case "EPSILON" :
73+ return lua . createBinaryExpression (
74+ lua . createNumericLiteral ( 2 ) ,
75+ lua . createNumericLiteral ( - 52 ) ,
76+ lua . SyntaxKind . PowerOperator ,
77+ node
78+ ) ;
79+ case "MIN_VALUE" :
80+ return lua . createBinaryExpression (
81+ lua . createNumericLiteral ( - 2 ) ,
82+ lua . createNumericLiteral ( 1074 ) ,
83+ lua . SyntaxKind . PowerOperator ,
84+ node
85+ ) ;
86+ case "MIN_SAFE_INTEGER" :
87+ return lua . createBinaryExpression (
88+ lua . createNumericLiteral ( - 2 ) ,
89+ lua . createNumericLiteral ( 1074 ) ,
90+ lua . SyntaxKind . PowerOperator ,
91+ node
92+ ) ;
93+ case "MAX_SAFE_INTEGER" :
94+ return lua . createBinaryExpression (
95+ lua . createNumericLiteral ( 2 ) ,
96+ lua . createNumericLiteral ( 1024 ) ,
97+ lua . SyntaxKind . PowerOperator ,
98+ node
99+ ) ;
100+ case "MAX_VALUE" :
101+ return lua . createBinaryExpression (
102+ lua . createNumericLiteral ( 2 ) ,
103+ lua . createNumericLiteral ( 1024 ) ,
104+ lua . SyntaxKind . PowerOperator ,
105+ node
106+ ) ;
107+
108+ default :
109+ context . diagnostics . push ( unsupportedProperty ( node . name , "Number" , name ) ) ;
110+ }
111+ }
112+
30113export function transformNumberConstructorCall (
31114 context : TransformationContext ,
32115 node : ts . CallExpression ,
0 commit comments