-
Notifications
You must be signed in to change notification settings - Fork 145
/
class.lua
165 lines (135 loc) · 3.08 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
local format = string.format
local meta = {}
local names = {}
local classes = {}
local objects = setmetatable({}, {__mode = 'k'})
function meta:__call(...)
local obj = setmetatable({}, self)
objects[obj] = true
obj:__init(...)
return obj
end
function meta:__tostring()
return 'class ' .. self.__name
end
local default = {}
function default:__tostring()
return self.__name
end
function default:__hash()
return self
end
local function isClass(cls)
return not not classes[cls]
end
local function isObject(obj)
return not not objects[obj]
end
local function isSubclass(sub, cls)
if isClass(sub) and isClass(cls) then
if sub == cls then
return true
else
for _, base in ipairs(sub.__bases) do
if isSubclass(base, cls) then
return true
end
end
end
end
return false
end
local function isInstance(obj, cls)
return isObject(obj) and isSubclass(obj.__class, cls)
end
local function profile()
local ret = setmetatable({}, {__index = function() return 0 end})
for obj in pairs(objects) do
local name = obj.__name
ret[name] = ret[name] + 1
end
return ret
end
local types = {['string'] = true, ['number'] = true, ['boolean'] = true}
local function _getPrimitive(v)
return types[type(v)] and v or v ~= nil and tostring(v) or nil
end
local function serialize(obj)
if isObject(obj) then
local ret = {}
for k, v in pairs(obj.__getters) do
ret[k] = _getPrimitive(v(obj))
end
return ret
else
return _getPrimitive(obj)
end
end
local rawtype = type
local function type(obj)
return isObject(obj) and obj.__name or rawtype(obj)
end
return setmetatable({
classes = names,
isClass = isClass,
isObject = isObject,
isSubclass = isSubclass,
isInstance = isInstance,
type = type,
profile = profile,
serialize = serialize,
}, {__call = function(_, name, ...)
if names[name] then return error(format('Class %q already defined', name)) end
local class = setmetatable({}, meta)
classes[class] = true
for k, v in pairs(default) do
class[k] = v
end
local bases = {...}
local getters = {}
local setters = {}
for _, base in ipairs(bases) do
for k1, v1 in pairs(base) do
class[k1] = v1
for k2, v2 in pairs(base.__getters) do
getters[k2] = v2
end
for k2, v2 in pairs(base.__setters) do
setters[k2] = v2
end
end
end
class.__name = name
class.__class = class
class.__bases = bases
class.__getters = getters
class.__setters = setters
local pool = {}
local n = #pool
function class:__index(k)
if getters[k] then
return getters[k](self)
elseif pool[k] then
return rawget(self, pool[k])
else
return class[k]
end
end
function class:__newindex(k, v)
if setters[k] then
return setters[k](self, v)
elseif class[k] or getters[k] then
return error(format('Cannot overwrite protected property: %s.%s', name, k))
elseif k:find('_', 1, true) ~= 1 then
return error(format('Cannot write property to object without leading underscore: %s.%s', name, k))
else
if not pool[k] then
n = n + 1
pool[k] = n
end
return rawset(self, pool[k], v)
end
end
names[name] = class
return class, getters, setters
end})