diff options
author | Waqas Hussain <waqas20@gmail.com> | 2009-02-20 02:18:07 +0500 |
---|---|---|
committer | Waqas Hussain <waqas20@gmail.com> | 2009-02-20 02:18:07 +0500 |
commit | 0fe02618e58fb93ebdf11ff38efcf71015a0ca81 (patch) | |
tree | 2da50443ea982c3010abcecef362e8d4c8b51d92 | |
parent | 4c696b7d8acaed5e4e324cae6f27d28d21a101d5 (diff) | |
download | prosody-0fe02618e58fb93ebdf11ff38efcf71015a0ca81.tar.gz prosody-0fe02618e58fb93ebdf11ff38efcf71015a0ca81.zip |
util/stanza: Added clone function
-rw-r--r-- | util/stanza.lua | 21 |
1 files changed, 20 insertions, 1 deletions
diff --git a/util/stanza.lua b/util/stanza.lua index 1c1cab0e..5bc15609 100644 --- a/util/stanza.lua +++ b/util/stanza.lua @@ -12,9 +12,10 @@ local t_concat = table.concat; local t_remove = table.remove; local t_concat = table.concat; local s_format = string.format; -local s_match = string.match; +local s_match = string.match; local tostring = tostring; local setmetatable = setmetatable; +local getmetatable = getmetatable; local pairs = pairs; local ipairs = ipairs; local type = type; @@ -215,6 +216,24 @@ function deserialize(stanza) return stanza; end +function clone(stanza) + local lookup_table = {}; + local function _copy(object) + if type(object) ~= "table" then + return object; + elseif lookup_table[object] then + return lookup_table[object]; + end + local new_table = {}; + lookup_table[object] = new_table; + for index, value in pairs(object) do + new_table[_copy(index)] = _copy(value); + end + return setmetatable(new_table, getmetatable(object)); + end + return _copy(stanza) +end + function message(attr, body) if not body then return stanza("message", attr); |