Skip to content

Commit c9e560b

Browse files
ehussongclaudevadi2
authored
Fix: table.update no longer errors when replacing non-table with table (#8694) (#8751)
## Summary Fixes #8694 - `table.update` would error when the first table had a non-table value (number, string, boolean) at a key where the second table had a table value. **Example that crashed:** ```lua table.update({ x = 1 }, { x = { y = 2 } }) -- Error: bad argument #1 to 'pairs' (table expected, got number) ``` **Root cause:** The recursion logic `tbl[k] = table.update(tbl[k] or {}, v)` only checked for nil/false before recursing, not for other non-table types. **Fix:** Check `type(existing) == "table"` before passing to recursive call. ## Test plan - [x] Added 5 new test cases in `TableUtils_spec.lua`: - Replace number with table - Replace table with number - Merge nested tables (both tables) - Replace string with table - Replace boolean with table - [x] Verified fix works: `luajit -e "dofile('...TableUtils.lua'); print(table.update({x=1}, {x={y=2}}).x.y)"` outputs `2` ## Notes Tests require running inside Mudlet with `runTests` command (uses Busted framework loaded by Mudlet). 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com> Co-authored-by: Vadim Peretokin <vperetokin@hey.com>
1 parent 4be7036 commit c9e560b

2 files changed

Lines changed: 43 additions & 2 deletions

File tree

src/mudlet-lua/lua/TableUtils.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -528,8 +528,8 @@ function table.update(t1, t2)
528528
tbl[k] = v
529529
end
530530
for k, v in pairs(t2) do
531-
if type(v) == "table" then
532-
tbl[k] = table.update(tbl[k] or {}, v)
531+
if type(v) == "table" and type(tbl[k]) == "table" then
532+
tbl[k] = table.update(tbl[k], v)
533533
else
534534
tbl[k] = v
535535
end

src/mudlet-lua/tests/TableUtils_spec.lua

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,5 +743,46 @@ describe("Tests TableUtils.lua functions", function()
743743
local actual = table.update(tblA, tblB)
744744
assert.same(expected, actual)
745745
end)
746+
747+
-- Tests for #8694: table.update should not error when t1 has non-table and t2 has table
748+
it("should replace non-table value with table value at same key", function()
749+
local tblA = {x = 1}
750+
local tblB = {x = {y = 2}}
751+
local expected = {x = {y = 2}}
752+
local actual = table.update(tblA, tblB)
753+
assert.same(expected, actual)
754+
end)
755+
756+
it("should replace table value with non-table value at same key", function()
757+
local tblA = {x = {y = 2}}
758+
local tblB = {x = 1}
759+
local expected = {x = 1}
760+
local actual = table.update(tblA, tblB)
761+
assert.same(expected, actual)
762+
end)
763+
764+
it("should merge nested tables when both have table at same key", function()
765+
local tblA = {x = {a = 1, b = 2}}
766+
local tblB = {x = {b = 3, c = 4}}
767+
local expected = {x = {a = 1, b = 3, c = 4}}
768+
local actual = table.update(tblA, tblB)
769+
assert.same(expected, actual)
770+
end)
771+
772+
it("should handle string value being replaced by table", function()
773+
local tblA = {config = "old"}
774+
local tblB = {config = {setting = true}}
775+
local expected = {config = {setting = true}}
776+
local actual = table.update(tblA, tblB)
777+
assert.same(expected, actual)
778+
end)
779+
780+
it("should handle boolean value being replaced by table", function()
781+
local tblA = {enabled = true}
782+
local tblB = {enabled = {feature1 = true, feature2 = false}}
783+
local expected = {enabled = {feature1 = true, feature2 = false}}
784+
local actual = table.update(tblA, tblB)
785+
assert.same(expected, actual)
786+
end)
746787
end)
747788
end)

0 commit comments

Comments
 (0)