functionshallowcopy(orig) local orig_type = type(orig) local copy if orig_type == 'table'then copy = {} for orig_key, orig_value inpairs(orig) do copy[orig_key] = orig_value end else copy = orig end return copy end
functiondeepcopy(orig) local orig_type = type(orig) local copy if orig_type == 'table'then copy = {} for orig_key, orig_value innext, orig, nildo copy[deepcopy(orig_key)] = deepcopy(orig_value) end setmetatable(copy, deepcopy(getmetatable(orig))) else copy = orig end return copy end
functiondeepcopy(orig, copies) copies = copies or {} local orig_type = type(orig) local copy if orig_type == 'table'then if copies[orig] then copy = copies[orig] else copy = {} for orig_key, orig_value innext, orig, nildo copy[deepcopy(orig_key, copies)] = deepcopy(orig_value, copies) end copies[orig] = copy setmetatable(copy, deepcopy(getmetatable(orig), copies)) end else copy = orig end return copy end