-
Notifications
You must be signed in to change notification settings - Fork 1
/
class.lua
219 lines (176 loc) · 4.79 KB
/
class.lua
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
local setmetatable = setmetatable
local anonClassCounter = 0
local ids = {}
local function injectSuperUpValue(class)
local superMethods = class.super.methods
for name, method in next, class.methods do
if type(method) == "function" then
for i = 1, debug.getinfo(method, "u").nups do
local name, value = debug.getupvalue(method, i)
if name == "super" then
debug.setupvalue(method, i, superMethods)
break
end
end
end
end
end
local function generateUID()
local id
repeat
id = math.random(0, 0xFFFFFFF)
until not ids[id]
ids[id] = true
return string.format("%x08", id)
end
function instanceof(instance, class)
return instance[class.uid] ~= nil
end
local classmethods = {}
classmethods.__index = classmethods
classmethods.name = "INTERNAL CLASS META"
function classmethods:getName()
return self.name
end
function classmethods:getMethods()
local ret = {}
for k, v in next, self.methods do
if not (k == "__index" or k == "__tostring" or k == "getClass" or k:match("%x%x%x%x%x%x%x%x")) then
ret[k] = v
end
end
return ret
end
function classmethods:getSuper()
return self.super
end
function classmethods:hasSuper()
return self.super ~= nil
end
function classmethods:getUID()
return self.uid
end
function classmethods:__tostring()
return ("Class: %s"):format(self.name)
end
function classmethods:isAnonymous()
return not not self.name:match("AnonymousClass%x+")
end
function classmethods:implements(otherClass, succ, ret)
if succ == nil then
succ = true
end
local methods = self:getMethods()
ret = ret or {}
for methodName, value in next, otherClass:getMethods() do
if (methodName:sub(1, 2) ~= "__") and methodName:sub(1, 1):match("%a") and type(methods[methodName]) ~= "function" and value == true then
local otherName = otherClass:getName()
ret[otherName] = ret[otherName] or {}
table.insert(ret[otherName], methodName)
succ = false
end
end
if otherClass:hasSuper() then
succ = otherClass:implements(otherClass:getSuper(), succ, ret)
end
return succ, ret
end
local singletonClassMeta = setmetatable({}, classmethods)
singletonClassMeta.__index = singletonClassMeta
singletonClassMeta.name = "INTERNAL SINGLETON CLASS META"
function singletonClassMeta:getInstance()
if not self.instance then
self.instance = self()
end
return self.instance
end
local function defaultToString(self)
return ("%s"):format(self:getClass():getName())
end
function abstractClass(name, methods, statics, superClass)
if not name then
name = string.format("AnonymousClass%08x", anonClassCounter)
anonClassCounter = anonClassCounter + 1
end
local uid = generateUID()
if methods.new then
error("Abstract class cannot have constructor")
end
methods.__index = methods.__index or methods
methods[uid] = true
local internalClassmethods = {
__tostring = classmethods.__tostring
}
internalClassmethods.__index = internalClassmethods
local classTable = setmetatable({
methods = methods,
uid = uid,
name = name,
new = constructor
}, setmetatable(internalClassmethods, classmethods))
if statics then
for k, v in next, statics do
classTable[k] = v
end
end
if superClass then
classTable.super = superClass
setmetatable(methods, superClass.methods)
classTable.super = superClass
injectSuperUpValue(classTable)
end
return classTable
end
function class(name, methods, statics, superClass, isSingleton, isAbstract)
if not name then
name = string.format("AnonymousClass%08x", anonClassCounter)
anonClassCounter = anonClassCounter + 1
end
local uid = generateUID()
local constructor = methods.new
methods.__index = methods.__index or methods
methods.__tostring = methods.__tostring or defaultToString
methods[uid] = true
local internalClassmethods = {
__call = function(classTbl, ...)
local new = setmetatable({}, methods)
if constructor then
constructor(new, ...)
end
return new
end,
__tostring = classmethods.__tostring
}
internalClassmethods.__index = internalClassmethods
local classTable = setmetatable({
methods = methods,
uid = uid,
name = name,
new = constructor
}, setmetatable(internalClassmethods, isSingleton and singletonClassMeta or classmethods))
if statics then
for k, v in next, statics do
classTable[k] = v
end
end
if superClass then
local succ, missing = classTable:implements(superClass)
if not succ then
print("ERROR LOADING CLASS")
for className, missingList in next, missing do
for idx, methodName in next, missingList do
print(("Method %s missing as defined in %s"):format(methodName, className))
end
end
error("Missing method")
end
setmetatable(methods, superClass.methods)
classTable.super = superClass
injectSuperUpValue(classTable)
end
methods.getClass = function()
return superClass
end
return classTable
end
return class