mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Scribunto
synced 2024-11-27 01:30:00 +00:00
33 lines
584 B
Lua
33 lines
584 B
Lua
|
mw = mw or {}
|
||
|
|
||
|
function mw.clone( val )
|
||
|
local tableRefs = {}
|
||
|
local function recursiveClone( val )
|
||
|
if type( val ) == 'table' then
|
||
|
-- Encode circular references correctly
|
||
|
if tableRefs[val] ~= nil then
|
||
|
return tableRefs[val]
|
||
|
end
|
||
|
|
||
|
local retVal
|
||
|
retVal = {}
|
||
|
tableRefs[val] = retVal
|
||
|
for key, elt in pairs( val ) do
|
||
|
retVal[key] = recursiveClone( elt )
|
||
|
end
|
||
|
return retVal
|
||
|
else
|
||
|
return val
|
||
|
end
|
||
|
end
|
||
|
return recursiveClone( val )
|
||
|
end
|
||
|
|
||
|
function mw.executeModule( chunk )
|
||
|
local env = mw.clone( _G )
|
||
|
setfenv( chunk, env )
|
||
|
return chunk()
|
||
|
end
|
||
|
|
||
|
return mw
|