Skip to content

Commit c117751

Browse files
committed
Extract inner functions from new
1 parent 792d638 commit c117751

1 file changed

Lines changed: 29 additions & 18 deletions

File tree

src/Modules/Common.lua

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,31 @@ function newClass(className, ...)
112112
end
113113
return class
114114
end
115+
116+
local parentCall = function(proxy, ...)
117+
local parent = proxy._parent
118+
local object = proxy._object
119+
120+
if not parent._constructor then
121+
error("Parent class '"..parent._className.."' has no constructor")
122+
end
123+
if object._parentInit[parent] then
124+
error("Parent class '"..parent._className.."' has already been initialised")
125+
end
126+
127+
parent._constructor(object, ...)
128+
object._parentInit[parent] = true
129+
end
130+
131+
local parentIndex = function(self, key)
132+
local v = rawget(self._object, key)
133+
if v ~= nil then
134+
return v
135+
else
136+
return self._parent[key]
137+
end
138+
end
139+
115140
function new(className, ...)
116141
local class = getClass(className)
117142
local object = setmetatable({ }, class)
@@ -121,25 +146,11 @@ function new(className, ...)
121146
object._parentInit = { }
122147
for parent in pairs(class._superParents) do
123148
local proxyMeta = {
124-
__index = function(self, key)
125-
local v = rawget(object, key)
126-
if v ~= nil then
127-
return v
128-
else
129-
return parent[key]
130-
end
131-
end,
149+
_parent = parent,
150+
_object = object,
151+
__index = parentIndex,
132152
__newindex = object,
133-
__call = function(...)
134-
if not parent._constructor then
135-
error("Parent class '"..parent._className.."' of class '"..class._className.."' has no constructor")
136-
end
137-
if object._parentInit[parent] then
138-
error("Parent class '"..parent._className.."' of class '"..class._className.."' has already been initialised")
139-
end
140-
parent._constructor(...)
141-
object._parentInit[parent] = true
142-
end,
153+
__call = parentCall,
143154
}
144155
object[parent._className] = setmetatable(proxyMeta, proxyMeta)
145156
end

0 commit comments

Comments
 (0)