Skip to content

Fix: table.update no longer errors when replacing non-table with table (#8694)#8751

Merged
vadi2 merged 2 commits intoMudlet:developmentfrom
ehussong:fix/table-update-type-check
Jan 23, 2026
Merged

Fix: table.update no longer errors when replacing non-table with table (#8694)#8751
vadi2 merged 2 commits intoMudlet:developmentfrom
ehussong:fix/table-update-type-check

Conversation

@ehussong
Copy link
Copy Markdown
Contributor

@ehussong ehussong commented Jan 5, 2026

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:

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

  • 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
  • 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

Mudlet#8694)

When table.update was called with a non-table value (number, string,
boolean) at a key where the second table had a table value, it would
error with "bad argument #1 to 'pairs' (table expected, got number)".

The fix checks if the existing value is actually a table before
attempting to recursively merge, otherwise it replaces entirely.

Added tests for:
- Replacing number with table
- Replacing table with non-table
- Merging nested tables
- Replacing string/boolean with table

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@ehussong ehussong requested a review from a team as a code owner January 5, 2026 02:38
@add-deployment-links
Copy link
Copy Markdown

add-deployment-links bot commented Jan 5, 2026

Hey there! Thanks for helping Mudlet improve. 🌟

Test versions

You can directly test the changes here:

No need to install anything - just unzip and run.
Let us know if it works well, and if it doesn't, please give details.

@ZookaOnGit
Copy link
Copy Markdown
Contributor

#8704 fixes this already, test cases are nice though

@vadi2
Copy link
Copy Markdown
Member

vadi2 commented Jan 6, 2026

@ehussong could you add the solution from #8704 in here and we merge this one that has the tests?

@ehussong
Copy link
Copy Markdown
Contributor Author

ehussong commented Jan 8, 2026

Brought in the fix from #8704: table.update now only recurses when both sides are tables, otherwise it replaces (src/mudlet-lua/lua/TableUtils.lua). The tests in this PR still cover the #8694 cases. I haven’t run Busted locally yet.

Only recurse when both source and destination are tables.
Otherwise, just replace the value directly.
@vadi2 vadi2 merged commit c9e560b into Mudlet:development Jan 23, 2026
12 checks passed
vadi2 added a commit that referenced this pull request Feb 3, 2026
#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>
(cherry picked from commit c9e560b)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

table.update errors out in unpredictable, input-dependent ways

3 participants