From c117751d51ca720d3b940d47138fb49f190e5c0e Mon Sep 17 00:00:00 2001 From: PJacek Date: Tue, 26 May 2026 01:04:44 +0200 Subject: [PATCH 1/2] Extract inner functions from new --- src/Modules/Common.lua | 47 ++++++++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/src/Modules/Common.lua b/src/Modules/Common.lua index 6fd1ec57a9..f010bbb7a9 100644 --- a/src/Modules/Common.lua +++ b/src/Modules/Common.lua @@ -112,6 +112,31 @@ function newClass(className, ...) end return class end + +local parentCall = function(proxy, ...) + local parent = proxy._parent + local object = proxy._object + + if not parent._constructor then + error("Parent class '"..parent._className.."' has no constructor") + end + if object._parentInit[parent] then + error("Parent class '"..parent._className.."' has already been initialised") + end + + parent._constructor(object, ...) + object._parentInit[parent] = true +end + +local parentIndex = function(self, key) + local v = rawget(self._object, key) + if v ~= nil then + return v + else + return self._parent[key] + end +end + function new(className, ...) local class = getClass(className) local object = setmetatable({ }, class) @@ -121,25 +146,11 @@ function new(className, ...) object._parentInit = { } for parent in pairs(class._superParents) do local proxyMeta = { - __index = function(self, key) - local v = rawget(object, key) - if v ~= nil then - return v - else - return parent[key] - end - end, + _parent = parent, + _object = object, + __index = parentIndex, __newindex = object, - __call = function(...) - if not parent._constructor then - error("Parent class '"..parent._className.."' of class '"..class._className.."' has no constructor") - end - if object._parentInit[parent] then - error("Parent class '"..parent._className.."' of class '"..class._className.."' has already been initialised") - end - parent._constructor(...) - object._parentInit[parent] = true - end, + __call = parentCall, } object[parent._className] = setmetatable(proxyMeta, proxyMeta) end From b28f90fbc01e03f37b2aa5d5933c6e2541fefc88 Mon Sep 17 00:00:00 2001 From: PJacek Date: Tue, 14 Jul 2026 17:03:55 +0200 Subject: [PATCH 2/2] Replace dynamic inheritance with static copy --- src/Modules/Common.lua | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/Modules/Common.lua b/src/Modules/Common.lua index f010bbb7a9..d0db40e98b 100644 --- a/src/Modules/Common.lua +++ b/src/Modules/Common.lua @@ -98,17 +98,13 @@ function newClass(className, ...) class._superParents = { } addSuperParents(class, class) -- Set up inheritance - setmetatable(class, { - __index = function(self, key) - for _, parent in ipairs(class._parents) do - local val = parent[key] - if val ~= nil then - self[key] = val - return val - end + for _, parent in ipairs(class._parents) do + for k, v in pairs(parent) do + if class[k] == nil then + class[k] = v end end - }) + end end return class end