-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmoss.lua
More file actions
313 lines (264 loc) · 11 KB
/
moss.lua
File metadata and controls
313 lines (264 loc) · 11 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
------------------------------------------------------------
-- A lightweight Lua class library
-- written by yours truly, CrispyBun.
-- crispybun@pm.me
-- https://github.com/CrispyBun/Moss
------------------------------------------------------------
--[[
MIT License
Copyright (c) 2024-2026 Ava "CrispyBun" Špráchalů
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
--]]
------------------------------------------------------------
local moss = {}
local META_KEY = setmetatable({}, {__tostring = function() return "[Metatable]" end})
local TREE_KEY = setmetatable({}, {__tostring = function() return "[Inheritance]" end})
moss.META_KEY = META_KEY -- Used to get the metatable of classes. There's not many uses for using this, but it's exposed anyway.
moss.TREE_KEY = TREE_KEY -- Used to get the inheritance information of classes. There's not many uses for using this, but it's exposed anyway.
-- If true, when inheriting from multiple classes, and the library finds
-- different implementations for the same method in the child's direct parents, it will force the child class
-- to disambiguate the methods by overriding and defining them itself.
-- (If it doesn't, the methods will be replaced by a dummy function that errors if you try to use it.)
--
-- This may have undesirable results if you mix function types with other types for the same key in a class tree.
moss.diamondDisambiguation = false
------------------------------------------------------------
local function ambiguousMethodError()
error("The method called is ambiguous due to it being implemented differently in multiple parent classes. It must be overridden in the inheriting class to disambiguate it.", 2)
end
local function copyTo(source, target)
if source then
for key, value in pairs(source) do
target[key] = value
end
end
return target
end
--- Creates a new instance of a Moss class, bypassing the `__new` method (if any) and just using the default Moss one.
--- You probably don't have to worry about this method, but its one good use is if you're implementing the `__new` method, and in it, you want to use the default Moss instancing.
---@param class table|fun(): table The class to instance
---@param ... unknown Arguments for the constructor
---@return table
function moss.generateInstance(class, ...)
local inst = setmetatable({}, class[META_KEY])
if inst.init then inst:init(...) end
return inst
end
--- Returns the metatable which the instances of the given moss class get assigned.
--- You most likely won't need to use this method for most purposes, but it's here anyway since otherwise it's a bit awkward to get access to this metatable.
---@param class table|fun(): table The class definition
---@return table Metatable The metatable used by the class' instances
function moss.getInstanceMetatable(class)
return class[META_KEY]
end
local mossClassMT = {
__call = function (t, ...)
local metatable = t[META_KEY]
if metatable and metatable.__new then return metatable.__new(t, metatable, ...) end
return moss.generateInstance(t, ...)
end,
__tostring = function (t)
return t[META_KEY] and t[META_KEY].__name or "Moss Class"
end,
__name = "Moss Class"
}
------------------------------------------------------------
------------------------------------------------------------
--- ### moss.inherit(...parents)
--- ### moss.extend(...parents)
--- Returns a class definition table with values inherited from the given parent classes (or from regular tables).
--- Example usage:
--- ```
--- local Rectangle = require 'Rectangle'
--- local Clickable = require 'Clickable'
---
--- local Button = moss.inherit( Rectangle, Clickable )
---
--- return moss.create(Button)
--- ```
---@param ... table|fun(): table The classes to inherit from
---@return table class
function moss.inherit(...)
local parents = {...}
local classTable = {}
local allParents = {}
local metatable = {}
local seenMethods = {}
local inheritMethods = {}
local forcedValues = {}
for parentIndex = #parents, 1, -1 do
local parent = parents[parentIndex]
allParents[parent] = true
-- Technically, the annotations suggest the parent can be a function.
-- The parents should really only ever be tables, or tables that can be called for instancing,
-- but the way that's annotated does imply it can be just a function that returns a table,
-- so let's handle that too:
if type(parent) == "function" then parent = parent() end
if parent[TREE_KEY] then
for prevParent in pairs(parent[TREE_KEY]) do
allParents[prevParent] = true
end
end
if parent[META_KEY] then
for methodName, method in pairs(parent[META_KEY]) do
metatable[methodName] = method
end
if parent[META_KEY].__inherit then inheritMethods[#inheritMethods+1] = parent[META_KEY].__inherit end
end
for key, value in pairs(parent) do
classTable[key] = value
if moss.diamondDisambiguation and type(value) == "function" then
if seenMethods[key] and seenMethods[key] ~= value then
forcedValues[key] = ambiguousMethodError
elseif not seenMethods[key] then
seenMethods[key] = value
end
end
end
end
metatable.__name = nil -- Don't inherit class names for clarity
classTable[TREE_KEY] = allParents
classTable[META_KEY] = metatable
for key, value in pairs(forcedValues) do
classTable[key] = value
end
for methodIndex = 1, #inheritMethods do
inheritMethods[methodIndex](classTable, metatable)
end
return classTable
end
moss.extend = moss.inherit
------------------------------------------------------------
--- ### moss.create(class)
--- Sets the necessary metatable properties of the class and returns it.
--- Calling the class definition ( `Class()` ) after this will create an instance of it.
---
--- Example usage:
--- #### Player.lua
--- ```
--- local moss = require 'moss'
---
--- local Player = {}
--- Player.x = 0
--- Player.y = 0
---
--- return moss.create(Player)
--- ```
--- ---
--- #### main.lua
--- ```
--- local Player = require 'Player'
--- local instance = Player()
--- ```
---@generic T
---@param class T A table defining the class' default values and methods
---@param metatable? table An optional metatable to be given to instances of this class
---@return T|fun(...: unknown): T
function moss.create(class, metatable)
local mt = {}
copyTo(class[META_KEY], mt)
copyTo(metatable, mt)
mt.__index = class
class[TREE_KEY] = class[TREE_KEY] or {}
class[TREE_KEY][class] = true
class[META_KEY] = mt
setmetatable(class, mossClassMT)
if mt.__create then mt.__create(class, mt) end
return class
end
------------------------------------------------------------
--- ### moss.is(instance, class)
--- ### moss.implements(instance, class)
--- ### moss.instanceof(instance, class)
--- Checks if an instance was instanced from a class, or from a child of the class.
--- Example usage:
--- ```
--- local ArmedEnemy = require 'ArmedEnemy'
---
--- if moss.is(someInst, ArmedEnemy) then
--- someInst:disarm()
--- end
--- ```
---@param instance table The instance to check
---@param class table|fun(): table The class to check for
---@return boolean
function moss.is(instance, class)
return instance[TREE_KEY] and instance[TREE_KEY][class] or false
end
moss.implements = moss.is
moss.instanceof = moss.is
------------------------------------------------------------
--- ### moss.type(instance)
--- ### moss.typeof(instance)
--- Returns the class definition the given moss instance was instanced from
---@param instance table
---@return table|fun(): table class The class definition
function moss.type(instance)
return instance[META_KEY] and instance[META_KEY].__index
end
moss.typeof = moss.type
------------------------------------------------------------
--- A basic class which can be used as a base for other classes.
--- Has basic type checking methods and an empty constructor.
---@class Moss.BaseClass
local BaseClass = {}
BaseClass.is = moss.is
BaseClass.implements = moss.implements
BaseClass.type = moss.type
function BaseClass:init() end
--- You can optionally inherit from this class to get a base for your classes.
--- Has basic type checking methods and an empty constructor,
--- so all classes inheriting from this one are guaranteed to have a constructor.
moss.BaseClass = moss.create(BaseClass, {__name = "MossBaseClass"})
------------------------------------------------------------
-- Class commons compat:
-- (https://github.com/bartbes/Class-Commons)
---@diagnostic disable-next-line: undefined-global
if not common then
---@diagnostic disable-next-line: lowercase-global
common = {}
---@generic T
---@param name string? The name of the class
---@param table T The class definition
---@param ... table|fun(): table The classes to inherit from
---@return table|fun(...: unknown): T class The instancable class
function common.class(name, table, ...)
local base = moss.inherit(...)
-- Class commons inherits when creating the class, moss requires it to be done beforehand,
-- so the best solution is to just inject the fields as if they were inherited first:
for key, value in pairs(base) do
table[key] = table[key] == nil and value or table[key]
end
return moss.create(table, {__name = name})
end
---@generic T
---@param class T The class to instance
---@param ... unknown Arguments for the constructor
---@return T
function common.instance(class, ...)
return class(...)
end
-- This isn't in the standard but why not add it anyway:
---@param instance table The instance to check
---@param class table|fun(): table The class to check for
---@return boolean
function common.instanceof(instance, class)
return moss.is(instance, class)
end
end
------------------------------------------------------------
return moss