<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://linguifex.com/w/index.php?action=history&amp;feed=atom&amp;title=Module%3Atable%2FdeepCopy</id>
	<title>Module:table/deepCopy - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://linguifex.com/w/index.php?action=history&amp;feed=atom&amp;title=Module%3Atable%2FdeepCopy"/>
	<link rel="alternate" type="text/html" href="https://linguifex.com/w/index.php?title=Module:table/deepCopy&amp;action=history"/>
	<updated>2026-04-05T16:14:23Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.6</generator>
	<entry>
		<id>https://linguifex.com/w/index.php?title=Module:table/deepCopy&amp;diff=474947&amp;oldid=prev</id>
		<title>Sware: 1 revision imported</title>
		<link rel="alternate" type="text/html" href="https://linguifex.com/w/index.php?title=Module:table/deepCopy&amp;diff=474947&amp;oldid=prev"/>
		<updated>2025-11-04T17:47:25Z</updated>

		<summary type="html">&lt;p&gt;1 revision imported&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 17:47, 4 November 2025&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-notice&quot; lang=&quot;en&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(No difference)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
		<author><name>Sware</name></author>
	</entry>
	<entry>
		<id>https://linguifex.com/w/index.php?title=Module:table/deepCopy&amp;diff=474946&amp;oldid=prev</id>
		<title>wikt&gt;Theknightwho at 09:04, 4 April 2025</title>
		<link rel="alternate" type="text/html" href="https://linguifex.com/w/index.php?title=Module:table/deepCopy&amp;diff=474946&amp;oldid=prev"/>
		<updated>2025-04-04T09:04:51Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;local debug_track_module = &amp;quot;Module:debug/track&amp;quot;&lt;br /&gt;
&lt;br /&gt;
local dump = mw.dumpObject&lt;br /&gt;
local error = error&lt;br /&gt;
local getmetatable = getmetatable&lt;br /&gt;
local next = next&lt;br /&gt;
local pairs = pairs&lt;br /&gt;
local rawget = rawget&lt;br /&gt;
local type = type&lt;br /&gt;
local setmetatable = setmetatable&lt;br /&gt;
&lt;br /&gt;
local function debug_track(...)&lt;br /&gt;
	debug_track = require(debug_track_module)&lt;br /&gt;
	return debug_track(...)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local tracked&lt;br /&gt;
&lt;br /&gt;
local function make_copy(orig, seen, mt_flag, keep_loaded_data)&lt;br /&gt;
	local mt, iter, state, init = getmetatable(orig)&lt;br /&gt;
	-- If `mt` is nil, just use `next`, as it&amp;#039;s faster.&lt;br /&gt;
	if mt == nil then&lt;br /&gt;
		iter, state, init = next, orig, nil&lt;br /&gt;
	-- `mt` could be a non-table if `__metatable` has been used, but discard it in such cases.&lt;br /&gt;
	elseif type(mt) ~= &amp;quot;table&amp;quot; then&lt;br /&gt;
		mt, iter, state, init = nil, pairs(orig)&lt;br /&gt;
	-- Data loaded via `mw.loadData`, which sets the key &amp;quot;mw_loadData&amp;quot; to true in the metatable.&lt;br /&gt;
	elseif rawget(mt, &amp;quot;mw_loadData&amp;quot;) == true then&lt;br /&gt;
		if keep_loaded_data then&lt;br /&gt;
			seen[orig] = orig&lt;br /&gt;
			return orig&lt;br /&gt;
		-- Track instances of such data being copied, which is very inefficient and usually unnecessary.&lt;br /&gt;
		elseif not tracked then&lt;br /&gt;
			debug_track(&amp;quot;table/deepCopy/loaded data&amp;quot;)&lt;br /&gt;
			tracked = true&lt;br /&gt;
		end&lt;br /&gt;
		-- Discard the metatable and use the `__pairs` metamethod.&lt;br /&gt;
		mt, iter, state, init = nil, pairs(orig)&lt;br /&gt;
	-- If `mt_flag` is &amp;quot;none&amp;quot;, discard the metatable and use the `__pairs` metamethod.&lt;br /&gt;
	elseif mt_flag == &amp;quot;none&amp;quot; then&lt;br /&gt;
		mt, iter, state, init = nil, pairs(orig)	&lt;br /&gt;
	-- Otherwise, keep `mt` and use `next` to copy the raw contents.&lt;br /&gt;
	else&lt;br /&gt;
		iter, state, init = next, orig, nil&lt;br /&gt;
	end&lt;br /&gt;
	local copy = {}&lt;br /&gt;
	seen[orig] = copy&lt;br /&gt;
	for k, v in iter, state, init do&lt;br /&gt;
		if k and type(k) == &amp;quot;table&amp;quot; then&lt;br /&gt;
			k = seen[k] or make_copy(k, seen, mt_flag, keep_loaded_data)&lt;br /&gt;
		end&lt;br /&gt;
		if v and type(v) == &amp;quot;table&amp;quot; then&lt;br /&gt;
			v = seen[v] or make_copy(v, seen, mt_flag, keep_loaded_data)&lt;br /&gt;
		end&lt;br /&gt;
		copy[k] = v&lt;br /&gt;
	end&lt;br /&gt;
	if mt == nil or mt_flag == &amp;quot;none&amp;quot; then&lt;br /&gt;
		return copy&lt;br /&gt;
	-- Copy the metatable if `mt_flag` is &amp;quot;copy&amp;quot;; otherwise, it will be &amp;quot;keep&amp;quot;, so keep it.&lt;br /&gt;
	elseif mt_flag == &amp;quot;copy&amp;quot; then&lt;br /&gt;
		mt = seen[mt] or make_copy(mt, seen, mt_flag, keep_loaded_data)&lt;br /&gt;
	end&lt;br /&gt;
	return setmetatable(copy, mt)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
--[==[&lt;br /&gt;
Recursive deep copy function. Preserves copied identities of subtables.&lt;br /&gt;
A more powerful version of {mw.clone}, with customizable options.&lt;br /&gt;
* `metatableFlag` can be one of three options:&lt;br /&gt;
*# &amp;quot;none&amp;quot; (the default): `pairs` will be used to copy the table, meaning that any `__pairs` metamethod will be used to copy the table, if available; the resulting table will not have a metatable.&lt;br /&gt;
*# &amp;quot;copy&amp;quot;: a raw copy of the table will be made (i.e. any `__pairs` metamethod will be ignored), and the copy will be given a copy of the original table&amp;#039;s metatable; this ensures that nothing from the original is retained, but may cause metamethods to behave unexpectedly, depending on their implementation.&lt;br /&gt;
*# &amp;quot;keep&amp;quot;: a raw copy of the table will be made (i.e. any `__pairs` metamethod will be ignored), and the copy will be given the original table&amp;#039;s metatable; this is useful when copying objects that inherit methods from a prototype object (e.g. language objects).&lt;br /&gt;
* If `keepLoadedData` is true, then any data loaded via {mw.loadData} will not be copied, and the original will be used instead. This is useful in iterative contexts where it is necessary to copy data being destructively modified, because objects loaded via mw.loadData are immutable.&lt;br /&gt;
* Notes:&lt;br /&gt;
*# Protected metatables will not be copied (i.e. those hidden behind a `__metatable` metamethod), as they are not accessible by Lua&amp;#039;s design. Instead, the output of the `__metatable` method will be used instead.&lt;br /&gt;
*# Data loaded via {mw.loadData} is always treated as though the &amp;quot;none&amp;quot; flag is set, because the way it has been implemented causes errors to be thrown if &amp;quot;copy&amp;quot; or &amp;quot;keep&amp;quot; are used with it.]==]&lt;br /&gt;
return function(orig, metatableFlag, keepLoadedData)&lt;br /&gt;
	if metatableFlag == nil then&lt;br /&gt;
		metatableFlag = &amp;quot;none&amp;quot;&lt;br /&gt;
	elseif not (metatableFlag == &amp;quot;keep&amp;quot; or metatableFlag == &amp;quot;copy&amp;quot; or metatableFlag == &amp;quot;none&amp;quot;) then&lt;br /&gt;
		error(&amp;#039;metatableFlag must be &amp;quot;none&amp;quot;, &amp;quot;copy&amp;quot;, &amp;quot;keep&amp;quot; or nil; received &amp;#039; .. dump(metatableFlag))&lt;br /&gt;
	end&lt;br /&gt;
	if orig and type(orig) == &amp;quot;table&amp;quot; then&lt;br /&gt;
		return make_copy(orig, {}, metatableFlag, keepLoadedData)&lt;br /&gt;
	end&lt;br /&gt;
	return orig&lt;br /&gt;
end&lt;/div&gt;</summary>
		<author><name>wikt&gt;Theknightwho</name></author>
	</entry>
</feed>