mirror of
https://github.com/vrld/hump.git
synced 2024-10-15 09:04:18 +00:00
Fix class commons compatibility
This commit is contained in:
parent
615dd8f111
commit
ef2c4f6c56
19
class.lua
19
class.lua
@ -25,7 +25,9 @@ THE SOFTWARE.
|
|||||||
]]--
|
]]--
|
||||||
|
|
||||||
local function include_helper(to, from, seen)
|
local function include_helper(to, from, seen)
|
||||||
if type(from) ~= 'table' then
|
if from == nil then
|
||||||
|
return to
|
||||||
|
elseif type(from) ~= 'table' then
|
||||||
return from
|
return from
|
||||||
elseif seen[from] then
|
elseif seen[from] then
|
||||||
return seen[from]
|
return seen[from]
|
||||||
@ -53,19 +55,20 @@ local function clone(other)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local function new(class)
|
local function new(class)
|
||||||
class.__index = class
|
|
||||||
class.init = class.init or function() end
|
|
||||||
class.include = class.include or include
|
|
||||||
class.clone = class.clone or clone
|
|
||||||
|
|
||||||
-- mixins
|
-- mixins
|
||||||
local inc = class.__includes or {}
|
local inc = class.__includes or {}
|
||||||
if getmetatable(inc) then inc = {inc} end
|
if getmetatable(inc) then inc = {inc} end
|
||||||
|
|
||||||
for _, other in pairs(inc) do
|
for _, other in ipairs(inc) do
|
||||||
include(class, other)
|
include(class, other)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- class implementation
|
||||||
|
class.__index = class
|
||||||
|
class.init = class.init or class[1] or function() end
|
||||||
|
class.include = class.include or include
|
||||||
|
class.clone = class.clone or clone
|
||||||
|
|
||||||
-- constructor call
|
-- constructor call
|
||||||
return setmetatable(class, {__call = function(c, ...)
|
return setmetatable(class, {__call = function(c, ...)
|
||||||
local o = setmetatable({}, c)
|
local o = setmetatable({}, c)
|
||||||
@ -78,7 +81,7 @@ end
|
|||||||
if class_commons ~= false and not common then
|
if class_commons ~= false and not common then
|
||||||
common = {}
|
common = {}
|
||||||
function common.class(name, prototype, parent)
|
function common.class(name, prototype, parent)
|
||||||
return include(new(prototype), parent)
|
return new{__includes = {prototype, parent}}
|
||||||
end
|
end
|
||||||
function common.instance(class, ...)
|
function common.instance(class, ...)
|
||||||
return class(...)
|
return class(...)
|
||||||
|
Loading…
Reference in New Issue
Block a user