[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'.
|
--- Test whether 'obj' is an instance of the given class 'cls'.
|
||||||
--
|
--
|
||||||
-- @tparam table obj
|
-- Note that nil is the only instance of the nil class.
|
||||||
-- @tparam table cls
|
--
|
||||||
|
-- @tparam table obj object instance to be tested
|
||||||
|
-- @tparam table cls class argument
|
||||||
-- @treturn bool
|
-- @treturn bool
|
||||||
function meta.isinstance(obj, cls)
|
function meta.isinstance(obj, cls)
|
||||||
|
if rawequal(cls, nil) then return rawequal(obj, nil) end
|
||||||
|
|
||||||
repeat
|
repeat
|
||||||
local m = getmetatable(obj)
|
local m = getmetatable(obj)
|
||||||
if rawequal(m, cls) then return true end
|
if rawequal(m, cls) then return true end
|
||||||
|
|
||||||
obj = m
|
obj = m
|
||||||
until obj == nil
|
until rawequal(obj, nil)
|
||||||
|
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue