Skip to content

Commit 950534b

Browse files
committed
Added experimental ooplib
1 parent c23dd7c commit 950534b

File tree

1 file changed

+128
-0
lines changed

1 file changed

+128
-0
lines changed

ooplib.lua

+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
local __CLASSNAME__
2+
local __BASECLASSES__
3+
local __CLASSES__ = {}
4+
local __MEMBERS__
5+
local __IS_STATIC = false
6+
7+
function static_class(name)
8+
__IS_STATIC = true
9+
return class(name)
10+
end
11+
12+
function maybeExtends(name)
13+
if name == extends then
14+
return extends
15+
else
16+
return buildClass(name)
17+
end
18+
end
19+
20+
local __MEMBERNAME__
21+
function buildMember(data)
22+
__MEMBERS__[__MEMBERNAME__] = data
23+
end
24+
25+
26+
function buildClass(definition)
27+
__CLASSES__[__CLASSNAME__] = definition
28+
_G[__CLASSNAME__] = definition
29+
definition.__CLASSNAME__ = __CLASSNAME__
30+
definition.__members__ = __MEMBERS__
31+
local parents = {}
32+
for k, v in pairs(__BASECLASSES__) do
33+
parents[k] = __CLASSES__[v]
34+
end
35+
36+
-- Prepare parent members
37+
local defaults = {}
38+
for k, class in pairs(parents) do
39+
for name, member in pairs(class.__members__) do
40+
defaults[name] = member.default
41+
end
42+
end
43+
44+
for k, v in pairs(__MEMBERS__) do
45+
defaults[k] = v.default
46+
end
47+
48+
setmetatable(definition,
49+
{
50+
__index = function(self, key)
51+
for k, v in pairs(parents) do
52+
if v[key] then
53+
return v[key]
54+
end
55+
end
56+
end;
57+
58+
__call = function(...)
59+
local member = defaults
60+
local instance = setmetatable({ __members__ = member, __class__ = definition },
61+
{
62+
__index = function(self, key)
63+
if definition.__members__[key] then
64+
if definition.__members__[key].get then
65+
return definition.__members__[key].get(self)
66+
end
67+
return self.__members__[key]
68+
end
69+
70+
return definition[key]
71+
end;
72+
-- Todo: Other metamethods
73+
74+
__newindex = function(self, key, value)
75+
if definition.__members__[key] then
76+
if definition.__members__[key].set then
77+
if not definition.__members__[key].set(self, value) then
78+
return
79+
end
80+
end
81+
self.__members__[key] = value
82+
end
83+
84+
-- Implicit member creation
85+
-- If you want, replace this by an error
86+
-- and make sure to add this line above
87+
-- to ensure proper setting for non-setter
88+
-- members
89+
self.__members__[key] = value
90+
end
91+
})
92+
93+
return instance
94+
end;
95+
})
96+
97+
if __IS_STATIC then
98+
if definition.constructor then
99+
definition:constructor()
100+
end
101+
end
102+
__IS_STATIC = false
103+
end
104+
105+
106+
function class(name)
107+
__CLASSNAME__ = name
108+
__BASECLASSES__ = {}
109+
__MEMBERS__ = {}
110+
return maybeExtends
111+
end
112+
113+
function extends(name)
114+
if type(name) == "string" then
115+
-- Handle base classes
116+
__BASECLASSES__[#__BASECLASSES__+1] = name
117+
return extends
118+
else
119+
-- Handle class definition
120+
return buildClass(name)
121+
end
122+
end
123+
124+
function member(name)
125+
__MEMBERNAME__ = name
126+
__MEMBERS__[name] = {}
127+
return buildMember
128+
end

0 commit comments

Comments
 (0)