Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

table.copy removes metatables #15744

Open
immagiov4 opened this issue Feb 1, 2025 · 2 comments
Open

table.copy removes metatables #15744

immagiov4 opened this issue Feb 1, 2025 · 2 comments
Labels
Feature request Issues that request the addition or enhancement of a feature @ Script API

Comments

@immagiov4
Copy link
Contributor

Luanti version

5.10

Operating system and version

Windows 11

CPU model

No response

GPU model

No response

Active renderer

No response

Summary

The title says it all I guess: table.copy doesn't keep metatables into copied tables.

Steps to reproduce

meta = {metavalue = "we_like_diversity"}

t = {value = "we_lack_diversity"}
setmetatable(t, meta)
core.chat_send_all(dump(getmetatable(t)))

final_t = table.copy(t)
core.chat_send_all(dump(getmetatable(final_t)))
@immagiov4 immagiov4 added the Unconfirmed bug Bug report that has not been confirmed to exist/be reproducible label Feb 1, 2025
@SmallJoker SmallJoker added Feature request Issues that request the addition or enhancement of a feature and removed Unconfirmed bug Bug report that has not been confirmed to exist/be reproducible labels Feb 2, 2025
@SmallJoker
Copy link
Member

Marked as feature request because I only intended to duplicate the data (key/values) of a table when I implemented the first version of table.copy. Metatables are an after-thought. Copying that (recursively) to the new table shouldn't be too complicated.

@appgurueu
Copy link
Contributor

Metatables are an after-thought. Copying that (recursively) to the new table shouldn't be too complicated.

I don't think the metatable should generally be copied, both for performance and correctness reasons (think vector.is being false for a copied vector because you copied the vector "class" as well). It should just be restored instead.

Short term I think we should just add a boolean param defaulting to false that indicates whether to preserve metatables, probably with a notice that we plan to make this the default eventually (though technically the fact that metatables are stripped is not something we guarantee in our docs).

Longer term, we can provide a recursive core.strip_metatables for those who want that, but e.g. vectors losing their metatables through copying is not a useful default. Cases where you want to strip metatables are much rarer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature request Issues that request the addition or enhancement of a feature @ Script API
Projects
None yet
Development

No branches or pull requests

4 participants