Repository navigation
Expand file tree
/
Copy pathType.lua
More file actions
350 lines (308 loc) · 7.55 KB
/
Copy pathType.lua
File metadata and controls
350 lines (308 loc) · 7.55 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
--[[
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 Char = System.Char
local Int = System.Int
local Double = System.Double
local String = System.String
local Boolean = System.Boolean
local Delegate = System.Delegate
local getClass = System.getClass
local InvalidCastException = System.InvalidCastException
local ArgumentNullException = System.ArgumentNullException
local TypeLoadException = System.TypeLoadException
local type = type
local getmetatable = getmetatable
local tinsert = table.insert
local ipairs = ipairs
local select = select
local unpack = table.unpack
local Type = {}
local numberType = setmetatable({ c = Double, name = "Number", fullName = "System.Number" }, Type)
local types = {
[Char] = numberType,
[Int] = numberType,
[Double] = numberType,
}
local function typeof(cls)
assert(cls)
local type = types[cls]
if type == nil then
type = setmetatable({ c = cls }, Type)
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.getIsEnum(this)
return this.c.__kind__ == "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)
local fullName = this.fullName
if fullName == nil then
fullName = this.c.__name__
this.fullName = fullName
end
return fullName
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 = this.c.__base__
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.__kind__ == "I"
end
Type.getIsInterface = getIsInterface
local function getIsValueType(this)
return this.c.__kind__ == "S"
end
Type.getIsValueType = getIsValueType
local function getInterfaces(this)
local interfaces = this.interfaces
if interfaces == nil then
interfaces = {}
local p = this.c
repeat
local interfacesCls = p.__interfaces__
if interfacesCls ~= nil then
for _, i in ipairs(interfacesCls) do
tinsert(interfaces, typeof(i))
end
end
p = p.__base__
until p == nil
this.interfaces = interfaces
end
return interfaces
end
function Type.getInterfaces(this)
local interfaces = getInterfaces(this)
local array = {}
for _, i in ipairs(interfaces) do
tinsert(array, i)
end
return System.arrayFromTable(array, Type)
end
local function implementInterface(this, ifaceType)
local t = this
while t ~= nil do
local interfaces = getInterfaces(this)
if interfaces ~= nil then
for _, i in ipairs(interfaces) do
if i == ifaceType or implementInterface(i, 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
System.define("System.Type", Type)
function isInterfaceOf(t, ifaceType)
repeat
local interfaces = t.__interfaces__
if interfaces then
for _, i in ipairs(interfaces) do
if i == ifaceType or isInterfaceOf(i, ifaceType) then
return true
end
end
end
t = t.__base__
until t == nil
return false
end
function isTypeOf(obj, cls)
if cls == Object then return true end
local typename = type(obj)
if typename == "number" then
return cls == Int or cls == Double or cls == Char
elseif typename == "string" then
return cls == String
elseif typename == "table" then
local t = getmetatable(obj)
if t == cls then
return true
end
if cls.__kind__ == "I" then
return isInterfaceOf(t, cls)
else
local base = t.__base__
while base ~= nil do
if base == cls then
return true
end
base = base.__base__
end
return false
end
elseif typename == "boolean" then
return cls == Boolean
else
return cls == Delegate
end
end
function System.is(obj, cls)
return obj ~= nil and isTypeOf(obj, cls)
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.__kind__ ~= "S" then
return nil
end
else
if isTypeOf(obj, cls) then
return obj
end
end
throw(InvalidCastException(), 1)
end
function System.new(cls)
if cls == Int or cls == Double then
return 0
end
if cls == Boolean then
return false
end
local ctor = cls.__ctor__
if type(ctor) == "table" then
ctor = ctor[1]
end
local this = setmetatable({}, cls)
ctor(this)
return this
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
local t = {}
for k, v in System.ipairs(args) do
t[k] = v
end
return type.c(unpack(t, 1, #args))
end
end
return type.c(...)
end