-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass.lua
More file actions
48 lines (42 loc) · 1 KB
/
class.lua
File metadata and controls
48 lines (42 loc) · 1 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
local mt = setmetatable
local rawget = rawget
local function _onCtor(ins, cls, ...)
if cls.super then
_onCtor(ins, cls.super, ...)
end
local ctor = rawget(cls, "Ctor")
if ctor then
ctor(ins, ...)
end
end
local _rootCls = {
__index = function(cls, key)
local super = rawget(cls, "super")
while super do
local ret = rawget(super, key)
if ret ~= nil then
cls[key] = ret
return ret
end
super = rawget(super, "super")
end
end,
__call = function(cls, ...)
local ins = mt({}, cls)
_onCtor(ins, cls, ...)
return ins
end
}
local _parseSuper = function(cls, super)
if super then
cls.__SCNAME = super.__CNAME --为了触发autoRequire
cls.super = super.__IMPL or super
end
end
function class(name, super)
local cls = {}
cls.__index = cls
cls.__CNAME = name
_parseSuper(cls, super)
return mt(cls, _rootCls)
end