[meta] Improve isinstance().
* Handle nil * Use rawequal() instead of plain ==
This commit is contained in:
parent
62d22658b3
commit
99fde174df
10
meta.lua
10
meta.lua
|
@ -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 rawequal(m, cls) then return true end
|
||||
|
||||
obj = m
|
||||
until obj == nil
|
||||
until rawequal(obj, nil)
|
||||
|
||||
return false
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue