Skip to content

Commit

Permalink
fix(metatable): Evaluate callability by fetching meta-fields with raw…
Browse files Browse the repository at this point in the history
…get (#198)
  • Loading branch information
Tieske authored Nov 7, 2024
1 parent 7b4f451 commit 2be28cb
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,14 @@ end
-- @param object element to inspect on being callable or not
-- @return boolean, true if the object is callable
function util.callable(object)
return type(object) == "function" or type((debug.getmetatable(object) or {}).__call) == "function"
if type(object) == 'function' then
return true
end
local mt = debug.getmetatable(object)
if not mt then
return false
end
return type(rawget(mt, "__call")) == "function"
end

-----------------------------------------------
Expand All @@ -282,7 +289,7 @@ end
-- @param object element to inspect on having tostring or not
-- @return boolean, true if the object has tostring
function util.hastostring(object)
return type(object) == "string" or type((debug.getmetatable(object) or {}).__tostring) == "function"
return type(object) == "string" or type(rawget(debug.getmetatable(object) or {}, "__tostring")) == "function"
end

-----------------------------------------------
Expand Down

0 comments on commit 2be28cb

Please sign in to comment.