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

lunarmodules / Penlight / 46279356752

29 Nov 2026 03:36PM UTC coverage: 83.225% (-7.1%) from 90.342%
46279356752

push

github

web-flow
Merge 0622ec222 into 44007e542

5125 of 6158 relevant lines covered (83.23%)

880.41 hits per line

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

83.33
/lua/pl/class.lua
1
--- Provides a reusable and convenient framework for creating classes in Lua.
2
-- Two possible notations:
3
--
4
--    B = class(A)
5
--    class.B(A)
6
--
7
-- The latter form creates a named class within the current environment. Note
8
-- that this implicitly brings in `pl.utils` as a dependency.
9
--
10
-- See the Guide for further @{01-introduction.md.Simplifying_Object_Oriented_Programming_in_Lua|discussion}
11
-- @module pl.class
12

13
local error, getmetatable, io, pairs, rawget, rawset, setmetatable, tostring, type =
14
    _G.error, _G.getmetatable, _G.io, _G.pairs, _G.rawget, _G.rawset, _G.setmetatable, _G.tostring, _G.type
×
15
local compat
16

17
-- this trickery is necessary to prevent the inheritance of 'super' and
18
-- the resulting recursive call problems.
19
local function call_ctor (c,obj,...)
20
    local init = rawget(c,'_init')
5,040 ✔
21
    local parent_with_init = rawget(c,'_parent_with_init')
5,040 ✔
22

23
    if parent_with_init then
5,040 ✔
24
        if not init then -- inheriting an init
1,638 ✔
25
            init = rawget(parent_with_init, '_init')
504 ✔
26
            parent_with_init = rawget(parent_with_init, '_parent_with_init')
504 ✔
27
        end
28
        if parent_with_init then -- super() points to one above wherever _init came from
1,638 ✔
29
            rawset(obj,'super',function(obj,...)
2,268 ✔
30
                call_ctor(parent_with_init,obj,...)
504 ✔
31
            end)
32
        end
33
    else
34
        -- Without this, calling super() where none exists will sometimes loop and stack overflow
35
        rawset(obj,'super',nil)
3,402 ✔
36
    end
37

38
    local res = init(obj,...)
5,040 ✔
39
    if parent_with_init then -- If this execution of call_ctor set a super, unset it
4,970 ✔
40
        rawset(obj,'super',nil)
1,106 ✔
41
    end
42

43
    return res
4,970 ✔
44
end
45

46
--- initializes an __instance__ upon creation.
47
-- @function class:_init
48
-- @param ... parameters passed to the constructor
49
-- @usage local Cat = class()
50
-- function Cat:_init(name)
51
--   --self:super(name)   -- call the ancestor initializer if needed
52
--   self.name = name
53
-- end
54
--
55
-- local pussycat = Cat("pussycat")
56
-- print(pussycat.name)  --> pussycat
57

58
--- checks whether an __instance__ is derived from some class.
59
-- Works the other way around as `class_of`. It has two ways of using;
60
-- 1) call with a class to check against, 2) call without params.
61
-- @function instance:is_a
62
-- @param some_class class to check against, or `nil` to return the class
63
-- @return `true` if `instance` is derived from `some_class`, or if `some_class == nil` then
64
-- it returns the class table of the instance
65
-- @usage local pussycat = Lion()  -- assuming Lion derives from Cat
66
-- if pussycat:is_a(Cat) then
67
--   -- it's true, it is a Lion, but also a Cat
68
-- end
69
--
70
-- if pussycat:is_a() == Lion then
71
--   -- It's true
72
-- end
73
local function is_a(self,klass)
74
    if klass == nil then
812 ✔
75
        -- no class provided, so return the class this instance is derived from
76
        return getmetatable(self)
14 ✔
77
    end
78
    local m = getmetatable(self)
798 ✔
79
    if not m then return false end --*can't be an object!
798 ✔
80
    while m do
952 ✔
81
        if m == klass then return true end
910 ✔
82
        m = rawget(m,'_base')
378 ✔
83
    end
84
    return false
42 ✔
85
end
86

87
--- checks whether an __instance__ is derived from some class.
88
-- Works the other way around as `is_a`.
89
-- @function some_class:class_of
90
-- @param some_instance instance to check against
91
-- @return `true` if `some_instance` is derived from `some_class`
92
-- @usage local pussycat = Lion()  -- assuming Lion derives from Cat
93
-- if Cat:class_of(pussycat) then
94
--   -- it's true
95
-- end
96
local function class_of(klass,obj)
97
    if type(klass) ~= 'table' or not rawget(klass,'is_a') then return false end
602 ✔
98
    return klass.is_a(obj,klass)
602 ✔
99
end
100

101
--- cast an object to another class.
102
-- It is not clever (or safe!) so use carefully.
103
-- @param some_instance the object to be changed
104
-- @function some_class:cast
105
local function cast (klass, obj)
106
    return setmetatable(obj,klass)
14 ✔
107
end
108

109

110
local function _class_tostring (obj)
111
    local mt = obj._class
×
112
    local name = rawget(mt,'_name')
×
113
    setmetatable(obj,nil)
×
114
    local str = tostring(obj)
×
115
    setmetatable(obj,mt)
×
116
    if name then str = name ..str:gsub('table','') end
×
117
    return str
×
118
end
119

120
local function tupdate(td,ts,dont_override)
121
    for k,v in pairs(ts) do
16,548 ✔
122
        if not dont_override or td[k] == nil then
15,330 ✔
123
            td[k] = v
15,288 ✔
124
        end
125
    end
126
end
127

128
local function _class(base,c_arg,c)
129
    -- the class `c` will be the metatable for all its objects,
130
    -- and they will look up their methods in it.
131
    local mt = {}   -- a metatable for the class to support __call and _handler
1,722 ✔
132
    -- can define class by passing it a plain table of methods
133
    local plain = type(base) == 'table' and not getmetatable(base)
1,722 ✔
134
    if plain then
1,722 ✔
135
        c = base
42 ✔
136
        base = c._base
42 ✔
137
    else
138
        c = c or {}
1,680 ✔
139
    end
140

141
    if type(base) == 'table' then
1,722 ✔
142
        -- our new class is a shallow copy of the base class!
143
        -- but be careful not to wipe out any methods we have been given at this point!
144
        tupdate(c,base,plain)
1,218 ✔
145
        c._base = base
1,218 ✔
146
        -- inherit the 'not found' handler, if present
147
        if rawget(c,'_handler') then mt.__index = c._handler end
1,218 ✔
148
    elseif base ~= nil then
504 ✔
149
        error("must derive from a table type",3)
×
150
    end
151

152
    c.__index = c
1,722 ✔
153
    setmetatable(c,mt)
1,722 ✔
154
    if not plain then
1,722 ✔
155
        if base and rawget(base,'_init') then c._parent_with_init = base end -- For super and inherited init
1,680 ✔
156
        c._init = nil
1,680 ✔
157
    end
158

159
    if base and rawget(base,'_class_init') then
1,722 ✔
160
        base._class_init(c,c_arg)
42 ✔
161
    end
162

163
    -- expose a ctor which can be called by <classname>(<args>)
164
    mt.__call = function(class_tbl,...)
165
        local obj
166
        if rawget(c,'_create') then obj = c._create(...) end
5,148 ✔
167
        if not obj then obj = {} end
4,564 ✔
168
        setmetatable(obj,c)
4,564 ✔
169

170
        if rawget(c,'_init') or rawget(c,'_parent_with_init') then -- constructor exists
4,564 ✔
171
            local res = call_ctor(c,obj,...)
4,536 ✔
172
            if res then -- _if_ a ctor returns a value, it becomes the object...
4,480 ✔
173
                obj = res
168 ✔
174
                setmetatable(obj,c)
168 ✔
175
            end
176
        end
177

178
        if base and rawget(base,'_post_init') then
4,508 ✔
179
            base._post_init(obj)
×
180
        end
181

182
        return obj
4,508 ✔
183
    end
184
    -- Call Class.catch to set a handler for methods/properties not found in the class!
185
    c.catch = function(self, handler)
186
        if type(self) == "function" then
42 ✔
187
            -- called using . instead of :
188
            handler = self
×
189
        end
190
        c._handler = handler
42 ✔
191
        mt.__index = handler
42 ✔
192
    end
193
    c.is_a = is_a
1,722 ✔
194
    c.class_of = class_of
1,722 ✔
195
    c.cast = cast
1,722 ✔
196
    c._class = c
1,722 ✔
197

198
    if not rawget(c,'__tostring') then
1,722 ✔
199
        c.__tostring = _class_tostring
490 ✔
200
    end
201

202
    return c
1,722 ✔
203
end
204

205
--- create a new class, derived from a given base class.
206
-- Supporting two class creation syntaxes:
207
-- either `Name = class(base)` or `class.Name(base)`.
208
-- The first form returns the class directly and does not set its `_name`.
209
-- The second form creates a variable `Name` in the current environment set
210
-- to the class, and also sets `_name`.
211
-- @function class
212
-- @param base optional base class
213
-- @param c_arg optional parameter to class constructor
214
-- @param c optional table to be used as class
215
local class
216
class = setmetatable({},{
×
217
    __call = function(fun,...)
218
        return _class(...)
1,624 ✔
219
    end,
220
    __index = function(tbl,key)
221
        if key == 'class' then
98 ✔
222
            io.stderr:write('require("pl.class").class is deprecated. Use require("pl.class")\n')
×
223
            return class
×
224
        end
225
        compat = compat or require 'pl.compat'
98 ✔
226
        local env = compat.getfenv(2)
98 ✔
227
        return function(...)
228
            local c = _class(...)
98 ✔
229
            c._name = key
98 ✔
230
            rawset(env,key,c)
98 ✔
231
            return c
98 ✔
232
        end
233
    end
234
})
235

236
class.properties = class()
×
237

238
function class.properties._class_init(klass)
×
239
    klass.__index = function(t,key)
240
        -- normal class lookup!
241
        local v = klass[key]
84 ✔
242
        if v then return v end
84 ✔
243
        -- is it a getter?
244
        v = rawget(klass,'get_'..key)
70 ✔
245
        if v then
70 ✔
246
            return v(t)
28 ✔
247
        end
248
        -- is it a field?
249
        return rawget(t,'_'..key)
42 ✔
250
    end
251
    klass.__newindex = function (t,key,value)
252
        -- if there's a setter, use that, otherwise directly set table
253
        local p = 'set_'..key
98 ✔
254
        local setter = klass[p]
98 ✔
255
        if setter then
98 ✔
256
            setter(t,value)
36 ✔
257
        else
258
            rawset(t,key,value)
70 ✔
259
        end
260
    end
261
end
262

263

264
return class
×
265

STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc