mirror of
https://codeberg.org/1414codeforge/gear.git
synced 2025-06-05 22:09:24 +02:00
[spec/*,*] Split library in submodules.
This commit is contained in:
38
meta.lua
Normal file
38
meta.lua
Normal file
@ -0,0 +1,38 @@
|
||||
--- Functions dealing with metatables and tables merging.
|
||||
--
|
||||
-- @module gear.meta
|
||||
-- @copyright 2022 The DoubleFourteen Code Forge
|
||||
-- @author Lorenzo Cogotti
|
||||
|
||||
local meta = {}
|
||||
|
||||
|
||||
--- Test whether 'obj' is an instance of a given class 'cls'.
|
||||
function meta.isinstance(obj, cls)
|
||||
repeat
|
||||
local m = getmetatable(obj)
|
||||
if m == cls then return true end
|
||||
|
||||
obj = m
|
||||
until obj == nil
|
||||
|
||||
return false
|
||||
end
|
||||
|
||||
--- Merge table 'from' into table 'to'.
|
||||
--
|
||||
-- For every field in 'from', copy it to destination
|
||||
-- table 'to', whenever the same field is nil in that table.
|
||||
--
|
||||
-- The same process is applied recursively to sub-tables.
|
||||
function meta.mergetable(to, from)
|
||||
for k,v in pairs(from) do
|
||||
if to[k] == nil then
|
||||
to[k] = type(v) == 'table' and meta.mergetable({}, v) or v
|
||||
end
|
||||
end
|
||||
|
||||
return to
|
||||
end
|
||||
|
||||
return meta
|
Reference in New Issue
Block a user