Skip to content

Commit 12615fc

Browse files
committed
support yanghuan#237
1 parent 36cca7d commit 12615fc

13 files changed

Lines changed: 239 additions & 164 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ local function each(t)
122122
end
123123

124124
function System.isArrayLike(t)
125-
return t.GetEnumerator == arrayEnumerator
125+
return type(t) == "table" and t.GetEnumerator == arrayEnumerator
126126
end
127127

128128
function System.isEnumerableLike(t)

‎CSharp.lua/CoreSystem.Lua/CoreSystem/Boolean.lua‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ limitations under the License.
1616

1717
local System = System
1818
local throw = System.throw
19+
local debugsetmetatable = System.debugsetmetatable
1920
local ArgumentException = System.ArgumentException
2021
local ArgumentNullException = System.ArgumentNullException
2122
local FormatException = System.FormatException
@@ -106,7 +107,9 @@ local Boolean = System.defStc("System.Boolean", {
106107
return { System.IComparable, System.IConvertible, System.IComparable_1(T), System.IEquatable_1(T) }
107108
end
108109
})
109-
System.debugsetmetatable(false, Boolean)
110+
if debugsetmetatable then
111+
debugsetmetatable(false, Boolean)
112+
end
110113

111114
local ValueType = System.ValueType
112115
local boolMetaTable = setmetatable({ __index = ValueType, __call = Boolean.default }, ValueType)

‎CSharp.lua/CoreSystem.Lua/CoreSystem/Console.lua‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ limitations under the License.
1515
--]]
1616

1717
local System = System
18+
local toString = System.toString
1819

1920
local io = io
2021
local stdin = io.stdin
@@ -29,13 +30,9 @@ local Format = string.Format
2930

3031
local function getWriteValue(v, ...)
3132
if select("#", ...) ~= 0 then
32-
v = Format(v, ...)
33-
elseif v ~= nil then
34-
v = v:ToString()
35-
else
36-
v = ""
33+
return Format(v, ...)
3734
end
38-
return v
35+
return toString(v)
3936
end
4037

4138
System.define("System.Console", {

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

Lines changed: 53 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1290,50 +1290,64 @@ setmetatable(Index, {
12901290
end
12911291
})
12921292

1293-
local debugsetmetatable = (debug and debug.setmetatable) or emptyFn
1293+
local debugsetmetatable = debug and debug.setmetatable
12941294
System.debugsetmetatable = debugsetmetatable
12951295

1296-
debugsetmetatable(nil, {
1297-
__concat = function(a, b)
1298-
if a == nil then
1299-
if b == nil then
1300-
return ""
1296+
if debugsetmetatable then
1297+
debugsetmetatable(nil, {
1298+
__concat = function(a, b)
1299+
if a == nil then
1300+
if b == nil then
1301+
return ""
1302+
else
1303+
return b
1304+
end
13011305
else
1302-
return b
1306+
return a
13031307
end
1304-
else
1305-
return a
1306-
end
1307-
end,
1308-
__add = function (a, b)
1309-
if a == nil then
1310-
if b == nil or type(b) == "number" then
1311-
return nil
1308+
end,
1309+
__add = function (a, b)
1310+
if a == nil then
1311+
if b == nil or type(b) == "number" then
1312+
return nil
1313+
end
1314+
return b
13121315
end
1313-
return b
1314-
end
1315-
return nil
1316-
end,
1317-
__sub = nilFn,
1318-
__mul = nilFn,
1319-
__div = nilFn,
1320-
__mod = nilFn,
1321-
__unm = nilFn,
1322-
__lt = falseFn,
1323-
__le = falseFn,
1324-
1325-
-- lua 5.3
1326-
__idiv = nilFn,
1327-
__band = nilFn,
1328-
__bor = nilFn,
1329-
__bxor = nilFn,
1330-
__bnot = nilFn,
1331-
__shl = nilFn,
1332-
__shr = nilFn,
1333-
})
1334-
1335-
function System.toString(t)
1336-
return t ~= nil and t:ToString() or ""
1316+
return nil
1317+
end,
1318+
__sub = nilFn,
1319+
__mul = nilFn,
1320+
__div = nilFn,
1321+
__mod = nilFn,
1322+
__unm = nilFn,
1323+
__lt = falseFn,
1324+
__le = falseFn,
1325+
1326+
-- lua 5.3
1327+
__idiv = nilFn,
1328+
__band = nilFn,
1329+
__bor = nilFn,
1330+
__bxor = nilFn,
1331+
__bnot = nilFn,
1332+
__shl = nilFn,
1333+
__shr = nilFn,
1334+
})
1335+
function System.toString(t)
1336+
return t ~= nil and t:ToString() or ""
1337+
end
1338+
else
1339+
function System.toString(obj)
1340+
if obj == nil then return "" end
1341+
local t = type(obj)
1342+
if t == "table" then
1343+
return t:ToString()
1344+
elseif t == "boolean" then
1345+
return t and "True" or "False"
1346+
elseif t == "function" then
1347+
return "System.Delegate"
1348+
end
1349+
return tostring(obj)
1350+
end
13371351
end
13381352

13391353
local function pointerAddress(p)

‎CSharp.lua/CoreSystem.Lua/CoreSystem/Delegate.lua‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
local System = System
1818
local throw = System.throw
1919
local Object = System.Object
20+
local debugsetmetatable = System.debugsetmetatable
2021
local ArgumentNullException = System.ArgumentNullException
2122

2223
local setmetatable = setmetatable
@@ -177,7 +178,9 @@ Delegate = System.define("System.Delegate", {
177178

178179
local delegateMetaTable = setmetatable({ __index = Object, __call = makeGenericTypes }, Object)
179180
setmetatable(Delegate, delegateMetaTable)
180-
System.debugsetmetatable(System.emptyFn, Delegate)
181+
if debugsetmetatable then
182+
debugsetmetatable(System.emptyFn, Delegate)
183+
end
181184

182185
multicast = setmetatable({
183186
__index = Delegate,

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

Lines changed: 78 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ local define = System.defStc
2020
local equals = System.equals
2121
local zeroFn = System.zeroFn
2222
local identityFn = System.identityFn
23+
local debugsetmetatable = System.debugsetmetatable
2324

2425
local IComparable = System.IComparable
2526
local IComparable_1 = System.IComparable_1
@@ -31,14 +32,61 @@ local ArgumentException = System.ArgumentException
3132
local ArgumentNullException = System.ArgumentNullException
3233
local FormatException = System.FormatException
3334
local OverflowException = System.OverflowException
34-
local NullReferenceException = System.NullReferenceException
3535

3636
local type = type
3737
local tonumber = tonumber
3838
local floor = math.floor
3939
local setmetatable = setmetatable
4040
local tostring = tostring
4141

42+
local function hexForamt(x, n)
43+
return n == "" and "%" .. x or "%0" .. n .. x
44+
end
45+
46+
local function floatForamt(x, n)
47+
return n == "" and "%.f" or "%." .. n .. 'f'
48+
end
49+
50+
local function integerFormat(x, n)
51+
return n == "" and "%d" or "%0" .. n .. 'd'
52+
end
53+
54+
local function exponentialFormat(x, n)
55+
return n == "" and "%" .. x or "%." .. n .. x
56+
end
57+
58+
local formats = {
59+
['x'] = hexForamt,
60+
['X'] = hexForamt,
61+
['f'] = floatForamt,
62+
['F'] = floatForamt,
63+
['d'] = integerFormat,
64+
['D'] = integerFormat,
65+
['e'] = exponentialFormat,
66+
['E'] = exponentialFormat
67+
}
68+
69+
local function toStringWithFormat(this, format)
70+
if #format ~= 0 then
71+
local i, j, x, n = format:find("^%s*([xXdDfFeE])(%d?)%s*$")
72+
if i then
73+
local f = formats[x]
74+
if f then
75+
format = f(x, n)
76+
end
77+
return format:format(this)
78+
end
79+
end
80+
return tostring(this)
81+
end
82+
83+
local function toString(this, format)
84+
if format then
85+
return toStringWithFormat(this, format)
86+
end
87+
return tostring(this)
88+
end
89+
4290
local function compareInt(this, v)
4391
if this < v then return -1 end
4492
if this > v then return 1 end
@@ -54,6 +102,7 @@ local Int = define("System.Int", {
54102
default = zeroFn,
55103
CompareTo = compareInt,
56104
Equals = equals,
105+
ToString = toString,
57106
GetHashCode = identityFn,
58107
CompareToObj = function (this, v)
59108
if v == nil then return 1 end
@@ -213,54 +262,6 @@ local function equalsDouble(this, v)
213262
return isNaN(this) and isNaN(v)
214263
end
215264

216-
local function hexForamt(x, n)
217-
return n == "" and "%" .. x or "%0" .. n .. x
218-
end
219-
220-
local function floatForamt(x, n)
221-
return n == "" and "%.f" or "%." .. n .. 'f'
222-
end
223-
224-
local function integerFormat(x, n)
225-
return n == "" and "%d" or "%0" .. n .. 'd'
226-
end
227-
228-
local function exponentialFormat(x, n)
229-
return n == "" and "%" .. x or "%." .. n .. x
230-
end
231-
232-
local formats = {
233-
['x'] = hexForamt,
234-
['X'] = hexForamt,
235-
['f'] = floatForamt,
236-
['F'] = floatForamt,
237-
['d'] = integerFormat,
238-
['D'] = integerFormat,
239-
['e'] = exponentialFormat,
240-
['E'] = exponentialFormat
241-
}
242-
243-
local function toStringWithFormat(this, format)
244-
if #format ~= 0 then
245-
local i, j, x, n = format:find("^%s*([xXdDfFeE])(%d?)%s*$")
246-
if i then
247-
local f = formats[x]
248-
if f then
249-
format = f(x, n)
250-
end
251-
return format:format(this)
252-
end
253-
end
254-
return tostring(this)
255-
end
256-
257-
local function toString(this, format)
258-
if format then
259-
return toStringWithFormat(this, format)
260-
end
261-
return tostring(this)
262-
end
263-
264265
local function equalsObj(this, v)
265266
if type(v) ~= "number" then
266267
return false
@@ -305,7 +306,9 @@ local Number = define("System.Number", {
305306
end
306307
})
307308
Number.__call = zeroFn
308-
System.debugsetmetatable(0, Number)
309+
if debugsetmetatable then
310+
debugsetmetatable(0, Number)
311+
end
309312

310313
local function parseDouble(s)
311314
if s == nil then
@@ -360,42 +363,35 @@ local Double = define("System.Double", {
360363
})
361364
setmetatable(Double, Number)
362365

363-
function System.concat(obj)
364-
if obj == nil then
365-
return ""
366-
end
367-
local t = type(obj)
368-
if t == "table" then
369-
return t:ToString()
370-
elseif t == "boolean" then
371-
return t and "True" or "False"
372-
elseif t == "function" then
373-
return "System.Delegate"
366+
if not debugsetmetatable then
367+
local NullReferenceException = System.NullReferenceException
368+
local toString = System.toString
369+
370+
function System.ObjectEquals(this, obj)
371+
if this == nil then throw(NullReferenceException()) end
372+
local t = type(this)
373+
if t == "number" then
374+
return equalsObj(this, obj)
375+
elseif t == "table" then
376+
return this:EqualsObj(obj)
377+
end
378+
return this == obj
374379
end
375-
return tostring(obj)
376-
end
377380

378-
function System.ObjectEquals(this, obj)
379-
if this == nil then throw(NullReferenceException()) end
380-
local t = type(this)
381-
if t == "number" then
382-
return equalsObj(this, obj)
383-
elseif t == "table" then
384-
return this:EqualsObj(obj)
381+
function System.ObjectGetHashCode(this)
382+
if this == nil then throw(NullReferenceException()) end
383+
local t = type(this)
384+
if t == "number" then
385+
return getHashCode(this)
386+
elseif t == "table" then
387+
return this:GetHashCode()
388+
end
389+
return this
385390
end
386-
return this == obj
387-
end
388391

389-
function System.ObjectGetHashCode(this)
390-
if this == nil then throw(NullReferenceException()) end
391-
local t = type(this)
392-
if t == "number" then
393-
return getHashCode(this)
394-
elseif t == "table" then
395-
return this:GetHashCode()
392+
function System.ObjectToString(this)
393+
if this == nil then throw(NullReferenceException()) end
394+
return toString(this)
396395
end
397-
return this
398396
end
399397

400-
401-

0 commit comments

Comments
 (0)