forked from yanghuan/CSharp.lua
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathType.lua
More file actions
509 lines (461 loc) · 12.4 KB
/
Type.lua
File metadata and controls
509 lines (461 loc) · 12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
--[[
Copyright 2017 YANG Huan (sy.yanghuan@gmail.com).
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
--]]
local System = System
local throw = System.throw
local Object = System.Object
local Boolean = System.Boolean
local Delegate = System.Delegate
local getClass = System.getClass
local getGenericClass = System.getGenericClass
local arrayFromTable = System.arrayFromTable
local InvalidCastException = System.InvalidCastException
local ArgumentNullException = System.ArgumentNullException
local MissingMethodException = System.MissingMethodException
local TypeLoadException = System.TypeLoadException
local NullReferenceException = System.NullReferenceException
local Char = System.Char
local SByte = System.SByte
local Byte = System.Byte
local Int16 = System.Int16
local UInt16 = System.UInt16
local Int32 = System.Int32
local UInt32 = System.UInt32
local Int64 = System.Int64
local UInt64 = System.UInt64
local Single = System.Single
local Double = System.Double
local Int = System.Int
local Number = System.Number
local ValueType = System.ValueType
local assert = assert
local type = type
local setmetatable = setmetatable
local getmetatable = getmetatable
local select = select
local unpack = table.unpack
local floor = math.floor
local Type, typeof
local function isGenericName(name)
return name:find('`') ~= nil
end
local function getBaseType(this)
local baseType = this.baseType
if baseType == nil then
local baseCls = getmetatable(this[1])
if baseCls ~= nil then
baseType = typeof(baseCls)
this.baseType = baseType
end
end
return baseType
end
local function isSubclassOf(this, c)
local p = this
if p == c then
return false
end
while p ~= nil do
if p == c then
return true
end
p = getmetatable(p)
end
return false
end
local function getIsInterface(this)
return this[1].class == "I"
end
local function fillInterfaces(t, cls, set)
local base = getmetatable(cls)
if base then
fillInterfaces(t, base, set)
end
local interface = cls.interface
if interface then
for i = 1, #interface do
local it = interface[i]
if not set[it] then
t[#t + 1] = typeof(it)
set[it] = true
end
fillInterfaces(t, it, set)
end
end
end
local function getInterfaces(this)
local t = this.interfaces
if t == nil then
t = arrayFromTable({}, Type, true)
fillInterfaces(t, this[1], {})
this.interfaces = t
end
return t
end
local function implementInterface(this, ifaceType)
local t = this
while t ~= nil do
local interfaces = getInterfaces(this)
if interfaces ~= nil then
for i = 1, #interfaces do
local it = interfaces[i]
if it == ifaceType or implementInterface(it, ifaceType) then
return true
end
end
end
t = getBaseType(t)
end
return false
end
local function isAssignableFrom(this, c)
if c == nil then
return false
end
if this == c then
return true
end
local left, right = this[1], c[1]
if left == Object then
return true
end
if isSubclassOf(right, left) then
return true
end
if left.class == "I" then
return implementInterface(c, this)
end
return false
end
local function isGenericTypeDefinition(this)
local cls = this[1]
return getGenericClass(cls) == cls
end
local function getIsArray(this)
return this[1].__name__:byte(-2) == 91
end
Type = System.define("System.Type", {
Equals = System.equals,
getIsGenericType = function (this)
return isGenericName(this[1].__name__)
end,
getContainsGenericParameters = function (this)
return isGenericName(this[1].__name__)
end,
getIsGenericTypeDefinition = isGenericTypeDefinition,
GetGenericTypeDefinition = function (this)
local genericClass = getGenericClass(this[1])
if genericClass then
return typeof(genericClass)
end
throw(System.InvalidOperationException())
end,
MakeGenericType = function (this, ...)
local T, n, args = this[1], select("#", ...)
if n == 0 then
return typeof(T())
end
if n == 1 then
args = ...
if System.isArrayLike(args) then
n = #args
if n == 0 then
return typeof(T())
end
if n == 1 then
return typeof(T(args[1][1]))
end
else
return typeof(T(args[1]))
end
else
args = { ... }
end
for i = 1, n do
args[i] = args[i][1]
end
return typeof(T(unpack(args)))
end,
getIsEnum = function (this)
return this[1].class == "E"
end,
getIsClass = function (this)
return this[1].class == "C"
end,
getIsValueType = function (this)
return this[1].class == "S"
end,
getName = function (this)
local name = this.name
if name == nil then
local clsName = this[1].__name__
local pattern = isGenericName(clsName) and "^.*()%.(.*)%[.+%]$" or "^.*()%.(.*)$"
name = clsName:gsub(pattern, "%2")
this.name = name
end
return name
end,
getFullName = function (this)
return this[1].__name__
end,
getNamespace = function (this)
local namespace = this.namespace
if namespace == nil then
local clsName = this[1].__name__
local pattern = isGenericName(clsName) and "^(.*)()%..*%[.+%]$" or "^(.*)()%..*$"
namespace = clsName:gsub(pattern, "%1")
this.namespace = namespace
end
return namespace
end,
getBaseType = function (this)
local cls = this[1]
if cls.class ~= "I" and cls ~= Object then
while true do
local base = getmetatable(cls)
if not base then
break
end
if base.__index == base then
return typeof(base)
end
cls = base
end
end
return nil
end,
IsSubclassOf = function (this, c)
return isSubclassOf(this[1], c[1])
end,
getIsInterface = getIsInterface,
GetInterfaces = getInterfaces,
IsAssignableFrom = isAssignableFrom,
IsInstanceOfType = function (this, obj)
if obj == nil then
return false
end
return isAssignableFrom(this, obj:GetType())
end,
getIsArray = getIsArray,
GetElementType = function (this)
if getIsArray(this) then
return typeof(this[1].__genericT__)
end
return nil
end,
ToString = function (this)
return this[1].__name__
end,
GetTypeFrom = function (typeName, throwOnError, ignoreCase)
if typeName == nil then
throw(ArgumentNullException("typeName"))
end
if #typeName == 0 then
if throwOnError then
throw(TypeLoadException("Arg_TypeLoadNullStr"))
end
return nil
end
assert(not ignoreCase, "ignoreCase is not support")
local cls = getClass(typeName)
if cls ~= nil then
return typeof(cls)
end
if throwOnError then
throw(TypeLoadException(typeName .. ": failed to load."))
end
return nil
end
})
Type.GetEnumValues = System.Enum.GetValues
local NumberType = {
__index = Type,
__eq = function (a, b)
local c1, c2 = a[1], b[1]
if c1 == c2 then
return true
end
if c1 == Number or c2 == Number then
return true
end
return false
end
}
local function newNumberType(c)
return setmetatable({ c }, NumberType)
end
local types = {
[Char] = newNumberType(Char),
[SByte] = newNumberType(SByte),
[Byte] = newNumberType(Byte),
[Int16] = newNumberType(Int16),
[UInt16] = newNumberType(UInt16),
[Int32] = newNumberType(Int32),
[UInt32] = newNumberType(UInt32),
[Int64] = newNumberType(Int64),
[UInt64] = newNumberType(UInt64),
[Single] = newNumberType(Single),
[Double] = newNumberType(Double),
[Int] = newNumberType(Int),
[Number] = newNumberType(Number),
}
local customTypeof = System.config.customTypeof
function typeof(cls)
assert(cls)
local t = types[cls]
if t == nil then
if customTypeof then
t = customTypeof(cls)
if t then
types[cls] = t
return t
end
end
t = setmetatable({ cls }, Type)
types[cls] = t
end
return t
end
local function getType(obj)
return typeof(getmetatable(obj))
end
System.typeof = typeof
System.Object.GetType = getType
local function addCheckInterface(set, cls)
local interface = cls.interface
if interface then
for i = 1, #interface do
local it = interface[i]
set[it] = true
addCheckInterface(set, it)
end
end
end
local function getCheckSet(cls)
local set = {}
local p = cls
repeat
set[p] = true
addCheckInterface(set, p)
p = getmetatable(p)
until not p
return set
end
local customTypeCheck = System.config.customTypeCheck
local checks = setmetatable({}, {
__index = function (checks, cls)
if customTypeCheck then
local f, add = customTypeCheck(cls)
if f then
if add then
checks[cls] = f
end
return f
end
end
local set = getCheckSet(cls)
local function check(obj, T)
return set[T] == true
end
checks[cls] = check
return check
end
})
checks[Number] = function (obj, T)
local set = getCheckSet(Number)
local numbers = {
[Char] = function (obj) return type(obj) == "number" and obj >= 0 and obj <= 65535 and floor(obj) == obj end,
[SByte] = function (obj) return type(obj) == "number" and obj >= -128 and obj <= 127 and floor(obj) == obj end,
[Byte] = function (obj) return type(obj) == "number" and obj >= 0 and obj <= 255 and floor(obj) == obj end,
[Int16] = function (obj) return type(obj) == "number" and obj >= -32768 and obj <= 32767 and floor(obj) == obj end,
[UInt16] = function (obj) return type(obj) == "number" and obj >= 0 and obj <= 32767 and floor(obj) == obj end,
[Int32] = function (obj) return type(obj) == "number" and obj >= -2147483648 and obj <= 2147483647 and floor(obj) == obj end,
[UInt32] = function (obj) return type(obj) == "number" and obj >= 0 and obj <= 4294967295 and floor(obj) == obj end,
[Int64] = function (obj) return type(obj) == "number" and obj >= (-9223372036854775807 - 1) and obj <= 9223372036854775807 and floor(obj) == obj end,
[UInt64] = function (obj) return type(obj) == "number" and obj >= 0 and obj <= 18446744073709551615 and floor(obj) == obj end,
[Single] = function (obj) return type(obj) == "number" and obj >= -3.40282347E+38 and obj <= 3.40282347E+38 end,
[Double] = function (obj) return type(obj) == "number" end
}
local function check(obj, T)
local number = numbers[T]
if number then
return number(obj)
end
return set[T] == true
end
checks[Number] = check
return check(obj, T)
end
local is, getName
if System.debugsetmetatable then
is = function (obj, T)
return checks[getmetatable(obj)](obj, T)
end
getName = function (obj)
return obj.__name__
end
System.getClassFromObj = getmetatable
else
local function getClassFromObj(obj)
local t = type(obj)
if t == "number" then
return Number
elseif t == "boolean" then
return Boolean
elseif t == "function" then
return Delegate
end
return getmetatable(obj)
end
function System.ObjectGetType(this)
if this == nil then throw(NullReferenceException()) end
return typeof(getClassFromObj(this))
end
is = function (obj, T)
local base = getClassFromObj(obj)
if base then
return checks[base](obj, T)
end
return false
end
getName = function (obj)
return getClassFromObj(obj).__name__
end
System.getClassFromObj = getClassFromObj
end
System.is = is
function System.as(obj, cls)
if obj ~= nil and is(obj, cls) then
return obj
end
return nil
end
local function cast(cls, obj, nullable)
if obj ~= nil then
if is(obj, cls) then
return obj
end
throw(InvalidCastException(("Unable to cast object of type '%s' to type '%s'."):format(getName(obj), cls.__name__)), 1)
else
if cls.class ~= "S" or nullable then
return nil
end
throw(NullReferenceException(), 1)
end
end
System.cast = cast
function System.castWithNullable(cls, obj)
if System.isNullable(cls) then
return cast(cls.__genericT__, obj, true)
end
return cast(cls, obj)
end