Module:table/setParent
Itsura
- This module lacks a documentation subpage. Please create it.
- Useful links: root page • root page’s subpages • links • transclusions • testcases • sandbox
local table_get_unprotected_metatable = "Module:table/getUnprotectedMetatable"
local table_index_ipairs_module = "Module:table/indexIpairs"
local table_index_pairs_module = "Module:table/indexPairs"
local require = require
local setmetatable = setmetatable
local function get_unprotected_metatable(...)
get_unprotected_metatable = require(table_get_unprotected_metatable)
return get_unprotected_metatable(...)
end
local index_ipairs
local function get_index_ipairs()
index_ipairs, get_index_ipairs = require(table_index_ipairs_module), nil
return index_ipairs
end
local index_pairs
local function get_index_pairs()
index_pairs, get_index_pairs = require(table_index_pairs_module), nil
return index_pairs
end
return function(t1, t2, setter)
-- If the metatable is protected, setmetatable() will throw an error, but
-- using get_unprotected_metatable() avoids arbitrary return values from a
-- __metatable metamethod.
local mt = get_unprotected_metatable(t1)
if not mt then
mt = {}
setmetatable(t1, mt)
end
mt.__index = t2
mt.__ipairs = index_ipairs or get_index_ipairs()
mt.__pairs = index_pairs or get_index_pairs()
if setter then
mt.__newindex = t2
end
return t1
end