2017-08-14 13:02:36 +00:00
|
|
|
--- @todo document this!
|
|
|
|
Class = (name, parent) ->
|
2017-08-14 12:48:36 +00:00
|
|
|
local newClass, base
|
|
|
|
base = {
|
|
|
|
__index: base
|
|
|
|
__class: newClass
|
|
|
|
}
|
|
|
|
|
|
|
|
newClass = setmetable {
|
|
|
|
__init: ->
|
|
|
|
__base: base
|
|
|
|
__name: name
|
|
|
|
}, {
|
|
|
|
__call: (cls, ...) ->
|
|
|
|
@ = setmetable({}, base)
|
|
|
|
cls.__init(@, ...)
|
|
|
|
return @
|
|
|
|
}
|
|
|
|
|
2017-08-14 13:02:36 +00:00
|
|
|
if parent
|
|
|
|
setmetable base, {
|
|
|
|
__parent: parent.__base
|
|
|
|
}
|
2017-08-14 12:48:36 +00:00
|
|
|
|
2017-08-14 13:02:36 +00:00
|
|
|
newClass.__parent = parent
|
|
|
|
newClass.__index = (cls, name) ->
|
|
|
|
val = rawget(base, name)
|
|
|
|
if val == nil
|
|
|
|
return parent[name]
|
|
|
|
else
|
|
|
|
return val
|
|
|
|
|
|
|
|
if parent.__inherited
|
|
|
|
parent\__inherited newClass
|
2017-08-14 12:48:36 +00:00
|
|
|
|
2017-08-14 13:02:36 +00:00
|
|
|
return newClass, base
|
|
|
|
|
|
|
|
return Class
|