Compare commits

...

3 Commits

Author SHA1 Message Date
Lorenzo Cogotti 99fde174df [meta] Improve isinstance().
* Handle nil
* Use rawequal() instead of plain ==
2 years ago
Lorenzo Cogotti 62d22658b3 Merge branch 'master' of https://gitea.it/1414codeforge/gear 2 years ago
Lorenzo Cogotti 75d60b98f5 [meta] Use rawequal() to compare tables in isinstance().
Ensures no fancy metamethod is invoked during testing.
2 years ago

@ -9,16 +9,20 @@ local meta = {}
--- Test whether 'obj' is an instance of the given class 'cls'.
--
-- @tparam table obj
-- @tparam table cls
-- Note that nil is the only instance of the nil class.
--
-- @tparam table obj object instance to be tested
-- @tparam table cls class argument
-- @treturn bool
function meta.isinstance(obj, cls)
if rawequal(cls, nil) then return rawequal(obj, nil) end
repeat
local m = getmetatable(obj)
if m == cls then return true end
if rawequal(m, cls) then return true end
obj = m
until obj == nil
until rawequal(obj, nil)
return false
end

Loading…
Cancel
Save