-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClass.lua
More file actions
79 lines (58 loc) · 1.46 KB
/
Class.lua
File metadata and controls
79 lines (58 loc) · 1.46 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
local RootClass = {
_name = "RootClass"
}
local ClassCache = setmetatable({}, {__mode = "k"})
local Framework
function RootClass:__index(i)
local value = rawget(ClassCache[self], i)
if not value then
local super = rawget(ClassCache[self], "_super")
if super then
value = super[i]
return value or Framework and Framework[i]
end
else
return value
end
end
function RootClass:__newindex(i, v)
rawset(ClassCache[self], i, v)
end
function RootClass:__tostring()
return rawget(ClassCache[self], "_name")
end
function RootClass.OnWrap(tbl)
Framework = tbl
end
local RootObj = newproxy(true)
local MT = getmetatable(RootObj)
MT.__index = RootClass.__index
MT.__newindex = RootClass.__newindex
MT.__tostring = RootClass.__tostring
ClassCache[RootObj] = RootClass
local function GetClassByName(name)
for i,v in pairs(ClassCache) do
if v._name == name then
return i
end
end
return nil
end
local Class = {}
function Class.new(name, super)
super = (typeof(super) == "string" and GetClassByName(super)) or ClassCache[super] or RootObj
local Object = newproxy(true)
local MT = getmetatable(Object)
MT.__index = super.__index
MT.__newindex = super.__newindex
MT.__tostring = super.__tostring
ClassCache[Object] = {
_name = name,
_super = super,
__index = MT.__index,
__newindex = MT.__newindex,
__tostring = MT.__tostring
}
return Object
end
return Class