Allow table.clone to copy tables with locked metatables

This commit is contained in:
ccuser44 2024-11-14 13:59:56 +02:00 committed by GitHub
parent 03608c7519
commit a1583ce972
Signed by: DevComp
GPG key ID: B5690EEEBB952194

View file

@ -0,0 +1,21 @@
# Feature name
## Summary
Allow `table.clone` to copy tables with locked metatables.
## Motivation
Currently `table.clone` cannot create shallow copies of a table if it has a locked metatable. The dictionary definition for shallow copying is to copy all the fields of a table to a new table but not doing so recursively. There is no mention of metatables in the first place so most users expect said behavior just to work, if they haven't taken a look at the manual of course.
## Design
When `table.clone` attempts to clone a table with a locked metatable the table is shallow copied with the exception of not assigning the metatable to the shallow copy.
## Drawbacks
There are no drawbacks to this. You can already create a shallow copy of a table with a locked metatable (without assigning the metatable to the copy) so there is no downside in allowing `table.clone` to do the same.
## Alternatives
An alternate mode of action would be to also clone the locked metatable to the new copy. This would however come with a few downsides including potential security and usability issues.