@@ -444,6 +444,9 @@ if prevSystem then
444444end
445445global .System = System
446446
447+ local debugsetmetatable = debug and debug.setmetatable
448+ System .debugsetmetatable = debugsetmetatable
449+
447450local _ , _ , version = sfind (_VERSION , " ^Lua (.*)$" )
448451version = tonumber (version )
449452System .luaVersion = version
@@ -521,9 +524,7 @@ if version < 5.3 then
521524 end
522525
523526 function System .divOfNull (x , y )
524- if x == nil or y == nil then
525- return nil
526- end
527+ if x == nil or y == nil then return nil end
527528 if y == 0 then throw (System .DivideByZeroException (), 1 ) end
528529 return trunc (x / y )
529530 end
@@ -537,6 +538,18 @@ if version < 5.3 then
537538 return v
538539 end
539540
541+ if debugsetmetatable then
542+ function System .modOfNull (x , y )
543+ if x == nil or y == nil then return nil end
544+ if y == 0 then throw (System .DivideByZeroException (), 1 ) end
545+ local v = x % y
546+ if v ~= 0 and x * y < 0 then
547+ return v - y
548+ end
549+ return v
550+ end
551+ end
552+
540553 function System .toUInt (v , max , mask , checked )
541554 if v >= 0 and v <= max then
542555 return v
719732 local System = System
720733 local throw = System.throw
721734 local trunc = System.trunc
735+ local debugsetmetatable = System.debugsetmetatable
722736
723737 function System.bnot(x) return ~x end
724738 function System.band(x, y) return x & y end
@@ -727,15 +741,36 @@ else
727741 function System.sl(x, y) return x << y end
728742 function System.sr(x, y) return x >> y end
729743 function System.div(x, y) if x ~ y < 0 then return -(-x // y) end return x // y end
730-
744+
731745 function System.mod(x, y)
732746 local v = x % y
733747 if v ~= 0 and 1.0 * x * y < 0 then
734748 return v - y
735749 end
736750 return v
737751 end
738-
752+
753+ if not debugsetmetatable then
754+ function System.bnotOfNull(x) if x == nil then return nil end return ~x end
755+ function System.bandOfNull(x, y) if x == nil or y == nil then return nil end return x & y end
756+ function System.borOfNull(x, y) if x == nil or y == nil then return nil end return x | y end
757+ function System.xorOfNull(x, y) if x == nil or y == nil then return nil end return x ~ y end
758+ function System.slOfNull(x, y) if x == nil or y == nil then return nil end return x << y end
759+ function System.srOfNull(x, y) if x == nil or y == nil then return nil end return x >> y end
760+ function System.divOfNull(x, y) if x == nil or y == nil then return nil end if x ~ y < 0 then return -(-x // y) end return x // y end
761+
762+ function System.modOfNull(x, y)
763+ if x == nil or y == nil then
764+ return nil
765+ end
766+ local v = x % y
767+ if v ~= 0 and 1.0 * x * y < 0 then
768+ return v - y
769+ end
770+ return v
771+ end
772+ end
773+
739774 local function toUInt (v, max, mask, checked)
740775 if v >= 0 and v <= max then
741776 return v
@@ -1013,9 +1048,6 @@ function System.base(this)
10131048 return getmetatable (getmetatable (this ))
10141049end
10151050
1016- local debugsetmetatable = debug and debug.setmetatable
1017- System .debugsetmetatable = debugsetmetatable
1018-
10191051local equalsObj , compareObj
10201052if debugsetmetatable then
10211053 equalsObj = function (x , y )
@@ -1151,14 +1183,28 @@ else
11511183 if obj == nil then return " " end
11521184 local t = type (obj )
11531185 if t == " table" then
1154- return t :ToString ()
1186+ return obj :ToString ()
11551187 elseif t == " boolean" then
1156- return t and " True" or " False"
1188+ return obj and " True" or " False"
11571189 elseif t == " function" then
11581190 return " System.Delegate"
11591191 end
11601192 return tostring (obj )
11611193 end
1194+
1195+ function System .addOfNull (x , y )
1196+ if x == nil or y == nil then
1197+ return nil
1198+ end
1199+ return x + y
1200+ end
1201+
1202+ function System .subOfNull (x , y )
1203+ if x == nil or y == nil then
1204+ return nil
1205+ end
1206+ return x - y
1207+ end
11621208end
11631209
11641210System .equalsObj = equalsObj
@@ -1360,7 +1406,10 @@ local Nullable = {
13601406 return this :GetHashCode ()
13611407 end ,
13621408 clone = function (t )
1363- return t and t :__clone__ ()
1409+ if type (t ) == " table" then
1410+ return t :__clone__ ()
1411+ end
1412+ return t
13641413 end
13651414}
13661415
0 commit comments