From 99fde174dfd4aa6e9a541f590a3e80bad21e460b Mon Sep 17 00:00:00 2001 From: Lorenzo Cogotti Date: Sun, 6 Nov 2022 17:32:55 +0100 Subject: [PATCH] [meta] Improve isinstance(). * Handle nil * Use rawequal() instead of plain == --- meta.lua | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/meta.lua b/meta.lua index 1e97d6f..ed67c49 100644 --- a/meta.lua +++ b/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