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

TradeSkillMaster / LibTSMClass / 22315371213

23 Feb 2026 04:35PM UTC coverage: 95.792%. Remained the same
22315371213

push

github

web-flow
Improving type annotations and allow for generic classes (#32)

478 of 499 relevant lines covered (95.79%)

27.23 hits per line

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

95.79
/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
56
---@field __name string
57
---@field __super Class?
58
---@field __isa fun(self, class: Class): boolean
59
---@field protected __closure fun(self, name: string): function
60
---@field __tostring fun(self): string
61
---@field __equals fun(self, other: any): boolean
62
---@field __dump fun(self)
63

64
---@alias ClassProperties
65
---|'"ABSTRACT"' # An abstract class cannot be directly instantiated
66

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

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

124
---Constructs a class from an existing table, preserving its keys.
125
---@generic T
126
---@param tbl table The table with existing keys to preserve
127
---@param class T The class to construct
128
---@param ... any Arguments to pass to the constructor
129
---@return T
130
function Lib.ConstructWithTable(tbl, class, ...)
1✔
131
        private.constructTbl = tbl
1✔
132
        local inst = class(...)
1✔
133
        assert(not private.constructTbl and inst == tbl, "Internal error")
1✔
134
        return inst
1✔
135
end
136

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

160

161

162
-- ============================================================================
163
-- Instance Metatable
164
-- ============================================================================
165

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

185
                -- Check if this key is an instance field first, since this is the most common case
186
                local res = instInfo.fields[key]
272✔
187
                if res ~= nil then
272✔
188
                        instInfo.currentClass = nil
84✔
189
                        return res
84✔
190
                end
191

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

211
                -- Reset the current class since we're not continuing the __super chain
212
                local class = instInfo.currentClass or instInfo.class
131✔
213
                instInfo.currentClass = nil
131✔
214

215
                -- Check if this is a static key
216
                local classInfo = private.classInfo[class]
131✔
217
                res = classInfo.static[key]
131✔
218
                if res ~= nil then
131✔
219
                        return res
72✔
220
                end
221

222
                -- Check if it's a static field in the superclass
223
                local superStaticRes = classInfo.superStatic[key]
59✔
224
                if superStaticRes then
59✔
225
                        res = superStaticRes.value
39✔
226
                        return res
39✔
227
                end
228

229
                -- Check if this field has a default value
230
                res = DEFAULT_INST_FIELDS[key]
20✔
231
                if res ~= nil then
20✔
232
                        return res
15✔
233
                end
234

235
                return nil
5✔
236
        end,
237
        __eq = function(self, other)
238
                if self.__class ~= other.__class then
5✔
239
                        return false
2✔
240
                end
241
                return self:__equals(other)
3✔
242
        end,
243
        __tostring = function(self)
244
                return self:__tostring()
4✔
245
        end,
246
        __metatable = false,
1✔
247
}
1✔
248

249

250

251
-- ============================================================================
252
-- Class Metatable
253
-- ============================================================================
254

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

499

500

501
-- ============================================================================
502
-- Extension Metatable
503
-- ============================================================================
504

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

545

546

547
-- ============================================================================
548
-- Helper Functions
549
-- ============================================================================
550

551
function private.ClassIsA(class, targetClass)
1✔
552
        while class do
4✔
553
                if class == targetClass then return true end
4✔
554
                class = class.__super
1✔
555
        end
556
end
557

558
function private.ClassExtend(class)
1✔
559
        local classInfo = private.classInfo[class]
1✔
560
        if not classInfo then
1✔
561
                error("__extend() must be called with `:` to pass the class", 2)
×
562
        elseif classInfo.subclassed then
1✔
563
                error("Can't add extension methods after a class is subclassed", 2)
×
564
        end
565
        classInfo.extended = true
1✔
566
        local extensionObj = setmetatable({}, private.EXTENSION_MT)
1✔
567
        private.extensionInfo[extensionObj] = {
1✔
568
                class = class,
1✔
569
        }
1✔
570
        return extensionObj
1✔
571
end
572

573
function private.InstMethodReturnHelper(class, instInfo, classInfo, ...)
1✔
574
        -- Reset methodClass and decrement inClassFunc now that the function returned
575
        instInfo.methodClass = class
122✔
576
        while classInfo do
319✔
577
                classInfo.inClassFunc = classInfo.inClassFunc - 1
197✔
578
                local superclass = classInfo.superclass
197✔
579
                classInfo = superclass and private.classInfo[superclass] or nil
197✔
580
        end
581
        return ...
122✔
582
end
583

584
function private.StaticFuncReturnHelper(classInfo, ...)
1✔
585
        -- Decrement inClassFunc now that the function returned
586
        while classInfo do
20✔
587
                classInfo.inClassFunc = classInfo.inClassFunc - 1
12✔
588
                local superclass = classInfo.superclass
12✔
589
                classInfo = superclass and private.classInfo[superclass] or nil
12✔
590
        end
591
        return ...
8✔
592
end
593

594
function private.InstIsA(inst, targetClass)
1✔
595
        return private.instInfo[inst].isClassLookup[targetClass]
2✔
596
end
597

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

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

672
function private.InstDump(inst, resultTbl, maxDepth, tableLookupFunc)
1✔
673
        local context = {
2✔
674
                resultTbl = resultTbl,
2✔
675
                maxDepth = maxDepth or 2,
2✔
676
                maxTableEntries = 100,
2✔
677
                tableLookupFunc = tableLookupFunc,
2✔
678
                tableRefs = {},
2✔
679
                depth = 0,
2✔
680
        }
681

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

708
        private.InstDumpVariable("self", inst, context, "")
2✔
709
end
710

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

799
function private.InstDumpLine(line, context)
1✔
800
        line = strrep("  ", context.depth)..line
36✔
801
        if context.resultTbl then
36✔
802
                tinsert(context.resultTbl, line)
18✔
803
        else
804
                print(line)
18✔
805
        end
806
end
807

808
function private.InstDumpKeyValue(key, value, context)
1✔
809
        key = tostring(key)
26✔
810
        if key == "" then
26✔
811
                key = "\"\""
2✔
812
        end
813
        if not context.resultTbl then
26✔
814
                key = "|cff88ccff"..key.."|r"
13✔
815
        end
816
        value = tostring(value)
26✔
817
        local line = format("%s = %s", key, value)
26✔
818
        private.InstDumpLine(line, context)
26✔
819
end
820

821

822

823
-- ============================================================================
824
-- Initialization Code
825
-- ============================================================================
826

827
do
828
        -- Register with LibStub
829
        local libStubTbl = LibStub:NewLibrary("LibTSMClass", MINOR_REVISION)
1✔
830
        if libStubTbl then
1✔
831
                for k, v in pairs(Lib) do
4✔
832
                        libStubTbl[k] = v
3✔
833
                end
834
        end
835
        -- Return the library and our private table for unit testing
836
        return {Lib, private}
1✔
837
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