-
Notifications
You must be signed in to change notification settings - Fork 81
Allow table.clone to copy tables with locked metatables #88
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
Open
ccuser44
wants to merge
8
commits into
luau-lang:master
Choose a base branch
from
ccuser44:patch-2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
101187d
Create table.clone-locked-metatables.md
ccuser44 c09c926
Misc improvements
ccuser44 2723717
Remove useless comment
ccuser44 a05c4c4
Fix grammar mistake
ccuser44 b400c9f
Remove "There are no drawbacks"
ccuser44 a5b158d
Remove value based language
ccuser44 251dbfd
Add example usages
ccuser44 ce907ca
Make concession on moot point
ccuser44 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| # Feature name | ||
|
|
||
| ## Summary | ||
|
|
||
| Allow `table.clone` to copy tables with locked metatables. | ||
|
|
||
| ## Motivation | ||
|
|
||
| As proposed by the [`table.clone` RFC](function-table-clone.md), the function `table.clone` cannot create shallow copies of a table if it has a locked metatable. | ||
| Due to this ugly limitation, it is extremely un-ergonomic to create shallow copies of arbitrary tables. | ||
| The current behavior of using `table.clone` on a table with a locked metatable is a hard-fail approach where `table.clone` spits out an error instead of having a soft-fail approach wherein a clone is generated without a metatable. | ||
| This hard-fail approach severely hinders the usefulness of `table.clone` due to in practice users having to re-implement the function in many scenarios. | ||
ccuser44 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| ## 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. | ||
|
|
||
| ie. The behavior (as described by Lua pseudocode) would go from: | ||
|
|
||
| ```lua | ||
| function table.clone(t) | ||
| assert(type(t) == "table") | ||
| local nt = {} | ||
|
|
||
| for k, v in pairs(t) do | ||
| nt[k] =v | ||
| end | ||
|
|
||
| if type(getmetatable(t)) == "table" then | ||
| setmetatable(nt, getmetatable(t)) | ||
| end | ||
|
|
||
| return nt | ||
| end | ||
| ``` | ||
|
|
||
| to: | ||
|
|
||
| ```lua | ||
| function table.clone(t) | ||
| assert(type(t) == "table") | ||
| local nt = {} | ||
|
|
||
| for k, v in pairs(t) do | ||
| nt[k] =v | ||
| end | ||
|
|
||
| local mtLocked = getmetatable(t) ~= nil and not pcall(setmetatable, t, getmetatable(t)) | ||
|
|
||
| if not mtLocked and type(getmetatable(t)) == "table" then | ||
| setmetatable(nt, getmetatable(t)) | ||
| end | ||
|
|
||
| return nt | ||
| end | ||
| ``` | ||
|
|
||
| ## Drawbacks | ||
|
|
||
| There are no drawbacks to this. | ||
ccuser44 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| The original RFC puts forth a rationale for restricting cloning tables with locked metatables due to security reasons. | ||
| This is a valid concern. The issue at hand being an avenue to assign metatables to unintended tables. | ||
| However, this is not an issue as the changes proposed in this RFC do not allow for such behavior. | ||
|
|
||
| Another potential drawback that could be levied at this proposal is a reduced debuggability of not hard-failing when the expected behavior would be to create a shallow copy with the metatable of the original table. | ||
| However, this point is most certainly moot in practice, as most users are very likely not even aware, or desire the fact that `table.clone` can assign the metatables of the original table to the clone. | ||
ccuser44 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ## 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, as pointed out in the original RFC. | ||
| This approach is also less flexible than the one proposed in this RFC as users couldn't create shallow copies without metatables at all and has no practical benefits that do not violate security guarantees. | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.