From e19470f3f11aea42eafc8b4d6f015c05e6ced3d9 Mon Sep 17 00:00:00 2001 From: RDW Date: Mon, 18 Dec 2023 07:55:06 +0100 Subject: [PATCH 1/2] Runtime: Add table functions for deep and shallow copying The simplest approach that could possibly work... Let's ignore edge cases like cdata, userdata, metatables, and others for now. --- Runtime/Extensions/tablex.lua | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/Runtime/Extensions/tablex.lua b/Runtime/Extensions/tablex.lua index d6e956cc..24e976e5 100644 --- a/Runtime/Extensions/tablex.lua +++ b/Runtime/Extensions/tablex.lua @@ -28,3 +28,27 @@ function table.count(object) return count end + +function table.copy(source) + local deepCopy = {} + + for key, value in pairs(source) do + if type(value) == "table" then + deepCopy[key] = table.copy(value) + else + deepCopy[key] = value + end + end + + return deepCopy +end + +function table.scopy(source) + local shallowCopy = {} + + for key, value in pairs(source) do + shallowCopy[key] = value + end + + return shallowCopy +end From bc0669dff6024cbef5eccfa966699e4b24374878 Mon Sep 17 00:00:00 2001 From: RDW Date: Mon, 18 Dec 2023 07:55:23 +0100 Subject: [PATCH 2/2] Tests: Add unit tests for the table copying extensions --- Tests/BDD/table-library.spec.lua | 40 ++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/Tests/BDD/table-library.spec.lua b/Tests/BDD/table-library.spec.lua index 87f3fb2a..27b39e03 100644 --- a/Tests/BDD/table-library.spec.lua +++ b/Tests/BDD/table-library.spec.lua @@ -61,4 +61,44 @@ describe("table", function() assertEquals(table.count({ hi = 42, nil, 43, nil, meep = 44 }), 3) end) end) + + describe("copy", function() + it("should create a deep copy if the given table contains another table", function() + local tableWithNestedSubtables = { + subtable = { 42 }, + 12345, + } + local deepCopy = table.copy(tableWithNestedSubtables) + local expectedResult = { + subtable = { 42 }, + 12345, + } + + assertEquals(#deepCopy, #expectedResult) + assertEquals(table.count(deepCopy), table.count(expectedResult)) + assertEquals(deepCopy[1], expectedResult[1]) + assertEquals(deepCopy.subtable[1], expectedResult.subtable[1]) + assert(deepCopy.subtable ~= expectedResult.subtable, "Both tables should not be identical") + end) + end) + + describe("scopy", function() + it("should create a shallow copy if the given table contains another table", function() + local tableWithNestedSubtables = { + subtable = { 42 }, + 12345, + } + local shallowCopy = table.scopy(tableWithNestedSubtables) + local expectedResult = { + subtable = tableWithNestedSubtables.subtable, + 12345, + } + + assertEquals(#shallowCopy, #expectedResult) + assertEquals(table.count(shallowCopy), table.count(expectedResult)) + assertEquals(shallowCopy[1], expectedResult[1]) + assertEquals(shallowCopy.subtable[1], expectedResult.subtable[1]) + assert(shallowCopy.subtable == expectedResult.subtable, "Both tables should be identical") + end) + end) end)