Repository navigation
Expand file tree
/
Copy pathType.lua
More file actions
432 lines (386 loc) · 9.75 KB
/
Copy pathType.lua
File metadata and controls
432 lines (386 loc) · 9.75 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
--[[
Copyright 2017 YANG Huan ([email protected]).
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 String = System.String
local Boolean = System.Boolean
local Delegate = System.Delegate
local getClass = System.getClass
local arrayFromTable = System.arrayFromTable
local InvalidCastException = System.InvalidCastException
local ArgumentNullException = System.ArgumentNullException
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 type = type
local getmetatable = getmetatable
local select = select
local unpack = table.unpack
local floor = math.floor
local Type = {}
local NumberType = {
__index = Type,
__eq = function (a, b)
local c1, c2 = a.c, b.c
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 = 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 createType = System.config.customTypeOf or function(cls) return setmetatable({ c = cls }, Type) end
local function typeof(cls)
assert(cls)
local type = types[cls]
if type == nil then
type = createType(cls)
types[cls] = type
end
return type
end
local function getType(obj)
return typeof(getmetatable(obj))
end
System.Object.GetType = getType
System.typeof = typeof
local function isGenericName(name)
return name:byte(#name) == 93
end
function Type.getIsGenericType(this)
return isGenericName(this.c.__name__)
end
function Type.MakeGenericType(this, ...)
local args = { ... }
for i = 1, #args do
args[i] = args[i].c
end
return typeof(this.c(unpack(args)))
end
function Type.getIsEnum(this)
return this.c.class == "E"
end
function Type.getName(this)
local name = this.name
if name == nil then
local clsName = this.c.__name__
local pattern = isGenericName(clsName) and "^.*()%.(.*)%[.+%]$" or "^.*()%.(.*)$"
name = clsName:gsub(pattern, "%2")
this.name = name
end
return name
end
function Type.getFullName(this)
return this.c.__name__
end
function Type.getNamespace(this)
local namespace = this.namespace
if namespace == nil then
local clsName = this.c.__name__
local pattern = isGenericName(clsName) and "^(.*)()%..*%[.+%]$" or "^(.*)()%..*$"
namespace = clsName:gsub(pattern, "%1")
this.namespace = namespace
end
return namespace
end
local function getBaseType(this)
local baseType = this.baseType
if baseType == nil then
local baseCls = getmetatable(this.c)
if baseCls ~= nil then
baseType = typeof(baseCls)
this.baseType = baseType
end
end
return baseType
end
Type.getBaseType = getBaseType
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 = getBaseType(p)
end
return false
end
Type.IsSubclassOf = isSubclassOf
local function getIsInterface(this)
return this.c.class == "I"
end
Type.getIsInterface = getIsInterface
local function getIsValueType(this)
return this.c.class == "S"
end
Type.getIsValueType = getIsValueType
local function getInterfaces(this)
local interfaces = this.interfaces
if interfaces == nil then
interfaces = arrayFromTable({}, Type, true)
local count = 1
local p = this.c
repeat
local interfacesCls = p.interface
if interfacesCls ~= nil then
for i = 1, #interfacesCls do
interfaces[count] = typeof(interfacesCls[i])
count = count + 1
end
end
p = getmetatable(p)
until p == nil
this.interfaces = interfaces
end
return interfaces
end
Type.getInterfaces = getInterfaces
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
if getIsInterface(this) then
return implementInterface(c, this)
else
return isSubclassOf(c, this)
end
end
Type.IsAssignableFrom = isAssignableFrom
function Type.IsInstanceOfType(this, obj)
if obj == nil then
return false
end
return isAssignableFrom(this, obj:GetType())
end
function Type.ToString(this)
return this.c.__name__
end
function Type.GetTypeFrom(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.Equals = System.equals
System.define("System.Type", Type)
local function isInterfaceOf(t, ifaceType)
repeat
local interfaces = t.interface
if interfaces then
for i = 1, #interfaces do
local it = interfaces[i]
if it == ifaceType or isInterfaceOf(it, ifaceType) then
return true
end
end
end
t = getmetatable(t)
until t == nil
return false
end
local isUserdataTypeOf = System.config.isUserdataTypeOf
local numbers = {
[Char] = { 0, 65535 },
[SByte] = { -128, 127 },
[Byte] = { 0, 255 },
[Int16] = { -32768, 32767 },
[UInt16] = { 0, 65535 },
[Int32] = { -2147483648, 2147483647 },
[UInt32] = { 0, 4294967295 },
[Int64] = { -9223372036854775808, 9223372036854775807 },
[UInt64] = { 0, 18446744073709551615 },
[Single] = { -3.40282347E+38, 3.40282347E+38, 1 },
[Double] = { nil, nil, 2 }
}
numbers[Int] = numbers[Int32]
local function isTypeOf(obj, cls)
if cls == Object then return true end
local typename = type(obj)
if typename == "table" then
local t = getmetatable(obj)
if t == cls then
return true
end
if cls.class == "I" then
return isInterfaceOf(t, cls)
else
local base = getmetatable(t)
while base ~= nil do
if base == cls then
return true
end
base = getmetatable(base)
end
return false
end
elseif typename == "number" then
local info = numbers[cls]
if info ~= nil then
local min, max, sign = info[1], info[2], info[3]
if sign == nil then
if obj < min or obj > max then
return false
end
if floor(obj) ~= obj then
return false
end
elseif sign == 1 then
if obj < min or obj > max then
return false
end
end
return true
elseif cls.class == "I" then
return isInterfaceOf(Number, cls)
elseif cls == ValueType then
return true
end
return false
elseif typename == "string" then
if cls == String then
return true
end
if cls.class == "I" then
return isInterfaceOf(String, cls)
end
return false
elseif typename == "boolean" then
if cls == Boolean or cls == ValueType then
return true
end
if cls.class == "I" then
return isInterfaceOf(Boolean, cls)
end
return false
elseif typename == "function" then
return cls == Delegate
elseif typename == "userdata" then
if isUserdataTypeOf then
return isUserdataTypeOf(obj, cls)
end
return true
else
assert(false)
end
end
function System.is(obj, cls)
if obj ~= nil then
return isTypeOf(obj, cls)
end
return cls == nil
end
function System.as(obj, cls)
if obj ~= nil and isTypeOf(obj, cls) then
return obj
end
return nil
end
function System.cast(cls, obj)
if obj == nil then
if cls.class ~= "S" then
return nil
end
throw(NullReferenceException(), 1)
else
if isTypeOf(obj, cls) then
return obj
end
throw(InvalidCastException(), 1)
end
end
function System.CreateInstance(type, ...)
if type == nil then
throw(ArgumentNullException("type"))
end
if getmetatable(type) ~= Type then -- is T
return type()
end
local len = select("#", ...)
if len == 1 then
local args = ...
if System.isArrayLike(args) then
return type.c(unpack(args))
end
end
return type.c(...)
end