-
Notifications
You must be signed in to change notification settings - Fork 3
/
muun.moon
53 lines (42 loc) · 1.2 KB
/
muun.moon
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
[[-----------------------------------------------------------------
-- muun - moonscript compatible class implementation
-- Copyright 2019, 2021 [email protected]
-- License: MIT. See LICENSE for details
-----------------------------------------------------------------]]
setup = (__name, __parent, __base) ->
mt =
__call: (...) =>
obj = setmetatable({}, __base)
@.__init(obj, ...)
obj
__index: __base
__newindex: (key, value) => __base[key] = value
__base.new or= =>
cls = setmetatable({
__init: (...) -> __base.new(...)
:__name
:__base
:__parent
}, mt)
with __base
.__class, .__index = cls, __base
cls
super = (parent) ->
setmetatable({}, {
__call: (this, ...) => parent.__init(this, ...)
__index: parent
})
extend = (name, parent, base) ->
setmetatable(base, parent.__base)
cls = setup(name, parent, base)
cls.__super = super(parent)
parent.__inherited(parent, cls) if parent.__inherited
cls
(name, parentOrBase, base) ->
error("Invalid class name") if type(name) ~= 'string'
parent = parentOrBase if type(parentOrBase) == 'table' and parentOrBase.__class
base = not parent and parentOrBase or base or {}
if parent
extend(name, parent, base)
else
setup(name, nil, base)