Skip to content

Commit f1c3c61

Browse files
committed
support lua5.4
1 parent 03208cf commit f1c3c61

18 files changed

Lines changed: 97 additions & 40 deletions

File tree

‎CSharp.lua/CoreSystem.Lua/All.lua‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ return function(dir, conf)
2222
load("Core")(conf)
2323
load("Interfaces")
2424
load("Exception")
25+
load("Math")
2526
load("Number")
2627
load("Char")
2728
load("String")
@@ -41,7 +42,6 @@ return function(dir, conf)
4142
load("Collections.LinkedList")
4243
load("Collections.Linq")
4344
load("Convert")
44-
load("Math")
4545
load("Random")
4646
load("Text.StringBuilder")
4747
load("Console")

‎CSharp.lua/CoreSystem.Lua/CoreSystem/Array.lua‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -838,7 +838,7 @@ Array = {
838838
end
839839
end
840840
local i, j = index + 1, index + count
841-
while i <= j do
841+
while i < j do
842842
t[i], t[j] = t[j], t[i]
843843
i = i + 1
844844
j = j - 1

‎CSharp.lua/CoreSystem.Lua/CoreSystem/Convert.lua‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ local function toChar(value)
8787
if value == nil then return 0 end
8888
local typename = type(value)
8989
if typename == "number" then
90-
if value ~= floor(value) or value > 9223372036854775807 or value < -9223372036854775808 then
90+
if value ~= floor(value) or value > 9223372036854775807 or value < (-9223372036854775807 - 1) then
9191
throw(InvalidCastException("InvalidCast_FromTo_Char"))
9292
end
9393
if value < 0 or value > 65535 then
@@ -254,7 +254,7 @@ local function objectToInt64(value)
254254
end
255255

256256
local function toInt64(value, fromBase)
257-
return toNumber(value, -9223372036854775808, 9223372036854775807, ParseInt64, objectToInt64, fromBase)
257+
return toNumber(value, (-9223372036854775807 - 1), 9223372036854775807, ParseInt64, objectToInt64, fromBase)
258258
end
259259

260260
local function objectToUInt64(value)
@@ -341,9 +341,23 @@ local function changeType(value, conversionType)
341341
return ic.ToType(conversionType)
342342
end
343343

344+
local rexp
345+
if math.frexp then
346+
rexp = function (x)
347+
local _, v = math.frexp(x)
348+
return v
349+
end
350+
else
351+
local abs, log = math.abs, math.log
352+
local log2 = log(2)
353+
rexp = function (x)
354+
return floor(log(abs(x)) / log2) + 1
355+
end
356+
end
357+
344358
local function toBits(num, bits)
345359
-- returns a table of bits, most significant first.
346-
bits = bits or math.max(1, select(2, math.frexp(num)))
360+
bits = bits or math.max(1, rexp(num))
347361
local t = {} -- will contain the bits
348362
for b = bits, 1, -1 do
349363
local i = num % 2

‎CSharp.lua/CoreSystem.Lua/CoreSystem/Core.lua‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@ if version < 5.3 then
635635
end
636636

637637
function System.toInt64(v, checked)
638-
if v >= -9223372036854775808 and v <= 9223372036854775807 then
638+
if v >= (-9223372036854775807 - 1) and v <= 9223372036854775807 then
639639
return v
640640
end
641641
if checked then
@@ -809,7 +809,7 @@ else
809809
end
810810
811811
function System.toInt64(v, checked)
812-
return toInt(v, -9223372036854775808, 9223372036854775807, 0xffffffffffffffff, 0x7fffffffffffffff, checked)
812+
return toInt(v, (-9223372036854775807 - 1), 9223372036854775807, 0xffffffffffffffff, 0x7fffffffffffffff, checked)
813813
end
814814
815815
function System.toUInt64(v, checked)
@@ -891,13 +891,13 @@ end
891891

892892
function System.ToInt64(v, checked)
893893
v = trunc(v)
894-
if v >= -9223372036854775808 and v <= 9223372036854775807 then
894+
if v >= (-9223372036854775807 - 1) and v <= 9223372036854775807 then
895895
return v
896896
end
897897
if checked then
898898
throw(System.OverflowException(), 1)
899899
end
900-
return -9223372036854775808
900+
return (-9223372036854775807 - 1)
901901
end
902902

903903
function System.ToSingle(v, checked)

‎CSharp.lua/CoreSystem.Lua/CoreSystem/Math.lua‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ local floor = math.floor
2222
local min = math.min
2323
local max = math.max
2424
local abs = math.abs
25+
local log = math.log
2526

2627
local function bigMul(a, b)
2728
return a * b
@@ -99,11 +100,17 @@ local function truncate(d)
99100
return trunc(d) * 1.0
100101
end
101102

103+
local log10 = math.log10
104+
if not log10 then
105+
log10 = function (x) return log(x, 10) end
106+
math.log10 = log10
107+
end
108+
102109
local exp = math.exp
103-
local cosh = math.cosh or function(x) return (exp(x) + exp(-x)) / 2.0 end
104-
local pow = math.pow or function(x, y) return x ^ y end
105-
local sinh = math.sinh or function(x) return (exp(x) - exp(-x)) / 2.0 end
106-
local tanh = math.tanh or function(x) return sinh(x) / cosh(x) end
110+
local cosh = math.cosh or function (x) return (exp(x) + exp(-x)) / 2.0 end
111+
local pow = math.pow or function (x, y) return x ^ y end
112+
local sinh = math.sinh or function (x) return (exp(x) - exp(-x)) / 2.0 end
113+
local tanh = math.tanh or function (x) return sinh(x) / cosh(x) end
107114

108115
local Math = math
109116
Math.Abs = abs
@@ -120,8 +127,8 @@ Math.DivRem = divRem
120127
Math.Exp = exp
121128
Math.Floor = math.floor
122129
Math.IEEERemainder = IEEERemainder
123-
Math.Log = math.log
124-
Math.Log10 = math.log10
130+
Math.Log = log
131+
Math.Log10 = log10
125132
Math.Max = math.max
126133
Math.Min = math.min
127134
Math.Pow = pow
@@ -134,4 +141,4 @@ Math.Tan = math.tan
134141
Math.Tanh = tanh
135142
Math.Truncate = truncate
136143

137-
System.define("System.Math", Math)
144+
System.define("System.Math", Math)

‎CSharp.lua/CoreSystem.Lua/CoreSystem/Number.lua‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,10 +218,10 @@ setmetatable(UInt32, Int)
218218

219219
local Int64 = define("System.Int64", {
220220
Parse = function (s)
221-
return parseIntWithException(s, -9223372036854775808, 9223372036854775807)
221+
return parseIntWithException(s, (-9223372036854775807 - 1), 9223372036854775807)
222222
end,
223223
TryParse = function (s)
224-
return tryParseInt(s, -9223372036854775808, 9223372036854775807)
224+
return tryParseInt(s, (-9223372036854775807 - 1), 9223372036854775807)
225225
end
226226
})
227227
setmetatable(Int64, Int)

‎CSharp.lua/CoreSystem.Lua/CoreSystem/String.lua‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ local getmetatable = getmetatable
4444
local setmetatable = setmetatable
4545
local select = select
4646
local type = type
47+
local tonumber = tonumber
4748
local String
4849

4950
local function toString(t, isch)
@@ -220,7 +221,7 @@ local function formatBuild(format, len, select, ...)
220221
end
221222
i, j, s = find(format, "^(%d+)}", i)
222223
if not i then throwFormatError() end
223-
s = s + 1
224+
s = tonumber(s) + 1
224225
if s > len then throwFormatError() end
225226
s = select(s, ...)
226227
s = (s ~= nil and s ~= System.null) and toString(s)

‎CSharp.lua/CoreSystem.Lua/CoreSystem/TimeSpan.lua‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ end
5252

5353
local function negate(this)
5454
local ticks = this.ticks
55-
if ticks == -9223372036854775808 then
55+
if ticks == (-9223372036854775807 - 1) then
5656
throw(OverflowException("Overflow_NegateTwosCompNum"))
5757
end
5858
return TimeSpan(-ticks)
@@ -138,7 +138,9 @@ local function parse(s)
138138
end
139139
end
140140
if sign == '-' then
141-
day, hour, minute, second, milliseconds = -day, -hour, -minute, -second, -milliseconds
141+
day, hour, minute, second, milliseconds = -tonumber(day), -tonumber(hour), -tonumber(minute), -tonumber(second), -tonumber(milliseconds)
142+
else
143+
day, hour, minute, second, milliseconds = tonumber(day), tonumber(hour), tonumber(minute), tonumber(second), tonumber(milliseconds)
142144
end
143145
return TimeSpan(day, hour, minute, second, milliseconds)
144146
end
@@ -223,7 +225,7 @@ TimeSpan = System.defStc("System.TimeSpan", {
223225
Subtract = subtract,
224226
Duration = function (this)
225227
local ticks = this.ticks
226-
if ticks == -9223372036854775808 then
228+
if ticks == (-9223372036854775807 - 1) then
227229
throw(OverflowException("Overflow_Duration"))
228230
end
229231
return TimeSpan(ticks >= 0 and ticks or - ticks)
@@ -297,4 +299,4 @@ TimeSpan = System.defStc("System.TimeSpan", {
297299
zero = TimeSpan(0)
298300
TimeSpan.Zero = zero
299301
TimeSpan.MaxValue = TimeSpan(9223372036854775807)
300-
TimeSpan.MinValue = TimeSpan(-9223372036854775808)
302+
TimeSpan.MinValue = TimeSpan((-9223372036854775807 - 1))

‎CSharp.lua/CoreSystem.Lua/CoreSystem/Type.lua‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ checks[Number] = function (obj, T)
395395
[UInt16] = function (obj) return type(obj) == "number" and obj >= 0 and obj <= 32767 and floor(obj) == obj end,
396396
[Int32] = function (obj) return type(obj) == "number" and obj >= -2147483648 and obj <= 2147483647 and floor(obj) == obj end,
397397
[UInt32] = function (obj) return type(obj) == "number" and obj >= 0 and obj <= 4294967295 and floor(obj) == obj end,
398-
[Int64] = function (obj) return type(obj) == "number" and obj >= -9223372036854775808 and obj <= 9223372036854775807 and floor(obj) == obj end,
398+
[Int64] = function (obj) return type(obj) == "number" and obj >= (-9223372036854775807 - 1) and obj <= 9223372036854775807 and floor(obj) == obj end,
399399
[UInt64] = function (obj) return type(obj) == "number" and obj >= 0 and obj <= 18446744073709551615 and floor(obj) == obj end,
400400
[Single] = function (obj) return type(obj) == "number" and obj >= -3.40282347E+38 and obj <= 3.40282347E+38 end,
401401
[Double] = function (obj) return type(obj) == "number" end

‎CSharp.lua/LuaSyntaxNodeTransform.Helper.cs‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -499,10 +499,15 @@ private LuaLiteralExpressionSyntax GetLiteralExpression(object constantValue) {
499499
double v = (double)constantValue;
500500
return (LuaDoubleLiteralExpressionSyntax)v;
501501
}
502-
default: {
503-
return new LuaIdentifierLiteralExpressionSyntax(constantValue.ToString());
502+
case TypeCode.Int64: {
503+
if (constantValue is long.MinValue) {
504+
const long kMinInteger = long.MinValue + 1; // in lua5.4 long.MinValue will be float
505+
return new LuaIdentifierLiteralExpressionSyntax($"({kMinInteger} - 1)");
506+
}
507+
break;
504508
}
505509
}
510+
return new LuaIdentifierLiteralExpressionSyntax(constantValue.ToString());
506511
}
507512

508513
return LuaIdentifierLiteralExpressionSyntax.Nil;

0 commit comments

Comments
 (0)