@thigasdevelopment/luam 0.13.0 → 0.18.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lua/class.lua CHANGED
@@ -24,21 +24,69 @@ local function bindSuper(value, inherited)
24
24
  end
25
25
  end
26
26
 
27
+ local function bindLateSuper(definition, key, value)
28
+ return function(self, ...)
29
+ local inherited = definition.__super and definition.__super[key]
30
+
31
+ if type(inherited) ~= 'function' then
32
+ return value(self, ...)
33
+ end
34
+
35
+ return bindSuper(value, inherited)(self, ...)
36
+ end
37
+ end
38
+
39
+ local function declare(name)
40
+ local existing = classes[name]
41
+
42
+ if existing then
43
+ return existing
44
+ end
45
+
46
+ local definition = { __name = name, __pending = true }
47
+
48
+ classes[name] = definition
49
+
50
+ return definition
51
+ end
52
+
53
+ local function pendingAncestor(definition)
54
+ local current = definition.__super
55
+
56
+ while current do
57
+ if current.__pending then
58
+ return current.__name
59
+ end
60
+
61
+ current = current.__super
62
+ end
63
+
64
+ return nil
65
+ end
66
+
27
67
  local function create(name, struct, options)
28
- if classes[name] then
68
+ local definition = classes[name]
69
+
70
+ if definition and not definition.__pending then
29
71
  error('Class ' .. tostring(name) .. ' already exists.')
30
72
  end
31
73
 
32
- local definition = {
33
- __name = name,
34
- __super = options.super,
35
- __metamethods = options.metamethods,
36
- }
74
+ definition = definition or { __name = name }
75
+
76
+ definition.__pending = nil
77
+ definition.__super = options.super
78
+ definition.__metamethods = options.metamethods
79
+
80
+ local pending = definition.__super and definition.__super.__pending
37
81
 
38
82
  for key, value in pairs(struct) do
39
83
  local inherited = definition.__super and definition.__super[key]
40
84
 
41
- if type(value) == 'function' and type(inherited) == 'function' then
85
+ if type(value) ~= 'function' then
86
+ definition[key] = value
87
+ elseif pending then
88
+ definition[key] = bindLateSuper(definition, key, value)
89
+ elseif type(inherited) == 'function' then
42
90
  definition[key] = bindSuper(value, inherited)
43
91
  else
44
92
  definition[key] = value
@@ -59,11 +107,7 @@ function class(name)
59
107
 
60
108
  local modifiers = {
61
109
  extends = function(self, super)
62
- options.super = classes[super]
63
-
64
- if not options.super then
65
- error('Class ' .. tostring(name) .. ' extends ' .. tostring(super) .. ', which is not defined.')
66
- end
110
+ options.super = declare(super)
67
111
 
68
112
  return self
69
113
  end,
@@ -131,13 +175,19 @@ end
131
175
  function new(name)
132
176
  local definition = classes[name]
133
177
 
134
- if not definition then
178
+ if not definition or definition.__pending then
135
179
  error('Class ' .. tostring(name) .. ' is not defined.')
136
180
  end
137
181
 
138
182
  local constructor = constructors[definition]
139
183
 
140
184
  if not constructor then
185
+ local missing = pendingAncestor(definition)
186
+
187
+ if missing then
188
+ error('Class ' .. tostring(name) .. ' extends ' .. tostring(missing) .. ', which is not defined.')
189
+ end
190
+
141
191
  constructor = createConstructor(definition)
142
192
  constructors[definition] = constructor
143
193
  end
@@ -170,9 +220,23 @@ function enum(names)
170
220
  end
171
221
 
172
222
  function getClasses()
173
- return classes
223
+ local defined = {}
224
+
225
+ for name, definition in pairs(classes) do
226
+ if not definition.__pending then
227
+ defined[name] = definition
228
+ end
229
+ end
230
+
231
+ return defined
174
232
  end
175
233
 
176
234
  function getClass(name)
177
- return classes[name]
235
+ local definition = classes[name]
236
+
237
+ if not definition or definition.__pending then
238
+ return nil
239
+ end
240
+
241
+ return definition
178
242
  end