• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

TradeSkillMaster / LibTSMClass / 25682189851

11 May 2026 04:12PM UTC coverage: 95.808% (+0.02%) from 95.792%
25682189851

push

github

web-flow
Switch to wowlua-ls (#34)

2 of 2 new or added lines in 1 file covered. (100.0%)

17 existing lines in 1 file now uncovered.

480 of 501 relevant lines covered (95.81%)

27.17 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.81
/LibTSMClass.lua
1
--- LibTSMClass Library
2
-- Allows for OOP in lua through the implementation of classes. Many features of proper classes are supported including
3
-- inhertiance, polymorphism, and virtual methods.
4
-- @author TradeSkillMaster Team (admin@tradeskillmaster.com)
5
-- @license MIT
6

7
local MINOR_REVISION = 2
1✔
8
local Lib = {} ---@class LibTSMClass
1✔
9
local private = {
1✔
10
        classInfo = {},
1✔
11
        extensionInfo = {},
1✔
12
        instInfo = {},
1✔
13
        constructTbl = nil,
1✔
14
        tempTable = {},
1✔
15
}
16
-- Set the keys as weak so that class extensions and instances of classes can be GC'd (classes are never GC'd)
17
local WEAK_KEY_MT = { __mode = "k" }
1✔
18
setmetatable(private.extensionInfo, WEAK_KEY_MT)
1✔
19
setmetatable(private.instInfo, WEAK_KEY_MT)
1✔
20
local RESERVED_KEYS = {
1✔
21
        __super = true,
1✔
22
        __isa = true,
1✔
23
        __class = true,
1✔
24
        __name = true,
1✔
25
        __as = true,
1✔
26
        __static = true,
1✔
27
        __private = true,
1✔
28
        __protected = true,
1✔
29
        __abstract = true,
1✔
30
        __closure = true,
1✔
31
        __extend = true,
1✔
32
}
33
local DEFAULT_INST_FIELDS = {
1✔
34
        __init = function(self)
35
                -- Do nothing
36
        end,
37
        __tostring = function(self)
38
                return private.instInfo[self].str
1✔
39
        end,
40
        __equals = function(self, other)
41
                return rawequal(self, other)
×
42
        end,
43
        __dump = function(self)
44
                private.InstDump(self)
1✔
45
        end,
46
}
47
local DUMP_KEY_PATH_DELIM = "\001"
1✔
48

49

50

51
-- ============================================================================
52
-- Public Library Functions
53
-- ============================================================================
54

55
---@class Class<S>
56
---@overload fun(): Class
57
---@constructor __init
58
---@field __class Class
59
---@field __name string
60
---@field __super S
61
---@field __isa fun(self, class: Class): boolean
62
---@field protected __closure fun(self, name: string): function
63
---@field __tostring fun(self): string
64
---@field __equals fun(self, other: any): boolean
65
---@field __dump fun(self)
66
---@accessor __private private
67
---@accessor __protected protected
68
---@accessor __abstract protected
69
---@accessor __static
70

71
---@alias ClassProperties
72
---|'"ABSTRACT"' # An abstract class cannot be directly instantiated
73

74
---Defines a new class.
75
---@defclass T : P
76
---@generic T: Class<P>
77
---@generic P: Class
78
---@param name `T` The name of the class
79
---@param superclass? P The superclass
80
---@param ... ClassProperties Properties to define the class with
81
---@return T
82
function Lib.DefineClass(name, superclass, ...)
1✔
83
        if type(name) ~= "string" then
37✔
84
                error("Invalid class name: "..tostring(name), 2)
1✔
85
        end
86
        if superclass ~= nil and (type(superclass) ~= "table" or not private.classInfo[superclass]) then
36✔
87
                error("Invalid superclass: "..tostring(superclass), 2)
1✔
88
        end
89
        local abstract = false
35✔
90
        for i = 1, select('#', ...) do
39✔
91
                local modifier = select(i, ...)
5✔
92
                if modifier == "ABSTRACT" then
5✔
93
                        abstract = true
4✔
94
                else
95
                        error("Invalid modifier: "..tostring(modifier), 2)
1✔
96
                end
97
        end
98

99
        local class = setmetatable({}, private.CLASS_MT)
34✔
100
        private.classInfo[class] = {
34✔
101
                name = name,
34✔
102
                static = {},
34✔
103
                superStatic = {},
34✔
104
                superclass = superclass,
34✔
105
                abstract = abstract,
34✔
106
                referenceType = nil,
34✔
107
                subclassed = false,
34✔
108
                extended = false,
34✔
109
                methodProperties = nil, -- Set as needed
34✔
110
                inClassFunc = 0,
34✔
111
                instances = setmetatable({}, WEAK_KEY_MT),
34✔
112
        }
34✔
113
        while superclass do
56✔
114
                local superclassInfo = private.classInfo[superclass]
22✔
115
                for key, value in pairs(superclassInfo.static) do
95✔
116
                        if not private.classInfo[class].superStatic[key] then
73✔
117
                                private.classInfo[class].superStatic[key] = {
67✔
118
                                        class = superclass,
67✔
119
                                        value = value,
67✔
120
                                        properties = superclassInfo.methodProperties and superclassInfo.methodProperties[key] or nil,
67✔
121
                                }
67✔
122
                        end
123
                end
124
                if superclassInfo.extended then
22✔
UNCOV
125
                        error("Cannot subclass a class after it's extended", 2)
×
126
                end
127
                superclassInfo.subclassed = true
22✔
128
                superclass = superclass.__super
22✔
129
        end
130
        return class
34✔
131
end
132

133
---Constructs a class from an existing table, preserving its keys.
134
---@generic T
135
---@param tbl table The table with existing keys to preserve
136
---@param class T The class to construct
137
---@param ... any Arguments to pass to the constructor
138
---@return T
139
function Lib.ConstructWithTable(tbl, class, ...)
1✔
140
        private.constructTbl = tbl
1✔
141
        local inst = class(...)
1✔
142
        assert(not private.constructTbl and inst == tbl, "Internal error")
1✔
143
        return inst
1✔
144
end
145

146
---Gets instance properties from an instance string for debugging purposes.
147
---@param instStr string The string representation of the instance
148
---@param maxDepth number The maximum depth to recurse into tables
149
---@param tableLookupFunc? fun(tbl: table): string? A lookup function which is used to get debug information for an unknown table
150
---@return string? @The properties dumped as a multiline string
151
function Lib.GetDebugInfo(instStr, maxDepth, tableLookupFunc)
1✔
152
        local inst = nil
153
        for obj, info in pairs(private.instInfo) do
5✔
154
                if info.str == instStr then
4✔
155
                        inst = obj
1✔
156
                        break
1✔
157
                end
158
        end
159
        if not inst then
2✔
160
                return nil
1✔
161
        end
162
        assert(not next(private.tempTable))
1✔
163
        private.InstDump(inst, private.tempTable, maxDepth, tableLookupFunc)
1✔
164
        local result = table.concat(private.tempTable, "\n")
1✔
165
        wipe(private.tempTable)
1✔
166
        return result
1✔
167
end
168

169

170

171
-- ============================================================================
172
-- Instance Metatable
173
-- ============================================================================
174

175
private.INST_MT = {
1✔
176
        __newindex = function(self, key, value)
177
                if RESERVED_KEYS[key] then
50✔
178
                        error("Can't set reserved key: "..tostring(key), 2)
1✔
179
                end
180
                if private.classInfo[self.__class].static[key] ~= nil then
49✔
181
                        private.classInfo[self.__class].static[key] = value
2✔
182
                elseif not private.instInfo[self].hasSuperclass then
47✔
183
                        -- We just set this directly on the instance table for better performance
184
                        rawset(self, key, value)
20✔
185
                else
186
                        private.instInfo[self].fields[key] = value
27✔
187
                end
188
        end,
189
        __index = function(self, key)
190
                -- This method is super optimized since it's used for every class instance access, meaning function calls and
191
                -- table lookup is kept to an absolute minimum, at the expense of readability and code reuse.
192
                local instInfo = private.instInfo[self]
272✔
193

194
                -- Check if this key is an instance field first, since this is the most common case
195
                local res = instInfo.fields[key]
272✔
196
                if res ~= nil then
272✔
197
                        instInfo.currentClass = nil
84✔
198
                        return res
84✔
199
                end
200

201
                -- Check if it's a special field / method
202
                if key == "__super" then
188✔
203
                        if not instInfo.hasSuperclass then
29✔
204
                                error("The class of this instance has no superclass", 2)
2✔
205
                        end
206
                        -- The class of the current class method we are in, or nil if we're not in a class method.
207
                        local methodClass = instInfo.methodClass
27✔
208
                        -- We can only access the superclass within a class method and will use the class which defined that method
209
                        -- as the base class to jump to the superclass of, regardless of what class the instance actually is.
210
                        if not methodClass then
27✔
211
                                error("The superclass can only be referenced within a class method", 2)
1✔
212
                        end
213
                        return private.InstAs(self, private.classInfo[instInfo.currentClass or methodClass].superclass)
26✔
214
                elseif key == "__as" then
159✔
215
                        return private.InstAs
17✔
216
                elseif key == "__closure" then
142✔
217
                        return private.InstClosure
11✔
218
                end
219

220
                -- Reset the current class since we're not continuing the __super chain
221
                local class = instInfo.currentClass or instInfo.class
131✔
222
                instInfo.currentClass = nil
131✔
223

224
                -- Check if this is a static key
225
                local classInfo = private.classInfo[class]
131✔
226
                res = classInfo.static[key]
131✔
227
                if res ~= nil then
131✔
228
                        return res
72✔
229
                end
230

231
                -- Check if it's a static field in the superclass
232
                local superStaticRes = classInfo.superStatic[key]
59✔
233
                if superStaticRes then
59✔
234
                        res = superStaticRes.value
39✔
235
                        return res
39✔
236
                end
237

238
                -- Check if this field has a default value
239
                res = DEFAULT_INST_FIELDS[key]
20✔
240
                if res ~= nil then
20✔
241
                        return res
15✔
242
                end
243

244
                return nil
5✔
245
        end,
246
        __eq = function(self, other)
247
                if self.__class ~= other.__class then
5✔
248
                        return false
2✔
249
                end
250
                return self:__equals(other)
3✔
251
        end,
252
        __tostring = function(self)
253
                return self:__tostring()
4✔
254
        end,
255
        __metatable = false,
1✔
256
}
1✔
257

258

259

260
-- ============================================================================
261
-- Class Metatable
262
-- ============================================================================
263

264
private.CLASS_MT = {
1✔
265
        __newindex = function(self, key, value)
266
                if type(key) ~= "string" then
106✔
267
                        error("Can't index class with non-string key", 2)
1✔
268
                end
269
                local classInfo = private.classInfo[self]
105✔
270
                if classInfo.subclassed or classInfo.extended then
105✔
271
                        error("Can't modify classes after they are subclassed or extended", 2)
1✔
272
                end
273
                if classInfo.static[key] ~= nil then
104✔
274
                        error("Can't modify or override static members", 2)
1✔
275
                end
276
                if RESERVED_KEYS[key] then
103✔
277
                        error("Reserved word: "..key, 2)
1✔
278
                end
279
                local isFunction = type(value) == "function"
102✔
280
                local methodProperty = classInfo.referenceType
102✔
281
                local isStatic = methodProperty == "STATIC"
102✔
282
                classInfo.referenceType = nil
102✔
283
                if isFunction and isStatic then
102✔
284
                        -- We are defining a static class function
285
                        classInfo.methodProperties = classInfo.methodProperties or {}
8✔
286
                        classInfo.methodProperties[key] = "STATIC"
8✔
287
                        -- We wrap static methods so that we can allow private or protected access within them
288
                        classInfo.static[key] = function(...)
8✔
289
                                local tempMethodClassInfo = classInfo
8✔
290
                                while tempMethodClassInfo do
20✔
291
                                        tempMethodClassInfo.inClassFunc = tempMethodClassInfo.inClassFunc + 1
12✔
292
                                        local superclass = tempMethodClassInfo.superclass
12✔
293
                                        tempMethodClassInfo = superclass and private.classInfo[superclass] or nil
12✔
294
                                end
295
                                return private.StaticFuncReturnHelper(classInfo, value(...))
8✔
296
                        end
297
                elseif isFunction and not isStatic then
94✔
298
                        -- We are defining a class method
299
                        local superclass = classInfo.superclass
89✔
300
                        while superclass do
121✔
301
                                local superclassInfo = private.classInfo[superclass]
57✔
302
                                local superclassMethodProperty = superclassInfo.methodProperties and superclassInfo.methodProperties[key] or nil
57✔
303
                                if superclassInfo.static[key] ~= nil or superclassMethodProperty ~= nil then
57✔
304
                                        if superclassInfo.static[key] ~= nil and type(superclassInfo.static[key]) ~= "function" then
25✔
305
                                                error(format("Attempting to override non-method superclass property (%s) with method", key), 2)
1✔
306
                                        end
307
                                        if superclassMethodProperty == nil then
24✔
308
                                                -- Can only override public methods with public methods
309
                                                if methodProperty ~= nil then
11✔
310
                                                        error(format("Overriding a public superclass method (%s) can only be done with a public method", key), 2)
1✔
311
                                                end
312
                                        elseif superclassMethodProperty == "ABSTRACT" then
13✔
313
                                                -- Can only override abstract methods with protected methods
314
                                                if methodProperty ~= "PROTECTED" then
5✔
315
                                                        error(format("Overriding an abstract superclass method (%s) can only be done with a protected method", key), 2)
1✔
316
                                                end
317
                                        elseif superclassMethodProperty == "PROTECTED" then
8✔
318
                                                -- Can only override protected methods with protected methods
319
                                                if methodProperty ~= "PROTECTED" then
5✔
320
                                                        error(format("Overriding a protected superclass method (%s) can only be done with a protected method", key), 2)
2✔
321
                                                end
322
                                        elseif superclassMethodProperty == "STATIC" then
3✔
323
                                                -- Can't override static properties with methods
324
                                                error(format("Can't override static superclass property (%s) with method", key), 2)
1✔
325
                                        elseif superclassMethodProperty == "PRIVATE" then
2✔
326
                                                -- Can't override private methods
327
                                                error(format("Can't override private superclass method (%s)", key), 2)
2✔
328
                                        else
329
                                                -- luacov: disable
330
                                                -- Should never get here
331
                                                error("Unexpected superclassMethodProperty: "..tostring(superclassMethodProperty))
332
                                                -- luacov: enable
333
                                        end
334
                                        -- Just need to go up the superclass tree until we find the first one which references this key
335
                                        break
336
                                end
337
                                superclass = superclassInfo.superclass
32✔
338
                        end
339
                        local isPrivate, isProtected = false, false
81✔
340
                        if methodProperty ~= nil then
81✔
341
                                classInfo.methodProperties = classInfo.methodProperties or {}
23✔
342
                                classInfo.methodProperties[key] = methodProperty
23✔
343
                                if methodProperty == "PRIVATE" then
23✔
344
                                        isPrivate = true
8✔
345
                                elseif methodProperty == "PROTECTED" then
15✔
346
                                        isProtected = true
12✔
347
                                elseif methodProperty == "ABSTRACT" then
3✔
348
                                        -- Just need to set the property
349
                                        return
3✔
350
                                else
351
                                        -- luacov: disable
352
                                        -- Should never get here
353
                                        error("Unknown method property: "..tostring(methodProperty))
354
                                        -- luacov: enable
355
                                end
356
                        end
357
                        -- We wrap class methods so that within them, the instance appears to be of the defining class
358
                        classInfo.static[key] = function(inst, ...)
78✔
359
                                local instInfo = private.instInfo[inst]
150✔
360
                                if not instInfo or not instInfo.isClassLookup[self] then
150✔
361
                                        error(format("Attempt to call class method on non-object (%s)", tostring(inst)), 2)
1✔
362
                                end
363
                                local prevMethodClass = instInfo.methodClass
149✔
364
                                if isPrivate and prevMethodClass ~= self and (prevMethodClass ~= nil or classInfo.inClassFunc == 0) then
149✔
365
                                        error(format("Attempting to call private method (%s) from outside of class", key), 2)
6✔
366
                                end
367
                                if isProtected and prevMethodClass == nil and classInfo.inClassFunc == 0 then
143✔
368
                                        -- Check if we're in a super class func
369
                                        error(format("Attempting to call protected method (%s) from outside of class", key), 2)
5✔
370
                                end
371
                                instInfo.methodClass = self
138✔
372
                                local tempMethodClassInfo = classInfo
138✔
373
                                while tempMethodClassInfo do
357✔
374
                                        tempMethodClassInfo.inClassFunc = tempMethodClassInfo.inClassFunc + 1
219✔
375
                                        local tempSuperclass = tempMethodClassInfo.superclass
219✔
376
                                        tempMethodClassInfo = tempSuperclass and private.classInfo[tempSuperclass] or nil
219✔
377
                                end
378
                                return private.InstMethodReturnHelper(prevMethodClass, instInfo, classInfo, value(inst, ...))
138✔
379
                        end
380
                elseif not isFunction then
5✔
381
                        -- We are defining a static property (shouldn't be explicitly marked as static)
382
                        if isStatic then
5✔
383
                                error("Unnecessary __static for non-function class property", 2)
1✔
384
                        end
385
                        classInfo.static[key] = value
4✔
386
                end
387
        end,
388
        __index = function(self, key)
389
                local classInfo = private.classInfo[self]
74✔
390
                if classInfo.referenceType ~= nil then
74✔
391
                        error("Can't index into property table", 2)
1✔
392
                end
393
                if key == "__isa" then
73✔
394
                        return private.ClassIsA
3✔
395
                elseif key == "__name" then
70✔
396
                        return classInfo.name
1✔
397
                elseif key == "__super" then
69✔
398
                        return classInfo.superclass
23✔
399
                elseif key == "__static" then
46✔
400
                        classInfo.referenceType = "STATIC"
9✔
401
                        return self
9✔
402
                elseif key == "__private" then
37✔
403
                        classInfo.referenceType = "PRIVATE"
9✔
404
                        return self
9✔
405
                elseif key == "__protected" then
28✔
406
                        classInfo.referenceType = "PROTECTED"
13✔
407
                        return self
13✔
408
                elseif key == "__abstract" then
15✔
409
                        if not classInfo.abstract then
4✔
410
                                error("Can only define abstract methods on abstract classes", 2)
1✔
411
                        end
412
                        classInfo.referenceType = "ABSTRACT"
3✔
413
                        return self
3✔
414
                elseif key == "__extend" then
11✔
415
                        return private.ClassExtend
1✔
416
                elseif classInfo.static[key] ~= nil then
10✔
417
                        return classInfo.static[key]
8✔
418
                elseif classInfo.superStatic[key] then
2✔
419
                        return classInfo.superStatic[key].value
1✔
420
                end
421
                error(format("Invalid static class key (%s)", tostring(key)), 2)
1✔
422
        end,
423
        __tostring = function(self)
424
                return "class:"..private.classInfo[self].name
11✔
425
        end,
426
        __call = function(self, ...)
427
                if private.classInfo[self].abstract then
42✔
428
                        error("Attempting to instantiate an abstract class", 2)
1✔
429
                end
430
                -- Create a new instance of this class
431
                local inst = private.constructTbl or {}
41✔
432
                local instStr = strmatch(tostring(inst), "table:[^1-9a-fA-F]*([0-9a-fA-F]+)")
41✔
433
                setmetatable(inst, private.INST_MT)
41✔
434
                local classInfo = private.classInfo[self]
41✔
435
                local hasSuperclass = classInfo.superclass and true or false
41✔
436
                private.instInfo[inst] = {
41✔
437
                        class = self,
41✔
438
                        fields = {
41✔
439
                                __class = self,
41✔
440
                                __isa = private.InstIsA,
41✔
441
                        },
41✔
442
                        str = classInfo.name..":"..instStr,
41✔
443
                        isClassLookup = {},
41✔
444
                        hasSuperclass = hasSuperclass,
41✔
445
                        currentClass = nil,
41✔
446
                        closures = {},
41✔
447
                }
41✔
448
                classInfo.instances[inst] = true
41✔
449
                if not hasSuperclass then
41✔
450
                        -- Set the static members directly on this object for better performance
451
                        for key, value in pairs(classInfo.static) do
65✔
452
                                rawset(inst, key, value)
46✔
453
                        end
454
                end
455
                -- Check that all the abstract methods have been defined
456
                assert(not next(private.tempTable))
41✔
457
                local c = self
41✔
458
                while c do
110✔
459
                        private.instInfo[inst].isClassLookup[c] = true
69✔
460
                        c = private.classInfo[c].superclass
69✔
461
                        if c and private.classInfo[c] and private.classInfo[c].methodProperties then
69✔
462
                                for methodName, property in pairs(private.classInfo[c].methodProperties) do
61✔
463
                                        if property == "ABSTRACT" then
40✔
464
                                                private.tempTable[methodName] = true
9✔
465
                                        end
466
                                end
467
                        end
468
                end
469
                for methodName in pairs(private.tempTable) do
48✔
470
                        if type(classInfo.static[methodName]) ~= "function" then
9✔
471
                                -- Check the superclasses
472
                                local found = false
4✔
473
                                local c2 = self
4✔
474
                                while c2 and not found do
11✔
475
                                        c2 = private.classInfo[c2].superclass
7✔
476
                                        if c2 and private.classInfo[c2] and private.classInfo[c2].static[methodName] then
7✔
477
                                                found = true
2✔
478
                                        end
479
                                end
480
                                if not found then
4✔
481
                                        wipe(private.tempTable)
2✔
482
                                        error("Missing abstract method: "..tostring(methodName), 2)
2✔
483
                                end
484
                        end
485
                end
486
                wipe(private.tempTable)
39✔
487
                if private.constructTbl then
39✔
488
                        -- Re-set all the object attributes through the proper metamethod
489
                        assert(not next(private.tempTable))
1✔
490
                        for k, v in pairs(inst) do
5✔
491
                                private.tempTable[k] = v
4✔
492
                        end
493
                        for k, v in pairs(private.tempTable) do
5✔
494
                                rawset(inst, k, nil)
4✔
495
                                inst[k] = v
4✔
496
                        end
497
                        wipe(private.tempTable)
1✔
498
                        private.constructTbl = nil
1✔
499
                end
500
                if select("#", inst:__init(...)) > 0 then
39✔
501
                        error("__init(...) must not return any values", 2)
1✔
502
                end
503
                return inst
34✔
504
        end,
505
        __metatable = false,
1✔
506
}
1✔
507

508

509

510
-- ============================================================================
511
-- Extension Metatable
512
-- ============================================================================
513

514
private.EXTENSION_MT = {
1✔
515
        __newindex = function(self, key, value)
516
                if type(key) ~= "string" then
2✔
UNCOV
517
                        error("Can't index class extension with non-string key", 2)
×
518
                elseif type(value) ~= "function" then
2✔
UNCOV
519
                        error("Can only add class methods via class extension", 2)
×
520
                elseif RESERVED_KEYS[key] then
2✔
521
                        error("Reserved word: "..key, 2)
×
522
                end
523
                local class = private.extensionInfo[self].class
2✔
524
                local classInfo = private.classInfo[class]
2✔
525
                if classInfo.subclassed then
2✔
UNCOV
526
                        error("Can't add extension methods after a class is subclassed", 2)
×
527
                end
528
                local testClass = class
2✔
529
                while testClass do
4✔
530
                        local testClassInfo = private.classInfo[testClass]
2✔
531
                        if testClassInfo.static[key] ~= nil then
2✔
UNCOV
532
                                error("Can't modify or override class members from extension", 2)
×
533
                        end
534
                        testClass = testClassInfo.superclass
2✔
535
                end
536
                -- Don't need to wrap extension methods because they are treated as if they are outside
537
                -- the class as far as access restrictions are concerned and don't need to worry about
538
                -- virtual methods since extensions only support non-subclassed classes.
539
                classInfo.static[key] = value
2✔
540
                -- Add the new method directly to all previously-created instances
541
                for inst in pairs(classInfo.instances) do
5✔
542
                        rawset(inst, key, value)
3✔
543
                end
544
        end,
545
        __index = function()
UNCOV
546
                error("Extension objects are write-only", 2)
×
547
        end,
548
        __tostring = function(self)
UNCOV
549
                return "classExtension:"..private.classInfo[private.extensionInfo[self].class].name
×
550
        end,
551
        __metatable = false,
1✔
552
}
1✔
553

554

555

556
-- ============================================================================
557
-- Helper Functions
558
-- ============================================================================
559

560
function private.ClassIsA(class, targetClass)
1✔
561
        while class do
4✔
562
                if class == targetClass then return true end
4✔
563
                class = class.__super
1✔
564
        end
565
end
566

567
function private.ClassExtend(class)
1✔
568
        local classInfo = private.classInfo[class]
1✔
569
        if not classInfo then
1✔
UNCOV
570
                error("__extend() must be called with `:` to pass the class", 2)
×
571
        elseif classInfo.subclassed then
1✔
UNCOV
572
                error("Can't add extension methods after a class is subclassed", 2)
×
573
        end
574
        classInfo.extended = true
1✔
575
        local extensionObj = setmetatable({}, private.EXTENSION_MT)
1✔
576
        private.extensionInfo[extensionObj] = {
1✔
577
                class = class,
1✔
578
        }
1✔
579
        return extensionObj
1✔
580
end
581

582
function private.InstMethodReturnHelper(class, instInfo, classInfo, ...)
1✔
583
        -- Reset methodClass and decrement inClassFunc now that the function returned
584
        instInfo.methodClass = class
122✔
585
        while classInfo do
319✔
586
                classInfo.inClassFunc = classInfo.inClassFunc - 1
197✔
587
                local superclass = classInfo.superclass
197✔
588
                classInfo = superclass and private.classInfo[superclass] or nil
197✔
589
        end
590
        return ...
122✔
591
end
592

593
function private.StaticFuncReturnHelper(classInfo, ...)
1✔
594
        -- Decrement inClassFunc now that the function returned
595
        while classInfo do
20✔
596
                classInfo.inClassFunc = classInfo.inClassFunc - 1
12✔
597
                local superclass = classInfo.superclass
12✔
598
                classInfo = superclass and private.classInfo[superclass] or nil
12✔
599
        end
600
        return ...
8✔
601
end
602

603
function private.InstIsA(inst, targetClass)
1✔
604
        return private.instInfo[inst].isClassLookup[targetClass]
2✔
605
end
606

607
function private.InstAs(inst, targetClass)
1✔
608
        local instInfo = private.instInfo[inst]
43✔
609
        -- Clear currentClass while we perform our checks so we can better recover from errors
610
        instInfo.currentClass = nil
43✔
611
        if not targetClass then
43✔
612
                error(format("Requested class does not exist"), 2)
4✔
613
        elseif not instInfo.isClassLookup[targetClass] then
39✔
614
                error(format("Object is not an instance of the requested class (%s)", tostring(targetClass)), 2)
3✔
615
        end
616
        -- For classes with no superclass, we don't go through the __index metamethod, so can't use __as
617
        if not instInfo.hasSuperclass then
36✔
618
                error("The class of this instance has no superclass", 2)
1✔
619
        end
620
        -- We can only access the superclass within a class method.
621
        if not instInfo.methodClass then
35✔
622
                error("The superclass can only be referenced within a class method", 2)
1✔
623
        end
624
        instInfo.currentClass = targetClass
34✔
625
        return inst
34✔
626
end
627

628
function private.InstClosure(inst, methodName)
1✔
629
        local instInfo = private.instInfo[inst]
11✔
630
        local methodClass = instInfo.methodClass
11✔
631
        if not methodClass then
11✔
632
                error("Closures can only be created within a class method", 2)
1✔
633
        elseif instInfo.currentClass then
10✔
634
                error("Cannot create closure as superclass", 2)
1✔
635
        end
636
        -- Check for this method on the class
637
        local classInfo = private.classInfo[instInfo.class]
9✔
638
        local methodFunc = classInfo.static[methodName]
9✔
639
        if methodFunc then
9✔
640
                -- If this method is private, make sure we're within the class
641
                if classInfo.methodProperties and classInfo.methodProperties[methodName] == "PRIVATE" and instInfo.class ~= methodClass then
5✔
642
                        error("Attempt to create closure for private virtual method", 2)
1✔
643
                end
644
        else
645
                -- Check for this method on the superclass
646
                local superInfo = classInfo.superStatic[methodName]
4✔
647
                if superInfo then
4✔
648
                        if superInfo.properties == "PRIVATE" and not private.classInfo[methodClass].static[methodName] then
3✔
649
                                error("Attempt to create closure for private superclass method", 2)
1✔
650
                        end
651
                        methodFunc = superInfo.value
2✔
652
                end
653
        end
654
        if type(methodFunc) ~= "function" then
7✔
655
                error("Attempt to create closure for non-method field", 2)
1✔
656
        end
657
        local methodClassInfo = private.classInfo[methodClass]
6✔
658
        local cacheKey = tostring(methodClass).."."..methodName
6✔
659
        if not instInfo.closures[cacheKey] then
6✔
660
                instInfo.closures[cacheKey] = function(...)
6✔
661
                        if instInfo.methodClass == methodClass then
8✔
662
                                -- We're already within a method of the class, so just call the method normally
663
                                return methodFunc(inst, ...)
5✔
664
                        else
665
                                -- Pretend we are within the class which created the closure
666
                                local prevClass = instInfo.methodClass
3✔
667
                                instInfo.methodClass = methodClass
3✔
668
                                local tempMethodClassInfo = methodClassInfo
3✔
669
                                while tempMethodClassInfo do
9✔
670
                                        tempMethodClassInfo.inClassFunc = tempMethodClassInfo.inClassFunc + 1
6✔
671
                                        local superclass = tempMethodClassInfo.superclass
6✔
672
                                        tempMethodClassInfo = superclass and private.classInfo[superclass] or nil
6✔
673
                                end
674
                                return private.InstMethodReturnHelper(prevClass, instInfo, methodClassInfo, methodFunc(inst, ...))
3✔
675
                        end
676
                end
677
        end
678
        return instInfo.closures[cacheKey]
6✔
679
end
680

681
function private.InstDump(inst, resultTbl, maxDepth, tableLookupFunc)
1✔
682
        local context = {
2✔
683
                resultTbl = resultTbl,
2✔
684
                maxDepth = maxDepth or 2,
2✔
685
                maxTableEntries = 100,
2✔
686
                tableLookupFunc = tableLookupFunc,
2✔
687
                tableRefs = {},
2✔
688
                depth = 0,
2✔
689
        }
690

691
        -- Build up our table references via a breadth-first search
692
        local bfsQueueKeyPath = {""}
2✔
693
        local bfsQueueDepth = {0}
2✔
694
        local bfsQueueValue = {inst}
2✔
695
        while #bfsQueueKeyPath > 0 do
18✔
696
                local keyPath = tremove(bfsQueueKeyPath, 1)
16✔
697
                local depth = tremove(bfsQueueDepth, 1)
16✔
698
                local value = tremove(bfsQueueValue, 1)
16✔
699
                if not context.tableRefs[value] then
16✔
700
                        context.tableRefs[value] = keyPath
14✔
701
                        if depth <= context.maxDepth and not private.classInfo[value] then
14✔
702
                                if private.instInfo[value] then
10✔
703
                                        local instInfo = private.instInfo[value]
2✔
704
                                        value = instInfo.hasSuperclass and instInfo.fields or value
2✔
705
                                end
706
                                for k, v in pairs(value) do
38✔
707
                                        if type(v) == "table" and (type(k) == "string" or type(k) == "number") and not strfind(k, DUMP_KEY_PATH_DELIM, nil, true) then
28✔
708
                                                tinsert(bfsQueueKeyPath, keyPath..DUMP_KEY_PATH_DELIM..k)
14✔
709
                                                tinsert(bfsQueueDepth, depth + 1)
14✔
710
                                                tinsert(bfsQueueValue, v)
14✔
711
                                        end
712
                                end
713
                        end
714
                end
715
        end
716

717
        private.InstDumpVariable("self", inst, context, "")
2✔
718
end
719

720
function private.InstDumpVariable(key, value, context, strKeyPath)
1✔
721
        if strfind(key, DUMP_KEY_PATH_DELIM, nil, true) then
30✔
722
                -- Ignore keys with our deliminator in them
UNCOV
723
                return
×
724
        end
725
        if type(value) == "table" and private.classInfo[value] then
30✔
726
                -- This is a class
727
                private.InstDumpKeyValue(key, "\""..tostring(value).."\"", context)
2✔
728
        elseif type(value) == "table" then
28✔
729
                local refKeyPath = context.tableRefs[value]
14✔
730
                if not refKeyPath then
14✔
UNCOV
731
                        return
×
732
                end
733
                if refKeyPath ~= strKeyPath then
14✔
734
                        local refValue = "\"REF{"..gsub(refKeyPath, DUMP_KEY_PATH_DELIM, ".").."}\""
2✔
735
                        private.InstDumpKeyValue(key, refValue, context)
2✔
736
                elseif private.instInfo[value] then
12✔
737
                        -- This is an instance of a class
738
                        if context.depth <= context.maxDepth then
2✔
739
                                -- Recurse into the class
740
                                local instInfo = private.instInfo[value]
2✔
741
                                local tbl = instInfo.hasSuperclass and instInfo.fields or value
2✔
742
                                private.InstDumpLine(key.." = <"..instInfo.str.."> {", context)
2✔
743
                                context.depth = context.depth + 1
2✔
744
                                for key2, value2 in pairs(tbl) do
16✔
745
                                        if type(key2) == "string" or type(key2) == "number" or type(key2) == "boolean" then
14✔
746
                                                key2 = tostring(key2)
14✔
747
                                                private.InstDumpVariable(key2, value2, context, strKeyPath..DUMP_KEY_PATH_DELIM..key2)
14✔
748
                                        end
749
                                end
750
                                context.depth = context.depth - 1
2✔
751
                                private.InstDumpLine("}", context)
2✔
752
                        else
UNCOV
753
                                private.InstDumpKeyValue(key, "\""..tostring(value).."\"", context)
×
754
                        end
755
                else
756
                        local isEmpty = true
10✔
757
                        for _, value2 in pairs(value) do
10✔
758
                                local valueType = type(value2)
8✔
759
                                if valueType == "string" or valueType == "number" or valueType == "boolean" or valueType == "table" then
8✔
760
                                        isEmpty = false
8✔
761
                                        break
8✔
762
                                end
763
                        end
764
                        if isEmpty then
10✔
765
                                local info = context.tableLookupFunc and context.tableLookupFunc(value) or nil
2✔
766
                                if info and context.depth <= context.maxDepth then
2✔
767
                                        -- Display the table values
UNCOV
768
                                        private.InstDumpKeyValue(key, "{", context)
×
UNCOV
769
                                        context.depth = context.depth + 1
×
UNCOV
770
                                        for _, line in ipairs({strsplit("\n", info)}) do
×
UNCOV
771
                                                private.InstDumpLine(line, context)
×
772
                                        end
773
                                        context.depth = context.depth - 1
×
774
                                        private.InstDumpLine("}", context)
×
775
                                elseif info then
2✔
UNCOV
776
                                        private.InstDumpKeyValue(key, "{ ... }", context)
×
777
                                else
778
                                        private.InstDumpKeyValue(key, "{}", context)
2✔
779
                                end
780
                        else
781
                                if context.depth <= context.maxDepth then
8✔
782
                                        -- Recurse into the table
783
                                        private.InstDumpKeyValue(key, "{", context)
6✔
784
                                        context.depth = context.depth + 1
6✔
785
                                        local numTableEntries = 0
6✔
786
                                        for key2, value2 in pairs(value) do
20✔
787
                                                if numTableEntries >= context.maxTableEntries then
14✔
788
                                                        break
789
                                                end
790
                                                if type(key2) == "string" or type(key2) == "number" or type(key2) == "boolean" then
14✔
791
                                                        numTableEntries = numTableEntries + 1
14✔
792
                                                        key2 = tostring(key2)
14✔
793
                                                        private.InstDumpVariable(key2, value2, context, strKeyPath..DUMP_KEY_PATH_DELIM..key2)
14✔
794
                                                end
795
                                        end
796
                                        context.depth = context.depth - 1
6✔
797
                                        private.InstDumpLine("}", context)
6✔
798
                                else
799
                                        private.InstDumpKeyValue(key, "{ ... }", context)
2✔
800
                                end
801
                        end
802
                end
803
        elseif type(value) == "string" then
14✔
804
                private.InstDumpKeyValue(key, "\""..value.."\"", context)
2✔
805
        elseif type(value) == "number" or type(value) == "boolean" then
12✔
806
                private.InstDumpKeyValue(key, value, context)
10✔
807
        end
808
end
809

810
function private.InstDumpLine(line, context)
1✔
811
        line = strrep("  ", context.depth)..line
36✔
812
        if context.resultTbl then
36✔
813
                tinsert(context.resultTbl, line)
18✔
814
        else
815
                print(line)
18✔
816
        end
817
end
818

819
function private.InstDumpKeyValue(key, value, context)
1✔
820
        key = tostring(key)
26✔
821
        if key == "" then
26✔
822
                key = "\"\""
2✔
823
        end
824
        if not context.resultTbl then
26✔
825
                key = "|cff88ccff"..key.."|r"
13✔
826
        end
827
        value = tostring(value)
26✔
828
        local line = format("%s = %s", key, value)
26✔
829
        private.InstDumpLine(line, context)
26✔
830
end
831

832

833

834
-- ============================================================================
835
-- Initialization Code
836
-- ============================================================================
837

838
do
839
        -- Register with LibStub
840
        local libStubTbl = LibStub:NewLibrary("LibTSMClass", MINOR_REVISION)
1✔
841
        if libStubTbl then
1✔
842
                for k, v in pairs(Lib) do
4✔
843
                        libStubTbl[k] = v
3✔
844
                end
845
        end
846
        -- Return the library and our private table for unit testing
847
        return {Lib, private}
1✔
848
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc